annotate user_photos/tests/test_forms.py @ 1205:510ef3cbf3e6 modernize tip

Getting SG101 running on my macbook. This is the start of a branch to modernize the SG101 website.
author Brian Neal <bgneal@gmail.com>
date Sat, 04 Jan 2025 21:34:31 -0600
parents 50e511e032db
children
rev   line source
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@1202 51 @mock.patch('user_photos.forms.process_upload')
bgneal@1202 52 def test_white_listed_url(self, upload_mock, dl_mock, rate_limit_mock):
bgneal@973 53 url = 'https://{}/a.jpg'.format(settings.USER_IMAGES_SOURCES[0])
bgneal@973 54 args = {'url': url}
bgneal@973 55 form = HotLinkImageForm(args, user=self.user)
bgneal@973 56 self.assertTrue(form.is_valid())
bgneal@973 57 result = form.save()
bgneal@973 58 self.assertEqual(result, url)
bgneal@973 59 self.assertFalse(dl_mock.called)
bgneal@973 60 self.assertFalse(upload_mock.called)
bgneal@973 61
bgneal@973 62 @mock.patch('user_photos.forms.rate_limit_user')
bgneal@973 63 @mock.patch('user_photos.forms.download_file')
bgneal@976 64 @mock.patch('user_photos.forms.remove_file', new=fake_remove_file)
bgneal@1202 65 @mock.patch('user_photos.forms.process_upload')
bgneal@1202 66 def test_happy_path(self, upload_mock, dl_mock, rate_limit_mock):
bgneal@973 67 url = 'http://example.com/a.jpg'
bgneal@973 68 args = {'url': url}
bgneal@973 69 form = HotLinkImageForm(args, user=self.user)
bgneal@973 70 self.assertTrue(form.is_valid())
bgneal@973 71 new_url = 'https://img.example.com/a.jpg'
bgneal@973 72 upload_mock.return_value = (new_url, None)
bgneal@973 73 result = form.save()
bgneal@973 74 dl_mock.assert_called_once_with(url)
bgneal@973 75 self.assertEqual(result, new_url)
bgneal@973 76