from unittest import mock import pytest from aws_ses import AwsSesService # fmt: off _response_verify_domain_dkim = {'DkimTokens': ['token1', 'token2', 'token3'], 'ResponseMetadata': {'RequestId': '6e4da72e-3063-41da-9434-dae7ef19c78e', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 17:27:40 GMT', 'content-type': 'text/xml', 'content-length': '469', 'connection': 'keep-alive', 'x-amzn-requestid': '6e4da72e-3063-41da-9434-dae7ef19c78e'}, 'RetryAttempts': 0}} _response_verify_domain_identity = {'VerificationToken': 'token', 'ResponseMetadata': {'RequestId': 'b733069c-115f-4457-b6cb-14e44f29d17f', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 17:27:12 GMT', 'content-type': 'text/xml', 'content-length': '370', 'connection': 'keep-alive', 'x-amzn-requestid': 'b733069c-115f-4457-b6cb-14e44f29d17f'}, 'RetryAttempts': 0}} _response_get_identity_dkim_attributes = {'DkimAttributes': {'example.com': {'DkimEnabled': True, 'DkimVerificationStatus': 'Success', 'DkimTokens': ['bgobmdqs2vbxr6ygwbmg753knfu6tos7', 'm6o6w6twpmwzwe3igpxxo7qsvwp3hlgj', 'zqfz4ekdelp7m2u2phsyad3h5dsfle4v']}}, 'ResponseMetadata': {'RequestId': 'ba466be1-79bc-499b-9766-ca1acd7d1c08', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 16:49:53 GMT', 'content-type': 'text/xml', 'content-length': '833', 'connection': 'keep-alive', 'x-amzn-requestid': 'ba466be1-79bc-499b-9766-ca1acd7d1c08'}, 'RetryAttempts': 0}} _response_get_identity_dkim_attributes_pending = {'DkimAttributes': {'example.com': {'DkimEnabled': True, 'DkimVerificationStatus': 'Pending', 'DkimTokens': ['bgobmdqs2vbxr6ygwbmg753knfu6tos7', 'm6o6w6twpmwzwe3igpxxo7qsvwp3hlgj', 'zqfz4ekdelp7m2u2phsyad3h5dsfle4v']}}, 'ResponseMetadata': {'RequestId': 'b24732d9-01e1-4266-8b34-550a9ecf634d', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 17:30:03 GMT', 'content-type': 'text/xml', 'content-length': '833', 'connection': 'keep-alive', 'x-amzn-requestid': 'b24732d9-01e1-4266-8b34-550a9ecf634d'}, 'RetryAttempts': 0}} _response_get_identity_verification_attributes = {'VerificationAttributes': {'example.com': {'VerificationStatus': 'Success', 'VerificationToken': 'XzMvLQqOPdC0iJ02YTdjqWBJr7FTqhGtAsEcfpUwfoA='}}, 'ResponseMetadata': {'RequestId': '022c3e6e-a6e8-426f-bf4d-149f71ca7913', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 16:56:19 GMT', 'content-type': 'text/xml', 'content-length': '637', 'connection': 'keep-alive', 'x-amzn-requestid': '022c3e6e-a6e8-426f-bf4d-149f71ca7913'}, 'RetryAttempts': 0}} _response_get_identity_verification_attributes_pending = {'VerificationAttributes': {'example.com': {'VerificationStatus': 'Pending', 'VerificationToken': 'XzMvLQqOPdC0iJ02YTdjqWBJr7FTqhGtAsEcfpUwfoA='}}, 'ResponseMetadata': {'RequestId': '022c3e6e-a6e8-426f-bf4d-149f71ca7913', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 16:56:19 GMT', 'content-type': 'text/xml', 'content-length': '637', 'connection': 'keep-alive', 'x-amzn-requestid': '022c3e6e-a6e8-426f-bf4d-149f71ca7913'}, 'RetryAttempts': 0}} _response_set_identity_mail_from_domain = {'ResponseMetadata': {'RequestId': 'c04acd33-40be-46cd-9960-c9e51397dc48', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 17:03:50 GMT', 'content-type': 'text/xml', 'content-length': '266', 'connection': 'keep-alive', 'x-amzn-requestid': 'c04acd33-40be-46cd-9960-c9e51397dc48'}, 'RetryAttempts': 0}} _response_get_identity_mail_from_domain_attributes = {'MailFromDomainAttributes': {'example.com': {'MailFromDomain': 'email.abdus.dev', 'MailFromDomainStatus': 'Success', 'BehaviorOnMXFailure': 'UseDefaultValue'}}, 'ResponseMetadata': {'RequestId': '63049966-c062-47f4-8238-145bd533d880', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 17:37:06 GMT', 'content-type': 'text/xml', 'content-length': '687', 'connection': 'keep-alive', 'x-amzn-requestid': '63049966-c062-47f4-8238-145bd533d880'}, 'RetryAttempts': 0}} _response_get_identity_mail_from_domain_attributes_pending = {'MailFromDomainAttributes': {'example.com': {'MailFromDomain': 'email.abdus.dev', 'MailFromDomainStatus': 'Pending', 'BehaviorOnMXFailure': 'UseDefaultValue'}}, 'ResponseMetadata': {'RequestId': '63049966-c062-47f4-8238-145bd533d880', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 03 Jan 2022 17:37:06 GMT', 'content-type': 'text/xml', 'content-length': '687', 'connection': 'keep-alive', 'x-amzn-requestid': '63049966-c062-47f4-8238-145bd533d880'}, 'RetryAttempts': 0}} # fmt: on @pytest.fixture() def mock_ses() -> mock.MagicMock: with mock.patch("boto3.client") as m: m_ses = m.return_value yield m_ses def test_init_domain_verification(mock_ses): m_verify_id = mock_ses.verify_domain_identity m_verify_dkim = mock_ses.verify_domain_dkim m_verify_id.return_value = _response_verify_domain_identity m_verify_dkim.return_value = _response_verify_domain_dkim domain = "example.com" dns = AwsSesService(mock_ses).configure_domain_identity(domain) m_verify_id.assert_called_once_with(Domain=domain) m_verify_dkim.assert_called_once_with(Domain=domain) assert dns == [ { "type": "TXT", "name": domain, "value": "token", }, { "type": "CNAME", "name": f"token1._domainkey.{domain}", "value": f"token1.dkim.amazonses.com", }, { "type": "CNAME", "name": f"token2._domainkey.{domain}", "value": f"token2.dkim.amazonses.com", }, { "type": "CNAME", "name": f"token3._domainkey.{domain}", "value": f"token3.dkim.amazonses.com", }, ] def test_get_dkim_status(mock_ses): m = mock_ses.get_identity_dkim_attributes m.return_value = _response_get_identity_dkim_attributes_pending domain = "example.com" status = AwsSesService(mock_ses).check_dkim_verification_status(domain) assert status == "Pending" m.assert_called_once_with(Identities=[domain]) # === m.reset_mock() m.return_value = _response_get_identity_dkim_attributes status = AwsSesService(mock_ses).check_dkim_verification_status(domain) assert status == "Success" def test_get_id_status(mock_ses): m = mock_ses.get_identity_verification_attributes m.return_value = _response_get_identity_verification_attributes domain = "example.com" status = AwsSesService(mock_ses).check_id_verification_status(domain) assert status == "Success" m.assert_called_once_with(Identities=[domain]) def test_set_custom_from_domain(mock_ses): mock_ses._client_config.region_name = "fake-region" m = mock_ses.set_identity_mail_from_domain m.return_value = _response_set_identity_mail_from_domain dns = AwsSesService(mock_ses).configure_from_domain("example.com", "mail.example.com") assert dns == [ { "type": "MX", "name": "mail.example.com", "value": f"feedback-smtp.fake-region.amazonses.com", "priority": "10", }, {"type": "TXT", "name": "mail.example.com", "value": f'"v=spf1 include:amazonses.com ~all"'}, ] m.assert_called_once_with( Identity="example.com", MailFromDomain="mail.example.com", BehaviorOnMXFailure="UseDefaultValue", ) def test_get_mail_from_status(mock_ses): m = mock_ses.get_identity_mail_from_domain_attributes m.return_value = _response_get_identity_mail_from_domain_attributes_pending domain = "example.com" status = AwsSesService(mock_ses).check_custom_from_domain_status(domain) assert status == "Pending" m.assert_called_once_with(Identities=[domain]) # === m.reset_mock() m.return_value = _response_get_identity_mail_from_domain_attributes status = AwsSesService(mock_ses).check_custom_from_domain_status(domain) assert status == "Success" m.assert_called_once_with(Identities=[domain])