Reformat tests file

This commit is contained in:
Anna Sidwell 2019-04-07 17:19:52 +01:00
parent c84de45b10
commit f17834cd3b

View File

@ -8,81 +8,83 @@ from .models import CaseStudy, CaseStudyDraft
class CaseStudyDraftAPITests(TestCase): class CaseStudyDraftAPITests(TestCase):
test_username = "testuser1"
test_user = 'testuser1' test_password = "12345"
test_pass = '12345'
def setUp(self): def setUp(self):
test_user1 = User.objects.create_user(username=self.test_user, password=self.test_pass) test_user1 = User.objects.create_user(
username=self.test_username, password=self.test_password
)
test_user1.save() test_user1.save()
activate('en-gb') activate("en-gb")
next_url = QueryDict.fromkeys(['next',], reverse('drafts')) next_url = QueryDict.fromkeys(["next"], reverse("drafts"))
self.login_url = reverse('auth_login') + '?' + next_url.urlencode() self.login_url = reverse("auth_login") + "?" + next_url.urlencode()
def login(self): def login(self):
return self.client.login(username=self.test_user, password=self.test_pass) return self.client.login(
username=self.test_username, password=self.test_password
)
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(reverse("drafts"), 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(reverse("drafts"), 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(reverse("drafts"), 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(reverse("drafts"))
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(reverse("drafts"), '{"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(reverse("drafts"))
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(reverse("drafts"), '{"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(reverse("drafts"))
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}')
def test_delete(self): def test_delete(self):
login = self.login() login = self.login()
# Create a draft # Create a draft
response = self.client.put(reverse('drafts'), '{"test":1}') response = self.client.put(reverse("drafts"), '{"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(reverse("drafts"))
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(reverse("drafts"))
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
class CaseStudyTests(TestCase): class CaseStudyTests(TestCase):
def test_youtube(self): def test_youtube(self):
case_study = CaseStudy() case_study = CaseStudy()
case_study.video = "https://www.youtube.com/watch?v=h2V4-UP7ML0" case_study.video = "https://www.youtube.com/watch?v=h2V4-UP7ML0"