Refactor CaseStudyDraft tests to make them more unit-y
This commit is contained in:
parent
30b0a680aa
commit
edf62268aa
@ -1,35 +1,48 @@
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.contrib.sessions.middleware import SessionMiddleware
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
|
from django.test import RequestFactory
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils.translation import activate
|
|
||||||
from django.urls import reverse
|
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):
|
class CaseStudyDraftAPITests(TestCase):
|
||||||
test_username = "testuser1"
|
|
||||||
test_password = "12345"
|
|
||||||
|
|
||||||
|
|
||||||
def setUp(self):
|
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
|
username=self.test_username, password=self.test_password
|
||||||
)
|
)
|
||||||
test_user1.save()
|
self.user.save()
|
||||||
|
|
||||||
activate("en-gb")
|
# Some useful vars
|
||||||
|
self.factory = RequestFactory()
|
||||||
self.url = reverse('drafts')
|
self.url = reverse("drafts")
|
||||||
|
print(self.url)
|
||||||
|
|
||||||
|
# Work out login URL
|
||||||
next_url = QueryDict.fromkeys(["next"], self.url)
|
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()
|
||||||
|
|
||||||
|
# Activate a language pack
|
||||||
|
activate("en-gb")
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
return self.client.login(
|
return self.client.login(
|
||||||
username=self.test_username, password=self.test_password
|
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):
|
def test_get_not_logged_in(self):
|
||||||
response = self.client.get(self.url, follow=True)
|
response = self.client.get(self.url, follow=True)
|
||||||
self.assertRedirects(response, self.login_url)
|
self.assertRedirects(response, self.login_url)
|
||||||
@ -42,48 +55,59 @@ class CaseStudyDraftAPITests(TestCase):
|
|||||||
response = self.client.delete(self.url, 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):
|
# The following tests use the requestfactory instead of the django client.
|
||||||
login = self.login()
|
|
||||||
|
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.status_code, 404)
|
||||||
self.assertEqual(response.content, b"")
|
self.assertEqual(response.content, b"")
|
||||||
|
|
||||||
# Putting a draft should succeed
|
def test_put_works(self):
|
||||||
response = self.client.put(self.url, '{"test":1}')
|
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)
|
self.assertEqual(response.status_code, 201)
|
||||||
|
|
||||||
# We should get it back
|
def test_get_with_draft(self):
|
||||||
response = self.client.get(self.url)
|
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.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.decode(), draft.data)
|
||||||
|
|
||||||
# Updating it should succeed
|
def test_update(self):
|
||||||
response = self.client.put(self.url, '{"test":2}')
|
draft = self.fake_draft()
|
||||||
self.assertEqual(response.status_code, 200)
|
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.status_code, 200)
|
||||||
self.assertEqual(response["Content-Type"], "application/json")
|
self.assertEqual(
|
||||||
self.assertEqual(response.content, b'{"test":2}')
|
CaseStudyDraft.objects.filter(author=self.user, data=new_contents).count(),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
login = self.login()
|
draft = self.fake_draft()
|
||||||
|
|
||||||
# Create a draft
|
request = self.factory.delete(self.url)
|
||||||
response = self.client.put(self.url, '{"test":1}')
|
request.user = self.user
|
||||||
self.assertEqual(response.status_code, 201)
|
response = DraftsAPI.as_view()(request)
|
||||||
|
|
||||||
# Delete it again
|
|
||||||
response = self.client.delete(self.url)
|
|
||||||
self.assertEqual(response.status_code, 204)
|
self.assertEqual(response.status_code, 204)
|
||||||
|
self.assertEqual(CaseStudyDraft.objects.filter(author=self.user).count(), 0)
|
||||||
# Check it's not there
|
|
||||||
response = self.client.get(self.url)
|
|
||||||
self.assertEqual(response.status_code, 404)
|
|
||||||
|
|
||||||
|
|
||||||
class CaseStudyTests(TestCase):
|
class CaseStudyTests(TestCase):
|
||||||
|
Loading…
Reference in New Issue
Block a user