API is only available to logged-in users

This commit is contained in:
Neil Martinsen-Burrell 2022-10-27 15:34:17 -05:00
parent 01553a4d91
commit 7e02661416
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
2 changed files with 13 additions and 2 deletions

View file

@ -2,7 +2,8 @@
import json
from django.test import client, TestCase, RequestFactory
from django.contrib.auth import get_user_model
from django.test import TestCase, RequestFactory
from ..views import available
@ -11,10 +12,12 @@ class AvailableViewTest(TestCase):
"""Test that the view function works as expected."""
def setUp(self):
self.user = get_user_model().objects.create(username="username")
self.factory = RequestFactory()
def test_view_function(self):
request = self.factory.get("available/test.gov")
request = self.factory.get("/available/test.gov")
request.user = self.user
response = available(request, domain="test.gov")
# has the right text in it
self.assertContains(response, "available")
@ -27,7 +30,11 @@ class AvailableAPITest(TestCase):
"""Test that the API can be called as expected."""
def setUp(self):
self.user = get_user_model().objects.create(username="username")
def test_available_get(self):
self.client.force_login(self.user)
response = self.client.get("/available/nonsense")
self.assertContains(response, "available")
response_object = json.loads(response.content)