OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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 |
| 8 import unittest |
| 9 import uuid |
| 10 |
| 11 import mox |
| 12 |
| 13 from google.appengine.api import app_identity |
| 14 from google.appengine.api import images |
| 15 from third_party import cloudstorage |
| 16 |
| 17 from framework import filecontent |
| 18 from framework import gcs_helpers |
| 19 from testing import fake |
| 20 from testing import testing_helpers |
| 21 |
| 22 |
| 23 class GcsHelpersTest(unittest.TestCase): |
| 24 |
| 25 def setUp(self): |
| 26 self.mox = mox.Mox() |
| 27 |
| 28 def tearDown(self): |
| 29 self.mox.UnsetStubs() |
| 30 self.mox.ResetAll() |
| 31 |
| 32 def testDeleteObjectFromGCS(self): |
| 33 object_id = 'aaaaa' |
| 34 bucket_name = 'test_bucket' |
| 35 object_path = '/' + bucket_name + object_id |
| 36 |
| 37 self.mox.StubOutWithMock(app_identity, 'get_default_gcs_bucket_name') |
| 38 app_identity.get_default_gcs_bucket_name().AndReturn(bucket_name) |
| 39 |
| 40 self.mox.StubOutWithMock(cloudstorage, 'delete') |
| 41 cloudstorage.delete(object_path) |
| 42 |
| 43 self.mox.ReplayAll() |
| 44 |
| 45 gcs_helpers.DeleteObjectFromGCS(object_id) |
| 46 self.mox.VerifyAll() |
| 47 |
| 48 def testStoreObjectInGCS_ResizableMimeType(self): |
| 49 guid = 'aaaaa' |
| 50 project_id = 100 |
| 51 object_id = '/%s/attachments/%s' % (project_id, guid) |
| 52 bucket_name = 'test_bucket' |
| 53 object_path = '/' + bucket_name + object_id |
| 54 mime_type = 'image/png' |
| 55 content = 'content' |
| 56 thumb_content = 'thumb_content' |
| 57 |
| 58 self.mox.StubOutWithMock(app_identity, 'get_default_gcs_bucket_name') |
| 59 app_identity.get_default_gcs_bucket_name().AndReturn(bucket_name) |
| 60 |
| 61 self.mox.StubOutWithMock(uuid, 'uuid4') |
| 62 uuid.uuid4().AndReturn(guid) |
| 63 |
| 64 self.mox.StubOutWithMock(cloudstorage, 'open') |
| 65 cloudstorage.open(object_path, 'w', mime_type).AndReturn(fake.FakeFile()) |
| 66 cloudstorage.open(object_path + '-thumbnail', 'w', mime_type).AndReturn( |
| 67 fake.FakeFile()) |
| 68 |
| 69 self.mox.StubOutWithMock(images, 'resize') |
| 70 images.resize(content, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 71 gcs_helpers.DEFAULT_THUMB_HEIGHT).AndReturn(thumb_content) |
| 72 |
| 73 self.mox.ReplayAll() |
| 74 |
| 75 ret_id = gcs_helpers.StoreObjectInGCS( |
| 76 content, mime_type, project_id, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 77 gcs_helpers.DEFAULT_THUMB_HEIGHT) |
| 78 self.mox.VerifyAll() |
| 79 self.assertEquals(object_id, ret_id) |
| 80 |
| 81 def testStoreObjectInGCS_NotResizableMimeType(self): |
| 82 guid = 'aaaaa' |
| 83 project_id = 100 |
| 84 object_id = '/%s/attachments/%s' % (project_id, guid) |
| 85 bucket_name = 'test_bucket' |
| 86 object_path = '/' + bucket_name + object_id |
| 87 mime_type = 'not_resizable_mime_type' |
| 88 content = 'content' |
| 89 |
| 90 self.mox.StubOutWithMock(app_identity, 'get_default_gcs_bucket_name') |
| 91 app_identity.get_default_gcs_bucket_name().AndReturn(bucket_name) |
| 92 |
| 93 self.mox.StubOutWithMock(uuid, 'uuid4') |
| 94 uuid.uuid4().AndReturn(guid) |
| 95 |
| 96 self.mox.StubOutWithMock(cloudstorage, 'open') |
| 97 cloudstorage.open(object_path, 'w', mime_type).AndReturn(fake.FakeFile()) |
| 98 |
| 99 self.mox.ReplayAll() |
| 100 |
| 101 ret_id = gcs_helpers.StoreObjectInGCS( |
| 102 content, mime_type, project_id, gcs_helpers.DEFAULT_THUMB_WIDTH, |
| 103 gcs_helpers.DEFAULT_THUMB_HEIGHT) |
| 104 self.mox.VerifyAll() |
| 105 self.assertEquals(object_id, ret_id) |
| 106 |
| 107 def testCheckMemeTypeResizable(self): |
| 108 for resizable_mime_type in gcs_helpers.RESIZABLE_MIME_TYPES: |
| 109 gcs_helpers.CheckMimeTypeResizable(resizable_mime_type) |
| 110 |
| 111 with self.assertRaises(gcs_helpers.UnsupportedMimeType): |
| 112 gcs_helpers.CheckMimeTypeResizable('not_resizable_mime_type') |
| 113 |
| 114 def testStoreLogoInGCS(self): |
| 115 file_name = 'test_file.png' |
| 116 mime_type = 'image/png' |
| 117 content = 'test content' |
| 118 project_id = 100 |
| 119 object_id = 123 |
| 120 |
| 121 self.mox.StubOutWithMock(filecontent, 'GuessContentTypeFromFilename') |
| 122 filecontent.GuessContentTypeFromFilename(file_name).AndReturn(mime_type) |
| 123 |
| 124 self.mox.StubOutWithMock(gcs_helpers, 'StoreObjectInGCS') |
| 125 gcs_helpers.StoreObjectInGCS( |
| 126 content, mime_type, project_id, |
| 127 thumb_width=gcs_helpers.LOGO_THUMB_WIDTH, |
| 128 thumb_height=gcs_helpers.LOGO_THUMB_HEIGHT).AndReturn(object_id) |
| 129 |
| 130 self.mox.ReplayAll() |
| 131 |
| 132 ret_id = gcs_helpers.StoreLogoInGCS(file_name, content, project_id) |
| 133 self.mox.VerifyAll() |
| 134 self.assertEquals(object_id, ret_id) |
OLD | NEW |