mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
add tests for ses client
This commit is contained in:
@@ -96,11 +96,17 @@ class AwsSesClient(EmailClient):
|
|||||||
ReplyToAddresses=reply_to_addresses
|
ReplyToAddresses=reply_to_addresses
|
||||||
)
|
)
|
||||||
except botocore.exceptions.ClientError as e:
|
except botocore.exceptions.ClientError as e:
|
||||||
|
self.statsd_client.incr("clients.ses.error")
|
||||||
|
|
||||||
|
# http://docs.aws.amazon.com/ses/latest/DeveloperGuide/api-error-codes.html
|
||||||
if e.response['Error']['Code'] == 'InvalidParameterValue':
|
if e.response['Error']['Code'] == 'InvalidParameterValue':
|
||||||
raise InvalidEmailError('email: "{}" message: "{}"'.format(
|
raise InvalidEmailError('email: "{}" message: "{}"'.format(
|
||||||
to_addresses[0],
|
to_addresses[0],
|
||||||
e.response['Error']['Message']
|
e.response['Error']['Message']
|
||||||
))
|
))
|
||||||
|
else:
|
||||||
|
self.statsd_client.incr("clients.ses.error")
|
||||||
|
raise AwsSesClientException(str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.statsd_client.incr("clients.ses.error")
|
self.statsd_client.incr("clients.ses.error")
|
||||||
raise AwsSesClientException(str(e))
|
raise AwsSesClientException(str(e))
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
import botocore
|
||||||
import pytest
|
import pytest
|
||||||
from unittest.mock import Mock, ANY
|
from unittest.mock import Mock, ANY
|
||||||
|
from notifications_utils.recipients import InvalidEmailError
|
||||||
|
|
||||||
from app import aws_ses_client
|
from app import aws_ses_client
|
||||||
from app.clients.email.aws_ses import get_aws_responses
|
from app.clients.email.aws_ses import get_aws_responses, AwsSesClientException
|
||||||
|
|
||||||
|
|
||||||
def test_should_return_correct_details_for_delivery():
|
def test_should_return_correct_details_for_delivery():
|
||||||
@@ -67,3 +69,52 @@ def test_send_email_handles_reply_to_address(notify_api, mocker, reply_to_addres
|
|||||||
Message=ANY,
|
Message=ANY,
|
||||||
ReplyToAddresses=expected_value
|
ReplyToAddresses=expected_value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_email_raises_bad_email_as_InvalidEmailError(mocker):
|
||||||
|
boto_mock = mocker.patch.object(aws_ses_client, '_client', create=True)
|
||||||
|
mocker.patch.object(aws_ses_client, 'statsd_client', create=True)
|
||||||
|
error_response = {
|
||||||
|
'Error': {
|
||||||
|
'Code': 'InvalidParameterValue',
|
||||||
|
'Message': 'some error message from amazon',
|
||||||
|
'Type': 'Sender'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boto_mock.send_email.side_effect = botocore.exceptions.ClientError(error_response, 'opname')
|
||||||
|
mocker.patch.object(aws_ses_client, 'statsd_client', create=True)
|
||||||
|
|
||||||
|
with pytest.raises(InvalidEmailError) as excinfo:
|
||||||
|
aws_ses_client.send_email(
|
||||||
|
source=Mock(),
|
||||||
|
to_addresses='clearly@invalid@email.com',
|
||||||
|
subject=Mock(),
|
||||||
|
body=Mock()
|
||||||
|
)
|
||||||
|
|
||||||
|
assert 'some error message from amazon' in excinfo.value.message
|
||||||
|
assert 'clearly@invalid@email.com' in excinfo.value.message
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_email_raises_other_errs_as_AwsSesClientException(mocker):
|
||||||
|
boto_mock = mocker.patch.object(aws_ses_client, '_client', create=True)
|
||||||
|
mocker.patch.object(aws_ses_client, 'statsd_client', create=True)
|
||||||
|
error_response = {
|
||||||
|
'Error': {
|
||||||
|
'Code': 'ServiceUnavailable',
|
||||||
|
'Message': 'some error message from amazon',
|
||||||
|
'Type': 'Sender'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boto_mock.send_email.side_effect = botocore.exceptions.ClientError(error_response, 'opname')
|
||||||
|
mocker.patch.object(aws_ses_client, 'statsd_client', create=True)
|
||||||
|
|
||||||
|
with pytest.raises(AwsSesClientException) as excinfo:
|
||||||
|
aws_ses_client.send_email(
|
||||||
|
source=Mock(),
|
||||||
|
to_addresses=Mock(),
|
||||||
|
subject=Mock(),
|
||||||
|
body=Mock()
|
||||||
|
)
|
||||||
|
|
||||||
|
assert 'some error message from amazon' in str(excinfo.value)
|
||||||
|
|||||||
Reference in New Issue
Block a user