1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
from collections.abc import Callable
from pathlib import Path
from typing import Any
import pytest
from jsonpath_ng.ext import parse
from pytest_mock import MockerFixture
from autosuspend.checks import ConfigurationError, TemporaryCheckError
from autosuspend.checks.json import JsonPath
from . import CheckTest
from .utils import config_section
class TestJsonPath(CheckTest):
def create_instance(self, name: str) -> JsonPath:
return JsonPath(
name=name,
url="url",
timeout=5,
username="userx",
password="pass",
jsonpath=parse("b"),
)
@staticmethod
@pytest.fixture
def json_get_mock(mocker: MockerFixture) -> Any:
mock_reply = mocker.MagicMock()
mock_reply.json.return_value = {"a": {"b": 42, "c": "ignore"}}
return mocker.patch("requests.Session.get", return_value=mock_reply)
def test_matching(self, json_get_mock: Any) -> None:
url = "nourl"
assert (
JsonPath("foo", jsonpath=parse("a.b"), url=url, timeout=5).check()
is not None
)
json_get_mock.assert_called_once_with(
url, timeout=5, headers={"Accept": "application/json"}
)
json_get_mock().json.assert_called_once()
def test_filter_expressions_work(self, json_get_mock: Any) -> None:
url = "nourl"
assert (
JsonPath(
"foo", jsonpath=parse("$[?(@.c=='ignore')]"), url=url, timeout=5
).check()
is not None
)
json_get_mock.assert_called_once_with(
url, timeout=5, headers={"Accept": "application/json"}
)
json_get_mock().json.assert_called_once()
def test_not_matching(self, json_get_mock: Any) -> None:
url = "nourl"
assert (
JsonPath("foo", jsonpath=parse("not.there"), url=url, timeout=5).check()
is None
)
json_get_mock.assert_called_once_with(
url, timeout=5, headers={"Accept": "application/json"}
)
json_get_mock().json.assert_called_once()
def test_network_errors_are_passed(
self, datadir: Path, serve_protected: Callable[[Path], tuple[str, str, str]]
) -> None:
with pytest.raises(TemporaryCheckError):
JsonPath(
name="name",
url=serve_protected(datadir / "data.txt")[0],
timeout=5,
username="wrong",
password="wrong",
jsonpath=parse("b"),
).check()
def test_not_json(self, datadir: Path, serve_file: Callable[[Path], str]) -> None:
with pytest.raises(TemporaryCheckError):
JsonPath(
name="name",
url=serve_file(datadir / "invalid.json"),
timeout=5,
jsonpath=parse("b"),
).check()
class TestCreate:
def test_it_works(self) -> None:
check: JsonPath = JsonPath.create(
"name",
config_section(
{
"url": "url",
"jsonpath": "a.b",
"username": "user",
"password": "pass",
"timeout": "42",
}
),
)
assert check._jsonpath == parse("a.b")
assert check._url == "url"
assert check._username == "user"
assert check._password == "pass"
assert check._timeout == 42
def test_raises_on_missing_json_path(self) -> None:
with pytest.raises(ConfigurationError):
JsonPath.create(
"name",
config_section(
{
"url": "url",
"username": "user",
"password": "pass",
"timeout": "42",
}
),
)
def test_raises_on_invalid_json_path(self) -> None:
with pytest.raises(ConfigurationError):
JsonPath.create(
"name",
config_section(
{
"url": "url",
"jsonpath": ",.asdfjasdklf",
"username": "user",
"password": "pass",
"timeout": "42",
}
),
)
|