From d11cfb0950771660f395255cc9c5f2146e8195fd Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:21:31 +0000 Subject: [PATCH 1/3] feat: change icon for better visibilty --- app/server/server.py | 2 +- app/tests/server/test_server.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/server/server.py b/app/server/server.py index 742368bf..f795d412 100644 --- a/app/server/server.py +++ b/app/server/server.py @@ -401,7 +401,7 @@ def handle_string_payload( case "UpptimePayload": # Temporary fix for Upptime payloads text = validated_payload.get("text", "") - header_text = "🟥 Web Application Down!" + header_text = "📈 Web Application Down!" blocks = [ {"type": "section", "text": {"type": "mrkdwn", "text": " "}}, { diff --git a/app/tests/server/test_server.py b/app/tests/server/test_server.py index e3af3c59..3e83c14c 100644 --- a/app/tests/server/test_server.py +++ b/app/tests/server/test_server.py @@ -298,7 +298,7 @@ def test_handle_string_payload_with_upptime_payload(validate_string_payload_type assert response.blocks == [ {"text": {"text": " ", "type": "mrkdwn"}, "type": "section"}, { - "text": {"text": "🟥 Web Application Down!", "type": "plain_text"}, + "text": {"text": "📈 Web Application Down!", "type": "plain_text"}, "type": "header", }, { From e292fa3b367041a1037df0e61b565519a1867e9a Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:41:09 +0000 Subject: [PATCH 2/3] feat: setup new method to count number of mathing params --- app/models/utils.py | 5 +++++ app/tests/models/test_models_utils.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/models/utils.py b/app/models/utils.py index 35bce404..284a9541 100644 --- a/app/models/utils.py +++ b/app/models/utils.py @@ -47,6 +47,11 @@ def is_parameter_in_model(model_params: List[str], payload: Dict[str, Any]) -> b return any(param in model_params for param in payload.keys()) +def has_parameters_in_model(model_params: List[str], payload: Dict[str, Any]) -> int: + """Returns the number of parameters in the payload that are in the model.""" + return sum(1 for param in model_params if param in payload.keys()) + + def are_all_parameters_in_model( model_params: List[str], payload: Dict[str, Any] ) -> bool: diff --git a/app/tests/models/test_models_utils.py b/app/tests/models/test_models_utils.py index 7370108a..d8aeff4d 100644 --- a/app/tests/models/test_models_utils.py +++ b/app/tests/models/test_models_utils.py @@ -47,6 +47,27 @@ def test_is_parameter_in_model(): assert not model_utils.is_parameter_in_model(model_params, non_string_keys_payload) +def test_has_parameters_in_model(): + model_params = ["field1", "field2", "field3"] + + payload = {"field1": "value", "field2": "value"} + assert model_utils.has_parameters_in_model(model_params, payload) == 2 + + payload = {"field1": "value", "non_field": "value"} + assert model_utils.has_parameters_in_model(model_params, payload) == 1 + + empty_payload = {} + assert model_utils.has_parameters_in_model(model_params, empty_payload) == 0 + + partial_payload = {"field1": "value"} + assert model_utils.has_parameters_in_model(model_params, partial_payload) == 1 + + non_string_keys_payload = {1: "value", 2: "value"} + assert ( + model_utils.has_parameters_in_model(model_params, non_string_keys_payload) == 0 + ) + + def test_are_all_parameters_in_model(): model_params = ["field1", "field2", "field3"] From 8283f9bd578e9464547b7a36d0a84a5f854edebe Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:41:46 +0000 Subject: [PATCH 3/3] feat: improve validate string to return closest payload type possible --- app/models/webhooks.py | 6 ++++-- app/tests/models/test_webhooks.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/models/webhooks.py b/app/models/webhooks.py index 228636e1..b9b191c2 100644 --- a/app/models/webhooks.py +++ b/app/models/webhooks.py @@ -199,10 +199,12 @@ def validate_string_payload_type(payload: str) -> tuple: ] model_params = model_utils.get_dict_of_parameters_from_models(known_models) + max_matches = 0 for model, params in model_params.items(): - if model_utils.is_parameter_in_model(params, payload_dict): + matches = model_utils.has_parameters_in_model(params, payload_dict) + if matches > max_matches: + max_matches = matches payload_type = model - break if payload_type: return payload_type, payload_dict diff --git a/app/tests/models/test_webhooks.py b/app/tests/models/test_webhooks.py index 05f99822..9b93075e 100644 --- a/app/tests/models/test_webhooks.py +++ b/app/tests/models/test_webhooks.py @@ -239,12 +239,37 @@ def test_validate_string_payload_type_valid_json( "TestModel": ["type"], "TestModel2": ["type2"], } - model_utils_mock.is_parameter_in_model.side_effect = [False, True] + model_utils_mock.has_parameters_in_model.side_effect = [0, 1, 0] assert webhooks.validate_string_payload_type('{"type": "test"}') == ( "TestModel", {"type": "test"}, ) - assert model_utils_mock.is_parameter_in_model.call_count == 2 + assert model_utils_mock.has_parameters_in_model.call_count == 3 + + +@patch("models.webhooks.model_utils") +def test_validate_string_payload_same_params_in_multiple_models_returns_first_found( + model_utils_mock, caplog +): + model_utils_mock.get_dict_of_parameters_from_models.return_value = { + "WrongModel": ["test"], + "TestModel": ["type", "type2"], + "TestModel2": ["type2"], + "TestModel3": ["type"], + } + model_utils_mock.has_parameters_in_model.side_effect = [0, 2, 0, 1] + response = webhooks.validate_string_payload_type( + '{"type": "test", "type2": "test"}' + ) + assert response == ( + "TestModel", + {"type": "test", "type2": "test"}, + ) + assert response != ( + "TestModel3", + {"type": "test"}, + ) + assert model_utils_mock.has_parameters_in_model.call_count == 4 def test_validate_string_payload_type_error_loading_json(caplog):