"""Integration API tests for POST /artist/bulk-ensure. Hits the deployed QA ows-artist directly (the ows-grass proxy doesn't forward this route — same call pattern lambda-product-transfer/ensure-artists uses). Authentication is via the Grass-Account-Type / Grass-Account-Id headers that verify_grass_headers expects. Source artists are resolved from QA's artist_info table so the test stays valid as long as the source vendor has at least one artist row. bulk-ensure is idempotent at the destination vendor (upsert on name + vendor_id), so destination_artist_id values persist across runs and the mapping assertions deliberately check shape, not specific IDs. """ from os import getenv import pytest from tests.testutils.api_client.api_client import APIClient from tests.testutils.mysql_client.mysql_client import MySQLClient # Caller vendor — matches tests/integration/test_artists.py workstation_user. _GRASS_VENDOR_ID = 7123 _DESTINATION_VENDOR_ID = 7123 # Source vendor with known artist rows in QA artist_info. _SOURCE_VENDOR_ID = 6971 # An artist_id that definitely does not exist (exercises the 404 path). _UNKNOWN_ARTIST_ID = 999999999 @pytest.fixture def api_client(): return APIClient(getenv('BASE_ARTISTS_URL')) @pytest.fixture(scope='session') def source_artist_id(): """Pick an existing source artist from QA artist_info.""" mysql_client = MySQLClient() row = mysql_client.fetch_one( 'SELECT artist_id FROM artist_info WHERE vendor_id=%s LIMIT 1', (_SOURCE_VENDOR_ID,)) assert row, ( 'No artist found in QA artist_info for vendor_id {}'.format(_SOURCE_VENDOR_ID)) return row[0] def test_bulk_ensure_returns_mapping(source_artist_id, api_client): """Returns 200 with a source→destination artist_id mapping.""" res = api_client.bulk_ensure_artists( source_artist_ids=[source_artist_id], destination_vendor_id=_DESTINATION_VENDOR_ID, grass_vendor_id=_GRASS_VENDOR_ID) assert res.status_code == 200, \ 'Result of POST was {}, expected 200. Body: {}'.format(res.status_code, res.text) mapping = res.json().get('mapping') assert isinstance(mapping, dict), \ 'Expected dict mapping, got {!r}'.format(mapping) assert str(source_artist_id) in mapping, \ 'Expected source_artist_id {} in mapping keys, got {}'.format( source_artist_id, list(mapping.keys())) assert isinstance(mapping[str(source_artist_id)], int), \ 'Expected int destination_artist_id, got {!r}'.format( mapping[str(source_artist_id)]) def test_bulk_ensure_is_idempotent(source_artist_id, api_client): """Calling twice returns the same destination_artist_id (upsert).""" res1 = api_client.bulk_ensure_artists( source_artist_ids=[source_artist_id], destination_vendor_id=_DESTINATION_VENDOR_ID, grass_vendor_id=_GRASS_VENDOR_ID) res2 = api_client.bulk_ensure_artists( source_artist_ids=[source_artist_id], destination_vendor_id=_DESTINATION_VENDOR_ID, grass_vendor_id=_GRASS_VENDOR_ID) assert res1.status_code == 200, \ 'First call returned {}: {}'.format(res1.status_code, res1.text) assert res2.status_code == 200, \ 'Second call returned {}: {}'.format(res2.status_code, res2.text) assert res1.json().get('mapping') == res2.json().get('mapping'), \ 'Expected identical mappings across two calls, got {} vs {}'.format( res1.json().get('mapping'), res2.json().get('mapping')) def test_bulk_ensure_unknown_source_artist_returns_404(api_client): """Unknown source_artist_id propagates the 404 from fetch_artist_by_id.""" res = api_client.bulk_ensure_artists( source_artist_ids=[_UNKNOWN_ARTIST_ID], destination_vendor_id=_DESTINATION_VENDOR_ID, grass_vendor_id=_GRASS_VENDOR_ID) assert res.status_code == 404, \ 'Result of POST was {}, expected 404. Body: {}'.format(res.status_code, res.text) def test_bulk_ensure_missing_source_artist_ids_returns_400(api_client): """Missing source_artist_ids fails handler-level validation.""" res = api_client.bulk_ensure_artists( source_artist_ids=None, destination_vendor_id=_DESTINATION_VENDOR_ID, grass_vendor_id=_GRASS_VENDOR_ID) assert res.status_code == 400, \ 'Result of POST was {}, expected 400. Body: {}'.format(res.status_code, res.text) def test_bulk_ensure_missing_destination_vendor_id_returns_400(api_client, source_artist_id): """Missing destination_vendor_id fails handler-level validation.""" res = api_client.bulk_ensure_artists( source_artist_ids=[source_artist_id], destination_vendor_id=None, grass_vendor_id=_GRASS_VENDOR_ID) assert res.status_code == 400, \ 'Result of POST was {}, expected 400. Body: {}'.format(res.status_code, res.text)