forked from p15670423/monkey
Island: Remove {to,from}_json_array()
It turns out nothing needs these functions. VakarisZ was right.
This commit is contained in:
parent
07a8d6194c
commit
ef25dd936b
|
@ -1,6 +1,5 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Mapping, MutableMapping, Sequence, Tuple
|
from typing import Any, Mapping, MutableMapping, Sequence, Tuple
|
||||||
|
|
||||||
|
@ -164,22 +163,15 @@ class Credentials(IJSONSerializable):
|
||||||
raise InvalidCredentialsError(str(err))
|
raise InvalidCredentialsError(str(err))
|
||||||
|
|
||||||
@staticmethod
|
@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
|
:param credentials: A Credentials object
|
||||||
:return: A Sequence of Credentials objects
|
:return: A mapping representing a Credentials object
|
||||||
: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
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
credentials_list = json.loads(credentials_array_json)
|
return CredentialsSchema().dump(credentials)
|
||||||
return [Credentials.from_mapping(c) for c in credentials_list]
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_json(cls, credentials: Credentials) -> str:
|
def to_json(cls, credentials: Credentials) -> str:
|
||||||
|
@ -191,23 +183,3 @@ class Credentials(IJSONSerializable):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
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]) + "]"
|
|
||||||
|
|
||||||
@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)
|
|
||||||
|
|
|
@ -93,35 +93,3 @@ def test_credentials_deserialization__invalid_component():
|
||||||
}
|
}
|
||||||
with pytest.raises(InvalidCredentialComponentError):
|
with pytest.raises(InvalidCredentialComponentError):
|
||||||
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():
|
|
||||||
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
|
|
||||||
|
|
Loading…
Reference in New Issue