bgneal@973
|
1 """
|
bgneal@973
|
2 Tests for the forms in the user_photos application.
|
bgneal@973
|
3 """
|
bgneal@973
|
4
|
bgneal@973
|
5 import mock
|
bgneal@973
|
6 from django import forms
|
bgneal@973
|
7 from django.conf import settings
|
bgneal@973
|
8 from django.contrib.auth.models import User
|
bgneal@973
|
9 from django.test import TestCase
|
bgneal@973
|
10
|
bgneal@973
|
11 from user_photos.forms import HotLinkImageForm
|
bgneal@973
|
12
|
bgneal@973
|
13
|
bgneal@973
|
14 class HotLinkImageFormTestCase(TestCase):
|
bgneal@973
|
15
|
bgneal@973
|
16 def setUp(self):
|
bgneal@973
|
17 self.username = 'test_user'
|
bgneal@973
|
18 self.pw = 'password'
|
bgneal@973
|
19 self.user = User.objects.create_user(self.username, '', self.pw)
|
bgneal@973
|
20 self.user.save()
|
bgneal@973
|
21 self.client.login(username=self.username, password=self.pw)
|
bgneal@973
|
22
|
bgneal@973
|
23 @mock.patch('user_photos.forms.rate_limit_user')
|
bgneal@973
|
24 def test_no_url(self, rate_limit_mock):
|
bgneal@973
|
25 args = {}
|
bgneal@973
|
26 form = HotLinkImageForm(args, user=self.user)
|
bgneal@973
|
27 self.assertFalse(form.is_valid())
|
bgneal@973
|
28
|
bgneal@973
|
29 @mock.patch('user_photos.forms.rate_limit_user')
|
bgneal@973
|
30 def test_bad_url(self, rate_limit_mock):
|
bgneal@973
|
31 args = {'url': 'jkdal;jkkls;$JSx49'}
|
bgneal@973
|
32 form = HotLinkImageForm(args, user=self.user)
|
bgneal@973
|
33 self.assertFalse(form.is_valid())
|
bgneal@973
|
34
|
bgneal@973
|
35 @mock.patch('user_photos.forms.rate_limit_user')
|
bgneal@973
|
36 def test_rate_limit(self, rate_limit_mock):
|
bgneal@973
|
37 rate_limit_mock.side_effect = forms.ValidationError("Rate limit exceeded")
|
bgneal@973
|
38 args = {'url': 'http://example.com/a.jpg'}
|
bgneal@973
|
39 form = HotLinkImageForm(args, user=self.user)
|
bgneal@973
|
40 self.assertFalse(form.is_valid())
|
bgneal@973
|
41
|
bgneal@973
|
42 @mock.patch('user_photos.forms.rate_limit_user')
|
bgneal@973
|
43 @mock.patch('user_photos.forms.download_file')
|
bgneal@973
|
44 @mock.patch('user_photos.forms.S3Bucket')
|
bgneal@973
|
45 @mock.patch('user_photos.forms.upload')
|
bgneal@973
|
46 def test_white_listed_url(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock):
|
bgneal@973
|
47 url = 'https://{}/a.jpg'.format(settings.USER_IMAGES_SOURCES[0])
|
bgneal@973
|
48 args = {'url': url}
|
bgneal@973
|
49 form = HotLinkImageForm(args, user=self.user)
|
bgneal@973
|
50 self.assertTrue(form.is_valid())
|
bgneal@973
|
51 result = form.save()
|
bgneal@973
|
52 self.assertEqual(result, url)
|
bgneal@973
|
53 self.assertFalse(dl_mock.called)
|
bgneal@973
|
54 self.assertFalse(bucket_mock.called)
|
bgneal@973
|
55 self.assertFalse(upload_mock.called)
|
bgneal@973
|
56
|
bgneal@973
|
57 @mock.patch('user_photos.forms.rate_limit_user')
|
bgneal@973
|
58 @mock.patch('user_photos.forms.download_file')
|
bgneal@973
|
59 @mock.patch('user_photos.forms.S3Bucket')
|
bgneal@973
|
60 @mock.patch('user_photos.forms.upload')
|
bgneal@973
|
61 def test_happy_path(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock):
|
bgneal@973
|
62 url = 'http://example.com/a.jpg'
|
bgneal@973
|
63 args = {'url': url}
|
bgneal@973
|
64 form = HotLinkImageForm(args, user=self.user)
|
bgneal@973
|
65 self.assertTrue(form.is_valid())
|
bgneal@973
|
66 new_url = 'https://img.example.com/a.jpg'
|
bgneal@973
|
67 upload_mock.return_value = (new_url, None)
|
bgneal@973
|
68 result = form.save()
|
bgneal@973
|
69 dl_mock.assert_called_once_with(url)
|
bgneal@973
|
70 self.assertEqual(result, new_url)
|
bgneal@973
|
71
|