75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""Tests for daemon client path parameter substitution."""
|
|
|
|
import contextlib
|
|
from unittest.mock import patch
|
|
|
|
from daemon.client import _format_path, request
|
|
|
|
|
|
class TestFormatPath:
|
|
def test_simple_substitution(self):
|
|
assert (
|
|
_format_path("/network/interfaces/<name>", {"name": "eth0"})
|
|
== "/network/interfaces/eth0"
|
|
)
|
|
|
|
def test_multiple_params(self):
|
|
assert _format_path("/a/<x>/b/<y>", {"x": "1", "y": "2"}) == "/a/1/b/2"
|
|
|
|
def test_no_params_unchanged(self):
|
|
assert (
|
|
_format_path("/network/interfaces/<name>", None)
|
|
== "/network/interfaces/<name>"
|
|
)
|
|
|
|
def test_empty_params_unchanged(self):
|
|
assert (
|
|
_format_path("/network/interfaces/<name>", {})
|
|
== "/network/interfaces/<name>"
|
|
)
|
|
|
|
def test_partial_substitution(self):
|
|
assert _format_path("/a/<x>/b/<y>", {"x": "1"}) == "/a/1/b/<y>"
|
|
|
|
def test_url_encodes_special_chars(self):
|
|
assert _format_path("/a/<x>", {"x": "foo bar"}) == "/a/foo%20bar"
|
|
|
|
def test_preserves_non_param_brackets(self):
|
|
assert _format_path("/foo[bar]/<x>", {"x": "z"}) == "/foo[bar]/z"
|
|
|
|
def test_numeric_value(self):
|
|
assert _format_path("/items/<id>", {"id": 42}) == "/items/42"
|
|
|
|
def test_path_without_params(self):
|
|
assert _format_path("/health", {"foo": "bar"}) == "/health"
|
|
|
|
|
|
class TestRequestPathSubstitution:
|
|
@patch("daemon.client.requests_unixsocket.Session")
|
|
def test_post_substitutes_name_from_body(self, mock_session_cls):
|
|
mock_sess = mock_session_cls.return_value
|
|
mock_resp = mock_sess.request.return_value
|
|
mock_resp.status_code = 200
|
|
mock_resp.json.return_value = {"ok": True, "data": {"name": "eth0"}}
|
|
|
|
with contextlib.suppress(Exception):
|
|
request("POST", "/network/interfaces/<name>", json_body={"name": "eth0"})
|
|
|
|
call_args = mock_sess.request.call_args
|
|
url = call_args[0][1] if call_args else ""
|
|
assert "/interfaces/eth0" in url
|
|
|
|
@patch("daemon.client.requests_unixsocket.Session")
|
|
def test_get_substitutes_name_from_query(self, mock_session_cls):
|
|
mock_sess = mock_session_cls.return_value
|
|
mock_resp = mock_sess.request.return_value
|
|
mock_resp.status_code = 200
|
|
mock_resp.json.return_value = {"ok": True, "data": {}}
|
|
|
|
with contextlib.suppress(Exception):
|
|
request("GET", "/network/interfaces/<name>", query_params={"name": "eth0"})
|
|
|
|
call_args = mock_sess.request.call_args
|
|
url = call_args[0][1] if call_args else ""
|
|
assert "/interfaces/eth0" in url
|