|
1 | 1 | import json |
2 | 2 | import warnings |
| 3 | +from typing import Any |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent |
@@ -288,6 +289,74 @@ def decorated_function(event): |
288 | 289 | ) |
289 | 290 |
|
290 | 291 |
|
| 292 | +def test_log_includes_client_cert_details(mocker: MockerFixture): |
| 293 | + @request_handler() |
| 294 | + def decorated_function() -> Response: |
| 295 | + return Response( |
| 296 | + statusCode="200", |
| 297 | + body=json.dumps({"message": "Hello, World!"}), |
| 298 | + headers={"Content-Type": "application/json"}, |
| 299 | + ) |
| 300 | + |
| 301 | + test_event = create_test_api_gateway_event() |
| 302 | + event = APIGatewayProxyEvent(test_event) |
| 303 | + |
| 304 | + mock_logger = mocker.patch("nrlf.core.decorators.logger") |
| 305 | + |
| 306 | + decorated_function(event, create_mock_context()) |
| 307 | + |
| 308 | + assert any( |
| 309 | + call[1]["code"].name == "HANDLER000" |
| 310 | + for call in mock_logger.log.call_args_list |
| 311 | + if call[1] |
| 312 | + ) |
| 313 | + |
| 314 | + logged_cert_info: dict[str, Any] = [ |
| 315 | + call[1:][0] |
| 316 | + for call in mock_logger.log.call_args_list |
| 317 | + if call[1] and "code" in call[1] and call[1]["code"].name == "HANDLER000" |
| 318 | + ][0]["client_cert_info"] |
| 319 | + |
| 320 | + client_cert = event.request_context.identity.client_cert |
| 321 | + assert logged_cert_info == { |
| 322 | + "subject_dn": client_cert.subject_dn, |
| 323 | + "issuer_dn": client_cert.issuer_dn, |
| 324 | + "serial_number": client_cert.serial_number, |
| 325 | + } |
| 326 | + |
| 327 | + |
| 328 | +def test_log_includes_client_cert_details_when_no_cert(mocker: MockerFixture): |
| 329 | + @request_handler() |
| 330 | + def decorated_function() -> Response: |
| 331 | + return Response( |
| 332 | + statusCode="200", |
| 333 | + body=json.dumps({"message": "Hello, World!"}), |
| 334 | + headers={"Content-Type": "application/json"}, |
| 335 | + ) |
| 336 | + |
| 337 | + test_event = create_test_api_gateway_event() |
| 338 | + test_event["requestContext"]["identity"]["clientCert"] = None |
| 339 | + event = APIGatewayProxyEvent(test_event) |
| 340 | + |
| 341 | + mock_logger = mocker.patch("nrlf.core.decorators.logger") |
| 342 | + |
| 343 | + decorated_function(event, create_mock_context()) |
| 344 | + |
| 345 | + assert any( |
| 346 | + call[1]["code"].name == "HANDLER000" |
| 347 | + for call in mock_logger.log.call_args_list |
| 348 | + if call[1] |
| 349 | + ) |
| 350 | + |
| 351 | + logged_cert_info: dict[str, Any] = [ |
| 352 | + call[1:][0] |
| 353 | + for call in mock_logger.log.call_args_list |
| 354 | + if call[1] and "code" in call[1] and call[1]["code"].name == "HANDLER000" |
| 355 | + ][0]["client_cert_info"] |
| 356 | + |
| 357 | + assert logged_cert_info == "No client certificate provided" |
| 358 | + |
| 359 | + |
291 | 360 | def test_verify_request_id_happy_path(): |
292 | 361 | test_event = create_test_api_gateway_event() |
293 | 362 |
|
|
0 commit comments