Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions agentex/tests/unit/api/test_deployments_authz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Tests for deployment route authorization."""

from __future__ import annotations

from inspect import signature
from unittest.mock import AsyncMock, MagicMock

import pytest
from src.api.routes.deployments import list_deployments
from src.api.schemas.authorization_types import (
AgentexResource,
AgentexResourceType,
AuthorizedOperationType,
)


def _dep_callable(annotation):
"""Pull the FastAPI dependency function out of an ``Annotated[str, Depends(...)]``."""
return annotation.__metadata__[0].dependency


@pytest.mark.unit
@pytest.mark.asyncio
class TestListDeploymentsAuthz:
async def test_agent_id_requires_agent_read_authorization(self):
annotation = signature(list_deployments).parameters["agent_id"].annotation
dep = _dep_callable(annotation)

authorization = MagicMock()
authorization.check = AsyncMock(return_value=True)

result = await dep(
authorization,
MagicMock(),
MagicMock(),
MagicMock(),
"agent-1",
)

assert result == "agent-1"
authorization.check.assert_awaited_once_with(
resource=AgentexResource(
type=AgentexResourceType.agent,
selector="agent-1",
),
operation=AuthorizedOperationType.read,
)
Loading