forked from p15670423/monkey
Common: Add Credentials.to_json_array()
This commit is contained in:
parent
6bd0e7dc3a
commit
4edb4f6971
|
@ -185,8 +185,18 @@ class Credentials:
|
||||||
"""
|
"""
|
||||||
Serialize a Credentials object to JSON
|
Serialize a Credentials object to JSON
|
||||||
|
|
||||||
:param credentials: A Credentials objcet
|
:param credentials: A Credentials object
|
||||||
:return: A JSON string representing a Credentials object
|
:return: A JSON string representing a Credentials object
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return CredentialsSchema().dumps(credentials)
|
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]) + "]"
|
||||||
|
|
|
@ -89,20 +89,33 @@ def test_credentials_deserialization__invalid_component():
|
||||||
Credentials.from_mapping(invalid_data)
|
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():
|
def test_from_json_array():
|
||||||
deserialized_credentials_0 = Credentials.from_mapping(CREDENTIALS_DICT)
|
credentials_sequence = Credentials.from_json_array(credentials_array_json)
|
||||||
deserialized_credentials_1 = Credentials(
|
|
||||||
secrets=(Password(PASSWORD),), identities=(Username("STUPID"),)
|
assert len(credentials_sequence) == 3
|
||||||
)
|
assert credentials_sequence[0] == DESERIALIZED_CREDENTIALS_0
|
||||||
deserialized_credentials_2 = Credentials(secrets=(LMHash(LM_HASH),), identities=tuple())
|
assert credentials_sequence[1] == DESERIALIZED_CREDENTIALS_1
|
||||||
credentials_array_json = (
|
assert credentials_sequence[2] == DESERIALIZED_CREDENTIALS_2
|
||||||
f"[{Credentials.to_json(deserialized_credentials_0)},"
|
|
||||||
f"{Credentials.to_json(deserialized_credentials_1)},"
|
|
||||||
f"{Credentials.to_json(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]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
credentials_sequence = Credentials.from_json_array(credentials_array_json)
|
assert actual_credentials_dict == expected_credentials_dict
|
||||||
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
|
|
||||||
|
|
Loading…
Reference in New Issue