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