Skip to content

Commit 92cb252

Browse files
authored
✅ Re-add ready_middleware unit tests (openwallet-foundation#3330)
Signed-off-by: ff137 <[email protected]>
1 parent 13e4d66 commit 92cb252

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

acapy_agent/admin/tests/test_admin_server.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
from typing import Optional
44
from unittest import IsolatedAsyncioTestCase
55

6+
import jwt
67
import pytest
78
from aiohttp import ClientSession, DummyCookieJar, TCPConnector, web
89
from aiohttp.test_utils import unused_port
10+
from marshmallow import ValidationError
911

1012
from ...askar.profile import AskarProfile
1113
from ...config.default_context import DefaultContextBuilder
1214
from ...config.injection_context import InjectionContext
1315
from ...core.event_bus import Event
1416
from ...core.goal_code_registry import GoalCodeRegistry
1517
from ...core.protocol_registry import ProtocolRegistry
18+
from ...multitenant.error import MultitenantManagerError
1619
from ...storage.base import BaseStorage
20+
from ...storage.error import StorageNotFoundError
1721
from ...storage.record import StorageRecord
1822
from ...storage.type import RECORD_TYPE_ACAPY_UPGRADING
1923
from ...tests import mock
@@ -103,6 +107,160 @@ async def test_ready_middleware(self):
103107
with self.assertRaises(KeyError):
104108
await test_module.ready_middleware(request, handler)
105109

110+
async def test_ready_middleware_http_unauthorized(self):
111+
"""Test handling of web.HTTPUnauthorized and related exceptions."""
112+
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
113+
mock_logger.info = mock.MagicMock()
114+
115+
request = mock.MagicMock(
116+
method="GET",
117+
path="/unauthorized",
118+
app=mock.MagicMock(_state={"ready": True}),
119+
)
120+
121+
# Test web.HTTPUnauthorized
122+
handler = mock.CoroutineMock(
123+
side_effect=web.HTTPUnauthorized(reason="Unauthorized")
124+
)
125+
with self.assertRaises(web.HTTPUnauthorized):
126+
await test_module.ready_middleware(request, handler)
127+
mock_logger.info.assert_called_with(
128+
"Unauthorized access during %s %s: %s",
129+
request.method,
130+
request.path,
131+
handler.side_effect,
132+
)
133+
134+
# Test jwt.InvalidTokenError
135+
handler = mock.CoroutineMock(
136+
side_effect=jwt.InvalidTokenError("Invalid token")
137+
)
138+
with self.assertRaises(web.HTTPUnauthorized):
139+
await test_module.ready_middleware(request, handler)
140+
mock_logger.info.assert_called_with(
141+
"Unauthorized access during %s %s: %s",
142+
request.method,
143+
request.path,
144+
handler.side_effect,
145+
)
146+
147+
# Test InvalidTokenError
148+
handler = mock.CoroutineMock(
149+
side_effect=test_module.InvalidTokenError("Token error")
150+
)
151+
with self.assertRaises(web.HTTPUnauthorized):
152+
await test_module.ready_middleware(request, handler)
153+
mock_logger.info.assert_called_with(
154+
"Unauthorized access during %s %s: %s",
155+
request.method,
156+
request.path,
157+
handler.side_effect,
158+
)
159+
160+
async def test_ready_middleware_http_bad_request(self):
161+
"""Test handling of web.HTTPBadRequest and MultitenantManagerError."""
162+
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
163+
mock_logger.info = mock.MagicMock()
164+
165+
request = mock.MagicMock(
166+
method="POST",
167+
path="/bad-request",
168+
app=mock.MagicMock(_state={"ready": True}),
169+
)
170+
171+
# Test web.HTTPBadRequest
172+
handler = mock.CoroutineMock(
173+
side_effect=web.HTTPBadRequest(reason="Bad request")
174+
)
175+
with self.assertRaises(web.HTTPBadRequest):
176+
await test_module.ready_middleware(request, handler)
177+
mock_logger.info.assert_called_with(
178+
"Bad request during %s %s: %s",
179+
request.method,
180+
request.path,
181+
handler.side_effect,
182+
)
183+
184+
# Test MultitenantManagerError
185+
handler = mock.CoroutineMock(
186+
side_effect=MultitenantManagerError("Multitenant error")
187+
)
188+
with self.assertRaises(web.HTTPBadRequest):
189+
await test_module.ready_middleware(request, handler)
190+
mock_logger.info.assert_called_with(
191+
"Bad request during %s %s: %s",
192+
request.method,
193+
request.path,
194+
handler.side_effect,
195+
)
196+
197+
async def test_ready_middleware_http_not_found(self):
198+
"""Test handling of web.HTTPNotFound and StorageNotFoundError."""
199+
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
200+
mock_logger.info = mock.MagicMock()
201+
202+
request = mock.MagicMock(
203+
method="GET",
204+
path="/not-found",
205+
app=mock.MagicMock(_state={"ready": True}),
206+
)
207+
208+
# Test web.HTTPNotFound
209+
handler = mock.CoroutineMock(side_effect=web.HTTPNotFound(reason="Not found"))
210+
with self.assertRaises(web.HTTPNotFound):
211+
await test_module.ready_middleware(request, handler)
212+
mock_logger.info.assert_called_with(
213+
"Not Found error occurred during %s %s: %s",
214+
request.method,
215+
request.path,
216+
handler.side_effect,
217+
)
218+
219+
# Test StorageNotFoundError
220+
handler = mock.CoroutineMock(
221+
side_effect=StorageNotFoundError("Item not found")
222+
)
223+
with self.assertRaises(web.HTTPNotFound):
224+
await test_module.ready_middleware(request, handler)
225+
mock_logger.info.assert_called_with(
226+
"Not Found error occurred during %s %s: %s",
227+
request.method,
228+
request.path,
229+
handler.side_effect,
230+
)
231+
232+
async def test_ready_middleware_http_unprocessable_entity(self):
233+
"""Test handling of web.HTTPUnprocessableEntity with nested ValidationError."""
234+
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
235+
mock_logger.info = mock.MagicMock()
236+
# Mock the extract_validation_error_message function
237+
with mock.patch.object(
238+
test_module, "extract_validation_error_message"
239+
) as mock_extract:
240+
mock_extract.return_value = {"field": ["Invalid input"]}
241+
242+
request = mock.MagicMock(
243+
method="POST",
244+
path="/unprocessable",
245+
app=mock.MagicMock(_state={"ready": True}),
246+
)
247+
248+
# Create a HTTPUnprocessableEntity exception with a nested ValidationError
249+
validation_error = ValidationError({"field": ["Invalid input"]})
250+
http_error = web.HTTPUnprocessableEntity(reason="Unprocessable Entity")
251+
http_error.__cause__ = validation_error
252+
253+
handler = mock.CoroutineMock(side_effect=http_error)
254+
with self.assertRaises(web.HTTPUnprocessableEntity):
255+
await test_module.ready_middleware(request, handler)
256+
mock_extract.assert_called_once_with(http_error)
257+
mock_logger.info.assert_called_with(
258+
"Unprocessable Entity occurred during %s %s: %s",
259+
request.method,
260+
request.path,
261+
mock_extract.return_value,
262+
)
263+
106264
async def get_admin_server(
107265
self, settings: Optional[dict] = None, context: Optional[InjectionContext] = None
108266
) -> AdminServer:

0 commit comments

Comments
 (0)