bgneal@972: """ bgneal@972: Tests for the views in the user_photos application. bgneal@972: bgneal@972: """ bgneal@972: import json bgneal@972: bgneal@972: import mock bgneal@972: from django.test import TestCase bgneal@972: from django.test.utils import override_settings bgneal@972: from django.contrib.auth.models import User bgneal@972: from django.core.urlresolvers import reverse bgneal@972: from django.http import JsonResponse bgneal@972: bgneal@972: bgneal@972: class HotlinkImageTestCase(TestCase): bgneal@972: bgneal@972: def setUp(self): bgneal@972: self.username = 'test_user' bgneal@972: self.pw = 'password' bgneal@972: self.user = User.objects.create_user(self.username, '', self.pw) bgneal@972: self.user.save() bgneal@972: self.assertTrue(self.client.login(username=self.username, password=self.pw)) bgneal@972: self.view_url = reverse('user_photos-hotlink') bgneal@972: bgneal@972: def testUnauthenticatedUser(self): bgneal@972: self.client.logout() bgneal@972: response = self.client.post(self.view_url, bgneal@972: {'url': 'http://example.com/test.jpg'}, bgneal@972: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 403) bgneal@972: bgneal@972: def testWrongMethod(self): bgneal@972: response = self.client.get(self.view_url) bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 405) bgneal@972: bgneal@972: def testNotAjax(self): bgneal@972: response = self.client.post(self.view_url, bgneal@972: {'url': 'http://example.com/test.jpg'}) bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 405) bgneal@972: bgneal@972: @override_settings(USER_PHOTOS_ENABLED=False) bgneal@972: def testServiceDisabled(self): bgneal@972: response = self.client.post(self.view_url, bgneal@972: {'url': 'http://example.com/test.jpg'}, bgneal@972: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 403) bgneal@972: bgneal@972: @mock.patch('user_photos.views.HotLinkImageForm') bgneal@972: def testFormInvalid(self, form_mock): bgneal@972: form_mock.return_value.is_valid.return_value = False bgneal@972: form_mock.return_value.non_field_errors.return_value = mock.Mock() bgneal@972: form_mock.return_value.non_field_errors.return_value.as_text.return_value = 'bad stuff' bgneal@972: form_mock.return_value.errors.return_value = {} bgneal@972: bgneal@972: response = self.client.post(self.view_url, bgneal@972: {'url': 'http://example.com/test.jpg'}, bgneal@972: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 400) bgneal@972: bgneal@972: @mock.patch('user_photos.views.HotLinkImageForm') bgneal@972: def testHappyPath(self, form_mock): bgneal@972: form_mock.return_value.is_valid.return_value = True bgneal@972: form_mock.return_value.save.return_value = 'https://example.com/a.jpg' bgneal@972: bgneal@972: response = self.client.post(self.view_url, bgneal@972: {'url': 'http://example.com/test.jpg'}, bgneal@972: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 200) bgneal@972: expected = {"url": "https://example.com/a.jpg", "error_msg": ""} bgneal@972: actual = json.loads(response.content) bgneal@972: self.assertEqual(actual, expected) bgneal@972: bgneal@972: @mock.patch('user_photos.views.HotLinkImageForm') bgneal@972: def testFormSaveRaises(self, form_mock): bgneal@972: form_mock.return_value.is_valid.return_value = True bgneal@972: form_mock.return_value.save.side_effect = ValueError bgneal@972: bgneal@972: response = self.client.post(self.view_url, bgneal@972: {'url': 'http://example.com/test.jpg'}, bgneal@972: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@972: self.assertIsInstance(response, JsonResponse) bgneal@972: self.assertEqual(response.status_code, 500)