|
3 | 3 | from typing import Optional |
4 | 4 | from unittest import IsolatedAsyncioTestCase |
5 | 5 |
|
| 6 | +import jwt |
6 | 7 | import pytest |
7 | 8 | from aiohttp import ClientSession, DummyCookieJar, TCPConnector, web |
8 | 9 | from aiohttp.test_utils import unused_port |
| 10 | +from marshmallow import ValidationError |
9 | 11 |
|
10 | 12 | from acapy_agent.tests import mock |
11 | 13 | from acapy_agent.wallet import singletons |
|
16 | 18 | from ...core.goal_code_registry import GoalCodeRegistry |
17 | 19 | from ...core.in_memory import InMemoryProfile |
18 | 20 | from ...core.protocol_registry import ProtocolRegistry |
| 21 | +from ...multitenant.error import MultitenantManagerError |
19 | 22 | from ...storage.base import BaseStorage |
| 23 | +from ...storage.error import StorageNotFoundError |
20 | 24 | from ...storage.record import StorageRecord |
21 | 25 | from ...storage.type import RECORD_TYPE_ACAPY_UPGRADING |
22 | 26 | from ...utils.stats import Collector |
@@ -108,6 +112,160 @@ async def test_ready_middleware(self): |
108 | 112 | with self.assertRaises(KeyError): |
109 | 113 | await test_module.ready_middleware(request, handler) |
110 | 114 |
|
| 115 | + async def test_ready_middleware_http_unauthorized(self): |
| 116 | + """Test handling of web.HTTPUnauthorized and related exceptions.""" |
| 117 | + with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger: |
| 118 | + mock_logger.info = mock.MagicMock() |
| 119 | + |
| 120 | + request = mock.MagicMock( |
| 121 | + method="GET", |
| 122 | + path="/unauthorized", |
| 123 | + app=mock.MagicMock(_state={"ready": True}), |
| 124 | + ) |
| 125 | + |
| 126 | + # Test web.HTTPUnauthorized |
| 127 | + handler = mock.CoroutineMock( |
| 128 | + side_effect=web.HTTPUnauthorized(reason="Unauthorized") |
| 129 | + ) |
| 130 | + with self.assertRaises(web.HTTPUnauthorized): |
| 131 | + await test_module.ready_middleware(request, handler) |
| 132 | + mock_logger.info.assert_called_with( |
| 133 | + "Unauthorized access during %s %s: %s", |
| 134 | + request.method, |
| 135 | + request.path, |
| 136 | + handler.side_effect, |
| 137 | + ) |
| 138 | + |
| 139 | + # Test jwt.InvalidTokenError |
| 140 | + handler = mock.CoroutineMock( |
| 141 | + side_effect=jwt.InvalidTokenError("Invalid token") |
| 142 | + ) |
| 143 | + with self.assertRaises(web.HTTPUnauthorized): |
| 144 | + await test_module.ready_middleware(request, handler) |
| 145 | + mock_logger.info.assert_called_with( |
| 146 | + "Unauthorized access during %s %s: %s", |
| 147 | + request.method, |
| 148 | + request.path, |
| 149 | + handler.side_effect, |
| 150 | + ) |
| 151 | + |
| 152 | + # Test InvalidTokenError |
| 153 | + handler = mock.CoroutineMock( |
| 154 | + side_effect=test_module.InvalidTokenError("Token error") |
| 155 | + ) |
| 156 | + with self.assertRaises(web.HTTPUnauthorized): |
| 157 | + await test_module.ready_middleware(request, handler) |
| 158 | + mock_logger.info.assert_called_with( |
| 159 | + "Unauthorized access during %s %s: %s", |
| 160 | + request.method, |
| 161 | + request.path, |
| 162 | + handler.side_effect, |
| 163 | + ) |
| 164 | + |
| 165 | + async def test_ready_middleware_http_bad_request(self): |
| 166 | + """Test handling of web.HTTPBadRequest and MultitenantManagerError.""" |
| 167 | + with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger: |
| 168 | + mock_logger.info = mock.MagicMock() |
| 169 | + |
| 170 | + request = mock.MagicMock( |
| 171 | + method="POST", |
| 172 | + path="/bad-request", |
| 173 | + app=mock.MagicMock(_state={"ready": True}), |
| 174 | + ) |
| 175 | + |
| 176 | + # Test web.HTTPBadRequest |
| 177 | + handler = mock.CoroutineMock( |
| 178 | + side_effect=web.HTTPBadRequest(reason="Bad request") |
| 179 | + ) |
| 180 | + with self.assertRaises(web.HTTPBadRequest): |
| 181 | + await test_module.ready_middleware(request, handler) |
| 182 | + mock_logger.info.assert_called_with( |
| 183 | + "Bad request during %s %s: %s", |
| 184 | + request.method, |
| 185 | + request.path, |
| 186 | + handler.side_effect, |
| 187 | + ) |
| 188 | + |
| 189 | + # Test MultitenantManagerError |
| 190 | + handler = mock.CoroutineMock( |
| 191 | + side_effect=MultitenantManagerError("Multitenant error") |
| 192 | + ) |
| 193 | + with self.assertRaises(web.HTTPBadRequest): |
| 194 | + await test_module.ready_middleware(request, handler) |
| 195 | + mock_logger.info.assert_called_with( |
| 196 | + "Bad request during %s %s: %s", |
| 197 | + request.method, |
| 198 | + request.path, |
| 199 | + handler.side_effect, |
| 200 | + ) |
| 201 | + |
| 202 | + async def test_ready_middleware_http_not_found(self): |
| 203 | + """Test handling of web.HTTPNotFound and StorageNotFoundError.""" |
| 204 | + with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger: |
| 205 | + mock_logger.info = mock.MagicMock() |
| 206 | + |
| 207 | + request = mock.MagicMock( |
| 208 | + method="GET", |
| 209 | + path="/not-found", |
| 210 | + app=mock.MagicMock(_state={"ready": True}), |
| 211 | + ) |
| 212 | + |
| 213 | + # Test web.HTTPNotFound |
| 214 | + handler = mock.CoroutineMock(side_effect=web.HTTPNotFound(reason="Not found")) |
| 215 | + with self.assertRaises(web.HTTPNotFound): |
| 216 | + await test_module.ready_middleware(request, handler) |
| 217 | + mock_logger.info.assert_called_with( |
| 218 | + "Not Found error occurred during %s %s: %s", |
| 219 | + request.method, |
| 220 | + request.path, |
| 221 | + handler.side_effect, |
| 222 | + ) |
| 223 | + |
| 224 | + # Test StorageNotFoundError |
| 225 | + handler = mock.CoroutineMock( |
| 226 | + side_effect=StorageNotFoundError("Item not found") |
| 227 | + ) |
| 228 | + with self.assertRaises(web.HTTPNotFound): |
| 229 | + await test_module.ready_middleware(request, handler) |
| 230 | + mock_logger.info.assert_called_with( |
| 231 | + "Not Found error occurred during %s %s: %s", |
| 232 | + request.method, |
| 233 | + request.path, |
| 234 | + handler.side_effect, |
| 235 | + ) |
| 236 | + |
| 237 | + async def test_ready_middleware_http_unprocessable_entity(self): |
| 238 | + """Test handling of web.HTTPUnprocessableEntity with nested ValidationError.""" |
| 239 | + with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger: |
| 240 | + mock_logger.info = mock.MagicMock() |
| 241 | + # Mock the extract_validation_error_message function |
| 242 | + with mock.patch.object( |
| 243 | + test_module, "extract_validation_error_message" |
| 244 | + ) as mock_extract: |
| 245 | + mock_extract.return_value = {"field": ["Invalid input"]} |
| 246 | + |
| 247 | + request = mock.MagicMock( |
| 248 | + method="POST", |
| 249 | + path="/unprocessable", |
| 250 | + app=mock.MagicMock(_state={"ready": True}), |
| 251 | + ) |
| 252 | + |
| 253 | + # Create a HTTPUnprocessableEntity exception with a nested ValidationError |
| 254 | + validation_error = ValidationError({"field": ["Invalid input"]}) |
| 255 | + http_error = web.HTTPUnprocessableEntity(reason="Unprocessable Entity") |
| 256 | + http_error.__cause__ = validation_error |
| 257 | + |
| 258 | + handler = mock.CoroutineMock(side_effect=http_error) |
| 259 | + with self.assertRaises(web.HTTPUnprocessableEntity): |
| 260 | + await test_module.ready_middleware(request, handler) |
| 261 | + mock_extract.assert_called_once_with(http_error) |
| 262 | + mock_logger.info.assert_called_with( |
| 263 | + "Unprocessable Entity occurred during %s %s: %s", |
| 264 | + request.method, |
| 265 | + request.path, |
| 266 | + mock_extract.return_value, |
| 267 | + ) |
| 268 | + |
111 | 269 | def get_admin_server( |
112 | 270 | self, settings: Optional[dict] = None, context: Optional[InjectionContext] = None |
113 | 271 | ) -> AdminServer: |
|
0 commit comments