mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-31 17:53:56 +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})
|
||||||
|
|
|
@ -86,6 +86,8 @@ INSTALLED_APPS = [
|
||||||
"djangooidc",
|
"djangooidc",
|
||||||
# let's be sure to install our own application!
|
# let's be sure to install our own application!
|
||||||
"registrar",
|
"registrar",
|
||||||
|
# Our internal API application
|
||||||
|
"api",
|
||||||
]
|
]
|
||||||
|
|
||||||
# Middleware are routines for processing web requests.
|
# Middleware are routines for processing web requests.
|
||||||
|
|
|
@ -10,6 +10,7 @@ from django.urls import include, path
|
||||||
from django.views.generic import RedirectView
|
from django.views.generic import RedirectView
|
||||||
|
|
||||||
from registrar.views import health, index, profile, whoami
|
from registrar.views import health, index, profile, whoami
|
||||||
|
from api.views import available
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", index.index, name="home"),
|
path("", index.index, name="home"),
|
||||||
|
@ -18,6 +19,7 @@ urlpatterns = [
|
||||||
path("health/", health.health),
|
path("health/", health.health),
|
||||||
path("edit_profile/", profile.edit_profile, name="edit-profile"),
|
path("edit_profile/", profile.edit_profile, name="edit-profile"),
|
||||||
path("openid/", include("djangooidc.urls")),
|
path("openid/", include("djangooidc.urls")),
|
||||||
|
path("available/<domain>", available, name="available"),
|
||||||
]
|
]
|
||||||
|
|
||||||
if not settings.DEBUG:
|
if not settings.DEBUG:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue