annotate user_photos/tests/test_views.py @ 1072:4054f22583b3

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