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.urls import reverse from django.utils.translation import activate from .views import DraftsAPI from .models import CaseStudy from .models import CaseStudyDraft class CaseStudyDraftAPITests(TestCase): def setUp(self): #  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 ) self.user.save() # 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) def test_put_not_logged_in(self): response = self.client.put(self.url, follow=True) self.assertRedirects(response, self.login_url) def test_delete_not_logged_in(self): response = self.client.delete(self.url, follow=True) self.assertRedirects(response, self.login_url) # 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) self.assertEqual(response.status_code, 404) self.assertEqual(response.content, b"") 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) 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.decode(), draft.data) 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) self.assertEqual(response.status_code, 200) self.assertEqual( CaseStudyDraft.objects.filter(author=self.user, data=new_contents).count(), 1, ) def test_delete(self): draft = self.fake_draft() request = self.factory.delete(self.url) request.user = self.user response = DraftsAPI.as_view()(request) self.assertEqual(response.status_code, 204) self.assertEqual(CaseStudyDraft.objects.filter(author=self.user).count(), 0) class CaseStudyTests(TestCase): def test_youtube(self): case_study = CaseStudy() case_study.video = "https://www.youtube.com/watch?v=h2V4-UP7ML0" self.assertIs(case_study.is_video_youtube(), True) # case_study.video = "https://youtu.be/h2V4-UP7ML0" # self.assertIs(case_study.is_video_youtube(), True)