2018-04-14 02:40:42 +00:00
|
|
|
from django.contrib.auth.models import User
|
2018-05-01 17:16:09 +00:00
|
|
|
from django.http import QueryDict
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils.translation import activate
|
2018-04-14 02:40:42 +00:00
|
|
|
from django.urls import reverse
|
2018-05-01 17:16:09 +00:00
|
|
|
|
|
|
|
from .models import CaseStudy, CaseStudyDraft
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CaseStudyDraftAPITests(TestCase):
|
|
|
|
|
|
|
|
test_user = 'testuser1'
|
|
|
|
test_pass = '12345'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
test_user1 = User.objects.create_user(username=self.test_user, password=self.test_pass)
|
|
|
|
test_user1.save()
|
|
|
|
|
2018-05-01 17:16:09 +00:00
|
|
|
activate('en-gb')
|
|
|
|
|
|
|
|
next_url = QueryDict.fromkeys(['next',], reverse('drafts'))
|
|
|
|
|
|
|
|
self.login_url = reverse('auth_login') + '?' + next_url.urlencode()
|
|
|
|
|
2018-04-14 02:40:42 +00:00
|
|
|
def login(self):
|
|
|
|
return self.client.login(username=self.test_user, password=self.test_pass)
|
|
|
|
|
|
|
|
def test_get_not_logged_in(self):
|
2018-05-01 17:16:09 +00:00
|
|
|
response = self.client.get(reverse('drafts'), follow=True)
|
|
|
|
self.assertRedirects(response, self.login_url)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
def test_put_not_logged_in(self):
|
2018-05-01 17:16:09 +00:00
|
|
|
response = self.client.put(reverse('drafts'), follow=True)
|
|
|
|
self.assertRedirects(response, self.login_url)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
def test_delete_not_logged_in(self):
|
2018-05-01 17:16:09 +00:00
|
|
|
response = self.client.delete(reverse('drafts'), follow=True)
|
|
|
|
self.assertRedirects(response, self.login_url)
|
2018-04-14 02:40:42 +00:00
|
|
|
|
|
|
|
def test_get_and_put(self):
|
|
|
|
login = self.login()
|
|
|
|
|
|
|
|
# No draft should currently exist
|
|
|
|
response = self.client.get(reverse('drafts'))
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
self.assertEqual(response.content, b"")
|
|
|
|
|
|
|
|
# Putting a draft should succeed
|
|
|
|
response = self.client.put(reverse('drafts'), '{"test":1}')
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
|
|
|
|
# We should get it back
|
|
|
|
response = self.client.get(reverse('drafts'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response['Content-Type'], "application/json")
|
|
|
|
self.assertEqual(response.content, b'{"test":1}')
|
|
|
|
|
|
|
|
# Updating it should succeed
|
|
|
|
response = self.client.put(reverse('drafts'), '{"test":2}')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
# We should get the update back
|
|
|
|
response = self.client.get(reverse('drafts'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response['Content-Type'], "application/json")
|
|
|
|
self.assertEqual(response.content, b'{"test":2}')
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
login = self.login()
|
|
|
|
|
|
|
|
# Create a draft
|
|
|
|
response = self.client.put(reverse('drafts'), '{"test":1}')
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
|
|
|
|
# Delete it again
|
|
|
|
response = self.client.delete(reverse('drafts'))
|
|
|
|
self.assertEqual(response.status_code, 204)
|
|
|
|
|
|
|
|
# Check it's not there
|
|
|
|
response = self.client.get(reverse('drafts'))
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
|
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)
|