|
| 1 | +from posthog.test.base import BaseTest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from django.test import override_settings |
| 5 | + |
| 6 | +from posthog.management.commands.migrate_ses_tenants import migrate_ses_tenants |
| 7 | +from posthog.models.integration import Integration |
| 8 | + |
| 9 | + |
| 10 | +class _FakeSESv2Client: |
| 11 | + def __init__(self): |
| 12 | + self.created_tenants: list[str] = [] |
| 13 | + self.associations: list[tuple[str, str]] = [] |
| 14 | + |
| 15 | + def get_caller_identity(self): |
| 16 | + return {"Account": "123456789012"} |
| 17 | + |
| 18 | + def create_tenant(self, TenantName: str, Tags: list[dict]): # noqa: N803 |
| 19 | + # emulate idempotency externally in test assertions |
| 20 | + if TenantName in self.created_tenants: |
| 21 | + from botocore.exceptions import ClientError |
| 22 | + |
| 23 | + raise ClientError({"Error": {"Code": "AlreadyExistsException", "Message": "Tenant exists"}}, "CreateTenant") |
| 24 | + self.created_tenants.append(TenantName) |
| 25 | + return {"TenantName": TenantName} |
| 26 | + |
| 27 | + def create_tenant_resource_association(self, TenantName: str, ResourceArn: str): # noqa: N803 |
| 28 | + # emulate idempotency externally in test assertions |
| 29 | + pair = (TenantName, ResourceArn) |
| 30 | + if pair in self.associations: |
| 31 | + from botocore.exceptions import ClientError |
| 32 | + |
| 33 | + raise ClientError( |
| 34 | + {"Error": {"Code": "AlreadyExistsException", "Message": "Association exists"}}, |
| 35 | + "CreateTenantResourceAssociation", |
| 36 | + ) |
| 37 | + self.associations.append(pair) |
| 38 | + return {"TenantName": TenantName, "ResourceArn": ResourceArn} |
| 39 | + |
| 40 | + |
| 41 | +class TestMigrateSESTenants(BaseTest): |
| 42 | + def setUp(self): |
| 43 | + super().setUp() |
| 44 | + # Two SES email integrations on the same domain (should dedupe by (team, domain)) |
| 45 | + Integration.objects.create( |
| 46 | + team=self.team, |
| 47 | + kind="email", |
| 48 | + integration_id="[email protected]", |
| 49 | + config={"domain": "example.com", "provider": "ses"}, |
| 50 | + created_by=self.user, |
| 51 | + ) |
| 52 | + Integration.objects.create( |
| 53 | + team=self.team, |
| 54 | + kind="email", |
| 55 | + integration_id="[email protected]", |
| 56 | + config={"domain": "example.com", "provider": "ses"}, |
| 57 | + created_by=self.user, |
| 58 | + ) |
| 59 | + # Non-SES provider should be ignored |
| 60 | + Integration.objects.create( |
| 61 | + team=self.team, |
| 62 | + kind="email", |
| 63 | + integration_id="[email protected]", |
| 64 | + config={"domain": "other.com", "provider": "mailjet"}, |
| 65 | + created_by=self.user, |
| 66 | + ) |
| 67 | + |
| 68 | + @override_settings(SES_ACCESS_KEY_ID="test", SES_SECRET_ACCESS_KEY="test", SES_REGION="us-east-1", SES_ENDPOINT="") |
| 69 | + @patch("posthog.management.commands.migrate_ses_tenants.boto3.client") |
| 70 | + def test_dry_run(self, mock_boto_client): |
| 71 | + # Arrange stub clients |
| 72 | + sesv2 = _FakeSESv2Client() |
| 73 | + mock_boto_client.side_effect = lambda service, **kwargs: sesv2 |
| 74 | + |
| 75 | + # Act: dry-run should not attempt create calls but will still resolve account id |
| 76 | + migrate_ses_tenants(team_ids=[], domains=[], dry_run=True) |
| 77 | + |
| 78 | + # Assert: no tenants/associations performed |
| 79 | + assert sesv2.created_tenants == [] |
| 80 | + assert sesv2.associations == [] |
| 81 | + |
| 82 | + @override_settings(SES_ACCESS_KEY_ID="test", SES_SECRET_ACCESS_KEY="test", SES_REGION="us-east-1", SES_ENDPOINT="") |
| 83 | + @patch("posthog.management.commands.migrate_ses_tenants.boto3.client") |
| 84 | + def test_migrate_for_team(self, mock_boto_client): |
| 85 | + sesv2 = _FakeSESv2Client() |
| 86 | + mock_boto_client.side_effect = lambda service, **kwargs: sesv2 |
| 87 | + |
| 88 | + migrate_ses_tenants(team_ids=[self.team.id], domains=[], dry_run=False) |
| 89 | + |
| 90 | + # Deduped: only one tenant and one association for (team, example.com) |
| 91 | + assert sesv2.created_tenants == [f"team-{self.team.id}"] |
| 92 | + expected_arn = f"arn:aws:ses:us-east-1:123456789012:identity/example.com" |
| 93 | + assert sesv2.associations == [(f"team-{self.team.id}", expected_arn)] |
| 94 | + |
| 95 | + @override_settings(SES_ACCESS_KEY_ID="test", SES_SECRET_ACCESS_KEY="test", SES_REGION="eu-west-1", SES_ENDPOINT="") |
| 96 | + @patch("posthog.management.commands.migrate_ses_tenants.boto3.client") |
| 97 | + def test_migrate_for_domain_filter(self, mock_boto_client): |
| 98 | + sesv2 = _FakeSESv2Client() |
| 99 | + mock_boto_client.side_effect = lambda service, **kwargs: sesv2 |
| 100 | + |
| 101 | + # Use domains filter; should match example.com only |
| 102 | + migrate_ses_tenants(team_ids=[], domains=["example.com"], dry_run=False) |
| 103 | + |
| 104 | + assert sesv2.created_tenants == [f"team-{self.team.id}"] |
| 105 | + expected_arn = f"arn:aws:ses:eu-west-1:123456789012:identity/example.com" |
| 106 | + assert sesv2.associations == [(f"team-{self.team.id}", expected_arn)] |
| 107 | + |
| 108 | + @override_settings(SES_ACCESS_KEY_ID="test", SES_SECRET_ACCESS_KEY="test", SES_REGION="us-east-1", SES_ENDPOINT="") |
| 109 | + @patch("posthog.management.commands.migrate_ses_tenants.boto3.client") |
| 110 | + def test_idempotent_on_repeated_run(self, mock_boto_client): |
| 111 | + sesv2 = _FakeSESv2Client() |
| 112 | + mock_boto_client.side_effect = lambda service, **kwargs: sesv2 |
| 113 | + |
| 114 | + # First run creates |
| 115 | + migrate_ses_tenants(team_ids=[self.team.id], domains=[], dry_run=False) |
| 116 | + # Second run should hit AlreadyExistsException internally and not error |
| 117 | + migrate_ses_tenants(team_ids=[self.team.id], domains=[], dry_run=False) |
| 118 | + |
| 119 | + # Still only one tenant and association recorded |
| 120 | + assert sesv2.created_tenants == [f"team-{self.team.id}"] |
| 121 | + expected_arn = f"arn:aws:ses:us-east-1:123456789012:identity/example.com" |
| 122 | + assert sesv2.associations == [(f"team-{self.team.id}", expected_arn)] |
0 commit comments