Some test refactoring

This commit is contained in:
Anna Sidwell 2019-04-07 17:47:03 +01:00
parent f17834cd3b
commit 30b0a680aa
2 changed files with 19 additions and 13 deletions

View File

@ -11,6 +11,7 @@ class CaseStudyDraftAPITests(TestCase):
test_username = "testuser1" test_username = "testuser1"
test_password = "12345" test_password = "12345"
def setUp(self): def setUp(self):
test_user1 = User.objects.create_user( test_user1 = User.objects.create_user(
username=self.test_username, password=self.test_password username=self.test_username, password=self.test_password
@ -19,8 +20,9 @@ class CaseStudyDraftAPITests(TestCase):
activate("en-gb") activate("en-gb")
next_url = QueryDict.fromkeys(["next"], reverse("drafts")) self.url = reverse('drafts')
next_url = QueryDict.fromkeys(["next"], self.url)
self.login_url = reverse("auth_login") + "?" + next_url.urlencode() self.login_url = reverse("auth_login") + "?" + next_url.urlencode()
def login(self): def login(self):
@ -29,41 +31,41 @@ class CaseStudyDraftAPITests(TestCase):
) )
def test_get_not_logged_in(self): def test_get_not_logged_in(self):
response = self.client.get(reverse("drafts"), follow=True) response = self.client.get(self.url, follow=True)
self.assertRedirects(response, self.login_url) self.assertRedirects(response, self.login_url)
def test_put_not_logged_in(self): def test_put_not_logged_in(self):
response = self.client.put(reverse("drafts"), follow=True) response = self.client.put(self.url, follow=True)
self.assertRedirects(response, self.login_url) self.assertRedirects(response, self.login_url)
def test_delete_not_logged_in(self): def test_delete_not_logged_in(self):
response = self.client.delete(reverse("drafts"), follow=True) response = self.client.delete(self.url, follow=True)
self.assertRedirects(response, self.login_url) self.assertRedirects(response, self.login_url)
def test_get_and_put(self): def test_get_and_put(self):
login = self.login() login = self.login()
# No draft should currently exist # No draft should currently exist
response = self.client.get(reverse("drafts")) response = self.client.get(self.url)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
self.assertEqual(response.content, b"") self.assertEqual(response.content, b"")
# Putting a draft should succeed # Putting a draft should succeed
response = self.client.put(reverse("drafts"), '{"test":1}') response = self.client.put(self.url, '{"test":1}')
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
# We should get it back # We should get it back
response = self.client.get(reverse("drafts")) response = self.client.get(self.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json") self.assertEqual(response["Content-Type"], "application/json")
self.assertEqual(response.content, b'{"test":1}') self.assertEqual(response.content, b'{"test":1}')
# Updating it should succeed # Updating it should succeed
response = self.client.put(reverse("drafts"), '{"test":2}') response = self.client.put(self.url, '{"test":2}')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# We should get the update back # We should get the update back
response = self.client.get(reverse("drafts")) response = self.client.get(self.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json") self.assertEqual(response["Content-Type"], "application/json")
self.assertEqual(response.content, b'{"test":2}') self.assertEqual(response.content, b'{"test":2}')
@ -72,15 +74,15 @@ class CaseStudyDraftAPITests(TestCase):
login = self.login() login = self.login()
# Create a draft # Create a draft
response = self.client.put(reverse("drafts"), '{"test":1}') response = self.client.put(self.url, '{"test":1}')
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
# Delete it again # Delete it again
response = self.client.delete(reverse("drafts")) response = self.client.delete(self.url)
self.assertEqual(response.status_code, 204) self.assertEqual(response.status_code, 204)
# Check it's not there # Check it's not there
response = self.client.get(reverse("drafts")) response = self.client.get(self.url)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)

View File

@ -194,7 +194,11 @@ class SpatialRefSysAutocomplete(autocomplete.Select2QuerySetView):
class DraftsAPI(LoginRequiredMixin, View): class DraftsAPI(LoginRequiredMixin, View):
"""Retrieve or save a draft.""" """
Retrieve or save a draft.
XXX This should be refactored to use csrf protection.
"""
def get_object(self, request): def get_object(self, request):
try: try: