|
2 | 2 | import pytest |
3 | 3 | from unittest.mock import MagicMock, patch |
4 | 4 |
|
5 | | -from datadog_lambda.asm import asm_start_request, asm_start_response |
6 | | -from datadog_lambda.trigger import parse_event_source, extract_trigger_tags |
| 5 | +from datadog_lambda.asm import ( |
| 6 | + asm_start_request, |
| 7 | + asm_start_response, |
| 8 | + get_asm_blocked_response, |
| 9 | +) |
| 10 | +from datadog_lambda.trigger import ( |
| 11 | + EventTypes, |
| 12 | + _EventSource, |
| 13 | + extract_trigger_tags, |
| 14 | + parse_event_source, |
| 15 | +) |
7 | 16 | from tests.utils import get_mock_context |
8 | 17 |
|
9 | 18 | event_samples = "tests/event_samples/" |
|
15 | 24 | "application_load_balancer", |
16 | 25 | "application-load-balancer.json", |
17 | 26 | "72.12.164.125", |
18 | | - "/lambda", |
| 27 | + "/lambda?query=1234ABCD", |
19 | 28 | "GET", |
20 | 29 | "", |
21 | 30 | False, |
|
27 | 36 | "application_load_balancer_multivalue_headers", |
28 | 37 | "application-load-balancer-mutivalue-headers.json", |
29 | 38 | "72.12.164.125", |
30 | | - "/lambda", |
| 39 | + "/lambda?query=1234ABCD", |
31 | 40 | "GET", |
32 | 41 | "", |
33 | 42 | False, |
|
51 | 60 | "api_gateway", |
52 | 61 | "api-gateway.json", |
53 | 62 | "127.0.0.1", |
54 | | - "/path/to/resource", |
| 63 | + "/path/to/resource?foo=bar", |
55 | 64 | "POST", |
56 | 65 | "eyJ0ZXN0IjoiYm9keSJ9", |
57 | 66 | True, |
|
199 | 208 | ), |
200 | 209 | ] |
201 | 210 |
|
| 211 | +ASM_BLOCKED_RESPONSE_TEST_CASES = [ |
| 212 | + # JSON blocking response |
| 213 | + ( |
| 214 | + {"status_code": 403, "type": "auto", "content-type": "application/json"}, |
| 215 | + 403, |
| 216 | + {"content-type": "application/json"}, |
| 217 | + ), |
| 218 | + # HTML blocking response |
| 219 | + ( |
| 220 | + { |
| 221 | + "status_code": 401, |
| 222 | + "type": "html", |
| 223 | + "content-type": "text/html", |
| 224 | + }, |
| 225 | + 401, |
| 226 | + {"content-type": "text/html"}, |
| 227 | + ), |
| 228 | + # Plain text redirect response |
| 229 | + ( |
| 230 | + {"status_code": 301, "type": "none", "location": "https://example.com/blocked"}, |
| 231 | + 301, |
| 232 | + { |
| 233 | + "content-type": "text/plain; charset=utf-8", |
| 234 | + "location": "https://example.com/blocked", |
| 235 | + }, |
| 236 | + ), |
| 237 | + # Default to content-type application/json and status code 403 when not provided |
| 238 | + ( |
| 239 | + {"type": "auto"}, |
| 240 | + 403, |
| 241 | + {"content-type": "application/json"}, |
| 242 | + ), |
| 243 | +] |
| 244 | + |
202 | 245 |
|
203 | 246 | @pytest.mark.parametrize( |
204 | 247 | "name,file,expected_ip,expected_uri,expected_method,expected_body,expected_base64,expected_query,expected_path_params,expected_route", |
@@ -327,3 +370,31 @@ def test_asm_start_response_parametrized( |
327 | 370 | else: |
328 | 371 | # Verify core.dispatch was not called for non-HTTP events |
329 | 372 | mock_core.dispatch.assert_not_called() |
| 373 | + |
| 374 | + |
| 375 | +@pytest.mark.parametrize( |
| 376 | + "blocked_config, expected_status, expected_headers", |
| 377 | + ASM_BLOCKED_RESPONSE_TEST_CASES, |
| 378 | +) |
| 379 | +@patch("datadog_lambda.asm.get_blocked") |
| 380 | +def test_get_asm_blocked_response_blocked( |
| 381 | + mock_get_blocked, |
| 382 | + blocked_config, |
| 383 | + expected_status, |
| 384 | + expected_headers, |
| 385 | +): |
| 386 | + mock_get_blocked.return_value = blocked_config |
| 387 | + event_source = _EventSource(event_type=EventTypes.API_GATEWAY) |
| 388 | + response = get_asm_blocked_response(event_source) |
| 389 | + assert response["statusCode"] == expected_status |
| 390 | + assert response["headers"] == expected_headers |
| 391 | + |
| 392 | + |
| 393 | +@patch("datadog_lambda.asm.get_blocked") |
| 394 | +def test_get_asm_blocked_response_not_blocked( |
| 395 | + mock_get_blocked, |
| 396 | +): |
| 397 | + mock_get_blocked.return_value = None |
| 398 | + event_source = _EventSource(event_type=EventTypes.API_GATEWAY) |
| 399 | + response = get_asm_blocked_response(event_source) |
| 400 | + assert response is None |
0 commit comments