"""Integration test for POST /subaccounts/applications/dataloader endpoint.""" import pytest import requests from tests.integration.consts.api import BULK_SUBACCOUNT_APPLICATIONS from tests.integration.consts.subaccounts import ( AMERICAN_RECORDINGS_USA_UUID, APEX_MUSIC_UUID, BOOSWEET_RECORDS_UUID, MUSHROOM_RECORDS_INTERNATIONAL_UUID, ) @pytest.mark.integration def test_post_bulk_subaccount_applications_success_with_all_access(bearer_token_with_vendor_star): """Test POST /subaccounts/applications/dataloader endpoint with valid subaccount UUIDs.""" # Test with known subaccount UUIDs payload = {'subaccount_uuids': [BOOSWEET_RECORDS_UUID, APEX_MUSIC_UUID]} headers = { 'Authorization': f'Bearer {bearer_token_with_vendor_star}', 'Content-Type': 'application/json', } response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 200 response_data = response.json() assert 'subaccounts' in response_data assert response_data['subaccounts'] == [ { 'uuid': BOOSWEET_RECORDS_UUID, 'applications': [ { 'application_id': 'CUSTOMER_ACCOUNTING_APP', 'roles': ['CUSTOMER_ACCOUNTING_BASE_ROLE'], 'url': 'https://accounting.theorchard.com', }, { 'application_id': 'INSIGHTS_APP', 'roles': ['INSIGHTS_BASE_ROLE'], 'url': 'https://insights.theorchard.com', }, { 'application_id': 'WORKSTATION_ANALYTICS_APP', 'roles': ['WORKSTATION_ANALYTICS_BASE_ROLE'], 'url': 'https://workstation.theorchard.com/analytics', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE', 'WORKSTATION_ADMIN_ROLE'], 'url': 'https://settings.theorchard.com', }, ], }, { 'uuid': APEX_MUSIC_UUID, 'applications': [ { 'application_id': 'CUSTOMER_ACCOUNTING_APP', 'roles': ['CUSTOMER_ACCOUNTING_BASE_ROLE'], 'url': 'https://accounting.theorchard.com', }, { 'application_id': 'INSIGHTS_APP', 'roles': ['INSIGHTS_BASE_ROLE'], 'url': 'https://insights.theorchard.com', }, { 'application_id': 'WORKSTATION_ANALYTICS_APP', 'roles': ['WORKSTATION_ANALYTICS_BASE_ROLE'], 'url': 'https://workstation.theorchard.com/analytics', }, { 'application_id': 'WORKSTATION_CATALOG_APP', 'roles': ['WORKSTATION_CATALOG_ROLE'], 'url': 'https://workstation.theorchard.com/catalog', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE', 'WORKSTATION_ADMIN_ROLE'], 'url': 'https://settings.theorchard.com', }, ], }, ] @pytest.mark.integration def test_post_bulk_subaccount_applications_success_without_all_access( bearer_token_without_vendor_star, ): """Test POST /subaccounts/applications/dataloader endpoint with valid subaccount UUIDs.""" # Test with known subaccount UUIDs payload = { 'subaccount_uuids': [MUSHROOM_RECORDS_INTERNATIONAL_UUID, AMERICAN_RECORDINGS_USA_UUID] } headers = { 'Authorization': f'Bearer {bearer_token_without_vendor_star}', 'Content-Type': 'application/json', } response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 200 response_data = response.json() assert 'subaccounts' in response_data assert response_data['subaccounts'] == [ { 'uuid': '06eb2a6e-a595-11ef-b6a8-0affe2ca6553', 'applications': [ { 'application_id': 'INSIGHTS_APP', 'roles': ['INSIGHTS_BASE_ROLE'], 'url': 'https://insights.sonymusic.com', }, { 'application_id': 'WORKSTATION_CATALOG_APP', 'roles': ['WORKSTATION_CATALOG_ROLE'], 'url': 'https://workstation.sonymusic.com/catalog', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE', 'WORKSTATION_ADMIN_ROLE'], 'url': 'https://settings.sonymusic.com', }, ], }, { 'uuid': 'b7bfda0f-a599-11ef-b6a8-0affe2ca6553', 'applications': [ { 'application_id': 'INSIGHTS_APP', 'roles': ['INSIGHTS_BASE_ROLE'], 'url': 'https://insights.sonymusic.com', }, { 'application_id': 'WORKSTATION_CATALOG_APP', 'roles': ['WORKSTATION_CATALOG_ROLE'], 'url': 'https://workstation.sonymusic.com/catalog', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE', 'WORKSTATION_ADMIN_ROLE'], 'url': 'https://settings.sonymusic.com', }, ], }, ] @pytest.mark.integration @pytest.mark.parametrize( 'payload', [ pytest.param({}, id='missing subaccount_uuids key'), pytest.param([], id='empty list'), pytest.param('a string', id='string instead of dict'), pytest.param(True, id='bool instead of dict'), pytest.param( {'subaccount_uuids': ['not-a-uuid', 'also-not-a-uuid']}, id='invalid UUID values' ), pytest.param({'subaccount_uuids': 'not a list'}, id='non-list payload'), ], ) def test_post_bulk_subaccount_applications_invalid_payload(bearer_token_with_vendor_star, payload): """Test POST /subaccounts/applications/dataloader endpoint with invalid payload.""" headers = { 'Authorization': f'Bearer {bearer_token_with_vendor_star}', 'Content-Type': 'application/json', } response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 400 @pytest.mark.integration def test_post_bulk_subaccount_applications_unauthorized(bearer_token_without_vendor_star): """Test POST /subaccounts/applications/dataloader endpoint without proper authorization.""" headers = { 'Authorization': f'Bearer {bearer_token_without_vendor_star}', 'Content-Type': 'application/json', } payload = {'subaccount_uuids': [BOOSWEET_RECORDS_UUID]} response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 403 # Test with invalid token headers = { 'Authorization': 'Bearer invalid_token', 'Content-Type': 'application/json', } response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 401 @pytest.mark.integration def test_post_bulk_subaccount_applications_subaccount_not_found(bearer_token_with_vendor_star): """Test POST /subaccounts/applications/dataloader endpoint with subaccount UUID that doesn't exist.""" payload = {'subaccount_uuids': ['00000000-0000-0000-0000-000000000000']} headers = { 'Authorization': f'Bearer {bearer_token_with_vendor_star}', 'Content-Type': 'application/json', } response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 200 response_data = response.json() assert response_data == {'subaccounts': [None]} @pytest.mark.integration def test_post_bulk_subaccount_applications_mixed_access(bearer_token_without_vendor_star): """Test POST /subaccounts/applications/dataloader endpoint with accessible and inaccessible subaccount UUIDs.""" # Mix of accessible and inaccessible subaccount UUIDs payload = { 'subaccount_uuids': [ MUSHROOM_RECORDS_INTERNATIONAL_UUID, # inaccessible BOOSWEET_RECORDS_UUID, # accessible ] } headers = { 'Authorization': f'Bearer {bearer_token_without_vendor_star}', 'Content-Type': 'application/json', } response = requests.post(BULK_SUBACCOUNT_APPLICATIONS, json=payload, headers=headers) assert response.status_code == 403 response_data = response.json() assert response_data['code'] == 'authorization_error' assert response_data['message'] == 'Unauthorized to access one or more tenants.'