|
1 | 1 | from unittest.mock import AsyncMock, patch |
2 | 2 | from uuid import uuid4 |
3 | 3 |
|
4 | | -from fastapi import FastAPI, Request |
| 4 | +from fastapi import FastAPI, HTTPException, Request |
| 5 | +from fastapi.testclient import TestClient |
| 6 | +import pytest |
5 | 7 |
|
6 | 8 | from oss.src.apis.fastapi.sessions.models import SessionInteractionTransitionRequest |
7 | 9 | from oss.src.apis.fastapi.sessions.router import InteractionsRouter |
@@ -34,6 +36,17 @@ async def test_transition_route_passes_resolution_to_the_domain_transition(): |
34 | 36 | captured = [] |
35 | 37 |
|
36 | 38 | class _InteractionsService: |
| 39 | + async def query_interactions(self, *, project_id, query): |
| 40 | + return [ |
| 41 | + SessionInteraction( |
| 42 | + project_id=project_id, |
| 43 | + session_id=query.session_id, |
| 44 | + token="approval-token", |
| 45 | + kind=SessionInteractionKind.user_approval, |
| 46 | + status=SessionInteractionStatus.pending, |
| 47 | + ) |
| 48 | + ] |
| 49 | + |
37 | 50 | async def transition_interaction(self, *, transition): |
38 | 51 | captured.append(transition) |
39 | 52 | return SessionInteraction( |
@@ -75,3 +88,80 @@ async def transition_interaction(self, *, transition): |
75 | 88 | assert response.interaction is not None |
76 | 89 | assert response.interaction.data is not None |
77 | 90 | assert response.interaction.data.resolution == captured[0].resolution |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "payload", |
| 95 | + [ |
| 96 | + { |
| 97 | + "session_id": "session-1", |
| 98 | + "token": "approval-token", |
| 99 | + "status": "pending", |
| 100 | + "resolution": {"verdict": "approved", "tool_call_id": "tool-1"}, |
| 101 | + }, |
| 102 | + { |
| 103 | + "session_id": "session-1", |
| 104 | + "token": "approval-token", |
| 105 | + "status": "resolved", |
| 106 | + "resolution": {"verdict": "maybe", "tool_call_id": "tool-1"}, |
| 107 | + }, |
| 108 | + { |
| 109 | + "session_id": "session-1", |
| 110 | + "token": "approval-token", |
| 111 | + "status": "resolved", |
| 112 | + "resolution": {"verdict": "approved"}, |
| 113 | + }, |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_transition_route_rejects_invalid_approval_resolution_with_422(payload): |
| 117 | + router = InteractionsRouter( |
| 118 | + interactions_service=AsyncMock(), |
| 119 | + workflows_service=AsyncMock(), |
| 120 | + respond_task=AsyncMock(), |
| 121 | + ) |
| 122 | + app = FastAPI() |
| 123 | + app.include_router(router.router) |
| 124 | + |
| 125 | + response = TestClient(app).post("/transition", json=payload) |
| 126 | + |
| 127 | + assert response.status_code == 422 |
| 128 | + |
| 129 | + |
| 130 | +async def test_transition_route_rejects_resolution_for_client_tool_with_409(): |
| 131 | + project_id = uuid4() |
| 132 | + user_id = uuid4() |
| 133 | + interactions_service = AsyncMock() |
| 134 | + interactions_service.query_interactions.return_value = [ |
| 135 | + SessionInteraction( |
| 136 | + project_id=project_id, |
| 137 | + session_id="session-1", |
| 138 | + token="client-tool-token", |
| 139 | + kind=SessionInteractionKind.client_tool, |
| 140 | + status=SessionInteractionStatus.pending, |
| 141 | + ) |
| 142 | + ] |
| 143 | + router = InteractionsRouter( |
| 144 | + interactions_service=interactions_service, |
| 145 | + workflows_service=AsyncMock(), |
| 146 | + respond_task=AsyncMock(), |
| 147 | + ) |
| 148 | + body = SessionInteractionTransitionRequest( |
| 149 | + session_id="session-1", |
| 150 | + token="client-tool-token", |
| 151 | + status=SessionInteractionStatus.resolved, |
| 152 | + resolution={"verdict": "approved", "tool_call_id": "tool-1"}, |
| 153 | + ) |
| 154 | + |
| 155 | + with patch( |
| 156 | + "oss.src.apis.fastapi.sessions.router.check_action_access", |
| 157 | + new_callable=AsyncMock, |
| 158 | + return_value=True, |
| 159 | + ): |
| 160 | + with pytest.raises(HTTPException) as caught: |
| 161 | + await router.transition_interaction( |
| 162 | + request=_make_authed_request(FastAPI(), project_id, user_id), |
| 163 | + body=body, |
| 164 | + ) |
| 165 | + |
| 166 | + assert caught.value.status_code == 409 |
| 167 | + interactions_service.transition_interaction.assert_not_awaited() |
0 commit comments