from unittest import mock import pytest from fansifter_common.auth.account import Account, AccountAccess from dmp.artists.dtos import ArtistAccount from dmp.artists.handlers import GetArtistAccountsHandler, GetArtistAccountsRequest from dmp.fandata.models import AccountArtistFullFanDataAccessDbt, FansByArtistAccountDbt from tests.unit.faker import FakerTyped from tests.unit.types import BuildModel, CreateReportingModel class TestGetArtistAccountsHandler: @pytest.mark.db def test_get_accounts_empty( self, handler: GetArtistAccountsHandler, fake: FakerTyped, identity_id: str ) -> None: artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=fake.uuid4_string(), ) ) assert artist_accounts == [] @pytest.mark.db def test_get_accounts_global_participant_id( self, handler: GetArtistAccountsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, identity_id: str, account: Account, ) -> None: global_participant_id = fake.uuid4_string() create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=account.vendor_id, fans_count=50, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=account.vendor_id, fans_count=100, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=fake.uuid4_string(), ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert len(artist_accounts) == 2 assert artist_accounts[0].global_participant_id == global_participant_id assert artist_accounts[0].fans_count == 100 assert artist_accounts[1].global_participant_id == global_participant_id assert artist_accounts[1].fans_count == 50 @pytest.mark.db def test_get_accounts_with_vendor( self, handler: GetArtistAccountsHandler, create_reporting_model: BuildModel, identity_id: str, account: Account, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, fans_count=50, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id + 1, fans_count=100, ) # Ignore this one create_reporting_model( FansByArtistAccountDbt, global_participant_id=fake.uuid4_string(), ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [ ArtistAccount( vendor_id=account.vendor_id, subaccount_id=account.subaccount_id + 1, global_participant_id=global_participant_id, fans_count=100, ), ArtistAccount( vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, fans_count=50, ), ] @pytest.mark.db def test_get_accounts_with_subaccount( self, handler: GetArtistAccountsHandler, auth_service_mock: mock.MagicMock, create_reporting_model: BuildModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() vendor_id = fake.integer() subaccount_id = fake.integer() auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[Account(vendor_id=vendor_id, subaccount_id=subaccount_id)], ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=vendor_id, subaccount_id=0, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=vendor_id, subaccount_id=subaccount_id, fans_count=100, ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [ ArtistAccount( vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, fans_count=100, ) ] @pytest.mark.db def test_get_accounts_for_artist_team_no_fan_data( self, handler: GetArtistAccountsHandler, auth_service_mock: mock.MagicMock, create_reporting_model: BuildModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() artist_team_vendor_id = 2000 artist_team_subaccount_id = 0 auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[ Account( vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, ) ], ) create_reporting_model( AccountArtistFullFanDataAccessDbt, global_participant_id=global_participant_id, vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, provider_vendor_id=None, ) # Ignore this one create_reporting_model( FansByArtistAccountDbt, global_participant_id=fake.uuid4_string(), ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [] @pytest.mark.db def test_get_accounts_for_artist_team_wrong_artist( self, handler: GetArtistAccountsHandler, auth_service_mock: mock.MagicMock, create_reporting_model: BuildModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() vendor_id = 1000 subaccount_id = 0 artist_team_vendor_id = 2000 artist_team_subaccount_id = 0 auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[ Account( vendor_id=vendor_id, subaccount_id=subaccount_id, ) ], ) create_reporting_model( AccountArtistFullFanDataAccessDbt, global_participant_id=fake.uuid4_string(), vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, provider_vendor_id=None, ) # Ignore this one create_reporting_model( FansByArtistAccountDbt, global_participant_id=fake.uuid4_string(), ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [] @pytest.mark.db def test_get_accounts_for_artist_team_with_vendor( self, handler: GetArtistAccountsHandler, auth_service_mock: mock.MagicMock, create_reporting_model: BuildModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() fan_data_owner_vendor_id = 1000 fan_data_owner_subaccount_id = 10000 artist_team_vendor_id = 2000 artist_team_subaccount_id = 0 auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[ Account( vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, ) ], ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=fan_data_owner_vendor_id, subaccount_id=fan_data_owner_subaccount_id, fans_count=50, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=fan_data_owner_vendor_id, subaccount_id=0, fans_count=100, ) create_reporting_model( AccountArtistFullFanDataAccessDbt, global_participant_id=global_participant_id, vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, provider_vendor_id=None, ) # Ignore this one create_reporting_model( FansByArtistAccountDbt, global_participant_id=fake.uuid4_string(), ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [ ArtistAccount( vendor_id=fan_data_owner_vendor_id, subaccount_id=0, global_participant_id=global_participant_id, fans_count=100, ), ArtistAccount( vendor_id=fan_data_owner_vendor_id, subaccount_id=fan_data_owner_subaccount_id, global_participant_id=global_participant_id, fans_count=50, ), ] @pytest.mark.db def test_get_accounts_for_artist_team_with_subaccount( self, handler: GetArtistAccountsHandler, auth_service_mock: mock.MagicMock, create_reporting_model: BuildModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() fan_data_owner_vendor_id = 1000 fan_data_owner_subaccount_id = 10000 artist_team_vendor_id = 2000 artist_team_subaccount_id = 20000 auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[ Account( vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, ) ], ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=fan_data_owner_vendor_id, subaccount_id=fan_data_owner_subaccount_id, fans_count=50, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=fan_data_owner_vendor_id, subaccount_id=0, fans_count=100, ) create_reporting_model( AccountArtistFullFanDataAccessDbt, global_participant_id=global_participant_id, vendor_id=artist_team_vendor_id, subaccount_id=artist_team_subaccount_id, provider_vendor_id=None, ) # Ignore this one create_reporting_model( FansByArtistAccountDbt, global_participant_id=fake.uuid4_string(), ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [ ArtistAccount( vendor_id=fan_data_owner_vendor_id, subaccount_id=0, global_participant_id=global_participant_id, fans_count=100, ), ArtistAccount( vendor_id=fan_data_owner_vendor_id, subaccount_id=fan_data_owner_subaccount_id, global_participant_id=global_participant_id, fans_count=50, ), ] @pytest.mark.db def test_get_accounts_for_artist_team_with_vendor_and_selected_provider_vendor_id( self, handler: GetArtistAccountsHandler, auth_service_mock: mock.MagicMock, create_reporting_model: BuildModel, identity_id: str, fake: FakerTyped, ) -> None: global_participant_id = fake.uuid4_string() fan_data_owner_vendor_id = 1000 additional_fan_data_owner_vendor_id = 2000 artist_team_vendor_id = 3000 auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[ Account( vendor_id=artist_team_vendor_id, subaccount_id=0, ) ], ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=fan_data_owner_vendor_id, subaccount_id=0, fans_count=50, ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=additional_fan_data_owner_vendor_id, subaccount_id=0, fans_count=404, ) create_reporting_model( AccountArtistFullFanDataAccessDbt, global_participant_id=global_participant_id, vendor_id=artist_team_vendor_id, subaccount_id=0, provider_vendor_id=fan_data_owner_vendor_id, ) artist_accounts = handler.handle( GetArtistAccountsRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert artist_accounts == [ ArtistAccount( vendor_id=fan_data_owner_vendor_id, subaccount_id=0, global_participant_id=global_participant_id, fans_count=50, ) ]