|
1 | 1 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | # SPDX-License-Identifier: MIT-0 |
3 | 3 |
|
| 4 | +import os |
| 5 | + |
| 6 | +import pytest |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | +from .lambda_context import LambdaContext |
| 10 | +from .helper import load_event, return_env_vars_dict |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def stepfunctions_event(): |
| 15 | + return load_event('tests/events/lambda/content_integrity_validator_function_success.json') |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def invalid_image_moderation(stepfunctions_event): |
| 20 | + e = { |
| 21 | + 'imageModerations': [ |
| 22 | + { |
| 23 | + 'ModerationLabels': [ |
| 24 | + { |
| 25 | + "Confidence": 99.24723052978516, |
| 26 | + "ParentName": "", |
| 27 | + "Name": "Explicit Nudity" |
| 28 | + }, |
| 29 | + { |
| 30 | + "Confidence": 99.24723052978516, |
| 31 | + "ParentName": "Explicit Nudity", |
| 32 | + "Name": "Graphic Male Nudity" |
| 33 | + }, |
| 34 | + ] |
| 35 | + } |
| 36 | + ] |
| 37 | + } |
| 38 | + return {**stepfunctions_event, **e} |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def invalid_content_sentiment(stepfunctions_event): |
| 43 | + e = { |
| 44 | + 'contentSentiment': { |
| 45 | + 'Sentiment': 'NEGATIVE' |
| 46 | + } |
| 47 | + } |
| 48 | + return {**stepfunctions_event, **e} |
| 49 | + |
| 50 | + |
| 51 | +@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True) |
| 52 | +def test_valid_image_and_valid_sentiment(stepfunctions_event): |
| 53 | + from properties_service import content_integrity_validator_function |
| 54 | + ret = content_integrity_validator_function.lambda_handler(stepfunctions_event, LambdaContext()) |
| 55 | + |
| 56 | + assert ret['validation_result'] == "PASS" |
| 57 | + assert ret['imageModerations'] == stepfunctions_event['imageModerations'] |
| 58 | + assert ret['contentSentiment'] == stepfunctions_event['contentSentiment'] |
| 59 | + |
| 60 | + |
| 61 | +@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True) |
| 62 | +def test_valid_image_and_invalid_sentiment(invalid_content_sentiment): |
| 63 | + event = invalid_content_sentiment |
| 64 | + |
| 65 | + from properties_service import content_integrity_validator_function |
| 66 | + ret = content_integrity_validator_function.lambda_handler(event, LambdaContext()) |
| 67 | + |
| 68 | + assert ret['validation_result'] == "FAIL" |
| 69 | + assert ret['imageModerations'] == event['imageModerations'] |
| 70 | + assert ret['contentSentiment'] == event['contentSentiment'] |
| 71 | + |
| 72 | + |
| 73 | +@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True) |
| 74 | +def test_invalid_image_and_valid_sentiment(invalid_image_moderation): |
| 75 | + event = invalid_image_moderation |
| 76 | + |
| 77 | + from properties_service import content_integrity_validator_function |
| 78 | + ret = content_integrity_validator_function.lambda_handler(event, LambdaContext()) |
| 79 | + |
| 80 | + assert ret['validation_result'] == "FAIL" |
| 81 | + assert ret['imageModerations'] == event['imageModerations'] |
| 82 | + assert ret['contentSentiment'] == event['contentSentiment'] |
| 83 | + |
| 84 | + |
| 85 | +@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True) |
| 86 | +def test_invalid_image_and_invalid_sentiment(invalid_image_moderation, invalid_content_sentiment): |
| 87 | + event = {**invalid_image_moderation, **invalid_content_sentiment} |
| 88 | + |
| 89 | + from properties_service import content_integrity_validator_function |
| 90 | + ret = content_integrity_validator_function.lambda_handler(event, LambdaContext()) |
| 91 | + |
| 92 | + assert ret['validation_result'] == "FAIL" |
| 93 | + assert ret['imageModerations'] == event['imageModerations'] |
| 94 | + assert ret['contentSentiment'] == event['contentSentiment'] |
0 commit comments