from unittest import mock import pytest from faker import Faker from fansifter_common.auth.account import Account, AccountAccess from dmp.artists.handlers import GetArtistsV2Handler, GetArtistsV2Request from dmp.artists.models import VirtualParticipant from dmp.fandata.models import GlobalFansByArtistDbt from dmp.rosters.models import ( ArtistRosterMainRep, CustomList, GlobalParticipantAccountDbt, ) from tests.unit.types import CreateReportingModel class TestGetArtistsV2Handler: @pytest.mark.db("reporting") def test_empty(self, handler: GetArtistsV2Handler, identity_id: str) -> None: response = handler.handle( GetArtistsV2Request( identity_id=identity_id, limit=10, offset=0, ) ) assert response.total == 0 assert response.items == [] @pytest.mark.db("reporting") def test_auth_filtering( self, handler: GetArtistsV2Handler, auth_service_mock: mock.MagicMock, create_reporting_model: CreateReportingModel, identity_id: str, faker: Faker, ) -> None: vendor_id = 100 subaccount_id = 200 other_vendor_id = 999 global_participant_id = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id, global_participant_name="Authorized Artist", ) create_reporting_model( GlobalParticipantAccountDbt, id=global_participant_id, spotify_id="2NtvkyG03ApOKPBj3TcQRt", vendor_id=vendor_id, subaccount_id=subaccount_id, ) other_subaccount_id = 999 other_gp_id = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=other_vendor_id, subaccount_id=other_subaccount_id, global_participant_id=other_gp_id, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=other_gp_id, global_participant_name="Unauthorized Artist", ) create_reporting_model( GlobalParticipantAccountDbt, id=other_gp_id, spotify_id="0bouHpX4JiuPnIfP2jFxRi", vendor_id=other_vendor_id, subaccount_id=other_subaccount_id, ) auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[Account(vendor_id=vendor_id, subaccount_id=subaccount_id)] ) response = handler.handle( GetArtistsV2Request( identity_id=identity_id, limit=50, offset=0, ) ) assert response.total == 1 assert len(response.items) == 1 assert response.items[0].global_participant_id == global_participant_id assert response.items[0].name == "Authorized Artist" @pytest.mark.db("reporting") def test_virtual_participant_included( self, handler: GetArtistsV2Handler, auth_service_mock: mock.MagicMock, create_reporting_model: CreateReportingModel, identity_id: str, ) -> None: vendor_id = 100 subaccount_id = 200 virtual_participant = create_reporting_model( VirtualParticipant, name="Custom List Artist", spotify_id=None, ) create_reporting_model( CustomList, vendor_id=vendor_id, subaccount_id=subaccount_id, virtual_participant_id=virtual_participant.id, ) auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[Account(vendor_id=vendor_id, subaccount_id=subaccount_id)] ) response = handler.handle( GetArtistsV2Request( identity_id=identity_id, limit=50, offset=0, ) ) assert response.total == 1 assert len(response.items) == 1 assert response.items[0].virtual_participant_id == virtual_participant.id assert response.items[0].global_participant_id is None assert response.items[0].name == "Custom List Artist" @pytest.mark.db("reporting") def test_merged_virtual_and_global( self, handler: GetArtistsV2Handler, auth_service_mock: mock.MagicMock, create_reporting_model: CreateReportingModel, identity_id: str, faker: Faker, ) -> None: vendor_id = 100 subaccount_id = 200 shared_spotify_id = "6vWDO969PvNqNYHIOW5v0m" global_participant_id = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=global_participant_id, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=global_participant_id, global_participant_name="Released Artist", ) create_reporting_model( GlobalParticipantAccountDbt, id=global_participant_id, spotify_id=shared_spotify_id, vendor_id=vendor_id, subaccount_id=subaccount_id, ) virtual_participant = create_reporting_model( VirtualParticipant, name="Released Artist (pending)", spotify_id=shared_spotify_id, ) create_reporting_model( CustomList, vendor_id=vendor_id, subaccount_id=subaccount_id, virtual_participant_id=virtual_participant.id, ) auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[Account(vendor_id=vendor_id, subaccount_id=subaccount_id)] ) response = handler.handle( GetArtistsV2Request( identity_id=identity_id, limit=50, offset=0, ) ) assert response.total == 1 assert len(response.items) == 1 participant = response.items[0] assert participant.global_participant_id == global_participant_id assert participant.virtual_participant_id == virtual_participant.id assert participant.spotify_id == shared_spotify_id assert participant.name == "Released Artist" @pytest.mark.db("reporting") def test_search( self, handler: GetArtistsV2Handler, auth_service_mock: mock.MagicMock, create_reporting_model: CreateReportingModel, identity_id: str, faker: Faker, ) -> None: vendor_id = 100 subaccount_id = 200 gp1 = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=gp1, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=gp1, global_participant_name="Taylor Swift", ) create_reporting_model( GlobalParticipantAccountDbt, id=gp1, spotify_id="06HL4z0CvFAxyc27GXpf02", vendor_id=vendor_id, subaccount_id=subaccount_id, ) gp2 = faker.uuid4() create_reporting_model( ArtistRosterMainRep, vendor_id=vendor_id, subaccount_id=subaccount_id, global_participant_id=gp2, ) create_reporting_model( GlobalFansByArtistDbt, global_participant_id=gp2, global_participant_name="Ed Sheeran", ) create_reporting_model( GlobalParticipantAccountDbt, id=gp2, spotify_id="6eUKZXaKkcviH0Ku9w2n3V", vendor_id=vendor_id, subaccount_id=subaccount_id, ) auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[Account(vendor_id=vendor_id, subaccount_id=subaccount_id)] ) response = handler.handle( GetArtistsV2Request( identity_id=identity_id, limit=50, offset=0, search="Taylor", ) ) assert response.total == 1 assert response.items[0].name == "Taylor Swift"