Initial /avilable API view and tests

This commit is contained in:
Neil Martinsen-Burrell 2022-10-27 13:15:22 -05:00
parent 5c105f1861
commit 01553a4d91
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
6 changed files with 58 additions and 0 deletions

0
src/api/__init__.py Normal file
View file

View file

View 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
View 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})

View file

@ -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.

View file

@ -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: