67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import urllib.error
|
||
|
|
|
||
|
|
from reuse_surface import statehub_bridge
|
||
|
|
|
||
|
|
|
||
|
|
def test_state_hub_reachable_true(monkeypatch):
|
||
|
|
monkeypatch.setattr(statehub_bridge, "_request", lambda *a, **k: {"status": "ok"})
|
||
|
|
assert statehub_bridge.state_hub_reachable() is True
|
||
|
|
|
||
|
|
|
||
|
|
def test_state_hub_reachable_false_on_error(monkeypatch):
|
||
|
|
def raise_error(*args, **kwargs):
|
||
|
|
raise urllib.error.URLError("no route")
|
||
|
|
|
||
|
|
monkeypatch.setattr(statehub_bridge, "_request", raise_error)
|
||
|
|
assert statehub_bridge.state_hub_reachable() is False
|
||
|
|
|
||
|
|
|
||
|
|
def test_file_capability_request_skips_when_unreachable(monkeypatch):
|
||
|
|
monkeypatch.setattr(statehub_bridge, "state_hub_reachable", lambda base_url=None: False)
|
||
|
|
result = statehub_bridge.file_capability_request(
|
||
|
|
title="t", description="d", requesting_domain="infotech", requesting_agent="a"
|
||
|
|
)
|
||
|
|
assert result is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_file_capability_request_posts_body(monkeypatch):
|
||
|
|
captured = {}
|
||
|
|
|
||
|
|
def fake_request(method, path, base_url, *, body=None, params=None, timeout=None):
|
||
|
|
captured["method"] = method
|
||
|
|
captured["path"] = path
|
||
|
|
captured["body"] = body
|
||
|
|
return {"id": "abc-123", **body}
|
||
|
|
|
||
|
|
monkeypatch.setattr(statehub_bridge, "state_hub_reachable", lambda base_url=None: True)
|
||
|
|
monkeypatch.setattr(statehub_bridge, "_request", fake_request)
|
||
|
|
result = statehub_bridge.file_capability_request(
|
||
|
|
title="Need X", description="desc", requesting_domain="infotech", requesting_agent="reuse-surface"
|
||
|
|
)
|
||
|
|
assert captured["method"] == "POST"
|
||
|
|
assert captured["path"] == "/capability-requests/"
|
||
|
|
assert captured["body"]["title"] == "Need X"
|
||
|
|
assert result["id"] == "abc-123"
|
||
|
|
|
||
|
|
|
||
|
|
def test_list_open_capability_requests_filters_terminal_status(monkeypatch):
|
||
|
|
monkeypatch.setattr(statehub_bridge, "state_hub_reachable", lambda base_url=None: True)
|
||
|
|
monkeypatch.setattr(
|
||
|
|
statehub_bridge,
|
||
|
|
"_request",
|
||
|
|
lambda *a, **k: [
|
||
|
|
{"id": "1", "status": "requested"},
|
||
|
|
{"id": "2", "status": "completed"},
|
||
|
|
{"id": "3", "status": "requested", "catalog_entry_id": "routed-but-open"},
|
||
|
|
],
|
||
|
|
)
|
||
|
|
result = statehub_bridge.list_open_capability_requests()
|
||
|
|
assert {r["id"] for r in result} == {"1", "3"}
|
||
|
|
|
||
|
|
|
||
|
|
def test_list_open_capability_requests_none_when_unreachable(monkeypatch):
|
||
|
|
monkeypatch.setattr(statehub_bridge, "state_hub_reachable", lambda base_url=None: False)
|
||
|
|
assert statehub_bridge.list_open_capability_requests() is None
|