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 |
| 13 | import uuid |
| 14 | |
| 15 | import mox |
| 16 | |
| 17 | from google.appengine.api import app_identity |
| 18 | from google.appengine.api import images |
| 19 | from google.appengine.api import urlfetch |
| 20 | from google.appengine.ext import testbed |
| 21 | from third_party import cloudstorage |
| 22 | |
| 23 | from framework import filecontent |
| 24 | from framework import gcs_helpers |
| 25 | from testing import fake |
| 26 | from testing import testing_helpers |
| 27 | |
| 28 | |
| 29 | class GcsHelpersTest(unittest.TestCase): |
| 30 | |
| 31 | def setUp(self): |
| 32 | self.mox = mox.Mox() |
| 33 | self.testbed = testbed.Testbed() |
| 34 | self.testbed.activate() |
| 35 | self.testbed.init_memcache_stub() |
| 36 | |
| 37 | def tearDown(self): |
| 38 | self.mox.UnsetStubs() |
| 39 | self.mox.ResetAll() |
| 40 | self.testbed.deactivate() |
| 41 | |
| 42 | def testDeleteObjectFromGCS(self): |
| 43 | object_id = 'aaaaa' |
| 44 | bucket_name = 'test_bucket' |
| 45 | object_path = '/' + bucket_name + object_id |
| 46 | |
| 47 | self.mox.StubOutWithMock(app_identity, 'get_default_gcs_bucket_name') |
| 48 | app_identity.get_default_gcs_bucket_name().AndReturn(bucket_name) |
| 49 | |
| 50 | self.mox.StubOutWithMock(cloudstorage, 'delete') |
| 51 | cloudstorage.delete(object_path) |
| 52 | |
| 53 | self.mox.ReplayAll() |
| 54 | |
| 55 | gcs_helpers.DeleteObjectFromGCS(object_id) |
| 56 | self.mox.VerifyAll() |
| 57 | |
| 58 | def testStoreObjectInGCS_ResizableMimeType(self): |
| 59 | guid = 'aaaaa' |
| 60 | project_id = 100 |
| 61 | object_id = '/%s/attachments/%s' % (project_id, guid) |
| 62 | bucket_name = 'test_bucket' |
| 63 | object_path = '/' + bucket_name + object_id |
| 64 | mime_type = 'image/png' |
| 65 | content = 'content' |
| 66 | thumb_content = 'thumb_content' |
| 67 | |
| 68 | self.mox.StubOutWithMock(app_identity, 'get_default_gcs_bucket_name') |
| 69 | app_identity.get_default_gcs_bucket_name().AndReturn(bucket_name) |
| 70 | |
| 71 | self.mox.StubOutWithMock(uuid, 'uuid4') |
| 72 | uuid.uuid4().AndReturn(guid) |
| 73 | |
| 74 | self.mox.StubOutWithMock(cloudstorage, 'open') |
| 75 | cloudstorage.open( |
| 76 | object_path, 'w', mime_type, options={} |
| 77 | ).AndReturn(fake.FakeFile()) |
| 78 | cloudstorage.open(object_path + '-thumbnail', 'w', mime_type).AndReturn( |
| 79 | fake.FakeFile()) |
| 80 | |
| 81 | self.mox.StubOutWithMock(images, 'resize') |
| 82 | images.resize(content, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 83 | gcs_helpers.DEFAULT_THUMB_HEIGHT).AndReturn(thumb_content) |
| 84 | |
| 85 | self.mox.ReplayAll() |
| 86 | |
| 87 | ret_id = gcs_helpers.StoreObjectInGCS( |
| 88 | content, mime_type, project_id, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 89 | gcs_helpers.DEFAULT_THUMB_HEIGHT) |
| 90 | self.mox.VerifyAll() |
| 91 | self.assertEqual(object_id, ret_id) |
| 92 | |
| 93 | def testStoreObjectInGCS_NotResizableMimeType(self): |
| 94 | guid = 'aaaaa' |
| 95 | project_id = 100 |
| 96 | object_id = '/%s/attachments/%s' % (project_id, guid) |
| 97 | bucket_name = 'test_bucket' |
| 98 | object_path = '/' + bucket_name + object_id |
| 99 | mime_type = 'not_resizable_mime_type' |
| 100 | content = 'content' |
| 101 | |
| 102 | self.mox.StubOutWithMock(app_identity, 'get_default_gcs_bucket_name') |
| 103 | app_identity.get_default_gcs_bucket_name().AndReturn(bucket_name) |
| 104 | |
| 105 | self.mox.StubOutWithMock(uuid, 'uuid4') |
| 106 | uuid.uuid4().AndReturn(guid) |
| 107 | |
| 108 | self.mox.StubOutWithMock(cloudstorage, 'open') |
| 109 | options = {'Content-Disposition': 'inline; filename="file.ext"'} |
| 110 | cloudstorage.open( |
| 111 | object_path, 'w', mime_type, options=options |
| 112 | ).AndReturn(fake.FakeFile()) |
| 113 | |
| 114 | self.mox.ReplayAll() |
| 115 | |
| 116 | ret_id = gcs_helpers.StoreObjectInGCS( |
| 117 | content, mime_type, project_id, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 118 | gcs_helpers.DEFAULT_THUMB_HEIGHT, filename='file.ext') |
| 119 | self.mox.VerifyAll() |
| 120 | self.assertEqual(object_id, ret_id) |
| 121 | |
| 122 | def testCheckMemeTypeResizable(self): |
| 123 | for resizable_mime_type in gcs_helpers.RESIZABLE_MIME_TYPES: |
| 124 | gcs_helpers.CheckMimeTypeResizable(resizable_mime_type) |
| 125 | |
| 126 | with self.assertRaises(gcs_helpers.UnsupportedMimeType): |
| 127 | gcs_helpers.CheckMimeTypeResizable('not_resizable_mime_type') |
| 128 | |
| 129 | def testStoreLogoInGCS(self): |
| 130 | file_name = 'test_file.png' |
| 131 | mime_type = 'image/png' |
| 132 | content = 'test content' |
| 133 | project_id = 100 |
| 134 | object_id = 123 |
| 135 | |
| 136 | self.mox.StubOutWithMock(filecontent, 'GuessContentTypeFromFilename') |
| 137 | filecontent.GuessContentTypeFromFilename(file_name).AndReturn(mime_type) |
| 138 | |
| 139 | self.mox.StubOutWithMock(gcs_helpers, 'StoreObjectInGCS') |
| 140 | gcs_helpers.StoreObjectInGCS( |
| 141 | content, mime_type, project_id, |
| 142 | thumb_width=gcs_helpers.LOGO_THUMB_WIDTH, |
| 143 | thumb_height=gcs_helpers.LOGO_THUMB_HEIGHT).AndReturn(object_id) |
| 144 | |
| 145 | self.mox.ReplayAll() |
| 146 | |
| 147 | ret_id = gcs_helpers.StoreLogoInGCS(file_name, content, project_id) |
| 148 | self.mox.VerifyAll() |
| 149 | self.assertEqual(object_id, ret_id) |
| 150 | |
| 151 | @mock.patch('google.appengine.api.urlfetch.fetch') |
| 152 | def testFetchSignedURL_Success(self, mock_fetch): |
| 153 | mock_fetch.return_value = testing_helpers.Blank( |
| 154 | headers={'Location': 'signed url'}) |
| 155 | actual = gcs_helpers._FetchSignedURL('signing req url') |
| 156 | mock_fetch.assert_called_with('signing req url', follow_redirects=False) |
| 157 | self.assertEqual('signed url', actual) |
| 158 | |
| 159 | @mock.patch('google.appengine.api.urlfetch.fetch') |
| 160 | def testFetchSignedURL_UnderpopulatedResult(self, mock_fetch): |
| 161 | mock_fetch.return_value = testing_helpers.Blank(headers={}) |
| 162 | self.assertRaises( |
| 163 | KeyError, gcs_helpers._FetchSignedURL, 'signing req url') |
| 164 | |
| 165 | @mock.patch('google.appengine.api.urlfetch.fetch') |
| 166 | def testFetchSignedURL_DownloadError(self, mock_fetch): |
| 167 | mock_fetch.side_effect = urlfetch.DownloadError |
| 168 | self.assertRaises( |
| 169 | urlfetch.DownloadError, |
| 170 | gcs_helpers._FetchSignedURL, 'signing req url') |
| 171 | |
| 172 | @mock.patch('framework.gcs_helpers._FetchSignedURL') |
| 173 | def testSignUrl_Success(self, mock_FetchSignedURL): |
| 174 | with mock.patch( |
| 175 | 'google.appengine.api.app_identity.get_access_token') as gat: |
| 176 | gat.return_value = ['token'] |
| 177 | mock_FetchSignedURL.return_value = 'signed url' |
| 178 | signed_url = gcs_helpers.SignUrl('bucket', '/object') |
| 179 | self.assertEqual('signed url', signed_url) |
| 180 | |
| 181 | @mock.patch('framework.gcs_helpers._FetchSignedURL') |
| 182 | def testSignUrl_DownloadError(self, mock_FetchSignedURL): |
| 183 | mock_FetchSignedURL.side_effect = urlfetch.DownloadError |
| 184 | self.assertEqual( |
| 185 | '/missing-gcs-url', gcs_helpers.SignUrl('bucket', '/object')) |