from unittest import mock import pytest from faker.proxy import Faker from fansifter_common.auth.account import Account, AccountAccess from fansifter_common.auth.types import Permission from dmp.artists.services import ArtistAccessService from dmp.rosters.constants import GLOBAL_FAN_DATA_LISTS_FEATURE_ID from dmp.rosters.exceptions import GlobalFanDataListNotAvailableError from dmp.rosters.models import ArtistRosterMainRep from tests.unit.types import CreateReportingModel class TestArtistAccessService: @pytest.mark.db def test_check_artist_access_for_account( self, service: ArtistAccessService, identity_id: str, faker: Faker ) -> None: account = Account(vendor_id=1, subaccount_id=1) global_participant_id = faker.uuid4() artist_access = service.check_artist_access( identity_id=identity_id, global_participant_id=global_participant_id, account=account, permission=Permission("test", "test"), ) assert artist_access.vendor_id == account.vendor_id assert artist_access.subaccount_id == account.subaccount_id assert artist_access.is_global is False @pytest.mark.db def test_check_artist_access_global( self, service: ArtistAccessService, create_reporting_model: CreateReportingModel, ows_account_client_mock: mock.MagicMock, auth_service_mock: mock.MagicMock, identity_id: str, account: Account, faker: Faker, ) -> None: global_participant_id = faker.uuid4() # Mocking the OwsAccountClient ows_account_client_mock.get_vendor_features.return_value = [ mock.Mock(feature_id=GLOBAL_FAN_DATA_LISTS_FEATURE_ID) ] # Create ArtistRosterMainRep create_reporting_model( ArtistRosterMainRep, global_participant_id=global_participant_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) auth_service_mock.authorize_for_permission.return_value = AccountAccess( accounts=[account] ) artist_access = service.check_artist_access( identity_id=identity_id, global_participant_id=global_participant_id, account=None, permission=Permission("test", "test"), ) assert artist_access.vendor_id is None assert artist_access.subaccount_id is None assert artist_access.is_global @pytest.mark.db def test_check_artist_access_global_not_available( self, service: ArtistAccessService, identity_id: str, faker: Faker, ) -> None: global_participant_id = faker.uuid4() with pytest.raises(GlobalFanDataListNotAvailableError): service.check_artist_access( identity_id=identity_id, global_participant_id=global_participant_id, account=None, permission=Permission("test", "test"), ) @pytest.mark.db def test_check_artist_access_global_no_reps( self, service: ArtistAccessService, ows_account_client_mock: mock.MagicMock, identity_id: str, faker: Faker, ) -> None: global_participant_id = faker.uuid4() # Mocking the OwsAccountClient ows_account_client_mock.get_vendor_features.return_value = [ mock.Mock(feature_id=GLOBAL_FAN_DATA_LISTS_FEATURE_ID) ] with pytest.raises(GlobalFanDataListNotAvailableError): service.check_artist_access( identity_id=identity_id, global_participant_id=global_participant_id, account=None, permission=Permission("test", "test"), )