1
0
mirror of https://github.com/quay/quay.git synced 2026-01-26 06:21:37 +03:00
Files
quay/util/test/test_jsontemplate.py
kalle (jag) cfd4e8c46b util: fix matching multiples in jsontemplate.py (PROJQUAY-0000) (#800)
Matching multiples caused exceptions to be raised. This made it so
webhook notification POST bodies could not template things like the
image tags as `${tags[*]}`. This was caused by a typo in the jsonpath_rw
match field that was used.

Signed-off-by: Kalle Jillheden <kalle.jillheden@iver.se>
2021-06-08 14:16:08 -04:00

89 lines
3.0 KiB
Python

import pytest
from util.jsontemplate import JSONTemplate, JSONTemplateParseException
@pytest.mark.parametrize(
"template_string, data, expected",
[
("{}", {}, {}),
('{"hello": "world"}', {}, {"hello": "world"}),
pytest.param(
'{"hello": "${thing}"}', {"thing": 1234}, {"hello": 1234}, id="direct expression"
),
pytest.param(
'{"hello": "cool-${thing}"}',
{"thing": "beans"},
{"hello": "cool-beans"},
id="inline expression",
),
pytest.param(
'{"hello": "${first.second.third}"}',
{"first": {"second": {"third": 42}}},
{"hello": 42},
id="nested object",
),
pytest.param(
'{"hello": "${first.second.third} is the answer"}',
{"first": {"second": {"third": 42}}},
{"hello": "42 is the answer"},
id="nested object with inline expression",
),
pytest.param(
'{"hello": "${first.doesnotexist.third}"}',
{"first": {"second": {"third": 42}}},
{"hello": None},
id="inline expression with unknown child",
),
pytest.param(
'{"hello": "${first[2]}"}', {"first": [1, 2, 42]}, {"hello": 42}, id="array access"
),
pytest.param(
'{"hello": "${first[2]}"}',
{"first": [1, 2]},
{"hello": None},
id="outside of array index",
),
pytest.param(
'{"hello": ["${first[1]}", "${first[0]}"]}',
{"first": [1, 2]},
{"hello": [2, 1]},
id="array to list",
),
pytest.param(
'{"hello": "hey-${first[2]}"}',
{"first": [1, 2]},
{"hello": "hey-(none)"},
id="outside of array index inline expression",
),
pytest.param(
'{"hello": "${tags[*]}"}',
{"tags": ["latest", "prod", "foo"]},
{"hello": ["latest", "prod", "foo"]},
id="match multiples inline",
),
pytest.param(
'{"hello": "tags: ${tags[*]}"}',
{"tags": ["latest", "prod", "foo"]},
{"hello": "tags: latest,prod,foo"},
id="match multiples with inline expression",
),
pytest.param('{"hello": "}', {}, JSONTemplateParseException, id="invalid template"),
pytest.param('{"hello": "${}"}', {}, JSONTemplateParseException, id="empty expression"),
pytest.param('{"hello": "${;;}"}', {}, JSONTemplateParseException, id="invalid expression"),
pytest.param(
'{"hello": "${first[}"}',
{},
JSONTemplateParseException,
id="another invalid expression",
),
],
)
def test_json_template(template_string, data, expected):
if expected == JSONTemplateParseException:
with pytest.raises(JSONTemplateParseException):
JSONTemplate(template_string)
else:
jt = JSONTemplate(template_string)
assert jt.apply(data) == expected