From fe6e3d75b5aa63d0a277d65d2e66342972d3235a Mon Sep 17 00:00:00 2001 From: vakarisz Date: Mon, 11 Jul 2022 16:53:43 +0300 Subject: [PATCH] Island, UT: Handle tuples in representations.py --- .../cc/services/representations.py | 21 +++++++++++-------- .../cc/services/test_representations.py | 19 +++++++++++------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/monkey/monkey_island/cc/services/representations.py b/monkey/monkey_island/cc/services/representations.py index 6267ccd8b..e066764aa 100644 --- a/monkey/monkey_island/cc/services/representations.py +++ b/monkey/monkey_island/cc/services/representations.py @@ -17,24 +17,27 @@ def _normalize_obj(obj): def _normalize_value(value): - if isinstance(value, list): - for i in range(0, len(value)): - value[i] = _normalize_value(value[i]) - if type(value) == dict: - return _normalize_obj(value) + # ObjectId is serializible by default, but returns a dict + # So serialize it first into a plain string if isinstance(value, bson.objectid.ObjectId): return str(value) + + if isinstance(value, list): + return [_normalize_value(_value) for _value in value] + if isinstance(value, tuple): + return tuple((_normalize_value(_value) for _value in value)) + if type(value) == dict: + return _normalize_obj(value) if isinstance(value, datetime): return str(value) if issubclass(type(value), Enum): return value.name try: + dumps(value) + return value + except TypeError: return value.__dict__ - except AttributeError: - pass - - return value def output_json(value, code, headers=None): diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_representations.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_representations.py index 6212fb892..6dfe43304 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_representations.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_representations.py @@ -13,7 +13,8 @@ class MockClass: obj_id_str = "123456789012345678901234" -mock_object = MockClass(1) +mock_object1 = MockClass(1) +mock_object2 = MockClass(2) def test_normalize_dicts(): @@ -21,9 +22,9 @@ def test_normalize_dicts(): assert {"a": "a"} == _normalize_value({"a": "a"}) - assert {"id": 12345} == _normalize_value({"_id": 12345}) + assert {"id": 12345} == _normalize_value({"id": 12345}) - assert {"id": obj_id_str} == _normalize_value({"_id": bson.objectid.ObjectId(obj_id_str)}) + assert {"id": obj_id_str} == _normalize_value({"id": bson.objectid.ObjectId(obj_id_str)}) dt = datetime.now() expected = {"a": str(dt)} @@ -39,7 +40,7 @@ def test_normalize_complex(): "bb": bson.objectid.ObjectId(obj_id_str), } ], - "b": {"_id": bson.objectid.ObjectId(obj_id_str)}, + "b": {"id": bson.objectid.ObjectId(obj_id_str)}, } expected_dict = {"a": [{"ba": obj_id_str, "bb": obj_id_str}], "b": {"id": obj_id_str}} @@ -47,16 +48,22 @@ def test_normalize_complex(): def test_normalize_list(): - mock_list = [bson.objectid.ObjectId(obj_id_str), {"a": "b"}, {"object": [mock_object]}] + mock_list = [bson.objectid.ObjectId(obj_id_str), {"a": "b"}, {"object": [mock_object1]}] expected_list = [obj_id_str, {"a": "b"}, {"object": [{"a": 1}]}] assert expected_list == _normalize_value(mock_list) -def test_normalize__enum(): +def test_normalize_enum(): class BogusEnum(Enum): bogus_val = "Bogus" my_obj = {"something": "something", "my_enum": BogusEnum.bogus_val} assert {"something": "something", "my_enum": "bogus_val"} == _normalize_value(my_obj) + + +def test_normalize_tuple(): + mock_tuple = [{"my_tuple": (mock_object1, mock_object2, b"one_two")}] + expected_tuple = [{"my_tuple": ({"a": 1}, {"a": 2}, b"one_two")}] + assert expected_tuple == _normalize_value(mock_tuple)