from urllib.parse import urljoin import pytest from fansifter_common.auth.account import Account from starlette.testclient import TestClient from dmp.config import Settings from dmp.songwhip.dtos import SongwhipCampaignType from dmp.songwhip.models import SongwhipCustomPageDbt, SongwhipPresavePageDbt from tests.unit.equals import IsISODatetime from tests.unit.types import CreateReportingModel @pytest.mark.db def test_get_campaigns_returns_presave_and_custom_pages( client: TestClient, create_reporting_model: CreateReportingModel, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) response = client.get( "/songwhip/campaigns", params={ "vendorId": account.vendor_id, "subaccountId": account.subaccount_id, }, ) assert response.status_code == 200 data = response.json() assert len(data) == 2 result_ids = {item["id"] for item in data} assert result_ids == {presave.id, custom.id} types_by_id = {item["id"]: item["type"] for item in data} assert types_by_id[presave.id] == SongwhipCampaignType.SONGWHIP_PRESAVE assert types_by_id[custom.id] == SongwhipCampaignType.SONGWHIP_EXCLUSIVE_CONTENT @pytest.mark.db def test_get_campaigns_response_schema( client: TestClient, create_reporting_model: CreateReportingModel, settings: Settings, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) response = client.get( "/songwhip/campaigns", params={ "vendorId": account.vendor_id, "subaccountId": account.subaccount_id, "globalParticipantIds": [presave.global_participant_id], }, ) assert response.status_code == 200 assert response.json() == [ { "id": presave.id, "type": SongwhipCampaignType.SONGWHIP_PRESAVE, "name": presave.name, "vendorId": account.vendor_id, "subaccountId": account.subaccount_id, "globalParticipantId": presave.global_participant_id, "createdAt": IsISODatetime(presave.created_at), "url": urljoin(settings.songwhip_url, presave.path), } ] @pytest.mark.db def test_get_campaigns_filters_by_campaign_ids( client: TestClient, create_reporting_model: CreateReportingModel, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) custom = create_reporting_model( SongwhipCustomPageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) response = client.get( "/songwhip/campaigns", params={"campaignIds": [presave.id, custom.id]}, ) assert response.status_code == 200 data = response.json() assert len(data) == 2 result_ids = {item["id"] for item in data} assert result_ids == {presave.id, custom.id} @pytest.mark.db def test_get_campaigns_filters_by_search( client: TestClient, create_reporting_model: CreateReportingModel, account: Account, ) -> None: presave = create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, name="My Artist Album", ) create_reporting_model( SongwhipPresavePageDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, name="Unrelated", ) response = client.get( "/songwhip/campaigns", params={ "vendorId": account.vendor_id, "subaccountId": account.subaccount_id, "search": "artist", }, ) assert response.status_code == 200 data = response.json() assert len(data) == 1 assert data[0]["id"] == presave.id