Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Unit tests for the framework_helpers module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import mock |
| 12 | import unittest |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | from google.appengine.api import urlfetch |
| 15 | from google.appengine.ext import testbed |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 16 | from google.cloud import storage |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 18 | from framework import gcs_helpers |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | from testing import testing_helpers |
| 20 | |
| 21 | |
| 22 | class GcsHelpersTest(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 25 | self.testbed = testbed.Testbed() |
| 26 | self.testbed.activate() |
| 27 | self.testbed.init_memcache_stub() |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 28 | self.testbed.init_app_identity_stub() |
| 29 | |
| 30 | self.test_storage_client = mock.create_autospec( |
| 31 | storage.Client, instance=True) |
| 32 | mock.patch.object( |
| 33 | storage, 'Client', return_value=self.test_storage_client).start() |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 34 | |
| 35 | def tearDown(self): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 36 | self.testbed.deactivate() |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 37 | self.test_storage_client = None |
| 38 | mock.patch.stopall() |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 39 | |
| 40 | def testDeleteObjectFromGCS(self): |
| 41 | object_id = 'aaaaa' |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 42 | gcs_helpers.DeleteObjectFromGCS(object_id) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 43 | # Verify order of client calls. |
| 44 | self.test_storage_client.assert_has_calls( |
| 45 | [ |
| 46 | mock.call.bucket().get_blob(object_id), |
| 47 | mock.call.bucket().get_blob().delete() |
| 48 | ]) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 49 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 50 | def testDeleteLegacyObjectFromGCS(self): |
| 51 | # A previous python module expected object ids with leading '/' |
| 52 | object_id = '/aaaaa' |
| 53 | object_id_without_leading_slash = 'aaaaa' |
| 54 | gcs_helpers.DeleteObjectFromGCS(object_id) |
| 55 | # Verify order of client calls. |
| 56 | self.test_storage_client.assert_has_calls( |
| 57 | [ |
| 58 | mock.call.bucket().get_blob(object_id_without_leading_slash), |
| 59 | mock.call.bucket().get_blob().delete() |
| 60 | ]) |
| 61 | |
| 62 | @mock.patch( |
| 63 | 'google.appengine.api.images.resize', return_value=mock.MagicMock()) |
| 64 | @mock.patch('uuid.uuid4') |
| 65 | def testStoreObjectInGCS_ResizableMimeType(self, mock_uuid4, mock_resize): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 66 | guid = 'aaaaa' |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 67 | mock_uuid4.return_value = guid |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 68 | project_id = 100 |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 69 | blob_name = '%s/attachments/%s' % (project_id, guid) |
| 70 | thumb_blob_name = '%s/attachments/%s-thumbnail' % (project_id, guid) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 71 | mime_type = 'image/png' |
| 72 | content = 'content' |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 73 | |
| 74 | ret_id = gcs_helpers.StoreObjectInGCS( |
| 75 | content, mime_type, project_id, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 76 | gcs_helpers.DEFAULT_THUMB_HEIGHT) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 77 | self.assertEqual('/%s' % blob_name, ret_id) |
| 78 | self.test_storage_client.assert_has_calls( |
| 79 | [ |
| 80 | mock.call.bucket().blob(blob_name), |
| 81 | mock.call.bucket().blob().upload_from_string( |
| 82 | content, content_type=mime_type), |
| 83 | mock.call.bucket().blob(thumb_blob_name), |
| 84 | ]) |
| 85 | mock_resize.assert_called() |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 86 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 87 | @mock.patch( |
| 88 | 'google.appengine.api.images.resize', return_value=mock.MagicMock()) |
| 89 | @mock.patch('uuid.uuid4') |
| 90 | def testStoreObjectInGCS_NotResizableMimeType(self, mock_uuid4, mock_resize): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 91 | guid = 'aaaaa' |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 92 | mock_uuid4.return_value = guid |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 93 | project_id = 100 |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 94 | blob_name = '%s/attachments/%s' % (project_id, guid) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 95 | mime_type = 'not_resizable_mime_type' |
| 96 | content = 'content' |
| 97 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 98 | ret_id = gcs_helpers.StoreObjectInGCS( |
| 99 | content, mime_type, project_id, gcs_helpers.DEFAULT_THUMB_WIDTH, |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 100 | gcs_helpers.DEFAULT_THUMB_HEIGHT) |
| 101 | self.assertEqual('/%s' % blob_name, ret_id) |
| 102 | self.test_storage_client.assert_has_calls( |
| 103 | [ |
| 104 | mock.call.bucket().blob(blob_name), |
| 105 | mock.call.bucket().blob().upload_from_string( |
| 106 | content, content_type=mime_type), |
| 107 | ]) |
| 108 | mock_resize.assert_not_called() |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 109 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 110 | def testCheckMimeTypeResizable(self): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 111 | for resizable_mime_type in gcs_helpers.RESIZABLE_MIME_TYPES: |
| 112 | gcs_helpers.CheckMimeTypeResizable(resizable_mime_type) |
| 113 | |
| 114 | with self.assertRaises(gcs_helpers.UnsupportedMimeType): |
| 115 | gcs_helpers.CheckMimeTypeResizable('not_resizable_mime_type') |
| 116 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 117 | @mock.patch('framework.filecontent.GuessContentTypeFromFilename') |
| 118 | @mock.patch('framework.gcs_helpers.StoreObjectInGCS') |
| 119 | def testStoreLogoInGCS(self, mock_store_object, mock_guess_content): |
| 120 | blob_name = 123 |
| 121 | mock_store_object.return_value = blob_name |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 122 | mime_type = 'image/png' |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 123 | mock_guess_content.return_value = mime_type |
| 124 | file_name = 'test_file.png' |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 125 | content = 'test content' |
| 126 | project_id = 100 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 127 | |
| 128 | ret_id = gcs_helpers.StoreLogoInGCS(file_name, content, project_id) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 129 | self.assertEqual(blob_name, ret_id) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 130 | |
| 131 | @mock.patch('google.appengine.api.urlfetch.fetch') |
| 132 | def testFetchSignedURL_Success(self, mock_fetch): |
| 133 | mock_fetch.return_value = testing_helpers.Blank( |
| 134 | headers={'Location': 'signed url'}) |
| 135 | actual = gcs_helpers._FetchSignedURL('signing req url') |
| 136 | mock_fetch.assert_called_with('signing req url', follow_redirects=False) |
| 137 | self.assertEqual('signed url', actual) |
| 138 | |
| 139 | @mock.patch('google.appengine.api.urlfetch.fetch') |
| 140 | def testFetchSignedURL_UnderpopulatedResult(self, mock_fetch): |
| 141 | mock_fetch.return_value = testing_helpers.Blank(headers={}) |
| 142 | self.assertRaises( |
| 143 | KeyError, gcs_helpers._FetchSignedURL, 'signing req url') |
| 144 | |
| 145 | @mock.patch('google.appengine.api.urlfetch.fetch') |
| 146 | def testFetchSignedURL_DownloadError(self, mock_fetch): |
| 147 | mock_fetch.side_effect = urlfetch.DownloadError |
| 148 | self.assertRaises( |
| 149 | urlfetch.DownloadError, |
| 150 | gcs_helpers._FetchSignedURL, 'signing req url') |
| 151 | |
| 152 | @mock.patch('framework.gcs_helpers._FetchSignedURL') |
| 153 | def testSignUrl_Success(self, mock_FetchSignedURL): |
| 154 | with mock.patch( |
| 155 | 'google.appengine.api.app_identity.get_access_token') as gat: |
| 156 | gat.return_value = ['token'] |
| 157 | mock_FetchSignedURL.return_value = 'signed url' |
| 158 | signed_url = gcs_helpers.SignUrl('bucket', '/object') |
| 159 | self.assertEqual('signed url', signed_url) |
| 160 | |
| 161 | @mock.patch('framework.gcs_helpers._FetchSignedURL') |
| 162 | def testSignUrl_DownloadError(self, mock_FetchSignedURL): |
| 163 | mock_FetchSignedURL.side_effect = urlfetch.DownloadError |
| 164 | self.assertEqual( |
| 165 | '/missing-gcs-url', gcs_helpers.SignUrl('bucket', '/object')) |