|
31 | 31 |
|
32 | 32 | # noinspection PyProtectedMember |
33 | 33 | from reportportal_client._internal.aio.http import RetryingClientSession |
| 34 | +from reportportal_client._internal.services.auth import ApiKeyAuthAsync |
34 | 35 |
|
35 | 36 | HTTP_TIMEOUT_TIME = 1.2 |
36 | 37 |
|
@@ -75,6 +76,21 @@ def do_GET(self): |
75 | 76 | self.wfile.flush() |
76 | 77 |
|
77 | 78 |
|
| 79 | +class UnauthorizedHttpHandler(http.server.BaseHTTPRequestHandler): |
| 80 | + def do_GET(self): |
| 81 | + auth_header = self.headers.get("Authorization") |
| 82 | + if auth_header == "Bearer test_api_key": |
| 83 | + self.send_response(200) |
| 84 | + self.send_header("Content-Type", "application/json") |
| 85 | + self.end_headers() |
| 86 | + self.wfile.write("{}\n\n".encode("utf-8")) |
| 87 | + else: |
| 88 | + self.send_response(401, "Unauthorized") |
| 89 | + self.end_headers() |
| 90 | + self.wfile.write("Unauthorized\n\n".encode("utf-8")) |
| 91 | + self.wfile.flush() |
| 92 | + |
| 93 | + |
78 | 94 | SERVER_PORT = 8000 |
79 | 95 | SERVER_ADDRESS = ("", SERVER_PORT) |
80 | 96 | SERVER_CLASS = socketserver.TCPServer |
@@ -163,3 +179,93 @@ async def test_no_retry_on_not_retryable_error(): |
163 | 179 | assert result is None |
164 | 180 | assert async_mock.call_count == 1 |
165 | 181 | assert total_time < 1 |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.asyncio |
| 185 | +async def test_auth_header_added_to_request(): |
| 186 | + """Test that auth header is added to requests when auth is configured.""" |
| 187 | + port = 8006 |
| 188 | + retry_number = 5 |
| 189 | + auth = ApiKeyAuthAsync("test_api_key") |
| 190 | + timeout = aiohttp.ClientTimeout(connect=1.0, sock_read=1.0) |
| 191 | + connector = aiohttp.TCPConnector(force_close=True) |
| 192 | + session = RetryingClientSession( |
| 193 | + f"http://localhost:{port}", |
| 194 | + timeout=timeout, |
| 195 | + max_retry_number=retry_number, |
| 196 | + base_retry_delay=0.01, |
| 197 | + auth=auth, |
| 198 | + connector=connector, |
| 199 | + ) |
| 200 | + |
| 201 | + with get_http_server(server_handler=UnauthorizedHttpHandler, server_address=("", port)): |
| 202 | + async with session: |
| 203 | + result = await session.get("/") |
| 204 | + assert result.ok |
| 205 | + assert result.status == 200 |
| 206 | + |
| 207 | + |
| 208 | +@pytest.mark.asyncio |
| 209 | +async def test_auth_refresh_on_401(): |
| 210 | + """Test that 401 response triggers auth refresh.""" |
| 211 | + port = 8007 |
| 212 | + retry_number = 5 |
| 213 | + |
| 214 | + # Create a mock auth that fails first, then succeeds |
| 215 | + auth = mock.AsyncMock() |
| 216 | + auth.get = mock.AsyncMock(side_effect=["Bearer invalid_token", "Bearer test_api_key"]) |
| 217 | + auth.refresh = mock.AsyncMock(return_value="Bearer test_api_key") |
| 218 | + |
| 219 | + timeout = aiohttp.ClientTimeout(connect=1.0, sock_read=1.0) |
| 220 | + connector = aiohttp.TCPConnector(force_close=True) |
| 221 | + session = RetryingClientSession( |
| 222 | + f"http://localhost:{port}", |
| 223 | + timeout=timeout, |
| 224 | + max_retry_number=retry_number, |
| 225 | + base_retry_delay=0.01, |
| 226 | + auth=auth, |
| 227 | + connector=connector, |
| 228 | + ) |
| 229 | + |
| 230 | + with get_http_server(server_handler=UnauthorizedHttpHandler, server_address=("", port)): |
| 231 | + async with session: |
| 232 | + result = await session.get("/") |
| 233 | + # First call to get() returns invalid token, which causes 401 |
| 234 | + # Then refresh() is called and returns valid token |
| 235 | + # Request is retried with valid token and succeeds |
| 236 | + assert result.ok |
| 237 | + assert result.status == 200 |
| 238 | + assert auth.get.call_count == 1 |
| 239 | + assert auth.refresh.call_count == 1 |
| 240 | + |
| 241 | + |
| 242 | +@pytest.mark.asyncio |
| 243 | +async def test_auth_refresh_only_once(): |
| 244 | + """Test that auth refresh is only performed once per request.""" |
| 245 | + port = 8008 |
| 246 | + retry_number = 5 |
| 247 | + |
| 248 | + # Create a mock auth that always fails |
| 249 | + auth = mock.AsyncMock() |
| 250 | + auth.get = mock.AsyncMock(return_value="Bearer invalid_token") |
| 251 | + auth.refresh = mock.AsyncMock(return_value="Bearer still_invalid_token") |
| 252 | + |
| 253 | + timeout = aiohttp.ClientTimeout(connect=1.0, sock_read=1.0) |
| 254 | + connector = aiohttp.TCPConnector(force_close=True) |
| 255 | + session = RetryingClientSession( |
| 256 | + f"http://localhost:{port}", |
| 257 | + timeout=timeout, |
| 258 | + max_retry_number=retry_number, |
| 259 | + base_retry_delay=0.01, |
| 260 | + auth=auth, |
| 261 | + connector=connector, |
| 262 | + ) |
| 263 | + |
| 264 | + with get_http_server(server_handler=UnauthorizedHttpHandler, server_address=("", port)): |
| 265 | + async with session: |
| 266 | + result = await session.get("/") |
| 267 | + # Auth refresh should only be attempted once |
| 268 | + assert not result.ok |
| 269 | + assert result.status == 401 |
| 270 | + assert auth.get.call_count == 1 |
| 271 | + assert auth.refresh.call_count == 1 |
0 commit comments