comparison user_photos/tests/test_forms.py @ 976:f5aa74dcdd7a

Ensure temporary files get deleted during hotlinking.
author Brian Neal <bgneal@gmail.com>
date Mon, 05 Oct 2015 20:07:44 -0500
parents 6f55c086db1e
children
comparison
equal deleted inserted replaced
975:8c3d52b7cbd1 976:f5aa74dcdd7a
1 """ 1 """
2 Tests for the forms in the user_photos application. 2 Tests for the forms in the user_photos application.
3 """ 3 """
4 from contextlib import contextmanager
4 5
5 import mock 6 import mock
6 from django import forms 7 from django import forms
7 from django.conf import settings 8 from django.conf import settings
8 from django.contrib.auth.models import User 9 from django.contrib.auth.models import User
9 from django.test import TestCase 10 from django.test import TestCase
10 11
11 from user_photos.forms import HotLinkImageForm 12 from user_photos.forms import HotLinkImageForm
13
14
15 @contextmanager
16 def fake_remove_file(path):
17 yield path
12 18
13 19
14 class HotLinkImageFormTestCase(TestCase): 20 class HotLinkImageFormTestCase(TestCase):
15 21
16 def setUp(self): 22 def setUp(self):
39 form = HotLinkImageForm(args, user=self.user) 45 form = HotLinkImageForm(args, user=self.user)
40 self.assertFalse(form.is_valid()) 46 self.assertFalse(form.is_valid())
41 47
42 @mock.patch('user_photos.forms.rate_limit_user') 48 @mock.patch('user_photos.forms.rate_limit_user')
43 @mock.patch('user_photos.forms.download_file') 49 @mock.patch('user_photos.forms.download_file')
50 @mock.patch('user_photos.forms.remove_file', new=fake_remove_file)
44 @mock.patch('user_photos.forms.S3Bucket') 51 @mock.patch('user_photos.forms.S3Bucket')
45 @mock.patch('user_photos.forms.upload') 52 @mock.patch('user_photos.forms.upload')
46 def test_white_listed_url(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock): 53 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]) 54 url = 'https://{}/a.jpg'.format(settings.USER_IMAGES_SOURCES[0])
48 args = {'url': url} 55 args = {'url': url}
54 self.assertFalse(bucket_mock.called) 61 self.assertFalse(bucket_mock.called)
55 self.assertFalse(upload_mock.called) 62 self.assertFalse(upload_mock.called)
56 63
57 @mock.patch('user_photos.forms.rate_limit_user') 64 @mock.patch('user_photos.forms.rate_limit_user')
58 @mock.patch('user_photos.forms.download_file') 65 @mock.patch('user_photos.forms.download_file')
66 @mock.patch('user_photos.forms.remove_file', new=fake_remove_file)
59 @mock.patch('user_photos.forms.S3Bucket') 67 @mock.patch('user_photos.forms.S3Bucket')
60 @mock.patch('user_photos.forms.upload') 68 @mock.patch('user_photos.forms.upload')
61 def test_happy_path(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock): 69 def test_happy_path(self, upload_mock, bucket_mock, dl_mock, rate_limit_mock):
62 url = 'http://example.com/a.jpg' 70 url = 'http://example.com/a.jpg'
63 args = {'url': url} 71 args = {'url': url}