Files
notifications-api/tests/app/clients/test_aws_cloudwatch.py

116 lines
4.5 KiB
Python
Raw Normal View History

2023-07-21 10:42:17 -07:00
# import pytest
2023-10-03 08:00:59 -07:00
from datetime import datetime
2023-05-04 07:56:24 -07:00
from flask import current_app
from app import aws_cloudwatch_client
def test_check_sms_no_event_error_condition(notify_api, mocker):
2023-08-29 14:54:30 -07:00
boto_mock = mocker.patch.object(aws_cloudwatch_client, "_client", create=True)
2023-05-04 08:15:08 -07:00
# TODO
# we do this to get the AWS account number, and it seems like unit tests locally have
# access to the env variables but when we push the PR they do not. Is there a better way to get it?
2023-08-29 14:54:30 -07:00
mocker.patch.dict("os.environ", {"SES_DOMAIN_ARN": "1111:"})
message_id = "aaa"
notification_id = "bbb"
2023-05-04 07:56:24 -07:00
boto_mock.filter_log_events.return_value = []
with notify_api.app_context():
aws_cloudwatch_client.init_app(current_app)
2023-07-21 11:24:22 -07:00
try:
aws_cloudwatch_client.check_sms(message_id, notification_id)
assert 1 == 0
2023-07-21 11:32:31 -07:00
except Exception:
2023-07-21 11:24:22 -07:00
assert 1 == 1
2023-05-04 07:56:24 -07:00
def side_effect(filterPattern, logGroupName, startTime, endTime):
2023-08-29 14:54:30 -07:00
if "Failure" in logGroupName and "fail" in filterPattern:
2023-05-04 07:56:24 -07:00
return {
2023-08-29 14:54:30 -07:00
"events": [
{
"logStreamName": "89db9712-c6d1-49f9-be7c-4caa7ed9efb1",
2023-11-03 08:28:21 -07:00
"message": '{"delivery":{"destination":"+1661","phoneCarrier":"ATT Mobility", '
'"providerResponse":"Invalid phone number"}}',
2023-08-29 14:54:30 -07:00
"eventId": "37535432778099870001723210579798865345508698025292922880",
}
]
2023-05-04 07:56:24 -07:00
}
2023-08-29 14:54:30 -07:00
elif "succeed" in filterPattern:
2023-05-04 07:56:24 -07:00
return {
2023-08-29 14:54:30 -07:00
"events": [
{
"logStreamName": "89db9712-c6d1-49f9-be7c-4caa7ed9efb1",
"timestamp": 1683147017911,
2023-11-03 08:28:21 -07:00
"message": '{"delivery":{"destination":"+1661","phoneCarrier":"ATT Mobility",'
'"providerResponse":"Phone accepted msg"}}',
2023-08-29 14:54:30 -07:00
"ingestionTime": 1683147018026,
"eventId": "37535432778099870001723210579798865345508698025292922880",
}
]
2023-05-04 07:56:24 -07:00
}
else:
return {"events": []}
def test_check_sms_success(notify_api, mocker):
aws_cloudwatch_client.init_app(current_app)
2023-08-29 14:54:30 -07:00
boto_mock = mocker.patch.object(aws_cloudwatch_client, "_client", create=True)
2023-05-04 07:56:24 -07:00
boto_mock.filter_log_events.side_effect = side_effect
mocker.patch.dict(
"os.environ",
{"SES_DOMAIN_ARN": "arn:aws:ses:us-west-2:12345:identity/ses-xxx.xxx.xxx.xxx"},
)
2023-05-04 08:15:08 -07:00
2023-08-29 14:54:30 -07:00
message_id = "succeed"
notification_id = "ccc"
2023-10-03 08:00:59 -07:00
created_at = datetime.utcnow()
2023-05-04 07:56:24 -07:00
with notify_api.app_context():
2023-10-03 08:00:59 -07:00
aws_cloudwatch_client.check_sms(message_id, notification_id, created_at)
2023-05-04 07:56:24 -07:00
# We check the 'success' log group first and if we find the message_id, we are done, so there is only 1 call
assert boto_mock.filter_log_events.call_count == 1
mock_call = str(boto_mock.filter_log_events.mock_calls[0])
2023-08-29 14:54:30 -07:00
assert "Failure" not in mock_call
assert "succeed" in mock_call
assert "notification.messageId" in mock_call
2023-05-04 07:56:24 -07:00
def test_check_sms_failure(notify_api, mocker):
aws_cloudwatch_client.init_app(current_app)
2023-08-29 14:54:30 -07:00
boto_mock = mocker.patch.object(aws_cloudwatch_client, "_client", create=True)
2023-05-04 07:56:24 -07:00
boto_mock.filter_log_events.side_effect = side_effect
mocker.patch.dict(
"os.environ",
{"SES_DOMAIN_ARN": "arn:aws:ses:us-west-2:12345:identity/ses-xxx.xxx.xxx.xxx"},
)
2023-08-29 14:54:30 -07:00
message_id = "fail"
notification_id = "bbb"
2023-10-03 08:00:59 -07:00
created_at = datetime.utcnow()
2023-05-04 07:56:24 -07:00
with notify_api.app_context():
2023-10-03 08:00:59 -07:00
aws_cloudwatch_client.check_sms(message_id, notification_id, created_at)
2023-05-04 07:56:24 -07:00
# We check the 'success' log group and find nothing, so we then check the 'fail' log group -- two calls.
assert boto_mock.filter_log_events.call_count == 2
mock_call = str(boto_mock.filter_log_events.mock_calls[1])
2023-08-29 14:54:30 -07:00
assert "Failure" in mock_call
assert "fail" in mock_call
assert "notification.messageId" in mock_call
def test_extract_account_number_gov_cloud():
domain_arn = "arn:aws-us-gov:ses:us-gov-west-1:12345:identity/ses-abc.xxx.xxx.xxx"
actual_account_number = aws_cloudwatch_client._extract_account_number(domain_arn)
assert len(actual_account_number) == 6
expected_account_number = "12345"
assert actual_account_number[4] == expected_account_number
def test_extract_account_number_gov_staging():
domain_arn = "arn:aws:ses:us-south-14:12345:identity/ses-abc.xxx.xxx.xxx"
actual_account_number = aws_cloudwatch_client._extract_account_number(domain_arn)
assert len(actual_account_number) == 6
expected_account_number = "12345"
assert actual_account_number[4] == expected_account_number