2018-04-14 02:40:42 +00:00
|
|
|
|
from django.contrib.auth.models import User
|
2019-04-07 17:32:40 +00:00
|
|
|
|
from django.contrib.sessions.middleware import SessionMiddleware
|
2018-05-01 17:16:09 +00:00
|
|
|
|
from django.http import QueryDict
|
2019-04-07 17:32:40 +00:00
|
|
|
|
from django.test import RequestFactory
|
2018-05-01 17:16:09 +00:00
|
|
|
|
from django.test import TestCase
|
2018-04-14 02:40:42 +00:00
|
|
|
|
from django.urls import reverse
|
2019-04-07 17:32:40 +00:00
|
|
|
|
from django.utils.translation import activate
|
2018-05-01 17:16:09 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
from .views import DraftsAPI
|
|
|
|
|
from .models import CaseStudy
|
|
|
|
|
from .models import CaseStudyDraft
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CaseStudyDraftAPITests(TestCase):
|
|
|
|
|
def setUp(self):
|
2019-04-07 17:32:40 +00:00
|
|
|
|
# Set up a user object
|
|
|
|
|
self.test_username = "testuser1"
|
|
|
|
|
self.test_password = "12345"
|
|
|
|
|
self.user = User.objects.create_user(
|
2019-04-07 16:19:52 +00:00
|
|
|
|
username=self.test_username, password=self.test_password
|
|
|
|
|
)
|
2019-04-07 17:32:40 +00:00
|
|
|
|
self.user.save()
|
2018-05-01 17:16:09 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
# Some useful vars
|
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
self.url = reverse("drafts")
|
|
|
|
|
print(self.url)
|
2018-05-01 17:16:09 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
# Work out login URL
|
2019-04-07 16:47:03 +00:00
|
|
|
|
next_url = QueryDict.fromkeys(["next"], self.url)
|
2019-04-07 16:19:52 +00:00
|
|
|
|
self.login_url = reverse("auth_login") + "?" + next_url.urlencode()
|
2018-05-01 17:16:09 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
# Activate a language pack
|
|
|
|
|
activate("en-gb")
|
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
def login(self):
|
2019-04-07 16:19:52 +00:00
|
|
|
|
return self.client.login(
|
|
|
|
|
username=self.test_username, password=self.test_password
|
|
|
|
|
)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
def fake_draft(self):
|
|
|
|
|
draft = CaseStudyDraft(author=self.user, data='{"test":1}')
|
|
|
|
|
draft.save()
|
|
|
|
|
return draft
|
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
def test_get_not_logged_in(self):
|
2019-04-07 16:47:03 +00:00
|
|
|
|
response = self.client.get(self.url, follow=True)
|
2018-05-01 17:16:09 +00:00
|
|
|
|
self.assertRedirects(response, self.login_url)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
def test_put_not_logged_in(self):
|
2019-04-07 16:47:03 +00:00
|
|
|
|
response = self.client.put(self.url, follow=True)
|
2018-05-01 17:16:09 +00:00
|
|
|
|
self.assertRedirects(response, self.login_url)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
def test_delete_not_logged_in(self):
|
2019-04-07 16:47:03 +00:00
|
|
|
|
response = self.client.delete(self.url, follow=True)
|
2018-05-01 17:16:09 +00:00
|
|
|
|
self.assertRedirects(response, self.login_url)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
# 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)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
self.assertEqual(response.content, b"")
|
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-04-07 16:19:52 +00:00
|
|
|
|
self.assertEqual(response["Content-Type"], "application/json")
|
2019-04-07 17:32:40 +00:00
|
|
|
|
self.assertEqual(response.content.decode(), draft.data)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
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)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-04-07 17:32:40 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
CaseStudyDraft.objects.filter(author=self.user, data=new_contents).count(),
|
|
|
|
|
1,
|
|
|
|
|
)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
def test_delete(self):
|
2019-04-07 17:32:40 +00:00
|
|
|
|
draft = self.fake_draft()
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
2019-04-07 17:32:40 +00:00
|
|
|
|
request = self.factory.delete(self.url)
|
|
|
|
|
request.user = self.user
|
|
|
|
|
response = DraftsAPI.as_view()(request)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 204)
|
2019-04-07 17:32:40 +00:00
|
|
|
|
self.assertEqual(CaseStudyDraft.objects.filter(author=self.user).count(), 0)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
2018-03-28 02:24:43 +00:00
|
|
|
|
class CaseStudyTests(TestCase):
|
2018-12-02 00:38:15 +00:00
|
|
|
|
def test_youtube(self):
|
2018-03-28 02:24:43 +00:00
|
|
|
|
case_study = CaseStudy()
|
2018-12-02 00:38:15 +00:00
|
|
|
|
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)
|