Search code examples
pythonpytestboto3amazon-sesmoto

moto mock_ses list_identities(IdentityType='EmailAddress') returns both email addresses and domains


I'm using mock_ses from moto to mock aws ses functions. I want to test some functions that uses list_identities() but the mocked list_identities() does not behave the same way as the actual function, and I cannot figure out why.

The actual list_identities() function can take IdentityType argument to only return list of email addresses or list of domains as defined here. But it looks like the mocked list_identities() function always returns a list that includes both email addresses and domains event if the IdentityType is set.

I have created a simplified unit test to demonstrate my issue. I would expect this unit test to pass, but it fails because the value of mock_response is ['testdomain.com', '[email protected]', '[email protected]']. it's including the testdomain.com in the list when I only want to get the list of email addresses.

I'm using moto 4.2.6

from moto import mock_ses

@mock_ses
class Test_TestEmailSenderTestCase(unittest.TestCase):

    # Setup actions for each test
    def setUp(self):
        ses_client = boto3.client("ses", "us-west-2")
        self.ses_client = ses_client
        ses_client.verify_email_identity(EmailAddress="[email protected]")
        ses_client.verify_email_identity(EmailAddress="[email protected]")
        ses_client.verify_domain_dkim(Domain='testdomain.com')
        
    # Teardown actions for each test
    def tearDown(self):
        ses_client = boto3.client("ses", "us-west-2")
        try:
            ses_client.delete_identity(Identity="[email protected]")
            ses_client.delete_identity(Identity="[email protected]")
            ses_client.delete_identity(Identity="testdomain.com")
        except:
            pass
        

    def test_get_email_address(self):
        expected_response = ["[email protected]","[email protected]"]
        mock_response = self.ses_client.list_identities(IdentityType='EmailAddress')["Identities"]
        self.assertEqual(mock_response, expected_response)

Solution

  • The IdentityType-argument is supported as of Moto 4.2.7, just released.

    See the ChangeLog: https://github.com/getmoto/moto/blob/master/CHANGELOG.md