from typing import Any, Dict, Optional from uuid import UUID import pytest from pdp.fastapi.schemas.resource_attributes import ( IdentityAndTenantOwnedResourceAttributes, IdentityOwnedResourceAttributes, TenantOwnedResourceAttributes, ) from pdp.fastapi.schemas.tenant import Tenant @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param( { "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), } }, False, { "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), } }, id="valid tenant resource attributes", ), pytest.param( { "tenant": { "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, True, None, id="missing tenant type", ), pytest.param( {}, True, None, id="missing tenant key", ), pytest.param( { "other": "attributes", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, False, { "other": "attributes", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, id="Allows attributes in addition to tenant", ), ], ) def test_tenant_owned_resource_attributes( payload: Optional[Tenant], expect_exception: bool, expected: Dict[str, Any], ) -> None: """Test the TenantResourceAttributes model.""" if expect_exception: with pytest.raises(Exception): TenantOwnedResourceAttributes.model_validate(payload) else: actual = TenantOwnedResourceAttributes.model_validate(payload) assert actual == TenantOwnedResourceAttributes(**expected) @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param( {}, True, None, id="identity_uuid", ), pytest.param( { "other": "attributes", "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", }, False, { "other": "attributes", "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", }, id="Allows attributes in addition to identity_uuid", ), pytest.param( { "identity_uuid": "", }, True, { "identity_uuid": "", }, id="identity_uuid cannot be empty string", ), pytest.param( { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, False, { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, id="both identity_uuid and tenant can be provided at the same time", ), pytest.param( { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", }, False, { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", }, id="identity_uuid should be provided", ), ], ) def test_identity_owned_resource_attributes( payload: Optional[Dict[str, Any]], expect_exception: bool, expected: Dict[str, Any], ) -> None: """Test the IdentityOwnedResourceAttributes model.""" if expect_exception: with pytest.raises(Exception): IdentityOwnedResourceAttributes.model_validate(payload) else: actual = IdentityOwnedResourceAttributes.model_validate(payload) assert actual == IdentityOwnedResourceAttributes(**expected) @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param( { "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), } }, False, { "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), } }, id="valid tenant resource attributes", ), pytest.param( { "tenant": { "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, True, None, id="missing tenant type", ), pytest.param( {}, True, None, id="missing tenant and identity", ), pytest.param( { "other": "attributes", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, False, { "other": "attributes", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, id="Allows attributes in addition to tenant", ), pytest.param( { "identity_uuid": "", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, True, { "identity_uuid": "", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, id="identity_uuid cannot be empty string", ), pytest.param( { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, False, { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", "tenant": { "tenant_type": "account", "tenant_uuid": UUID("89b79b52-de18-4644-bd2b-bd28fdbeebaa"), }, }, id="both identity_uuid and tenant can be provided at the same time", ), pytest.param( { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", }, False, { "identity_uuid": "3fb72946-6b1a-4310-92a8-438a817c3c66", }, id="identity_uuid can be provided without tenant", ), ], ) def test_identity_and_tenant_owned_resource_attributes( payload: Optional[Dict[str, Any]], expect_exception: bool, expected: Dict[str, Any], ) -> None: """Test the IdentityOrTenantOwnedResourceAttributes model.""" if expect_exception: with pytest.raises(Exception): IdentityAndTenantOwnedResourceAttributes.model_validate(payload) else: actual = IdentityAndTenantOwnedResourceAttributes.model_validate(payload) assert actual == IdentityAndTenantOwnedResourceAttributes(**expected)