from unittest import mock import pytest from anydi import Container from faker import Faker from fansifter_common.adapters.graphql_router import GlobalParticipant from fansifter_common.auth.account import Account from ows_text_campaigns.artist.models import ArtistPhoneNumber, ArtistSettings from ows_text_campaigns.campaigns.handlers import ( CreateCampaignHandler, CreateCampaignRequest, ) from ows_text_campaigns.rosters.exceptions import MainRepArtistOnlyAllowedError from ows_text_campaigns.rosters.services import GlobalFanDataAccessService from tests.unit.types import CreateModel class TestCreateCampaignHandler: @pytest.mark.db def test_create_campaign( self, handler: CreateCampaignHandler, identity_id: str, account: Account, graphql_router_client_mock: mock.MagicMock, create_model: CreateModel, faker: Faker, ) -> None: name = "Test Campaign" global_participant = GlobalParticipant(id=faker.uuid4(), name="Artist Name") create_model( ArtistSettings, global_participant_id=global_participant.id, phone_numbers=[ ArtistPhoneNumber( global_participant_id=global_participant.id, phone_number=faker.phone_number(), ) ], ) graphql_router_client_mock.get_global_participant_by_gp_id.return_value = ( global_participant ) campaign = handler.handle( CreateCampaignRequest( identity_id=identity_id, name=name, vendor_id=account.vendor_id, subaccount_id=0, global_participant_id=global_participant.id, audience_id=None, ) ) assert campaign.name == name assert campaign.vendor_id == account.vendor_id assert campaign.subaccount_id == account.subaccount_id assert campaign.global_participant_id == global_participant.id @pytest.mark.db def test_create_campaign_main_rep_required( self, container: Container, handler: CreateCampaignHandler, identity_id: str, account: Account, faker: Faker, ) -> None: global_participant_id = faker.uuid4() global_fandata_access_service_mock = mock.MagicMock( spec=GlobalFanDataAccessService, is_enabled_for_vendor=mock.MagicMock(return_value=True), ) with ( container.override( GlobalFanDataAccessService, instance=global_fandata_access_service_mock, ), pytest.raises(MainRepArtistOnlyAllowedError) as exc_info, ): handler.handle( CreateCampaignRequest( identity_id=identity_id, name=faker.name(), vendor_id=account.vendor_id, subaccount_id=0, global_participant_id=global_participant_id, audience_id=None, ) ) assert exc_info.value.message == "Main Rep Artist Only Allowed" assert exc_info.value.global_participant_id == global_participant_id