comparison user_photos/tests/test_forms.py @ 973:6f55c086db1e

Guess file extension based on content-type. When downloading a file, and no path is supplied to store it, guess the file extension using mimetypes and the content-type header. Also supply a unit test for the HotLinkImageForm.
author Brian Neal <bgneal@gmail.com>
date Thu, 01 Oct 2015 19:44:45 -0500
parents 7138883966b3
children f5aa74dcdd7a
comparison
equal deleted inserted replaced
972:7138883966b3 973:6f55c086db1e
1 """
2 Tests for the forms in the user_photos application.
3 """
4
5 import mock
6 from django import forms
7 from django.conf import settings
8 from django.contrib.auth.models import User
9 from django.test import TestCase
10
11 from user_photos.forms import HotLinkImageForm
12
13
14 class HotLinkImageFormTestCase(TestCase):
15
16 def setUp(self):
17 self.username = 'test_user'
18 self.pw = 'password'
19 self.user = User.objects.create_user(self.username, '', self.pw)
20 self.user.save()
21 self.client.login(username=self.username, password=self.pw)
22
23 @mock.patch('user_photos.forms.rate_limit_user')
24 def test_no_url(self, rate_limit_mock):
25 args = {}
26 form = HotLinkImageForm(args, user=self.user)
27 self.assertFalse(form.is_valid())
28
29 @mock.patch('user_photos.forms.rate_limit_user')
30 def test_bad_url(self, rate_limit_mock):
31 args = {'url': 'jkdal;jkkls;$JSx49'}
32 form = HotLinkImageForm(args, user=self.user)
33 self.assertFalse(form.is_valid())
34
35 @mock.patch('user_photos.forms.rate_limit_user')
36 def test_rate_limit(self, rate_limit_mock):
37 rate_limit_mock.side_effect = forms.ValidationError("Rate limit exceeded")
38 args = {'url': 'http://example.com/a.jpg'}
39 form = HotLinkImageForm(args, user=self.user)
40 self.assertFalse(form.is_valid())
41
42 @mock.patch('user_photos.forms.rate_limit_user')
43 @mock.patch('user_photos.forms.download_file')
44 @mock.patch('user_photos.forms.S3Bucket')
45 @mock.patch('user_photos.forms.upload')
46 def test_white_listed_url(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock):
47 url = 'https://{}/a.jpg'.format(settings.USER_IMAGES_SOURCES[0])
48 args = {'url': url}
49 form = HotLinkImageForm(args, user=self.user)
50 self.assertTrue(form.is_valid())
51 result = form.save()
52 self.assertEqual(result, url)
53 self.assertFalse(dl_mock.called)
54 self.assertFalse(bucket_mock.called)
55 self.assertFalse(upload_mock.called)
56
57 @mock.patch('user_photos.forms.rate_limit_user')
58 @mock.patch('user_photos.forms.download_file')
59 @mock.patch('user_photos.forms.S3Bucket')
60 @mock.patch('user_photos.forms.upload')
61 def test_happy_path(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock):
62 url = 'http://example.com/a.jpg'
63 args = {'url': url}
64 form = HotLinkImageForm(args, user=self.user)
65 self.assertTrue(form.is_valid())
66 new_url = 'https://img.example.com/a.jpg'
67 upload_mock.return_value = (new_url, None)
68 result = form.save()
69 dl_mock.assert_called_once_with(url)
70 self.assertEqual(result, new_url)
71