Mercurial > public > sg101
comparison core/tests/images/test_upload.py @ 1230:f2977a75bf6e modernize
Add unit test for local upload of images.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 20 Apr 2025 17:49:44 -0500 |
parents | |
children | bbabbbb80d60 |
comparison
equal
deleted
inserted
replaced
1229:51172fb756f4 | 1230:f2977a75bf6e |
---|---|
1 import unittest | |
2 | |
3 from mock import patch, call, mock_open, Mock | |
4 from core.images.upload import process_upload | |
5 | |
6 class ProcessUploadTestCase(unittest.TestCase): | |
7 | |
8 @patch('core.images.upload.Site.objects.get_current') | |
9 @patch('core.images.upload.os.chmod') | |
10 @patch('core.images.upload.uuid') | |
11 @patch('core.images.upload.shutil') | |
12 @patch('core.images.upload.BytesIO') | |
13 @patch('core.images.upload.orient_image') | |
14 @patch('core.images.upload.Image') | |
15 @patch('core.images.upload.settings.SITE_SCHEME', new='https') | |
16 @patch('core.images.upload.settings.USER_PHOTOS_LOCAL_UPLOAD', new=True) | |
17 @patch('core.images.upload.settings.MEDIA_ROOT', new='/media') | |
18 @patch('core.images.upload.settings.USER_PHOTOS_LOCAL_UPLOAD_DIR', new='user_upload') | |
19 def test_local_upload(self, image_mock, orient_mock, bytes_mock, | |
20 shutil_mock, uuid_mock, chmod_mock, current_mock): | |
21 filename = 'image.jpg' | |
22 orient_mock.return_value = (True, image_mock.open) | |
23 | |
24 image = Mock() | |
25 image.size = (2000, 2000) | |
26 image_mock.open.return_value = image | |
27 | |
28 uuid_mock.uuid4().hex = 'deadbeef' | |
29 | |
30 site_mock = Mock() | |
31 site_mock.configure_mock(name='Site', domain='site.com') | |
32 current_mock.return_value = site_mock | |
33 | |
34 open_mock = mock_open() | |
35 with patch('__builtin__.open', open_mock): | |
36 result = process_upload(None, filename) | |
37 | |
38 self.assertEqual(image_mock.open.mock_calls, [ | |
39 call(filename), | |
40 call.save(filename), | |
41 call(filename), | |
42 call().thumbnail((660, 720), image_mock.ANTIALIAS), | |
43 call().save(filename), | |
44 call(filename), | |
45 call().thumbnail((120, 120), image_mock.ANTIALIAS), | |
46 call().save(bytes_mock(), format=image.format), | |
47 ]) | |
48 orient_mock.assert_called_with(image_mock.open()) | |
49 | |
50 expected_path = '/media/user_upload/deadbeef.jpg' | |
51 expected_thumb_path = '/media/user_upload/deadbeeft.jpg' | |
52 shutil_mock.copy.assert_called_once_with(filename, expected_path) | |
53 self.assertEqual(chmod_mock.mock_calls, [ | |
54 call(expected_path, 0644), | |
55 call(expected_thumb_path, 0644), | |
56 ]) | |
57 | |
58 bytes_mock().seek.assert_called_once_with(0) | |
59 self.assertEqual(open_mock.mock_calls, [ | |
60 call(expected_thumb_path, 'wb'), | |
61 call().__enter__(), | |
62 call().write(bytes_mock().getvalue()), | |
63 call().__exit__(None, None, None), | |
64 ]) | |
65 self.assertEqual(result, | |
66 ('https://site.com/media/user_upload/deadbeef.jpg', | |
67 'https://site.com/media/user_upload/deadbeeft.jpg')) |