from unittest import mock import faker import pytest from fansifter_common.adapters.ows_account import Vendor from fansifter_common.auth.account import Account, AccountAccess from email_campaigns.emails.handlers import ( GetEmailDomainsHandler, GetEmailDomainsRequest, ) from email_campaigns.emails.models import EmailDomain from tests.unit.types import BuildModel, CreateModel class TestGetEmailDomainsHandler: @pytest.mark.db def test_get_domains_with_brand_only( self, handler: GetEmailDomainsHandler, create_model: CreateModel, identity_id: str, faker: faker.Faker, ows_account_client_mock: mock.Mock, ) -> None: vendor_id = faker.pyint() email_domain = create_model( EmailDomain, brand="sme", vendor_id=None, subaccount_id=None ) ows_account_client_mock.get_vendor.return_value = Vendor( vendor_id=vendor_id, account_name="Test", company_brand="sme", ) email_domains = handler.handle( GetEmailDomainsRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=faker.pyint(), ) ) assert email_domains == [email_domain] @pytest.mark.db def test_get_domains_with_brand_not_found( self, handler: GetEmailDomainsHandler, create_model: CreateModel, identity_id: str, faker: faker.Faker, ows_account_client_mock: mock.Mock, ) -> None: vendor_id = faker.pyint() create_model( EmailDomain, brand="theorchard", vendor_id=None, subaccount_id=None ) ows_account_client_mock.get_vendor.return_value = Vendor( vendor_id=vendor_id, account_name="Test", company_brand="sme", ) email_domains = handler.handle( GetEmailDomainsRequest( identity_id=identity_id, vendor_id=vendor_id, subaccount_id=faker.pyint(), ) ) assert email_domains == [] @pytest.mark.db def test_get_domains_allowed_account( self, handler: GetEmailDomainsHandler, build_model: BuildModel, create_model: CreateModel, identity_id: str, ows_account_client_mock: mock.Mock, auth_service_mock: mock.MagicMock, ) -> None: account = build_model(Account) auth_service_mock.authorize_account = mock.Mock( return_value=AccountAccess(accounts=[account]) ) email_domain_1 = create_model( EmailDomain, brand="sme", vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_model( EmailDomain, brand="sme", vendor_id=account.vendor_id, subaccount_id=account.subaccount_id + 1, ) ows_account_client_mock.get_vendor.return_value = Vendor( vendor_id=account.vendor_id, account_name="Test", company_brand="sme", ) email_domains = handler.handle( GetEmailDomainsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) ) assert email_domains == [email_domain_1]