""" Tests for Mock Financial Client. Validates tenant resolution from email domains. """ import pytest from src.clients.financial_client import MockFinancialClient, get_financial_client from src.models.tenant import TenantStatus class TestMockFinancialClient: """Tests for MockFinancialClient.""" @pytest.fixture def client(self): """Create a mock financial client.""" return MockFinancialClient() @pytest.mark.asyncio async def test_resolve_known_tenant_oestepan(self, client): """Test resolving OESTEPAN tenant from email.""" tenant = await client.resolve_tenant_from_email("joao@oestepan.com.br") assert tenant is not None assert tenant.id == "tenant_oestepan" assert tenant.name == "OESTEPAN Ltda" assert tenant.status == TenantStatus.ACTIVE @pytest.mark.asyncio async def test_resolve_known_tenant_enseg(self, client): """Test resolving ENSEG tenant from email.""" tenant = await client.resolve_tenant_from_email("admin@enseg.com.br") assert tenant is not None assert tenant.id == "tenant_enseg" assert tenant.name == "ENSEG Corretora de Seguros" @pytest.mark.asyncio async def test_resolve_known_tenant_itguys(self, client): """Test resolving internal iT Guys tenant.""" tenant = await client.resolve_tenant_from_email("arthur@itguys.com.br") assert tenant is not None assert tenant.id == "tenant_itguys" assert tenant.rate_limit_per_hour == 50 # Higher for internal @pytest.mark.asyncio async def test_resolve_unknown_domain(self, client): """Test that unknown domains return None.""" tenant = await client.resolve_tenant_from_email("user@unknown-company.com") assert tenant is None @pytest.mark.asyncio async def test_resolve_inactive_tenant(self, client): """Test that inactive tenants are not resolved.""" tenant = await client.resolve_tenant_from_email("user@demo-cliente.com.br") assert tenant is None # Inactive tenant should not be returned @pytest.mark.asyncio async def test_resolve_invalid_email(self, client): """Test handling of invalid email format.""" tenant = await client.resolve_tenant_from_email("not-an-email") assert tenant is None @pytest.mark.asyncio async def test_get_tenant_by_id(self, client): """Test getting tenant by ID.""" tenant = await client.get_tenant_by_id("tenant_oestepan") assert tenant is not None assert tenant.id == "tenant_oestepan" @pytest.mark.asyncio async def test_get_nonexistent_tenant_by_id(self, client): """Test getting nonexistent tenant by ID.""" tenant = await client.get_tenant_by_id("tenant_does_not_exist") assert tenant is None @pytest.mark.asyncio async def test_list_active_tenants(self, client): """Test listing all active tenants.""" tenants = await client.list_active_tenants() assert len(tenants) >= 3 # At least oestepan, enseg, itguys # All returned tenants should be active for tenant in tenants: assert tenant.status == TenantStatus.ACTIVE # Demo tenant (inactive) should not be in list tenant_ids = [t.id for t in tenants] assert "tenant_demo" not in tenant_ids @pytest.mark.asyncio async def test_domain_case_insensitive(self, client): """Test that domain matching is case-insensitive.""" tenant1 = await client.get_tenant_by_email_domain("OESTEPAN.COM.BR") tenant2 = await client.get_tenant_by_email_domain("oestepan.com.br") assert tenant1 is not None assert tenant2 is not None assert tenant1.id == tenant2.id @pytest.mark.asyncio async def test_multiple_domains_same_tenant(self, client): """Test tenant with multiple email domains.""" tenant1 = await client.get_tenant_by_email_domain("enseg.com.br") tenant2 = await client.get_tenant_by_email_domain("enseg.net.br") assert tenant1 is not None assert tenant2 is not None assert tenant1.id == tenant2.id == "tenant_enseg" class TestFinancialClientFactory: """Tests for the financial client factory function.""" def test_get_financial_client_returns_mock(self): """Test that factory returns mock client.""" client = get_financial_client() assert isinstance(client, MockFinancialClient)