from unittest import mock import pytest from fansifter_common.adapters.ows_account import Feature from fansifter_common.auth.account import Account from email_campaigns.rosters.constants import GLOBAL_FAN_DATA_LISTS_FEATURE_ID from email_campaigns.rosters.exceptions import MainRepArtistOnlyAllowedError from email_campaigns.rosters.models import ArtistRosterLocalRep, ArtistRosterMainRep from email_campaigns.rosters.services import FanDataListService from email_campaigns.rosters.types import FanDataListId from tests.unit.types import CreateModel class TestFanDataListService: def test_ensure_artist_access_allows_custom_list( self, service: FanDataListService ) -> None: service.ensure_artist_access( FanDataListId.from_custom_list_id("custom_list_id"), account=Account(vendor_id=1, subaccount_id=0), ) assert True def test_ensure_artist_access_when_feature_disabled( self, service: FanDataListService, ows_account_client_mock: mock.MagicMock ) -> None: ows_account_client_mock.get_vendor_features.return_value = [] service.ensure_artist_access( FanDataListId.from_artist_id("global_participant_id"), account=Account(vendor_id=1, subaccount_id=0), ) ows_account_client_mock.get_vendor_features.assert_called_once() @pytest.mark.db def test_ensure_artist_access_raises_when_not_main_rep( self, service: FanDataListService, ows_account_client_mock: mock.MagicMock ) -> None: ows_account_client_mock.get_vendor_features.return_value = [ Feature( feature_id=GLOBAL_FAN_DATA_LISTS_FEATURE_ID, feature_name="GLOBAL_FAN_DATA_LISTS_FEATURE_ID", ), ] with pytest.raises(MainRepArtistOnlyAllowedError): service.ensure_artist_access( FanDataListId.from_artist_id("global_participant_id"), account=Account(vendor_id=1, subaccount_id=0), ) @pytest.mark.db def test_ensure_artist_access_allows_main_rep( self, service: FanDataListService, ows_account_client_mock: mock.MagicMock, create_model: CreateModel, ) -> None: ows_account_client_mock.get_vendor_features.return_value = [ Feature( feature_id=GLOBAL_FAN_DATA_LISTS_FEATURE_ID, feature_name="GLOBAL_FAN_DATA_LISTS_FEATURE_ID", ), ] account = Account(vendor_id=1, subaccount_id=0) global_participant_id = "global_participant_id" create_model( ArtistRosterMainRep, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, ) service.ensure_artist_access( FanDataListId.from_artist_id(global_participant_id), account=account, ) assert True @pytest.mark.db def test_ensure_artist_access_allows_local_rep( self, service: FanDataListService, ows_account_client_mock: mock.MagicMock, create_model: CreateModel, ) -> None: ows_account_client_mock.get_vendor_features.return_value = [ Feature( feature_id=GLOBAL_FAN_DATA_LISTS_FEATURE_ID, feature_name="GLOBAL_FAN_DATA_LISTS_FEATURE_ID", ), ] account = Account(vendor_id=1, subaccount_id=0) global_participant_id = "global_participant_id" create_model( ArtistRosterLocalRep, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, country_code="DE", ) service.ensure_artist_access( FanDataListId.from_artist_id(global_participant_id), account=account, allow_local_rep=True, ) assert True @pytest.mark.db def test_ensure_artist_access_denies_local_rep_when_not_allowed( self, service: FanDataListService, ows_account_client_mock: mock.MagicMock, create_model: CreateModel, ) -> None: # Without allow_local_rep (the default, used by email campaigns), a local # rep must not gain access — only the main rep may. ows_account_client_mock.get_vendor_features.return_value = [ Feature( feature_id=GLOBAL_FAN_DATA_LISTS_FEATURE_ID, feature_name="GLOBAL_FAN_DATA_LISTS_FEATURE_ID", ), ] account = Account(vendor_id=1, subaccount_id=0) global_participant_id = "global_participant_id" create_model( ArtistRosterLocalRep, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, country_code="DE", ) with pytest.raises(MainRepArtistOnlyAllowedError): service.ensure_artist_access( FanDataListId.from_artist_id(global_participant_id), account=account, )