22from unittest .mock import AsyncMock
33
44import pytest
5- from src .api .authentication_cache import reset_auth_cache
65from src .api .schemas .authorization_types import AgentexResource , AuthorizedOperationType
76from src .domain .services .authorization_service import AuthorizationService
87
@@ -24,53 +23,23 @@ def _service(principal_context, gateway):
2423 )
2524
2625
27- @pytest .mark .unit
28- @pytest .mark .asyncio
29- async def test_agent_authorization_check_uses_cache ():
30- await reset_auth_cache ()
31- try :
32- gateway = AsyncMock ()
33- gateway .check .return_value = True
34- service = _service ({"user_id" : "user-1" , "account_id" : "acct-1" }, gateway )
35-
36- assert (
37- await service .check (
38- AgentexResource .agent ("agent-1" ), AuthorizedOperationType .read
39- )
40- is True
41- )
42- assert (
43- await service .check (
44- AgentexResource .agent ("agent-1" ), AuthorizedOperationType .read
45- )
46- is True
47- )
48-
49- assert gateway .check .await_count == 1
50- finally :
51- await reset_auth_cache ()
52-
53-
5426@pytest .mark .unit
5527@pytest .mark .asyncio
5628@pytest .mark .parametrize (
5729 "resource" ,
5830 [
31+ AgentexResource .agent ("agent-1" ),
5932 AgentexResource .task ("task-1" ),
6033 AgentexResource .api_key ("api-key-1" ),
6134 AgentexResource .schedule ("agent-1/schedule-1" ),
6235 ],
6336)
64- async def test_subresource_authorization_checks_call_gateway_each_time (resource ):
65- await reset_auth_cache ()
66- try :
67- gateway = AsyncMock ()
68- gateway .check .return_value = True
69- service = _service ({"user_id" : "user-1" , "account_id" : "acct-1" }, gateway )
37+ async def test_authorization_checks_call_gateway_each_time (resource ):
38+ gateway = AsyncMock ()
39+ gateway .check .return_value = True
40+ service = _service ({"user_id" : "user-1" , "account_id" : "acct-1" }, gateway )
7041
71- assert await service .check (resource , AuthorizedOperationType .read ) is True
72- assert await service .check (resource , AuthorizedOperationType .read ) is True
42+ assert await service .check (resource , AuthorizedOperationType .read ) is True
43+ assert await service .check (resource , AuthorizedOperationType .read ) is True
7344
74- assert gateway .check .await_count == 2
75- finally :
76- await reset_auth_cache ()
45+ assert gateway .check .await_count == 2
0 commit comments