Mercurial > public > sg101
comparison user_photos/tests/test_views.py @ 972:7138883966b3
Started unit tests for hotlinking images.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 23 Sep 2015 21:26:09 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
971:4f265f61874b | 972:7138883966b3 |
---|---|
1 """ | |
2 Tests for the views in the user_photos application. | |
3 | |
4 """ | |
5 import json | |
6 | |
7 import mock | |
8 from django.test import TestCase | |
9 from django.test.utils import override_settings | |
10 from django.contrib.auth.models import User | |
11 from django.core.urlresolvers import reverse | |
12 from django.http import JsonResponse | |
13 | |
14 | |
15 class HotlinkImageTestCase(TestCase): | |
16 | |
17 def setUp(self): | |
18 self.username = 'test_user' | |
19 self.pw = 'password' | |
20 self.user = User.objects.create_user(self.username, '', self.pw) | |
21 self.user.save() | |
22 self.assertTrue(self.client.login(username=self.username, password=self.pw)) | |
23 self.view_url = reverse('user_photos-hotlink') | |
24 | |
25 def testUnauthenticatedUser(self): | |
26 self.client.logout() | |
27 response = self.client.post(self.view_url, | |
28 {'url': 'http://example.com/test.jpg'}, | |
29 HTTP_X_REQUESTED_WITH='XMLHttpRequest') | |
30 self.assertIsInstance(response, JsonResponse) | |
31 self.assertEqual(response.status_code, 403) | |
32 | |
33 def testWrongMethod(self): | |
34 response = self.client.get(self.view_url) | |
35 self.assertIsInstance(response, JsonResponse) | |
36 self.assertEqual(response.status_code, 405) | |
37 | |
38 def testNotAjax(self): | |
39 response = self.client.post(self.view_url, | |
40 {'url': 'http://example.com/test.jpg'}) | |
41 self.assertIsInstance(response, JsonResponse) | |
42 self.assertEqual(response.status_code, 405) | |
43 | |
44 @override_settings(USER_PHOTOS_ENABLED=False) | |
45 def testServiceDisabled(self): | |
46 response = self.client.post(self.view_url, | |
47 {'url': 'http://example.com/test.jpg'}, | |
48 HTTP_X_REQUESTED_WITH='XMLHttpRequest') | |
49 self.assertIsInstance(response, JsonResponse) | |
50 self.assertEqual(response.status_code, 403) | |
51 | |
52 @mock.patch('user_photos.views.HotLinkImageForm') | |
53 def testFormInvalid(self, form_mock): | |
54 form_mock.return_value.is_valid.return_value = False | |
55 form_mock.return_value.non_field_errors.return_value = mock.Mock() | |
56 form_mock.return_value.non_field_errors.return_value.as_text.return_value = 'bad stuff' | |
57 form_mock.return_value.errors.return_value = {} | |
58 | |
59 response = self.client.post(self.view_url, | |
60 {'url': 'http://example.com/test.jpg'}, | |
61 HTTP_X_REQUESTED_WITH='XMLHttpRequest') | |
62 self.assertIsInstance(response, JsonResponse) | |
63 self.assertEqual(response.status_code, 400) | |
64 | |
65 @mock.patch('user_photos.views.HotLinkImageForm') | |
66 def testHappyPath(self, form_mock): | |
67 form_mock.return_value.is_valid.return_value = True | |
68 form_mock.return_value.save.return_value = 'https://example.com/a.jpg' | |
69 | |
70 response = self.client.post(self.view_url, | |
71 {'url': 'http://example.com/test.jpg'}, | |
72 HTTP_X_REQUESTED_WITH='XMLHttpRequest') | |
73 self.assertIsInstance(response, JsonResponse) | |
74 self.assertEqual(response.status_code, 200) | |
75 expected = {"url": "https://example.com/a.jpg", "error_msg": ""} | |
76 actual = json.loads(response.content) | |
77 self.assertEqual(actual, expected) | |
78 | |
79 @mock.patch('user_photos.views.HotLinkImageForm') | |
80 def testFormSaveRaises(self, form_mock): | |
81 form_mock.return_value.is_valid.return_value = True | |
82 form_mock.return_value.save.side_effect = ValueError | |
83 | |
84 response = self.client.post(self.view_url, | |
85 {'url': 'http://example.com/test.jpg'}, | |
86 HTTP_X_REQUESTED_WITH='XMLHttpRequest') | |
87 self.assertIsInstance(response, JsonResponse) | |
88 self.assertEqual(response.status_code, 500) |