From 929d8355a6785cdb5e858f6013153bb84108294c Mon Sep 17 00:00:00 2001 From: Rafid Aslam Date: Thu, 22 Feb 2024 17:06:34 +0700 Subject: [PATCH] json/get_value: Send ProviderError when key_path is not found --- src/tirith/providers/json/handler.py | 12 +++++++++++- tests/providers/json/policy.json | 12 ++++++++++++ tests/providers/json/test_get_value.py | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/providers/json/test_get_value.py diff --git a/src/tirith/providers/json/handler.py b/src/tirith/providers/json/handler.py index ceba38c..173e56d 100644 --- a/src/tirith/providers/json/handler.py +++ b/src/tirith/providers/json/handler.py @@ -1,7 +1,7 @@ import pydash from typing import Callable, Dict, List -from ..common import create_result_dict +from ..common import create_result_dict, ProviderError class PydashPathNotFound: @@ -39,6 +39,16 @@ def get_value(provider_args: Dict, input_data: Dict) -> List[dict]: key_path: str = provider_args["key_path"] values = get_path_value_from_dict(key_path, input_data) + + if len(values) == 0: + severity_value = 2 + return [ + create_result_dict( + value=ProviderError(severity_value=severity_value), + err=f"key_path: `{key_path}` is not found (severity: {severity_value})", + ) + ] + outputs = [create_result_dict(value=value, meta=None, err=None) for value in values] return outputs diff --git a/tests/providers/json/policy.json b/tests/providers/json/policy.json index e18b662..90ae178 100644 --- a/tests/providers/json/policy.json +++ b/tests/providers/json/policy.json @@ -4,6 +4,18 @@ "required_provider": "stackguardian/json" }, "evaluators": [ + { + "id": "check0", + "provider_args": { + "operation_type": "get_value", + "key_path": "z.b" + }, + "condition": { + "type": "LessThanEqualTo", + "value": 1, + "error_tolerance": 2 + } + }, { "id": "check1", "provider_args": { diff --git a/tests/providers/json/test_get_value.py b/tests/providers/json/test_get_value.py new file mode 100644 index 0000000..7d754a3 --- /dev/null +++ b/tests/providers/json/test_get_value.py @@ -0,0 +1,16 @@ +import json +import os + +from tirith.core.core import start_policy_evaluation_from_dict + + +# TODO: Need to split this into multiple tests +def test_get_value(): + test_dir = os.path.dirname(os.path.realpath(__file__)) + with open(os.path.join(test_dir, "input.json")) as f: + input_data = json.load(f) + with open(os.path.join(test_dir, "policy.json")) as f: + policy = json.load(f) + + result = start_policy_evaluation_from_dict(policy, input_data) + assert result["final_result"] == True