Skip to content

Commit 1ca23e1

Browse files
committed
feat: add multiple positive and negative test cases
1 parent 7ebea15 commit 1ca23e1

File tree

5 files changed

+129
-7
lines changed

5 files changed

+129
-7
lines changed

tests/aws/services/events/helper_functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def get_message(queue_url):
7373
messages = retry(get_message, retries=5, queue_url=queue_url)
7474

7575
if should_match:
76-
actual_event = json.loads(messages[0]["Body"])
76+
try:
77+
actual_event = json.loads(messages[0]["Body"])
78+
except json.JSONDecodeError:
79+
actual_event = messages[0]["Body"]
7780
if isinstance(actual_event, dict) and "detail" in actual_event:
7881
assert_valid_event(actual_event)
7982
return messages

tests/aws/services/events/test_events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
)
3434

3535
EVENT_DETAIL = {"command": "update-account", "payload": {"acc_id": "0a787ecb-4015", "sf_id": "baz"}}
36+
SPECIAL_EVENT_DETAIL = {
37+
"command": "update-account",
38+
"payload": {"acc_id": "0a787ecb-4015", "sf_id": "baz"},
39+
"listsingle": ["HIGH"],
40+
"listmulti": ["ACTIVE", "INACTIVE"],
41+
}
3642

3743
TEST_EVENT_PATTERN = {
3844
"source": ["core.update-account-command"],

tests/aws/services/events/test_events_inputs.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
is_old_provider,
1212
sqs_collect_messages,
1313
)
14-
from tests.aws.services.events.test_events import EVENT_DETAIL, TEST_EVENT_PATTERN
14+
from tests.aws.services.events.test_events import (
15+
EVENT_DETAIL,
16+
SPECIAL_EVENT_DETAIL,
17+
TEST_EVENT_PATTERN,
18+
)
1519

1620
EVENT_DETAIL_DUPLICATED_KEY = {
1721
"command": "update-account",
@@ -397,6 +401,10 @@ def test_put_events_with_input_transformer_missing_keys(
397401
"input_template",
398402
[INPUT_TEMPLATE_PREDEFINE_VARIABLES_STR, INPUT_TEMPLATE_PREDEFINED_VARIABLES_JSON],
399403
)
404+
# Todo deal with
405+
# "instance": "$.detail.resources[0].id",
406+
# "platform": "$.detail.resources[0].details.awsEc2Instance.platform",
407+
# "region": "$.detail.resources[0].region",
400408
def test_input_transformer_predefined_variables(
401409
self,
402410
input_template,
@@ -473,6 +481,9 @@ def test_input_transformer_predefined_variables(
473481
[
474482
'{"method": "PUT", "path": "users-service/users/<userId>", "bod": <payload>}',
475483
'"Payload of <payload> with path users-service/users/<userId>"',
484+
'{"method": "PUT", "path": "users-service/users/<userId>", "bod": "<userId>"}',
485+
'{"method": "PUT", "path": "users-service/users/<userId>", "bod": [<userId>, "hardcoded"]}',
486+
'"<listsingle> single list item"\n"<listmulti> multiple list items"\n"<systemstring> system account id"\n"<payload> payload"\n"<userId> user id"',
476487
],
477488
)
478489
def test_input_transformer_nested_keys_replacement(
@@ -489,14 +500,17 @@ def test_input_transformer_nested_keys_replacement(
489500
{
490501
"Source": TEST_EVENT_PATTERN["source"][0],
491502
"DetailType": TEST_EVENT_PATTERN["detail-type"][0],
492-
"Detail": json.dumps(EVENT_DETAIL),
503+
"Detail": json.dumps(SPECIAL_EVENT_DETAIL),
493504
}
494505
]
495506
entries_asserts = [(entries, True)]
496507

497508
input_path_map = {
498509
"userId": "$.detail.payload.acc_id",
499510
"payload": "$.detail.payload",
511+
"systemstring": "$.detail.awsAccountId",
512+
"listsingle": "$.detail.listsingle",
513+
"listmulti": "$.detail.listmulti",
500514
}
501515
input_transformer = {
502516
"InputPathsMap": input_path_map,
@@ -514,3 +528,43 @@ def test_input_transformer_nested_keys_replacement(
514528
]
515529
)
516530
snapshot.match("input-transformed-messages", messages)
531+
532+
@markers.aws.validated
533+
@pytest.mark.skipif(
534+
is_old_provider(),
535+
reason="V1 provider does not support this feature",
536+
)
537+
@pytest.mark.parametrize(
538+
"input_template",
539+
[
540+
'{"not_valid": "users-service/users/<payload>", "bod": <payload>}',
541+
],
542+
)
543+
def test_input_transformer_nested_keys_replacement_not_valid(
544+
self,
545+
input_template,
546+
put_events_with_filter_to_sqs,
547+
):
548+
"""
549+
Mapping a nested key via input path map must be a valid string or json
550+
else it will be silently ignored
551+
"""
552+
entries = [
553+
{
554+
"Source": TEST_EVENT_PATTERN["source"][0],
555+
"DetailType": TEST_EVENT_PATTERN["detail-type"][0],
556+
"Detail": json.dumps(SPECIAL_EVENT_DETAIL),
557+
}
558+
]
559+
entries_asserts = [(entries, False)]
560+
561+
input_path_map = {"userId": "$.detail.payload.acc_id", "payload": "$.detail.payload"}
562+
input_transformer = {
563+
"InputPathsMap": input_path_map,
564+
"InputTemplate": input_template,
565+
}
566+
put_events_with_filter_to_sqs(
567+
pattern=TEST_EVENT_PATTERN,
568+
entries_asserts=entries_asserts,
569+
input_transformer=input_transformer,
570+
)

tests/aws/services/events/test_events_inputs.snapshot.json

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
}
269269
},
270270
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[{\"method\": \"PUT\", \"path\": \"users-service/users/<userId>\", \"bod\": <payload>}]": {
271-
"recorded-date": "06-12-2024, 12:10:34",
271+
"recorded-date": "13-12-2024, 12:23:34",
272272
"recorded-content": {
273273
"input-transformed-messages": [
274274
{
@@ -288,7 +288,7 @@
288288
}
289289
},
290290
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[\"Payload of <payload> with path users-service/users/<userId>\"]": {
291-
"recorded-date": "06-12-2024, 12:13:58",
291+
"recorded-date": "13-12-2024, 12:23:37",
292292
"recorded-content": {
293293
"input-transformed-messages": [
294294
{
@@ -299,5 +299,55 @@
299299
}
300300
]
301301
}
302+
},
303+
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[{\"method\": \"PUT\", \"path\": \"users-service/users/<userId>\", \"bod\": \"<userId>\"}]": {
304+
"recorded-date": "13-12-2024, 12:23:39",
305+
"recorded-content": {
306+
"input-transformed-messages": [
307+
{
308+
"MessageId": "<uuid:1>",
309+
"ReceiptHandle": "<receipt-handle:1>",
310+
"MD5OfBody": "<m-d5-of-body:1>",
311+
"Body": {
312+
"method": "PUT",
313+
"path": "users-service/users/0a787ecb-4015",
314+
"bod": "0a787ecb-4015"
315+
}
316+
}
317+
]
318+
}
319+
},
320+
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[{\"method\": \"PUT\", \"path\": \"users-service/users/<userId>\", \"bod\": [<userId>, \"hardcoded\"]}]": {
321+
"recorded-date": "13-12-2024, 12:23:41",
322+
"recorded-content": {
323+
"input-transformed-messages": [
324+
{
325+
"MessageId": "<uuid:1>",
326+
"ReceiptHandle": "<receipt-handle:1>",
327+
"MD5OfBody": "<m-d5-of-body:1>",
328+
"Body": {
329+
"method": "PUT",
330+
"path": "users-service/users/0a787ecb-4015",
331+
"bod": [
332+
"0a787ecb-4015",
333+
"hardcoded"
334+
]
335+
}
336+
}
337+
]
338+
}
339+
},
340+
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[\"<listsingle> single list item\"\\n\"<listmulti> multiple list items\"\\n\"<systemstring> system account id\"\\n\"<payload> payload\"\\n\"<userId> user id\"]": {
341+
"recorded-date": "13-12-2024, 12:23:43",
342+
"recorded-content": {
343+
"input-transformed-messages": [
344+
{
345+
"MessageId": "<uuid:1>",
346+
"ReceiptHandle": "<receipt-handle:1>",
347+
"MD5OfBody": "<m-d5-of-body:1>",
348+
"Body": "\"[HIGH] single list item\"\n\"[ACTIVE,INACTIVE] multiple list items\"\n\" system account id\"\n\"{acc_id:0a787ecb-4015,sf_id:baz} payload\"\n\"0a787ecb-4015 user id\""
349+
}
350+
]
351+
}
302352
}
303353
}

tests/aws/services/events/test_events_inputs.validation.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement": {
1818
"last_validated_date": "2024-12-06T11:07:17+00:00"
1919
},
20+
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[\"<listsingle> single list item\"\\n\"<listmulti> multiple list items\"\\n\"<systemstring> system account id\"\\n\"<payload> payload\"\\n\"<userId> user id\"]": {
21+
"last_validated_date": "2024-12-13T12:23:43+00:00"
22+
},
2023
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[\"Payload of <payload> with path users-service/users/<userId>\"]": {
21-
"last_validated_date": "2024-12-06T12:13:58+00:00"
24+
"last_validated_date": "2024-12-13T12:23:37+00:00"
25+
},
26+
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[{\"method\": \"PUT\", \"path\": \"users-service/users/<userId>\", \"bod\": \"<userId>\"}]": {
27+
"last_validated_date": "2024-12-13T12:23:39+00:00"
2228
},
2329
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[{\"method\": \"PUT\", \"path\": \"users-service/users/<userId>\", \"bod\": <payload>}]": {
24-
"last_validated_date": "2024-12-06T12:10:34+00:00"
30+
"last_validated_date": "2024-12-13T12:23:34+00:00"
31+
},
32+
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_nested_keys_replacement[{\"method\": \"PUT\", \"path\": \"users-service/users/<userId>\", \"bod\": [<userId>, \"hardcoded\"]}]": {
33+
"last_validated_date": "2024-12-13T12:23:41+00:00"
2534
},
2635
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_input_transformer_predefined_variables[\"Message containing all pre defined variables <aws.events.rule-arn> <aws.events.rule-name> <aws.events.event.ingestion-time>\"]": {
2736
"last_validated_date": "2024-06-11T08:33:10+00:00"

0 commit comments

Comments
 (0)