from unittest import mock import pytest from dmp.meta.exceptions import MetaAdAccountNotFoundError from dmp.meta.handlers import ( DeleteMetaUserAdAccountHandler, DeleteMetaUserAdAccountRequest, ) from dmp.meta.models import ( MetaAdAccount, MetaAdReportingConnection, MetaUserAdAccount, MetaUserConnection, ) from dmp.meta.repositories import ( MetaAdReportingConnectionRepository, MetaUserAdAccountRepository, MetaUserConnectionRepository, ) from tests.unit.faker import FakerTyped from tests.unit.types import CreateModel class TestDeleteMetaUserAdAccountHandler: @pytest.mark.db def test_delete_user_ad_account( self, handler: DeleteMetaUserAdAccountHandler, facebook_client_mock: mock.MagicMock, create_model: CreateModel, identity_id: str, user_connection_repository: MetaUserConnectionRepository, ad_reporting_connection_repository: MetaAdReportingConnectionRepository, user_ad_account_repository: MetaUserAdAccountRepository, ) -> None: ad_account_1 = create_model(MetaAdAccount) ad_account_2 = create_model(MetaAdAccount) create_model( MetaUserConnection, is_valid=True, identity_id=identity_id, ad_accounts=[ad_account_1, ad_account_2], ) create_model( MetaAdReportingConnection, identity_id=identity_id, ad_accounts=[ad_account_1, ad_account_2], ) create_model( MetaUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_1.id ) create_model( MetaUserAdAccount, identity_id=identity_id, ad_account_id=ad_account_2.id ) handler.handle( DeleteMetaUserAdAccountRequest( identity_id=identity_id, ad_account_id=ad_account_1.id ), ) user_ad_account = ( user_ad_account_repository.find_by_ad_accounts_ids_and_identity_id( identity_id=identity_id, ad_accounts_ids=[ad_account_1.id] ) ) user_connection = user_connection_repository.get_by_identity_id(identity_id) ad_reporting_connection = ad_reporting_connection_repository.get_by_identity_id( identity_id ) facebook_client_mock.disconnect_user.assert_not_called() assert not user_ad_account assert user_connection assert user_connection.ad_accounts == [ad_account_2] assert ad_reporting_connection assert ad_reporting_connection.ad_accounts == [ad_account_2] @pytest.mark.db def test_delete_user_ad_account_last( self, handler: DeleteMetaUserAdAccountHandler, facebook_client_mock: mock.MagicMock, create_model: CreateModel, identity_id: str, user_connection_repository: MetaUserConnectionRepository, ad_reporting_connection_repository: MetaAdReportingConnectionRepository, user_ad_account_repository: MetaUserAdAccountRepository, ) -> None: ad_account = create_model(MetaAdAccount) create_model( MetaUserConnection, is_valid=True, identity_id=identity_id, ad_accounts=[ad_account], ) create_model( MetaAdReportingConnection, identity_id=identity_id, ad_accounts=[ad_account], ) create_model( MetaUserAdAccount, identity_id=identity_id, ad_account_id=ad_account.id ) handler.handle( DeleteMetaUserAdAccountRequest( identity_id=identity_id, ad_account_id=ad_account.id ), ) user_ad_account = ( user_ad_account_repository.find_by_ad_accounts_ids_and_identity_id( identity_id=identity_id, ad_accounts_ids=[ad_account.id] ) ) user_connection = user_connection_repository.get_by_identity_id(identity_id) ad_reporting_connection = ad_reporting_connection_repository.get_by_identity_id( identity_id ) facebook_client_mock.disconnect_user.assert_called_once() assert not user_ad_account assert not user_connection assert not ad_reporting_connection @pytest.mark.db def test_delete_user_ad_account_wrong_ad_account_id( self, handler: DeleteMetaUserAdAccountHandler, identity_id: str, fake: FakerTyped, ) -> None: ad_account_id = fake.uuid4_string() with pytest.raises(MetaAdAccountNotFoundError): handler.handle( DeleteMetaUserAdAccountRequest( identity_id=identity_id, ad_account_id=ad_account_id ), )