From 7062aaf261e9d87d132e04616e4014f437a63de9 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Fri, 8 Jul 2022 17:26:23 +0200 Subject: [PATCH] Common: Add to_mapping in Credentials --- monkey/common/credentials/credentials.py | 10 ++++++++++ .../unit_tests/common/credentials/test_credentials.py | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/monkey/common/credentials/credentials.py b/monkey/common/credentials/credentials.py index 846137af1..e027f5bfc 100644 --- a/monkey/common/credentials/credentials.py +++ b/monkey/common/credentials/credentials.py @@ -200,3 +200,13 @@ class Credentials: :return: A JSON string representing an array of Credentials objects """ return "[" + ",".join([Credentials.to_json(c) for c in credentials]) + "]" + + @staticmethod + def to_mapping(credentials: Credentials) -> Mapping: + """ + Serialize a Credentials object to Mapping + + :param credentials: A Credentials object + :return: A mapping representing a Credentials object + """ + return CredentialsSchema().dump(credentials) diff --git a/monkey/tests/unit_tests/common/credentials/test_credentials.py b/monkey/tests/unit_tests/common/credentials/test_credentials.py index def11896b..ac8953096 100644 --- a/monkey/tests/unit_tests/common/credentials/test_credentials.py +++ b/monkey/tests/unit_tests/common/credentials/test_credentials.py @@ -56,6 +56,12 @@ def test_credentials_serialization_json(): assert json.loads(serialized_credentials) == CREDENTIALS_DICT +def test_credentials_serialization_mapping(): + serialized_credentials = Credentials.to_mapping(CREDENTIALS_OBJECT) + + assert serialized_credentials == CREDENTIALS_DICT + + def test_credentials_deserialization__from_mapping(): deserialized_credentials = Credentials.from_mapping(CREDENTIALS_DICT)