mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-25 03:58:39 +02:00
Initial /avilable API view and tests
This commit is contained in:
parent
5c105f1861
commit
01553a4d91
6 changed files with 58 additions and 0 deletions
0
src/api/__init__.py
Normal file
0
src/api/__init__.py
Normal file
0
src/api/tests/__init__.py
Normal file
0
src/api/tests/__init__.py
Normal file
39
src/api/tests/test_available.py
Normal file
39
src/api/tests/test_available.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
"""Test the available domain API."""
|
||||
|
||||
import json
|
||||
|
||||
from django.test import client, TestCase, RequestFactory
|
||||
|
||||
from ..views import available
|
||||
|
||||
class AvailableViewTest(TestCase):
|
||||
|
||||
"""Test that the view function works as expected."""
|
||||
|
||||
def setUp(self):
|
||||
self.factory = RequestFactory()
|
||||
|
||||
def test_view_function(self):
|
||||
request = self.factory.get("available/test.gov")
|
||||
response = available(request, domain="test.gov")
|
||||
# has the right text in it
|
||||
self.assertContains(response, "available")
|
||||
# can be parsed as JSON
|
||||
response_object = json.loads(response.content)
|
||||
self.assertIn("available", response_object)
|
||||
|
||||
|
||||
class AvailableAPITest(TestCase):
|
||||
|
||||
"""Test that the API can be called as expected."""
|
||||
|
||||
def test_available_get(self):
|
||||
response = self.client.get("/available/nonsense")
|
||||
self.assertContains(response, "available")
|
||||
response_object = json.loads(response.content)
|
||||
self.assertIn("available", response_object)
|
||||
|
||||
def test_available_post(self):
|
||||
"""Cannot post to the /available/ API endpoint."""
|
||||
response = self.client.post("/available/nonsense")
|
||||
self.assertEqual(response.status_code, 405)
|
15
src/api/views.py
Normal file
15
src/api/views.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
"""Internal API views"""
|
||||
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.http import JsonResponse
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
def available(request, domain=""):
|
||||
|
||||
"""Is a given domain available or not.
|
||||
|
||||
Response is a JSON dictionary with the key "available" and value true or
|
||||
false.
|
||||
"""
|
||||
return JsonResponse({"available": False})
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue