"""Test ows-participant connector and types.""" from typing import Any, Optional from uuid import UUID import httpx import pytest from owsclient import AsyncOwsClient from owsclient.test import OwsClientMock from pdp.connectors.ows_participant import ( LookupParticipant, LookupParticipantsRequest, LookupParticipantsResponse, OwsParticipantClient, ) @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed."), ( { "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, True, None, "Missing uuid", ), ( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, LookupParticipant.model_validate( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", } ), "Should be a valid participant hierarchy object.", ), ( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "subaccount_uuid": "913223f2-1d45-11ef-8f44-4a2888760682", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, LookupParticipant.model_validate( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": None, "subaccount_uuid": "913223f2-1d45-11ef-8f44-4a2888760682", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", } ), "Missing vendor_uuid should return None.", ), ( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, LookupParticipant.model_validate( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", } ), "Missing subaccount_uuid should return None.", ), ( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": "913223f2-1d45-11ef-8f44-4a2888760682", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, LookupParticipant.model_validate( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": "913223f2-1d45-11ef-8f44-4a2888760682", "company_brand_uuid": None, "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", } ), "Missing company_brand_uuid should return None.", ), ( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": "913223f2-1d45-11ef-8f44-4a2888760682", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": None, }, False, LookupParticipant.model_validate( { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": "913223f2-1d45-11ef-8f44-4a2888760682", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": None, } ), "Missing (or 'None') parent_company_uuid should return None.", ), ( { "uuid": "no.such.uuid", }, True, None, "Should fail due to an invalid UUID.", ), ], ) def test_ows_participant_lookup_model_validate( payload: Any, expect_exception: bool, expected: Optional[LookupParticipant], description: str, ) -> None: """Test the LookupParticipant model.""" if expect_exception: with pytest.raises(Exception): LookupParticipant.model_validate(payload) else: assert LookupParticipant.model_validate(payload) == expected, description @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed."), ( {"uuids": []}, False, LookupParticipantsRequest.model_validate({"uuids": []}), "An empty uuids list is allowed", ), ( { "uuids": [ "fce419d8-1df0-11ef-9266-4a2888760682", "1cb3117d-d468-4fab-8e16-f7ae11649256", ] }, False, LookupParticipantsRequest.model_validate( { "uuids": [ "fce419d8-1df0-11ef-9266-4a2888760682", "1cb3117d-d468-4fab-8e16-f7ae11649256", ] } ), "Should be a valid request object", ), ], ) def test_lookup_participant_request( payload: Any, expect_exception: bool, expected: Optional[LookupParticipantsRequest], description: str, ) -> None: """Test the LookupParticipantsRequest model.""" if expect_exception: with pytest.raises(Exception): LookupParticipantsRequest.model_validate(payload) else: assert LookupParticipantsRequest.model_validate(payload) == expected, ( description ) @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed."), ( {"label_participants": []}, False, LookupParticipantsResponse.model_validate({"label_participants": []}), "An empty label_participants list is allowed", ), ( { "label_participants": [ {"uuid": "fce419d8-1df0-11ef-9266-4a2888760682"}, { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, ] }, False, LookupParticipantsResponse.model_validate( { "label_participants": [ {"uuid": "fce419d8-1df0-11ef-9266-4a2888760682"}, { "uuid": "54381846-a817-4ef4-9e58-7eae15bcf1dd", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", # noqa "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", # noqa }, ] } ), "Should be a valid request object", ), ], ) def test_lookup_participant_response( payload: Any, expect_exception: bool, expected: Optional[LookupParticipantsResponse], description: str, ) -> None: """Test the LookupParticipantsRequest model.""" if expect_exception: with pytest.raises(Exception): LookupParticipantsResponse.model_validate(payload) else: assert LookupParticipantsResponse.model_validate(payload) == expected, ( description ) @pytest.fixture def ows_participant_client( mock_async_ows_client: AsyncOwsClient, ) -> OwsParticipantClient: """Create a mock participant client.""" return OwsParticipantClient(async_ows_client=mock_async_ows_client) async def test_lookup_participants_by_uuids( ows_participant_client: OwsParticipantClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_participants_by_uuids.""" uuids = [ UUID("a689b5f1-ad4b-4a4a-8030-90c9e387f22f"), UUID("6fbb8f0a-1e9a-11ef-9249-4a2888760682"), UUID("c640f3b0-5a8f-488b-a482-9b1658b8f8e4"), ] ows_client_mock.post( "ows-participant", path="/lookup/label-participants/uuids/tenant-hierarchy/", json={"uuids": [str(_uuid) for _uuid in uuids]}, ).mock( return_value=httpx.Response( 200, json={ "label_participants": [ None, { "uuid": "6fbb8f0a-1e9a-11ef-9249-4a2888760682", "vendor_uuid": "0757c0f1-4bcb-41ef-b8e0-1e980effe998", "subaccount_uuid": None, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, { "uuid": "c640f3b0-5a8f-488b-a482-9b1658b8f8e4", "vendor_uuid": "b40430ee-4acb-4c14-b3ef-35b3c01869c7", "subaccount_uuid": "184f1303-0c64-4d90-9715-6838e5cded31", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, ] }, ) ) result = await ows_participant_client.lookup_participants_by_uuids(uuids=uuids) assert result assert result == LookupParticipantsResponse( label_participants=[ LookupParticipant( uuid="6fbb8f0a-1e9a-11ef-9249-4a2888760682", vendor_uuid="0757c0f1-4bcb-41ef-b8e0-1e980effe998", subaccount_uuid=None, company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), LookupParticipant( uuid="c640f3b0-5a8f-488b-a482-9b1658b8f8e4", vendor_uuid="b40430ee-4acb-4c14-b3ef-35b3c01869c7", subaccount_uuid="184f1303-0c64-4d90-9715-6838e5cded31", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), ] ) async def test_lookup_participants_by_uuids_empty_list( ows_participant_client: OwsParticipantClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_participants_by_uuids with an empty list.""" result = await ows_participant_client.lookup_participants_by_uuids(uuids=[]) assert result assert result == LookupParticipantsResponse(label_participants=[]) async def test_lookup_participants_by_uuids_failure( ows_participant_client: OwsParticipantClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_participants_by_uuids call with 400 response raises exception.""" ows_client_mock.post( "ows-participant", path="/lookup/label-participants/uuids/tenant-hierarchy/", json={"uuids": ["c640f3b0-5a8f-488b-a482-9b1658b8f8e4"]}, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_participant_client.lookup_participants_by_uuids( uuids=[UUID("c640f3b0-5a8f-488b-a482-9b1658b8f8e4")] )