from typing import Any from uuid import UUID import pytest from pdp.constants.constants import CacheEntryType from pdp.fastapi.schemas.cache import IdentityAllowedTenantsCacheObject from pdp.fastapi.schemas.get_allowed_tenants import GetAllowedTenantsResponse @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param({}, True, None, id="An empty payload raises an error"), pytest.param( {"identity_uuid": "INVALID"}, True, None, id="Invalid identity_uuid", ), pytest.param( { "identity_uuid": "INVALID", "resource_type": "account", "action": "view_account_info", }, True, None, id="Invalid identity_uuid", ), pytest.param( { "identity_uuid": "a9dd9b42-e53d-11ee-be6d-4a2888760682", "resource_type": "account", "action": "view_account_info", "get_allowed_tenants_response": { "tenants": [ { "tenant_uuid": "74e4aefa-d4a4-44f1-878f-1627612f0bb8", "tenant_type": "account", }, { "tenant_uuid": "INVALID", "tenant_type": "subaccount", }, ], "resource_type": "account", "action": "view_account_info", }, }, True, None, id="Invalid tenants", ), pytest.param( { "identity_uuid": "a9dd9b42-e53d-11ee-be6d-4a2888760682", "resource_type": "account", "action": "view_account_info", "get_allowed_tenants_response": { "tenants": [ { "tenant_uuid": "74e4aefa-d4a4-44f1-878f-1627612f0bb8", "tenant_type": "account", }, ], "resource_type": "account", "action": "view_account_info", }, }, False, { "identity_uuid": UUID("a9dd9b42-e53d-11ee-be6d-4a2888760682"), "resource_type": "account", "action": "view_account_info", "get_allowed_tenants_response": { "tenants": [ { "tenant_uuid": UUID("74e4aefa-d4a4-44f1-878f-1627612f0bb8"), "tenant_type": "account", }, ], "resource_type": "account", "action": "view_account_info", }, }, id="Valid payload", ), ], ) def test_identity_allowed_tenants_cache_object_validate( payload: Any, expect_exception: bool, expected: Any ) -> None: """Test IdentityAllowedTenantsCacheObject validation.""" if expect_exception: with pytest.raises(ValueError): IdentityAllowedTenantsCacheObject.model_validate(payload) else: assert IdentityAllowedTenantsCacheObject.model_validate( payload ) == IdentityAllowedTenantsCacheObject(**expected) @pytest.fixture() def cache_object() -> IdentityAllowedTenantsCacheObject: """Reusable IdentityAllowedTenantsCacheObject fixture.""" return IdentityAllowedTenantsCacheObject( identity_uuid="a9dd9b42-e53d-11ee-be6d-4a2888760682", resource_type="account", action="view_account_info", ) def test_identity_allowed_tenants_cache_object_to_key_vals_tuple( cache_object: IdentityAllowedTenantsCacheObject, ) -> None: """Test IdentityAllowedTenantsCacheObject.to_key_vals_tuple().""" assert cache_object.to_key_vals_tuple() == [ ("action", "view_account_info"), ("identity_uuid", "a9dd9b42-e53d-11ee-be6d-4a2888760682"), ("resource_type", "account"), ] def test_identity_allowed_tenants_cache_object_get_cache_entry_type( cache_object: IdentityAllowedTenantsCacheObject, ) -> None: """Test IdentityAllowedTenantsCacheObject.get_cache_entry_type().""" assert ( cache_object.get_cache_entry_type() == CacheEntryType.CACHE_ENTRY_IDENTITY_ALLOWED_TENANTS ) def test_identity_allowed_tenants_cache_object_to_cache_key( cache_object: IdentityAllowedTenantsCacheObject, ) -> None: """Test IdentityAllowedTenantsCacheObject.to_cache_key().""" assert ( cache_object.to_cache_key() == "allowed_tenants@action#view_account_info|identity_uuid#a9dd9b42-e53d-11ee-be6d-4a2888760682|resource_type#account" # noqa: E501 ) def test_identity_allowed_tenants_cache_object_get_ttl( cache_object: IdentityAllowedTenantsCacheObject, ) -> None: """Test IdentityAllowedTenantsCacheObject.get_ttl().""" assert cache_object.get_ttl() == 86400 def test_identity_allowed_tenants_cache_object_is_valid_to_save( cache_object: IdentityAllowedTenantsCacheObject, ) -> None: """Test IdentityAllowedTenantsCacheObject.is_valid_to_save().""" cache_object.get_allowed_tenants_response = GetAllowedTenantsResponse( resource_type=cache_object.resource_type, action=cache_object.action, tenants=[], ) assert cache_object.is_valid_to_save() def test_identity_allowed_tenants_cache_object_is_not_valid_to_save( cache_object: IdentityAllowedTenantsCacheObject, ) -> None: """Test IdentityAllowedTenantsCacheObject.is_valid_to_save() fails.""" assert not cache_object.is_valid_to_save()