"""Test auth utils.""" import uuid from typing import Optional import pytest from pdp.fastapi.schemas.identity import Resource, TenantRoles from pdp.utils.auth import ( build_identity_resource_from_tenant_role, get_tenant_uuid_from_identity_resource_id, ) @pytest.fixture() def tenant_role() -> TenantRoles: """Return a reusable tenant_role.""" return TenantRoles( tenant_type="account", tenant_uuid="86b60b84-70d2-4cae-9215-cae20bc329db", roles=[], ) def test_build_identity_resource_from_tenant_role( tenant_role: TenantRoles, ) -> None: """Test build_identity_resource_from_tenant_role.""" actual = build_identity_resource_from_tenant_role( uuid.UUID("9399f80e-f210-431e-bcb4-c91e91503380"), tenant_role, ) assert actual == Resource( resource_id="9399f80e-f210-431e-bcb4-c91e91503380#86b60b84-70d2-4cae-9215-cae20bc329db", # noqa: E501 resource_type="identity", attributes={ "tenant": { "tenant_uuid": "86b60b84-70d2-4cae-9215-cae20bc329db", "tenant_type": "account", }, "identity_uuid": "9399f80e-f210-431e-bcb4-c91e91503380", }, ) @pytest.mark.parametrize( "resource_id, expect_exception, expected_tenant_uuid, description", [ ("", True, None, "Empty string has no tenant uuid"), ("identity", True, None, "Just an identity has no tenant uuid"), ("identity#", True, None, "Just an identity with delimiter has no tenant uuid"), ("identity#tenant", True, None, "Tenant uuid actually needs to be a uuid"), ( "identity#c2026d6c-3f4e-48aa-85bb-93f0605af385", False, uuid.UUID("c2026d6c-3f4e-48aa-85bb-93f0605af385"), "This has a tenant_uuid", ), ], ) def test_get_tenant_uuid_from_identity_resource_id( resource_id: str, expect_exception: bool, expected_tenant_uuid: Optional[uuid.UUID], description: str, ) -> None: """Test get_tenant_uuid_from_identity_resource_id.""" if expect_exception: with pytest.raises(Exception): get_tenant_uuid_from_identity_resource_id(resource_id) else: actual = get_tenant_uuid_from_identity_resource_id(resource_id) assert actual == expected_tenant_uuid, description