Skip to content

Commit

Permalink
json/get_value: Send ProviderError when key_path is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
refeed committed Feb 22, 2024
1 parent 264d193 commit e51f720
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/tirith/providers/json/handler.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/providers/json/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
16 changes: 16 additions & 0 deletions tests/providers/json/test_get_value.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e51f720

Please sign in to comment.