from unittest import mock import pytest from fansifter_common.adapters.sendgrid.models import FanData from fansifter_common.legal_info.types import LegalEntity from fansifter_common.translation.types import Terms from app.config import Settings from app.services import SeedlistService class TestSeedlistService: @pytest.fixture(scope="class") def seedlist_service( self, s3_client_mock: mock.MagicMock, legal_info_service_mock: mock.MagicMock, translation_service_mock: mock.MagicMock, ) -> SeedlistService: settings = Settings( environment="test", seedlist_s3_key="test-key.csv", privacy_footer_utm_source_name="test_source", ) return SeedlistService( s3_client=s3_client_mock, legal_info_service=legal_info_service_mock, translation_service=translation_service_mock, settings=settings, ) def test_get_seedlist_emails_success( self, seedlist_service: SeedlistService, s3_client_mock: mock.MagicMock, ) -> None: csv_content = ( 'Email\n"test1@example.com"\n"test2@example.com"\ntest3@example.com\n' ) s3_client_mock.get_object_content.return_value = csv_content result = seedlist_service.get_seedlist_emails() assert result == ["test1@example.com", "test2@example.com", "test3@example.com"] s3_client_mock.get_object_content.assert_called_once_with( bucket="test-sendgrid-send-emails-inboxmonster-seedlist", key="test-key.csv" ) def test_get_seedlist_emails_empty_rows( self, seedlist_service: SeedlistService, s3_client_mock: mock.MagicMock, ) -> None: csv_content = "Email\ntest1@example.com\n\n \ntest2@example.com\n" s3_client_mock.get_object_content.return_value = csv_content result = seedlist_service.get_seedlist_emails() assert result == ["test1@example.com", "test2@example.com"] def test_get_seedlist_emails_s3_error( self, seedlist_service: SeedlistService, s3_client_mock: mock.MagicMock, ) -> None: s3_client_mock.get_object_content.side_effect = Exception("S3 error") result = seedlist_service.get_seedlist_emails() assert result == [] def test_create_seedlist_fan_data( self, seedlist_service: SeedlistService, legal_info_service_mock: mock.MagicMock, translation_service_mock: mock.MagicMock, ) -> None: legal_entity = LegalEntity(name="Test Entity", address="Test Address") legal_info_service_mock.get_legal_entity.return_value = legal_entity legal_info_service_mock.prepare_privacy_links_for_country.return_value = ( "Privacy links" ) translation = Terms( optInInfo="Opt in info", optInInfoExternal="Opt in external info", unsubscribe="Unsubscribe", ) translation_service_mock.get_translations_by_country_code.return_value = { "en": translation } emails = ["test1@example.com", "test2@example.com"] result = seedlist_service.create_seedlist_fan_data(emails) assert len(result) == 2 assert all(isinstance(fan_data, FanData) for fan_data in result) assert result[0].email == "test1@example.com" assert result[1].email == "test2@example.com" assert result[0].profile_token is None assert result[0].legal_entity_name == "Test Entity" assert result[0].legal_entity_address == "Test Address" assert result[0].privacy_links_block == "Privacy links" legal_info_service_mock.get_legal_entity.assert_called_with("UK") legal_info_service_mock.prepare_privacy_links_for_country.assert_called_with( "UK", "test_source" ) translation_service_mock.get_translations_by_country_code.assert_called_with( "UK" )