"""Test the SNS connector of the reverse payout flow.""" from unittest import mock from botocore import exceptions import pytest from accounting.flows.reserve_payouts import setting from accounting.flows.reserve_payouts.connectors import sns from accounting.flows.reserve_payouts.constants import sns as sns_constants from accounting.util import sns as sns_util @pytest.mark.parametrize('exception, expected_result', [ (exceptions.BotoCoreError(), False), (exceptions.ClientError({}, 1), False), (Exception, False), (None, True), ]) @mock.patch('accounting.flows.reserve_payouts.connectors.sns.setting') def test_health_check( mock_setting, exception, expected_result, monkeypatch): """Test health_check function.""" arn_list = ['test1', 'test2', 'test-sns-success', 'test-sns-fail'] mock_sns = mock.MagicMock() mock_sns.topics.all.return_value = [ mock.MagicMock(arn=arn) for arn in arn_list] monkeypatch.setattr(sns_util, 'get_sns_resource', mock.MagicMock( return_value=mock_sns)) if exception: mock_sns.topics.all.side_effect = exception mock_setting.SNS_SUCCESS_TOPIC = 'test-sns-success' mock_setting.SNS_FAIL_TOPIC = 'test-sns-fail' result = sns.health_check() assert result.bool == expected_result @mock.patch('accounting.flows.reserve_payouts.connectors.sns.setting') def test_health_check_topic_not_exist( mock_setting, monkeypatch): """Test health_check function.""" mock_sns = mock.MagicMock() arn_list = ['test1', 'test2', 'test3', 'test4'] mock_sns.topics.all.return_value = [ mock.MagicMock(arn=arn) for arn in arn_list] monkeypatch.setattr(sns_util, 'get_sns_resource', mock.MagicMock( return_value=mock_sns)) mock_setting.SNS_SUCCESS_TOPIC = 'test-sns-success' mock_setting.SNS_FAIL_TOPIC = 'test-sns-fail' result = sns.health_check() assert not result.bool @pytest.mark.parametrize('status, status_result, expected_result', [ ('success', {'ResponseMetadata': {'HTTPStatusCode': 200}}, True), ('failed', {'ResponseMetadata': {'HTTPStatusCode': 400}}, False) ]) def test_send_status_message( status, status_result, expected_result, monkeypatch): """Test send status message.""" monkeypatch.setattr( sns_util, 'publish_sns_message_to_email', mock.MagicMock( return_value=status_result)) result = sns.send_status_message(status, 'message') assert result == expected_result def test_format_success_status_message(monkeypatch): """Test the proper message format is used for success message.""" monkeypatch.setattr( sns_util, 'publish_sns_message_to_email', mock.MagicMock( return_value={'ResponseMetadata': {'HTTPStatusCode': 200}})) setting.SNS_SUCCESS_TOPIC = 'success-topic' sns.send_status_message('success', 'success message') expected_calls = [mock.call( sns_constants.SNS_SUCCESS_MESSAGE.format('success message'), setting.SNS_SUCCESS_TOPIC)] sns_util.publish_sns_message_to_email.assert_has_calls(expected_calls) def test_format_fail_status_message(monkeypatch): """Test the proper message format is used for fail message.""" monkeypatch.setattr( sns_util, 'publish_sns_message_to_email', mock.MagicMock( return_value={'ResponseMetadata': {'HTTPStatusCode': 200}})) setting.SNS_FAIL_TOPIC = 'fail-topic' sns.send_status_message('fail', 'fail message') expected_calls = [mock.call( sns_constants.SNS_FAIL_MESSAGE.format('fail message'), setting.SNS_FAIL_TOPIC)] sns_util.publish_sns_message_to_email.assert_has_calls(expected_calls)