Refactor CaseStudyDraft tests to make them more unit-y

This commit is contained in:
Anna Sidwell 2019-04-07 18:32:40 +01:00
parent 30b0a680aa
commit edf62268aa

View File

@ -1,35 +1,48 @@
from django.contrib.auth.models import User
from django.contrib.sessions.middleware import SessionMiddleware
from django.http import QueryDict
from django.test import RequestFactory
from django.test import TestCase
from django.utils.translation import activate
from django.urls import reverse
from django.utils.translation import activate
from .models import CaseStudy, CaseStudyDraft
from .views import DraftsAPI
from .models import CaseStudy
from .models import CaseStudyDraft
class CaseStudyDraftAPITests(TestCase):
test_username = "testuser1"
test_password = "12345"
def setUp(self):
test_user1 = User.objects.create_user(
#  Set up a user object
self.test_username = "testuser1"
self.test_password = "12345"
self.user = User.objects.create_user(
username=self.test_username, password=self.test_password
)
test_user1.save()
self.user.save()
activate("en-gb")
self.url = reverse('drafts')
# Some useful vars
self.factory = RequestFactory()
self.url = reverse("drafts")
print(self.url)
# Work out login URL
next_url = QueryDict.fromkeys(["next"], self.url)
self.login_url = reverse("auth_login") + "?" + next_url.urlencode()
# Activate a language pack
activate("en-gb")
def login(self):
return self.client.login(
username=self.test_username, password=self.test_password
)
def fake_draft(self):
draft = CaseStudyDraft(author=self.user, data='{"test":1}')
draft.save()
return draft
def test_get_not_logged_in(self):
response = self.client.get(self.url, follow=True)
self.assertRedirects(response, self.login_url)
@ -42,48 +55,59 @@ class CaseStudyDraftAPITests(TestCase):
response = self.client.delete(self.url, follow=True)
self.assertRedirects(response, self.login_url)
def test_get_and_put(self):
login = self.login()
# The following tests use the requestfactory instead of the django client.
def test_get_empty(self):
request = self.factory.get(self.url)
request.user = self.user
response = DraftsAPI.as_view()(request)
# No draft should currently exist
response = self.client.get(self.url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.content, b"")
# Putting a draft should succeed
response = self.client.put(self.url, '{"test":1}')
def test_put_works(self):
contents = '{"test":1}'
request = self.factory.put(self.url, data=contents)
request.user = self.user
response = DraftsAPI.as_view()(request)
self.assertEqual(response.status_code, 201)
# We should get it back
response = self.client.get(self.url)
def test_get_with_draft(self):
draft = self.fake_draft()
request = self.factory.get(self.url)
request.user = self.user
response = DraftsAPI.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertEqual(response.content, b'{"test":1}')
self.assertEqual(response.content.decode(), draft.data)
# Updating it should succeed
response = self.client.put(self.url, '{"test":2}')
self.assertEqual(response.status_code, 200)
def test_update(self):
draft = self.fake_draft()
new_contents = '{"fnord": 7}'
request = self.factory.put(self.url, data=new_contents)
request.user = self.user
response = DraftsAPI.as_view()(request)
# We should get the update back
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertEqual(response.content, b'{"test":2}')
self.assertEqual(
CaseStudyDraft.objects.filter(author=self.user, data=new_contents).count(),
1,
)
def test_delete(self):
login = self.login()
draft = self.fake_draft()
# Create a draft
response = self.client.put(self.url, '{"test":1}')
self.assertEqual(response.status_code, 201)
request = self.factory.delete(self.url)
request.user = self.user
response = DraftsAPI.as_view()(request)
# Delete it again
response = self.client.delete(self.url)
self.assertEqual(response.status_code, 204)
# Check it's not there
response = self.client.get(self.url)
self.assertEqual(response.status_code, 404)
self.assertEqual(CaseStudyDraft.objects.filter(author=self.user).count(), 0)
class CaseStudyTests(TestCase):