Best Python code snippet using localstack_python
test_ses_domain_identity.py
Source:test_ses_domain_identity.py
1from .base import HandlerTestCase, MOCK_ANY2from aws_cfn_ses_domain.ses_domain_identity import handle_domain_identity_request3class TestDomainIdentityHandler(HandlerTestCase):4 patch_base = 'aws_cfn_ses_domain.ses_domain_identity'5 def test_domain_required(self):6 event = {7 "RequestType": "Create",8 "ResourceProperties": {},9 "StackId": self.mock_stack_id}10 handle_domain_identity_request(event, self.mock_context)11 self.assertSentResponse(12 event, status="FAILED",13 reason="The 'Domain' property is required.",14 physical_resource_id="MISSING")15 def test_non_empty_domain_required(self):16 event = {17 "RequestType": "Create",18 "ResourceProperties": {19 "Domain": " . ",20 },21 "StackId": self.mock_stack_id}22 handle_domain_identity_request(event, self.mock_context)23 self.assertSentResponse(24 event, status="FAILED",25 reason="The 'Domain' property is required.",26 physical_resource_id="MISSING")27 def test_create_default(self):28 event = {29 "RequestType": "Create",30 "ResourceProperties": {31 "Domain": "example.com.",32 },33 "StackId": self.mock_stack_id}34 self.ses_stubber.add_response(35 'verify_domain_identity',36 {'VerificationToken': "ID_TOKEN"},37 {'Domain': "example.com"})38 self.ses_stubber.add_response(39 'verify_domain_dkim',40 {'DkimTokens': ["DKIM_TOKEN_1", "DKIM_TOKEN_2"]},41 {'Domain': "example.com"})42 self.ses_stubber.add_response(43 'set_identity_mail_from_domain',44 {},45 {'Identity': "example.com", 'MailFromDomain': "mail.example.com"})46 handle_domain_identity_request(event, self.mock_context)47 # Should default to SES in current region (where stack is running):48 self.mock_boto3_client.assert_called_once_with('ses', region_name="mock-region")49 outputs = self.assertSentResponse(50 event, physical_resource_id="arn:aws:ses:mock-region:111111111111:identity/example.com")51 self.assertEqual(outputs["Domain"], "example.com")52 self.assertEqual(outputs["VerificationToken"], "ID_TOKEN")53 self.assertEqual(outputs["DkimTokens"], ["DKIM_TOKEN_1", "DKIM_TOKEN_2"])54 self.assertEqual(outputs["MailFromDomain"], "mail.example.com")55 self.assertEqual(outputs["MailFromMX"], "feedback-smtp.mock-region.amazonses.com")56 self.assertEqual(outputs["MailFromSPF"], '"v=spf1 include:amazonses.com -all"')57 self.assertEqual(outputs["DMARC"], '"v=DMARC1; p=none; pct=100; sp=none; aspf=r;"')58 self.assertNotIn("ReceiveMX", outputs)59 self.assertCountEqual(outputs["Route53RecordSets"], [60 {'Type': 'TXT', 'Name': '_amazonses.example.com.', 'TTL': '1800',61 'ResourceRecords': ['"ID_TOKEN"']},62 {'Type': 'CNAME', 'Name': 'DKIM_TOKEN_1._domainkey.example.com.', 'TTL': '1800',63 'ResourceRecords': ['DKIM_TOKEN_1.dkim.amazonses.com.']},64 {'Type': 'CNAME', 'Name': 'DKIM_TOKEN_2._domainkey.example.com.', 'TTL': '1800',65 'ResourceRecords': ['DKIM_TOKEN_2.dkim.amazonses.com.']},66 {'Type': 'MX', 'Name': 'mail.example.com.', 'TTL': '1800',67 'ResourceRecords': ['10 feedback-smtp.mock-region.amazonses.com.']},68 {'Type': 'TXT', 'Name': 'mail.example.com.', 'TTL': '1800',69 'ResourceRecords': ['"v=spf1 include:amazonses.com -all"']},70 {'Type': 'TXT', 'Name': '_dmarc.example.com.', 'TTL': '1800',71 'ResourceRecords': ['"v=DMARC1; p=none; pct=100; sp=none; aspf=r;"']},72 ])73 self.assertCountEqual(outputs["ZoneFileEntries"], [74 '_amazonses.example.com. \t1800\tIN\tTXT \t"ID_TOKEN"',75 'DKIM_TOKEN_1._domainkey.example.com.\t1800\tIN\tCNAME\tDKIM_TOKEN_1.dkim.amazonses.com.',76 'DKIM_TOKEN_2._domainkey.example.com.\t1800\tIN\tCNAME\tDKIM_TOKEN_2.dkim.amazonses.com.',77 'mail.example.com. \t1800\tIN\tMX \t10 feedback-smtp.mock-region.amazonses.com.',78 'mail.example.com. \t1800\tIN\tTXT \t"v=spf1 include:amazonses.com -all"',79 '_dmarc.example.com. \t1800\tIN\tTXT \t"v=DMARC1; p=none; pct=100; sp=none; aspf=r;"',80 ])81 self.assertEqual(outputs["Region"], "mock-region")82 self.assertEqual(outputs["Arn"], "arn:aws:ses:mock-region:111111111111:identity/example.com")83 def test_create_all_options(self):84 event = {85 "RequestType": "Create",86 "ResourceProperties": {87 "Domain": "example.com.",88 "EnableSend": "true",89 "EnableReceive": "true",90 "MailFromSubdomain": "bounce",91 "CustomDMARC": '"v=DMARC1; p=quarantine; rua=mailto:d@example.com;"',92 "TTL": "300",93 "Region": "us-test-2",94 },95 "StackId": self.mock_stack_id}96 self.ses_stubber.add_response(97 'verify_domain_identity',98 {'VerificationToken': "ID_TOKEN"},99 {'Domain': "example.com"})100 self.ses_stubber.add_response(101 'verify_domain_dkim',102 {'DkimTokens': ["DKIM_TOKEN_1", "DKIM_TOKEN_2"]},103 {'Domain': "example.com"})104 self.ses_stubber.add_response(105 'set_identity_mail_from_domain',106 {},107 {'Identity': "example.com", 'MailFromDomain': "bounce.example.com"})108 handle_domain_identity_request(event, self.mock_context)109 # Should override SES region when Region property provided:110 self.mock_boto3_client.assert_called_once_with('ses', region_name="us-test-2")111 outputs = self.assertSentResponse(112 event, physical_resource_id="arn:aws:ses:us-test-2:111111111111:identity/example.com")113 self.assertEqual(outputs["Domain"], "example.com")114 self.assertEqual(outputs["VerificationToken"], "ID_TOKEN")115 self.assertEqual(outputs["DkimTokens"], ["DKIM_TOKEN_1", "DKIM_TOKEN_2"])116 self.assertEqual(outputs["MailFromDomain"], "bounce.example.com")117 self.assertEqual(outputs["MailFromMX"], "feedback-smtp.us-test-2.amazonses.com")118 self.assertEqual(outputs["MailFromSPF"], '"v=spf1 include:amazonses.com -all"')119 self.assertEqual(outputs["DMARC"], '"v=DMARC1; p=quarantine; rua=mailto:d@example.com;"')120 self.assertEqual(outputs["ReceiveMX"], "inbound-smtp.us-test-2.amazonaws.com")121 self.assertCountEqual(outputs["Route53RecordSets"], [122 {'Type': 'TXT', 'Name': '_amazonses.example.com.', 'TTL': '300',123 'ResourceRecords': ['"ID_TOKEN"']},124 {'Type': 'CNAME', 'Name': 'DKIM_TOKEN_1._domainkey.example.com.', 'TTL': '300',125 'ResourceRecords': ['DKIM_TOKEN_1.dkim.amazonses.com.']},126 {'Type': 'CNAME', 'Name': 'DKIM_TOKEN_2._domainkey.example.com.', 'TTL': '300',127 'ResourceRecords': ['DKIM_TOKEN_2.dkim.amazonses.com.']},128 {'Type': 'MX', 'Name': 'bounce.example.com.', 'TTL': '300',129 'ResourceRecords': ['10 feedback-smtp.us-test-2.amazonses.com.']},130 {'Type': 'TXT', 'Name': 'bounce.example.com.', 'TTL': '300',131 'ResourceRecords': ['"v=spf1 include:amazonses.com -all"']},132 {'Type': 'TXT', 'Name': '_dmarc.example.com.', 'TTL': '300',133 'ResourceRecords': ['"v=DMARC1; p=quarantine; rua=mailto:d@example.com;"']},134 {'Type': 'MX', 'Name': 'example.com.', 'TTL': '300',135 'ResourceRecords': ['10 inbound-smtp.us-test-2.amazonaws.com.']}136 ])137 self.assertCountEqual(outputs["ZoneFileEntries"], [138 '_amazonses.example.com. \t300\tIN\tTXT \t"ID_TOKEN"',139 'DKIM_TOKEN_1._domainkey.example.com.\t300\tIN\tCNAME\tDKIM_TOKEN_1.dkim.amazonses.com.',140 'DKIM_TOKEN_2._domainkey.example.com.\t300\tIN\tCNAME\tDKIM_TOKEN_2.dkim.amazonses.com.',141 'bounce.example.com. \t300\tIN\tMX \t10 feedback-smtp.us-test-2.amazonses.com.',142 'bounce.example.com. \t300\tIN\tTXT \t"v=spf1 include:amazonses.com -all"',143 '_dmarc.example.com. \t300\tIN\tTXT \t"v=DMARC1; p=quarantine; rua=mailto:d@example.com;"',144 'example.com. \t300\tIN\tMX \t10 inbound-smtp.us-test-2.amazonaws.com.',145 ])146 self.assertEqual(outputs["Region"], "us-test-2")147 self.assertEqual(outputs["Arn"], "arn:aws:ses:us-test-2:111111111111:identity/example.com")148 def test_update_receive_only(self):149 event = {150 "RequestType": "Update",151 "ResourceProperties": {152 "Domain": "example.com.",153 "EnableSend": "false",154 "EnableReceive": "true",155 "CustomDMARC": None,156 },157 "StackId": self.mock_stack_id}158 self.ses_stubber.add_response(159 'verify_domain_identity',160 {'VerificationToken': "ID_TOKEN"},161 {'Domain': "example.com"})162 self.ses_stubber.add_response(163 'set_identity_mail_from_domain',164 {},165 {'Identity': "example.com", 'MailFromDomain': ""})166 handle_domain_identity_request(event, self.mock_context)167 outputs = self.assertSentResponse(168 event, physical_resource_id="arn:aws:ses:mock-region:111111111111:identity/example.com")169 self.assertEqual(outputs["Domain"], "example.com")170 self.assertEqual(outputs["VerificationToken"], "ID_TOKEN")171 self.assertNotIn("DkimTokens", outputs)172 self.assertNotIn("MailFromDomain", outputs)173 self.assertNotIn("MailFromMX", outputs)174 self.assertNotIn("MailFromSPF", outputs)175 self.assertNotIn("DMARC", outputs)176 self.assertEqual(outputs["ReceiveMX"], "inbound-smtp.mock-region.amazonaws.com")177 self.assertCountEqual(outputs["Route53RecordSets"], [178 {'Type': 'TXT', 'Name': '_amazonses.example.com.', 'TTL': '1800',179 'ResourceRecords': ['"ID_TOKEN"']},180 {'Type': 'MX', 'Name': 'example.com.', 'TTL': '1800',181 'ResourceRecords': ['10 inbound-smtp.mock-region.amazonaws.com.']}182 ])183 self.assertCountEqual(outputs["ZoneFileEntries"], [184 '_amazonses.example.com.\t1800\tIN\tTXT \t"ID_TOKEN"',185 'example.com. \t1800\tIN\tMX \t10 inbound-smtp.mock-region.amazonaws.com.',186 ])187 def test_delete(self):188 event = {189 "RequestType": "Delete",190 "PhysicalResourceId": "arn:aws:ses:mock-region:111111111111:identity/example.com",191 "ResourceProperties": {192 "Domain": "example.com.",193 "EnableSend": "true",194 "EnableReceive": "true",195 },196 "StackId": self.mock_stack_id}197 self.ses_stubber.add_response(198 'delete_identity',199 {},200 {'Identity': "example.com"})201 self.ses_stubber.add_response(202 'set_identity_mail_from_domain',203 {},204 {'Identity': "example.com", 'MailFromDomain': ""})205 handle_domain_identity_request(event, self.mock_context)206 outputs = self.assertSentResponse(207 event, physical_resource_id="arn:aws:ses:mock-region:111111111111:identity/example.com")208 self.assertEqual(outputs["Domain"], "example.com")209 self.assertNotIn("VerificationToken", outputs)210 self.assertNotIn("DkimTokens", outputs)211 self.assertNotIn("MailFromDomain", outputs)212 self.assertNotIn("MailFromMX", outputs)213 self.assertNotIn("MailFromSPF", outputs)214 self.assertNotIn("DMARC", outputs)215 self.assertNotIn("ReceiveMX", outputs)216 self.assertEqual(outputs["Route53RecordSets"], [])217 self.assertEqual(outputs["ZoneFileEntries"], [])218 def test_v0_3_physical_id_change(self):219 # Prior to v0.3, the PhysicalResourceId was just the cleaned Domain.220 # Make sure we ignore the Delete operation that CF will issue on the221 # old physical ID after upgrading.222 event = {223 "RequestType": "Delete",224 "PhysicalResourceId": "example.com", # old format: just the domain225 "ResourceProperties": {226 "Domain": "example.com.",227 "EnableSend": "true",228 "EnableReceive": "true",229 },230 "StackId": self.mock_stack_id}231 # self.ses_stubber.nothing: *no* SES ops should occur232 handle_domain_identity_request(event, self.mock_context)233 outputs = self.assertSentResponse(event, physical_resource_id="example.com")234 self.assertEqual(outputs["Domain"], "example.com")235 def test_boto_error(self):236 event = {237 "RequestType": "Create",238 "ResourceProperties": {239 "Domain": "bad domain name",240 },241 "StackId": self.mock_stack_id}242 self.ses_stubber.add_client_error(243 'verify_domain_identity',244 "InvalidParameterValue",245 "Invalid domain name bad domain name.",246 expected_params={'Domain': "bad domain name"})247 with self.assertLogs(level="ERROR") as cm:248 handle_domain_identity_request(event, self.mock_context)249 self.assertSentResponse(250 event, status="FAILED",251 reason="An error occurred (InvalidParameterValue) when calling the"252 " VerifyDomainIdentity operation: Invalid domain name bad domain name.",253 physical_resource_id=MOCK_ANY)254 # Check that the exception got logged255 self.assertEqual(len(cm.output), 1)256 self.assertIn(257 'ERROR:root:Error updating SES: An error occurred (InvalidParameterValue) when'258 ' calling the VerifyDomainIdentity operation: Invalid domain name bad domain name.',259 cm.output[0])260 def test_invalid_boolean_property(self):261 event = {262 "RequestType": "Create",263 "ResourceProperties": {264 "Domain": "example.com",265 "EnableSend": "yes",266 },267 "StackId": self.mock_stack_id}268 handle_domain_identity_request(event, self.mock_context)269 self.assertSentResponse(270 event, status="FAILED",271 reason="The 'EnableSend' property must be 'true' or 'false', not 'yes'.",...
stackmate_aws_ses_mailfrom.py
Source:stackmate_aws_ses_mailfrom.py
...21class StackmateSESMailFromEnabler(BaseAWSSES):22 """Handles AWS SES DKIM validation and status"""23 def enable(self):24 """Enables and verifies the DKIM setting"""25 response = self.ses.set_identity_mail_from_domain(26 BehaviorOnMXFailure='UseDefaultValue',27 Identity=self.domain,28 MailFromDomain=self.get_outbound_subdomain(),29 )30 if not self.response_success(response):31 raise AnsibleError(32 'Failed to enable MAIL FROM for domain {}'.format(self.domain)33 )34 return self.update_dns_records(self.get_records(), action=BaseAWSSES.DNS_ACTION_UPSERT)35 def disable(self):36 """Disables DKIM for an SES identity"""37 self.ses.set_identity_mail_from_domain(38 BehaviorOnMXFailure='UseDefaultValue',39 Identity=self.domain,40 MailFromDomain=None,41 )42 return self.update_dns_records(self.get_records(), action=BaseAWSSES.DNS_ACTION_REMOVE)43 def get_outbound_subdomain(self):44 """Returns the outbound subdomain to set"""45 return 'mailer.{}'.format(self.domain)46 def get_records(self):47 """Returns the DNS records to set in Route53"""48 outbound_subdomain = self.get_outbound_subdomain()49 return [50 {51 'record': outbound_subdomain,...
mail_from_domain_provider.py
Source:mail_from_domain_provider.py
...57 ses = boto3.client("ses", region_name=self.region)58 mx_failure_behaviour = self.behavior_on_mx_failure59 if mx_failure_behaviour is None:60 mx_failure_behaviour = "UseDefaultValue"61 ses.set_identity_mail_from_domain(62 Identity=self.domain,63 MailFromDomain=f"{self.mail_from_subdomain}.{self.domain}",64 BehaviorOnMXFailure=mx_failure_behaviour,65 )66 self.physical_resource_id = f"{self.domain}@{self.region}"67 self.set_attribute("Domain", self.domain)68 self.set_attribute("Region", self.region)69 self.set_attribute("RecordSets", self.generate_dns_recordsets())70 except Exception as e:71 if not self.physical_resource_id:72 self.physical_resource_id = "could-not-create"73 self.fail(74 f"could not set mail from domain for {self.mail_from_subdomain}.{self.domain}, {e}"75 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!