from typing import Any import pytest from pdp.constants.constants import CacheEntryType from pdp.fastapi.schemas.cache import IdentityRolesResponseCacheObject from pdp.fastapi.schemas.identity import ImmutablePaginationCursor, RolesResponse @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param({}, True, None, id="An empty payload raises an error"), pytest.param( {"principal_identity_uuid": "INVALID", "listed_identity_uuid": "INVALID"}, True, None, id="Invalid identity_uuid", ), pytest.param( { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "INVALID", "cursor_param": "1", }, True, None, id="Valid identity_uuid but invalid listed_identity_uuid", ), pytest.param( { "principal_identity_uuid": "INVALID", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", }, True, None, id="Valid listed_identity_uuid but invalid identity_uuid", ), pytest.param( { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", }, False, { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", }, id="Valid payload", ), pytest.param( { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "45155372-baab-433f-9365-e6d69418108d", "cursor_param": "1", }, False, { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "45155372-baab-433f-9365-e6d69418108d", "cursor_param": "1", }, id="Valid payload where principal is different from listed identity", ), pytest.param( { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", "roles_response": { "tenants": { "74e4aefa-d4a4-44f1-878f-1627612f0bb8": { "tenant_uuid": "74e4aefa-d4a4-44f1-878f-1627612f0bb8", "tenant_type": "account", "roles": [{"role": "dog_pitcher"}], } }, }, }, False, { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", "roles_response": { "tenants": { "74e4aefa-d4a4-44f1-878f-1627612f0bb8": { "tenant_uuid": "74e4aefa-d4a4-44f1-878f-1627612f0bb8", "tenant_type": "account", "roles": [{"role": "dog_pitcher"}], } }, "cursor": ImmutablePaginationCursor(), "errors": {}, }, }, id="Valid payload with roles_response", ), pytest.param( { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", "roles_response": { "tenants": { "74e4aefa-d4a4-44f1-878f-1627612f0bb8": { "tenant_uuid": "74e4aefa-d4a4-44f1-878f-1627612f0bb8", "tenant_type": "account", "roles": [{"role": "dog_pitcher"}], } }, "cursor": {"cursor": "gibberish", "shorthand": "abbreviated"}, }, }, False, { "principal_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "listed_identity_uuid": "89b79b52-de18-4644-bd2b-bd28fdbeebaa", "cursor_param": "1", "roles_response": { "tenants": { "74e4aefa-d4a4-44f1-878f-1627612f0bb8": { "tenant_uuid": "74e4aefa-d4a4-44f1-878f-1627612f0bb8", "tenant_type": "account", "roles": [{"role": "dog_pitcher"}], } }, "cursor": {"cursor": "gibberish", "shorthand": "abbreviated"}, "errors": {}, }, }, id="Valid payload with roles_response including cursor", ), ], ) def test_identity_roles_response_cache_object_validate( payload: Any, expect_exception: bool, expected: Any ) -> None: """Test IdentityRolesResponseCacheObject validation.""" if expect_exception: with pytest.raises(ValueError): IdentityRolesResponseCacheObject.model_validate(payload) else: assert IdentityRolesResponseCacheObject.model_validate( payload ) == IdentityRolesResponseCacheObject(**expected) @pytest.fixture() def cache_object() -> IdentityRolesResponseCacheObject: """Reusable IdentityRolesResponseCacheObject fixture.""" return IdentityRolesResponseCacheObject( principal_identity_uuid="89b79b52-de18-4644-bd2b-bd28fdbeebaa", listed_identity_uuid="e4ad76b8-ac35-4e56-bb2a-275173ac107b", cursor="gibberish", ) def test_identity_roles_response_cache_object_to_key_vals_tuple( cache_object: IdentityRolesResponseCacheObject, ) -> None: """Test IdentityRolesResponseCacheObject.to_key_vals_tuple().""" assert cache_object.to_key_vals_tuple() == [ ("principal_identity_uuid", "89b79b52-de18-4644-bd2b-bd28fdbeebaa"), ("listed_identity_uuid", "e4ad76b8-ac35-4e56-bb2a-275173ac107b"), ("cursor", "gibberish"), ] def test_identity_roles_response_cache_object_get_cache_entry_type( cache_object: IdentityRolesResponseCacheObject, ) -> None: """Test IdentityRolesResponseCacheObject.get_cache_entry_type().""" assert ( cache_object.get_cache_entry_type() == CacheEntryType.CACHE_ENTRY_IDENTITY_ROLES ) def test_identity_roles_response_cache_object_to_cache_key( cache_object: IdentityRolesResponseCacheObject, ) -> None: """Test IdentityRolesResponseCacheObject.to_cache_key().""" assert ( cache_object.to_cache_key() == "list_tenant_roles@principal_identity_uuid#89b79b52-de18-4644-bd2b-bd28fdbeebaa|listed_identity_uuid#e4ad76b8-ac35-4e56-bb2a-275173ac107b|cursor#gibberish" # noqa: E501 ) def test_identity_roles_response_cache_object_get_ttl( cache_object: IdentityRolesResponseCacheObject, ) -> None: """Test IdentityRolesResponseCacheObject.get_ttl().""" assert cache_object.get_ttl() == 86400 def test_identity_roles_response_cache_object_is_valid_to_save( cache_object: IdentityRolesResponseCacheObject, ) -> None: """Test IdentityRolesResponseCacheObject.is_valid_to_save().""" cache_object.roles_response = RolesResponse() assert cache_object.is_valid_to_save() def test_identity_roles_response_cache_object_is_not_valid_to_save( cache_object: IdentityRolesResponseCacheObject, ) -> None: """Test IdentityRolesResponseCacheObject.is_valid_to_save() fails.""" assert not cache_object.is_valid_to_save()