from unittest import mock import pytest from anydi import Container from faker import Faker from fansifter_common.auth.account import Account from dmp.artists.dtos import Label from dmp.artists.handlers import ( GetArtistAccountsWithFanDataHandler, GetArtistAccountsWithFanDataRequest, ) from dmp.fandata.models import FansByArtistAccountDbt from dmp.rosters.services import GlobalFanDataAccessService from tests.unit.types import CreateReportingModel class TestGetArtistAccountsWithFanDataHandler: @pytest.mark.db def test_get_artist_accounts_empty( self, container: Container, handler: GetArtistAccountsWithFanDataHandler, faker: Faker, identity_id: str, ) -> None: global_fandata_access_service_mock = mock.MagicMock( spec=GlobalFanDataAccessService ) global_fandata_access_service_mock.is_enabled_for_any_vendor.return_value = ( False ) with container.override( GlobalFanDataAccessService, global_fandata_access_service_mock ): result = handler.handle( GetArtistAccountsWithFanDataRequest( identity_id=identity_id, global_participant_id=faker.uuid4(), ) ) assert result.has_fandata is False assert result.has_global_fandata_list is False assert result.accounts == [] @pytest.mark.db def test_get_artist_accounts( self, container: Container, handler: GetArtistAccountsWithFanDataHandler, create_reporting_model: CreateReportingModel, faker: Faker, identity_id: str, account: Account, ) -> None: global_participant_id = faker.uuid4() global_fandata_access_service_mock = mock.MagicMock( spec=GlobalFanDataAccessService, is_enabled_for_any_vendor=mock.MagicMock(return_value=False), ) create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) with container.override( GlobalFanDataAccessService, global_fandata_access_service_mock ): result = handler.handle( GetArtistAccountsWithFanDataRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert result.has_fandata is True assert result.has_global_fandata_list is False assert result.accounts == [ Label(vendor_id=account.vendor_id, subaccount_id=account.subaccount_id) ] @pytest.mark.db def test_get_artist_accounts_global_fandata_enabled( self, container: Container, handler: GetArtistAccountsWithFanDataHandler, create_reporting_model: CreateReportingModel, faker: Faker, identity_id: str, account: Account, ) -> None: global_participant_id = faker.uuid4() global_fandata_access_service_mock = mock.MagicMock( spec=GlobalFanDataAccessService ) global_fandata_access_service_mock.is_enabled_for_any_vendor.return_value = True create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=account.vendor_id, ) with container.override( GlobalFanDataAccessService, global_fandata_access_service_mock ): result = handler.handle( GetArtistAccountsWithFanDataRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert result.has_fandata is True assert result.has_global_fandata_list is True assert result.accounts == [] @pytest.mark.db def test_get_artist_accounts_global_fandata_enabled_fan_data_collected_by_other_vendor( self, container: Container, handler: GetArtistAccountsWithFanDataHandler, create_reporting_model: CreateReportingModel, faker: Faker, identity_id: str, ) -> None: global_participant_id = faker.uuid4() # 1) fan data was collected by a vendor I don't have access to other_vendor_id = 10000 global_fandata_access_service_mock = mock.MagicMock( spec=GlobalFanDataAccessService ) global_fandata_access_service_mock.is_enabled_for_any_vendor.return_value = True # 2) but I have access to a vendor that is a Main / Local rep global_fandata_access_service_mock.has_access_for_artist_and_any_vendor.return_value = True create_reporting_model( FansByArtistAccountDbt, global_participant_id=global_participant_id, vendor_id=other_vendor_id, ) with container.override( GlobalFanDataAccessService, global_fandata_access_service_mock ): result = handler.handle( GetArtistAccountsWithFanDataRequest( identity_id=identity_id, global_participant_id=global_participant_id, ) ) assert result.has_fandata is True assert result.has_global_fandata_list is True assert result.accounts == []