from typing import Any, Dict from uuid import UUID import pytest from pdp.constants.constants import TenantType from pdp.fastapi.schemas.identity import CheckResourcesResponse from pdp.fastapi.schemas.tenant import Tenant @pytest.mark.parametrize( "payload, expect_exception, expected", [ ({}, True, None), ({"resources": []}, True, None), ({"request_id": ""}, True, None), ( {"request_id": "", "resources": []}, False, {"request_id": "", "resources": []}, ), ( { "request_id": "correlation", "resources": [ { "resource": { "resource_id": 123, "resource_type": "identity", }, "action": "view", "effect": "allow", } ], }, False, { "request_id": "correlation", "resources": [ { "resource": { "resource_id": "123", "resource_type": "identity", "attributes": {}, }, "action": "view", "effect": "allow", "errors": {}, } ], }, ), ( { "request_id": "correlation2", "resources": [ { "resource": { "resource_id": 123, "resource_type": "identity", }, "action": "view", "effect": "deny", "errors": {"validation_errors": ["error1", "error2"]}, } ], }, False, { "request_id": "correlation2", "resources": [ { "resource": { "resource_id": "123", "resource_type": "identity", "attributes": {}, }, "action": "view", "effect": "deny", "errors": {"validation_errors": ["error1", "error2"]}, } ], }, ), ], ) def test_model_validate( payload: Any, expect_exception: bool, expected: Dict[str, Any] ) -> None: """Parametrized testing of CheckResourcesResponse schema.""" if expect_exception: with pytest.raises(Exception): CheckResourcesResponse.model_validate(payload) else: actual = CheckResourcesResponse.model_validate(payload) assert actual == CheckResourcesResponse(**expected) def test_filter_for_allowed_tenants() -> None: """Test CheckResourcesResponse filter_for_allowed_tenants.""" resources = [ { "resource": { "resource_id": 0, "resource_type": "identity", }, "action": "view", "effect": "allow", }, { "resource": { "resource_id": 0, "resource_type": "identity", }, "action": "view", "effect": "deny", }, { "resource": { "resource_id": 0, "resource_type": "identity", "attributes": { "tenant": { "tenant_type": "account", "tenant_uuid": "b6a95a16-3b14-481e-ba7e-a37a9f07f006", } }, }, "action": "duplicate", "effect": "allow", }, { "resource": { "resource_id": 0, "resource_type": "identity", "attributes": { "tenant": { "tenant_type": "account", "tenant_uuid": "b6a95a16-3b14-481e-ba7e-a37a9f07f006", } }, }, "action": "view", "effect": "allow", }, { "resource": { "resource_id": 0, "resource_type": "identity", "attributes": { "tenant": { "tenant_type": "account", "tenant_uuid": "74bc605a-e53e-11ee-b603-4a2888760682", } }, }, "action": "view", "effect": "deny", }, { "resource": { "resource_id": 0, "resource_type": "identity", "attributes": { "tenant": { "tenant_type": "subaccount", "tenant_uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", } }, }, "action": "view", "effect": "allow", }, { "resource": { "resource_id": 0, "resource_type": "identity", "attributes": { "tenant": { "tenant_type": "notlegit", "tenant_uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", } }, }, "action": "view", "effect": "allow", }, ] obj = CheckResourcesResponse( request_id="unique list of tenants returned", resources=resources, ) allowed_tenants = obj.filter_for_allowed_tenants() assert allowed_tenants == [ Tenant( tenant_type=TenantType.TENANT_TYPE_ACCOUNT, tenant_uuid=UUID("b6a95a16-3b14-481e-ba7e-a37a9f07f006"), ), Tenant( tenant_type=TenantType.TENANT_TYPE_SUBACCOUNT, tenant_uuid=UUID("fff741c2-6def-4493-bfdf-c2bcb1128e02"), ), ] def test_filter_for_allowed_tenants_empty() -> None: """Test CheckResourcesResponse filter_for_allowed_tenants on empty list.""" obj = CheckResourcesResponse( request_id="no allowed tenants", resources=[], ) allowed_tenants = obj.filter_for_allowed_tenants() assert allowed_tenants == []