diff --git a/monkey/common/credentials/credentials.py b/monkey/common/credentials/credentials.py index eed392b6c..4916a44e4 100644 --- a/monkey/common/credentials/credentials.py +++ b/monkey/common/credentials/credentials.py @@ -1,6 +1,5 @@ from __future__ import annotations -import json from dataclasses import dataclass from typing import Any, Mapping, MutableMapping, Sequence, Tuple @@ -164,22 +163,15 @@ class Credentials(IJSONSerializable): raise InvalidCredentialsError(str(err)) @staticmethod - def from_json_array(credentials_array_json: str) -> Sequence[Credentials]: + def to_mapping(credentials: Credentials) -> Mapping: """ - Construct a sequence of Credentials object from a JSON string + Serialize a Credentials object to a Mapping - :param credentials: A JSON string that represents an array of Credentials objects - :return: A Sequence of Credentials objects - :raises InvalidCredentialsError: If the provided JSON does not represent a valid - Credentials object - :raises InvalidCredentialComponentError: If any of the contents of `identities` or `secrets` - are not a valid ICredentialComponent - :raises JSONDecodeError: If the provided string is not valid JSON - :raises TypeError: If the provided JSON does not represent an array + :param credentials: A Credentials object + :return: A mapping representing a Credentials object """ - credentials_list = json.loads(credentials_array_json) - return [Credentials.from_mapping(c) for c in credentials_list] + return CredentialsSchema().dump(credentials) @classmethod def to_json(cls, credentials: Credentials) -> str: @@ -191,23 +183,3 @@ class Credentials(IJSONSerializable): """ return CredentialsSchema().dumps(credentials) - - @staticmethod - def to_json_array(credentials: Sequence[Credentials]) -> str: - """ - Serialize a Sequence of Credentials objects to a JSON array - - :param credentials: A Sequence of Credentials objects - :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 a 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 ac8953096..0f5fc519a 100644 --- a/monkey/tests/unit_tests/common/credentials/test_credentials.py +++ b/monkey/tests/unit_tests/common/credentials/test_credentials.py @@ -93,35 +93,3 @@ def test_credentials_deserialization__invalid_component(): } with pytest.raises(InvalidCredentialComponentError): Credentials.from_mapping(invalid_data) - - -DESERIALIZED_CREDENTIALS_0 = Credentials.from_mapping(CREDENTIALS_DICT) -DESERIALIZED_CREDENTIALS_1 = Credentials( - secrets=(Password(PASSWORD),), identities=(Username("STUPID"),) -) -DESERIALIZED_CREDENTIALS_2 = Credentials(secrets=(LMHash(LM_HASH),), identities=tuple()) -credentials_array_json = ( - f"[{Credentials.to_json(DESERIALIZED_CREDENTIALS_0)}," - f"{Credentials.to_json(DESERIALIZED_CREDENTIALS_1)}," - f"{Credentials.to_json(DESERIALIZED_CREDENTIALS_2)}]" -) - - -def test_from_json_array(): - credentials_sequence = Credentials.from_json_array(credentials_array_json) - - assert len(credentials_sequence) == 3 - assert credentials_sequence[0] == DESERIALIZED_CREDENTIALS_0 - assert credentials_sequence[1] == DESERIALIZED_CREDENTIALS_1 - assert credentials_sequence[2] == DESERIALIZED_CREDENTIALS_2 - - -def test_to_json_array(): - expected_credentials_dict = json.loads(credentials_array_json) - actual_credentials_dict = json.loads( - Credentials.to_json_array( - [DESERIALIZED_CREDENTIALS_0, DESERIALIZED_CREDENTIALS_1, DESERIALIZED_CREDENTIALS_2] - ) - ) - - assert actual_credentials_dict == expected_credentials_dict