"""Test the POST /identity/admin/tenant-access endpoint.""" from collections import Counter import requests from tests.integration import config test_tenants = { 'tenants': [ # Thirty Tigers (21989) {'tenant_type': 'account', 'tenant_uuid': 'ec1fd7e2-9c95-4e09-a037-e924e0244283'}, # Black Jungle Records (88797) under Thirty Tigers {'tenant_type': 'subaccount', 'tenant_uuid': '908d75dd-54dd-11ef-abbe-120c1dfc8269'}, # label participant under thirty tigers {'tenant_type': 'label_participant', 'tenant_uuid': 'b5a0f9ac-c7d5-40f7-b11f-0d2a84e36a5b'}, # Lovestyle Records Kft (32496) {'tenant_type': 'account', 'tenant_uuid': '25364b17-36df-4ff8-96cd-65456778dac5'}, # Subaccount (7708) under MRI (21899) {'tenant_type': 'subaccount', 'tenant_uuid': 'b2b374e9-04e8-42fd-83ec-6513630c1826'}, # label participant under Subaccount (7708) {'tenant_type': 'label_participant', 'tenant_uuid': '84aa4aa8-d798-46fe-80d9-3154250f1a2b'}, # label participant under Columbia (34514) {'tenant_type': 'label_participant', 'tenant_uuid': 'a7adfa7e-1192-4d24-a20d-28d3e8cd7f6d'}, # collaborator under Lovestyle (32496) {'tenant_type': 'collaborator', 'tenant_uuid': 'e1f85616-b4f0-444f-9a8f-731218cfd450'}, # Test Collaborator under Platform Test Vendor {'tenant_type': 'collaborator', 'tenant_uuid': '03f7cf37-5136-4658-b924-f62efb2879fe'}, # label participant under MRI (21899) {'tenant_type': 'label_participant', 'tenant_uuid': 'f62d7c5b-1d8c-4c1f-b527-f21ea798d337'}, ] } def dict_to_sorted_tuple(d): """Convert a dictionary to a tuple of sorted key-value pairs.""" return tuple(sorted(d.items())) def test_post_check_admin_access_success_no_vendor_star( bearer_token_user_without_vendor_star, ) -> None: """POST /v2/identity/self/tenant-access without vendor star.""" response = requests.post( '{qa_base_url}/v2/identity/self/tenant-access'.format(qa_base_url=config.QA_BASE_URL), json=test_tenants, headers={'authorization': f'Bearer {bearer_token_user_without_vendor_star}'}, ) assert response.status_code == 200 tenants = response.json()['tenants'] tenants_as_tuples = [dict_to_sorted_tuple(d) for d in tenants] assert Counter(tenants_as_tuples) == Counter( [ dict_to_sorted_tuple(d) for d in [ { 'tenant_uuid': 'ec1fd7e2-9c95-4e09-a037-e924e0244283', 'tenant_type': 'account', 'access': True, }, { 'tenant_uuid': '25364b17-36df-4ff8-96cd-65456778dac5', 'tenant_type': 'account', 'access': False, }, { 'tenant_uuid': '908d75dd-54dd-11ef-abbe-120c1dfc8269', 'tenant_type': 'subaccount', 'access': True, }, { 'tenant_uuid': 'b2b374e9-04e8-42fd-83ec-6513630c1826', 'tenant_type': 'subaccount', 'access': True, }, { 'tenant_uuid': 'b5a0f9ac-c7d5-40f7-b11f-0d2a84e36a5b', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': '84aa4aa8-d798-46fe-80d9-3154250f1a2b', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': 'a7adfa7e-1192-4d24-a20d-28d3e8cd7f6d', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': 'f62d7c5b-1d8c-4c1f-b527-f21ea798d337', 'tenant_type': 'label_participant', 'access': False, }, { 'tenant_uuid': 'e1f85616-b4f0-444f-9a8f-731218cfd450', 'tenant_type': 'collaborator', 'access': False, }, { 'tenant_uuid': '03f7cf37-5136-4658-b924-f62efb2879fe', 'tenant_type': 'collaborator', 'access': True, }, ] ] ) def test_post_check_admin_access_success_vendor_star(bearer_token_user_with_vendor_star) -> None: """POST /v2/identity/self/tenant-access with vendor star.""" response = requests.post( '{qa_base_url}/v2/identity/self/tenant-access'.format(qa_base_url=config.QA_BASE_URL), json=test_tenants, headers={'authorization': f'Bearer {bearer_token_user_with_vendor_star}'}, ) assert response.status_code == 200 tenants = response.json()['tenants'] tenants_as_tuples = [dict_to_sorted_tuple(d) for d in tenants] assert Counter(tenants_as_tuples) == Counter( [ dict_to_sorted_tuple(d) for d in [ { 'tenant_uuid': 'ec1fd7e2-9c95-4e09-a037-e924e0244283', 'tenant_type': 'account', 'access': True, }, { 'tenant_uuid': '25364b17-36df-4ff8-96cd-65456778dac5', 'tenant_type': 'account', 'access': True, }, { 'tenant_uuid': '908d75dd-54dd-11ef-abbe-120c1dfc8269', 'tenant_type': 'subaccount', 'access': True, }, { 'tenant_uuid': 'b2b374e9-04e8-42fd-83ec-6513630c1826', 'tenant_type': 'subaccount', 'access': True, }, { 'tenant_uuid': 'b5a0f9ac-c7d5-40f7-b11f-0d2a84e36a5b', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': '84aa4aa8-d798-46fe-80d9-3154250f1a2b', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': 'a7adfa7e-1192-4d24-a20d-28d3e8cd7f6d', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': 'f62d7c5b-1d8c-4c1f-b527-f21ea798d337', 'tenant_type': 'label_participant', 'access': True, }, { 'tenant_uuid': 'e1f85616-b4f0-444f-9a8f-731218cfd450', 'tenant_type': 'collaborator', 'access': True, }, { 'tenant_uuid': '03f7cf37-5136-4658-b924-f62efb2879fe', 'tenant_type': 'collaborator', 'access': True, }, ] ] ) def test_post_check_admin_access_unauthorized() -> None: """Integration test for unauthorized POST /v2/identity/self/tenant-access.""" payload = {'tenants': [{'tenant_type': 'account', 'tenant_uuid': 'valid-tenant_uuid'}]} response = requests.post( '{qa_base_url}/v2/identity/self/tenant-access'.format(qa_base_url=config.QA_BASE_URL), json=payload, ) assert response.status_code == 401 def test_post_check_admin_access_forbidden(bearer_token_user_without_settings) -> None: """Integration test for forbidden POST /v2/identity/self/tenant-access.""" payload = {'tenants': [{'tenant_type': 'account', 'tenant_uuid': 'valid-tenant_uuid'}]} response = requests.post( '{qa_base_url}/v2/identity/self/tenant-access'.format(qa_base_url=config.QA_BASE_URL), json=payload, headers={'authorization': f'Bearer {bearer_token_user_without_settings}'}, ) assert response.status_code == 403