From 1f9a056b0be5e735559babecf6459dfbe12aaa9c Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 24 Jun 2022 11:51:33 -0400 Subject: [PATCH] Agent: Add AgentConfiguration.from_json() --- monkey/common/configuration/agent_configuration.py | 4 ++++ .../common/configuration/test_agent_configuration.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/monkey/common/configuration/agent_configuration.py b/monkey/common/configuration/agent_configuration.py index 36fa24b85..8ba9e998b 100644 --- a/monkey/common/configuration/agent_configuration.py +++ b/monkey/common/configuration/agent_configuration.py @@ -28,6 +28,10 @@ class AgentConfiguration: def from_dict(dict_: dict): return AgentConfigurationSchema().load(dict_) + @staticmethod + def from_json(config_json: dict): + return AgentConfigurationSchema().loads(config_json) + class AgentConfigurationSchema(Schema): keep_tunnel_open_time = fields.Float() diff --git a/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py b/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py index 13cd106a8..6156a0b69 100644 --- a/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py +++ b/monkey/tests/unit_tests/common/configuration/test_agent_configuration.py @@ -189,3 +189,12 @@ def test_from_dict(): config = AgentConfiguration.from_dict(dict_) assert schema.dump(config) == dict_ + + +def test_from_json(): + schema = AgentConfigurationSchema() + dict_ = json.loads(DEFAULT_AGENT_CONFIGURATION_JSON) + + config = AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON) + + assert schema.dump(config) == dict_