|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from collections import UserDict |
| 16 | +import uuid |
| 17 | + |
| 18 | +from mock import MagicMock, patch |
| 19 | + |
| 20 | +import image |
| 21 | + |
| 22 | + |
| 23 | +@patch('image.__blur_image') |
| 24 | +@patch('image.vision_client') |
| 25 | +@patch('image.storage_client') |
| 26 | +def test_process_offensive_image( |
| 27 | + storage_client, |
| 28 | + vision_client, |
| 29 | + __blur_image, |
| 30 | + capsys): |
| 31 | + result = UserDict() |
| 32 | + result.safe_search_annotation = UserDict() |
| 33 | + result.safe_search_annotation.adult = 5 |
| 34 | + result.safe_search_annotation.violence = 5 |
| 35 | + vision_client.safe_search_detection = MagicMock(return_value=result) |
| 36 | + |
| 37 | + filename = str(uuid.uuid4()) |
| 38 | + data = { |
| 39 | + 'bucket': 'my-bucket', |
| 40 | + 'name': filename |
| 41 | + } |
| 42 | + |
| 43 | + image.blur_offensive_images(data) |
| 44 | + |
| 45 | + out, _ = capsys.readouterr() |
| 46 | + assert 'Analyzing %s.' % filename in out |
| 47 | + assert 'The image %s was detected as inappropriate.' % filename in out |
| 48 | + assert image.__blur_image.called |
| 49 | + |
| 50 | + |
| 51 | +@patch('image.__blur_image') |
| 52 | +@patch('image.vision_client') |
| 53 | +@patch('image.storage_client') |
| 54 | +def test_process_safe_image( |
| 55 | + storage_client, |
| 56 | + vision_client, |
| 57 | + __blur_image, |
| 58 | + capsys): |
| 59 | + result = UserDict() |
| 60 | + result.safe_search_annotation = UserDict() |
| 61 | + result.safe_search_annotation.adult = 1 |
| 62 | + result.safe_search_annotation.violence = 1 |
| 63 | + vision_client.safe_search_detection = MagicMock(return_value=result) |
| 64 | + |
| 65 | + filename = str(uuid.uuid4()) |
| 66 | + data = { |
| 67 | + 'bucket': 'my-bucket', |
| 68 | + 'name': filename |
| 69 | + } |
| 70 | + |
| 71 | + image.blur_offensive_images(data) |
| 72 | + |
| 73 | + out, _ = capsys.readouterr() |
| 74 | + |
| 75 | + assert 'Analyzing %s.' % filename in out |
| 76 | + assert 'The image %s was detected as OK.' % filename in out |
| 77 | + assert __blur_image.called is False |
| 78 | + |
| 79 | + |
| 80 | +@patch('image.os') |
| 81 | +@patch('image.Image') |
| 82 | +@patch('image.storage_client') |
| 83 | +def test_blur_image(storage_client, image_mock, os_mock, capsys): |
| 84 | + filename = str(uuid.uuid4()) |
| 85 | + blur_bucket = 'blurred-bucket-' + str(uuid.uuid4()) |
| 86 | + |
| 87 | + os_mock.remove = MagicMock() |
| 88 | + os_mock.path = MagicMock() |
| 89 | + os_mock.path.basename = MagicMock(side_effect=(lambda x: x)) |
| 90 | + |
| 91 | + os_mock.getenv = MagicMock(return_value=blur_bucket) |
| 92 | + |
| 93 | + image_mock.return_value = image_mock |
| 94 | + image_mock.__enter__.return_value = image_mock |
| 95 | + |
| 96 | + blob = UserDict() |
| 97 | + blob.name = filename |
| 98 | + blob.bucket = UserDict() |
| 99 | + blob.download_to_filename = MagicMock() |
| 100 | + blob.upload_from_filename = MagicMock() |
| 101 | + |
| 102 | + image.__blur_image(blob) |
| 103 | + |
| 104 | + out, _ = capsys.readouterr() |
| 105 | + |
| 106 | + assert f'Image {filename} was downloaded to' in out |
| 107 | + assert f'Image {filename} was blurred.' in out |
| 108 | + assert f'Blurred image uploaded to: gs://{blur_bucket}/{filename}' in out |
| 109 | + assert os_mock.remove.called |
| 110 | + assert image_mock.resize.called |
0 commit comments