feat(widgets): rebind widgets to the service registry
PR 2 of 4 for the runtime service registry change. - dashboard_widgets gains service_id + widget_kind columns (legacy addon_id/widget_type kept but unused). - Source adapters take (service: ServiceRecord | None, widget_kind, config). SERVICE_ADAPTERS keyed by service_type; BUILTIN_ADAPTERS for backups/static. - Backups and static stay as service-less built-ins (service_id nullable), exposed via GET /api/widgets/builtin. - SSH task adapter resolves the task + instance, runs over SSH, and appends a service_task_runs history row on success/failure/timeout/error. - Retire widgets/registry.py; widget metadata now comes from the integrations registry + widgets/builtin. Remove /api/widgets/types and /api/widgets/sources. - Stop default widget seeding (fresh install = empty dashboard). - Rewrite widget tests around the service-bound + built-in model (26 tests). Backend-only breaking change; frontend is reconciled in Slice 3. Build/lint stay green; pytest 222 passed.
This commit is contained in:
+255
-344
@@ -1,22 +1,32 @@
|
||||
"""Tests for the dashboard widget backend: registry, CRUD, validation, seeding, adapters."""
|
||||
"""Tests for the dashboard widget system: service-bound + built-in widgets."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from cryptography.fernet import Fernet
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from media_library_viewer_api.dependencies import get_settings_store
|
||||
from media_library_viewer_api.main import app
|
||||
from media_library_viewer_api.services.settings_store import SettingsStore
|
||||
from media_library_viewer_api.widgets.sources import (
|
||||
SOURCE_REGISTRY,
|
||||
BackupsWidgetSource,
|
||||
GrafanaWidgetSource,
|
||||
SshTaskWidgetSource,
|
||||
ServiceRecord,
|
||||
StaticWidgetSource,
|
||||
)
|
||||
|
||||
TEST_KEY = Fernet.generate_key().decode()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _encryption_key(monkeypatch):
|
||||
monkeypatch.setenv("MANAGE_ENCRYPTION_KEY", TEST_KEY)
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path):
|
||||
@@ -30,446 +40,347 @@ def client(tmp_path):
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_widget_sources(client):
|
||||
response = client.get("/api/widgets/sources")
|
||||
def _make_grafana_service(client, name="Production Grafana", **config_overrides):
|
||||
config = {"base_url": "https://grafana.example.com"}
|
||||
config.update(config_overrides)
|
||||
return client.post(
|
||||
"/api/services/instances",
|
||||
json={"service_type": "grafana", "name": name, "config": config, "enabled": True},
|
||||
).json()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Built-in kinds + built-in widget CRUD
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_list_builtin_kinds(client):
|
||||
response = client.get("/api/widgets/builtin")
|
||||
assert response.status_code == 200
|
||||
assert set(response.json()) == {
|
||||
"jellyfin",
|
||||
"backups",
|
||||
"grafana",
|
||||
"prometheus",
|
||||
"ssh_task",
|
||||
"static",
|
||||
}
|
||||
kinds = {item["kind"] for item in response.json()}
|
||||
assert kinds == {"backups", "static"}
|
||||
|
||||
|
||||
def test_widget_types(client):
|
||||
response = client.get("/api/widgets/types")
|
||||
assert response.status_code == 200
|
||||
types = {item["widget_type"] for item in response.json()}
|
||||
assert types == {
|
||||
"jellyfin",
|
||||
"backups",
|
||||
"grafana-link",
|
||||
"prometheus-metric",
|
||||
"ssh-task",
|
||||
"static",
|
||||
}
|
||||
|
||||
|
||||
def test_create_and_read_widget(client):
|
||||
def test_create_and_read_static_widget(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"widget_kind": "static",
|
||||
"title": "Note",
|
||||
"config": {"text": "hello"},
|
||||
"enabled": True,
|
||||
"sort_order": 5,
|
||||
},
|
||||
)
|
||||
assert response.status_code == 201
|
||||
widget = response.json()
|
||||
assert widget["title"] == "Note"
|
||||
assert widget["config"] == {"text": "hello"}
|
||||
assert widget["enabled"] is True
|
||||
assert widget["sort_order"] == 5
|
||||
widget_id = widget["id"]
|
||||
created = response.json()
|
||||
assert created["widget_kind"] == "static"
|
||||
assert created["service_id"] is None
|
||||
assert created["config"]["text"] == "hello"
|
||||
|
||||
response = client.get("/api/widgets/instances")
|
||||
assert response.status_code == 200
|
||||
assert any(w["id"] == widget_id for w in response.json())
|
||||
listed = client.get("/api/widgets/instances").json()
|
||||
assert len(listed) == 1
|
||||
assert listed[0]["id"] == created["id"]
|
||||
|
||||
|
||||
def test_update_widget(client):
|
||||
def test_create_backups_widget(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={"widget_kind": "backups", "title": "Backups", "config": {}},
|
||||
)
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
def test_unknown_builtin_kind_rejected(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={"widget_kind": "bogus", "title": "x", "config": {}},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_credential_key_in_config_rejected(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={"widget_kind": "static", "title": "x", "config": {"api_key": "leak"}},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Service-bound widget CRUD
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_create_service_bound_widget(client):
|
||||
service = _make_grafana_service(client)
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Note",
|
||||
"config": {"text": "hello"},
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "link",
|
||||
"title": "Dashboard",
|
||||
"config": {"dashboard_uid": "overview"},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
response = client.put(
|
||||
f"/api/widgets/instances/{widget_id}",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Updated",
|
||||
"config": {"text": "world"},
|
||||
"enabled": False,
|
||||
"sort_order": 10,
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["title"] == "Updated"
|
||||
assert data["config"] == {"text": "world"}
|
||||
assert data["enabled"] is False
|
||||
assert data["sort_order"] == 10
|
||||
assert response.status_code == 201
|
||||
created = response.json()
|
||||
assert created["service_id"] == service["id"]
|
||||
assert created["widget_kind"] == "link"
|
||||
|
||||
|
||||
def test_delete_widget(client):
|
||||
def test_service_bound_widget_unknown_kind_rejected(client):
|
||||
service = _make_grafana_service(client)
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "To delete",
|
||||
"config": {"text": "bye"},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
response = client.delete(f"/api/widgets/instances/{widget_id}")
|
||||
assert response.status_code == 200
|
||||
|
||||
response = client.get("/api/widgets/instances")
|
||||
assert not any(w["id"] == widget_id for w in response.json())
|
||||
|
||||
|
||||
def test_unknown_widget_type_rejected(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "unknown",
|
||||
"title": "Bad",
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "metric",
|
||||
"title": "x",
|
||||
"config": {},
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_addon_id_mismatch_rejected(client):
|
||||
def test_service_bound_widget_service_not_found_rejected(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "grafana",
|
||||
"widget_type": "static",
|
||||
"title": "Bad",
|
||||
"config": {"text": "x"},
|
||||
"service_id": "missing",
|
||||
"widget_kind": "link",
|
||||
"title": "x",
|
||||
"config": {"dashboard_uid": "u"},
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_credential_key_rejected(client):
|
||||
def test_service_bound_widget_invalid_config_rejected(client):
|
||||
service = _make_grafana_service(client)
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Bad",
|
||||
"config": {"api_key": "secret123"},
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "link",
|
||||
"title": "x",
|
||||
"config": {"dashboard_uid": ""}, # empty still validates; use bad type
|
||||
},
|
||||
)
|
||||
# Empty string passes Pydantic; force a real failure with a bad type.
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "link",
|
||||
"title": "x",
|
||||
"config": {"dashboard_uid": 123},
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_update_nonexistent_widget(client):
|
||||
def test_update_and_delete_widget(client):
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={"widget_kind": "static", "title": "Note", "config": {"text": "a"}},
|
||||
).json()
|
||||
updated = client.put(
|
||||
f"/api/widgets/instances/{created['id']}",
|
||||
json={"widget_kind": "static", "title": "Note2", "config": {"text": "b"}},
|
||||
).json()
|
||||
assert updated["title"] == "Note2"
|
||||
|
||||
assert client.delete(f"/api/widgets/instances/{created['id']}").status_code == 200
|
||||
assert client.get("/api/widgets/instances").json() == []
|
||||
|
||||
|
||||
def test_update_nonexistent_returns_404(client):
|
||||
response = client.put(
|
||||
"/api/widgets/instances/does-not-exist",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Bad",
|
||||
"config": {"text": "x"},
|
||||
},
|
||||
"/api/widgets/instances/missing",
|
||||
json={"widget_kind": "static", "title": "x", "config": {}},
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_delete_nonexistent_widget(client):
|
||||
response = client.delete("/api/widgets/instances/does-not-exist")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_default_widgets_seeded(client):
|
||||
response = client.get("/api/widgets/instances")
|
||||
assert response.status_code == 200
|
||||
widgets = response.json()
|
||||
types = [w["widget_type"] for w in widgets]
|
||||
assert "jellyfin" in types
|
||||
assert "backups" in types
|
||||
|
||||
|
||||
def test_no_reseed_when_widgets_exist(tmp_path):
|
||||
db_path = tmp_path / "settings.sqlite"
|
||||
store = SettingsStore(db_path)
|
||||
store.ensure_defaults()
|
||||
widgets = store.list_widgets()
|
||||
assert len(widgets) == 2
|
||||
|
||||
store.delete_widget(widgets[0]["id"])
|
||||
store.ensure_defaults()
|
||||
|
||||
remaining = store.list_widgets()
|
||||
assert len(remaining) == 1
|
||||
|
||||
|
||||
def test_update_id_mismatch_returns_400(client):
|
||||
response = client.post(
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Note",
|
||||
"config": {"text": "hello"},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
json={"widget_kind": "static", "title": "x", "config": {}},
|
||||
).json()
|
||||
response = client.put(
|
||||
f"/api/widgets/instances/{widget_id}",
|
||||
json={
|
||||
"id": "different-id",
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Updated",
|
||||
"config": {"text": "world"},
|
||||
},
|
||||
f"/api/widgets/instances/{created['id']}",
|
||||
json={"id": "other", "widget_kind": "static", "title": "x", "config": {}},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_empty_title_rejected(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "",
|
||||
"config": {"text": "hello"},
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_config_type_error_rejected(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "grafana",
|
||||
"widget_type": "grafana-link",
|
||||
"title": "Grafana",
|
||||
"config": {"panel_id": "not-an-integer"},
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_list_instances_respects_sort_order(client):
|
||||
response = client.get("/api/widgets/instances")
|
||||
assert response.status_code == 200
|
||||
widgets = response.json()
|
||||
orders = [w["sort_order"] for w in widgets]
|
||||
assert orders == sorted(orders)
|
||||
|
||||
|
||||
def test_enabled_round_trip(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Toggle",
|
||||
"config": {"text": "x"},
|
||||
"enabled": False,
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
response = client.put(
|
||||
f"/api/widgets/instances/{widget_id}",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Toggle",
|
||||
"config": {"text": "x"},
|
||||
"enabled": True,
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["enabled"] is True
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data endpoint
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_fetch_static_widget_data(client):
|
||||
response = client.post(
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Note",
|
||||
"config": {"text": "hello world"},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
response = client.get(f"/api/widgets/instances/{widget_id}/data")
|
||||
json={"widget_kind": "static", "title": "Note", "config": {"text": "hello"}},
|
||||
).json()
|
||||
response = client.get(f"/api/widgets/instances/{created['id']}/data")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["widget_id"] == widget_id
|
||||
assert data["widget_type"] == "static"
|
||||
assert data["data"] == {"text": "hello world"}
|
||||
assert data["error"] is None
|
||||
assert isinstance(data["fetched_at"], int)
|
||||
body = response.json()
|
||||
assert body["data"]["text"] == "hello"
|
||||
assert body["error"] is None
|
||||
|
||||
|
||||
def test_fetch_grafana_widget_data(client):
|
||||
response = client.post(
|
||||
def test_fetch_backups_widget_data(client):
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "grafana",
|
||||
"widget_type": "grafana-link",
|
||||
"title": "Grafana",
|
||||
"config": {"dashboard_uid": "overview", "panel_id": 3},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
response = client.get(f"/api/widgets/instances/{widget_id}/data")
|
||||
json={"widget_kind": "backups", "title": "Backups", "config": {}},
|
||||
).json()
|
||||
response = client.get(f"/api/widgets/instances/{created['id']}/data")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["widget_type"] == "grafana-link"
|
||||
assert data["data"]["url"] == "http://grafana:3000/d/overview?viewPanel=3"
|
||||
assert "total_jobs" in response.json()["data"]
|
||||
|
||||
|
||||
def test_fetch_prometheus_widget_data(client):
|
||||
response = client.post(
|
||||
def test_fetch_grafana_link_widget_data(client):
|
||||
service = _make_grafana_service(client)
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "prometheus",
|
||||
"widget_type": "prometheus-metric",
|
||||
"title": "CPU",
|
||||
"config": {"promql": '100 - avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100'},
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "link",
|
||||
"title": "Dashboard",
|
||||
"config": {"dashboard_uid": "overview", "panel_id": 2},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
fake_payload = {"data": {"resultType": "scalar", "result": [1718900000, "42.5"]}}
|
||||
with patch("media_library_viewer_api.widgets.sources.requests.get") as mock_get:
|
||||
mock_response = MagicMock()
|
||||
mock_response.raise_for_status.return_value = None
|
||||
mock_response.json.return_value = fake_payload
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
response = client.get(f"/api/widgets/instances/{widget_id}/data")
|
||||
|
||||
).json()
|
||||
response = client.get(f"/api/widgets/instances/{created['id']}/data")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["widget_type"] == "prometheus-metric"
|
||||
assert data["data"]["result"]["resultType"] == "scalar"
|
||||
assert response.json()["data"]["url"] == "https://grafana.example.com/d/overview?viewPanel=2"
|
||||
|
||||
|
||||
def test_fetch_jellyfin_widget_data_error(client):
|
||||
response = client.post(
|
||||
def test_fetch_widget_service_not_found(client):
|
||||
service = _make_grafana_service(client)
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "jellyfin",
|
||||
"title": "Activity",
|
||||
"config": {"machine_id": ""},
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "link",
|
||||
"title": "x",
|
||||
"config": {"dashboard_uid": "u"},
|
||||
},
|
||||
).json()
|
||||
# Deleting the service cascade-deletes its widgets, so the widget is gone.
|
||||
client.delete(f"/api/services/instances/{service['id']}")
|
||||
assert client.get("/api/widgets/instances").json() == []
|
||||
assert client.get(f"/api/widgets/instances/{created['id']}/data").status_code == 404
|
||||
|
||||
|
||||
def test_fetch_widget_service_disabled(client):
|
||||
service = _make_grafana_service(client)
|
||||
created = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"service_id": service["id"],
|
||||
"widget_kind": "link",
|
||||
"title": "x",
|
||||
"config": {"dashboard_uid": "u"},
|
||||
},
|
||||
).json()
|
||||
client.put(
|
||||
f"/api/services/instances/{service['id']}",
|
||||
json={
|
||||
"service_type": "grafana",
|
||||
"name": service["name"],
|
||||
"config": {"base_url": "https://grafana.example.com"},
|
||||
"enabled": False,
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
|
||||
response = client.get(f"/api/widgets/instances/{widget_id}/data")
|
||||
response = client.get(f"/api/widgets/instances/{created['id']}/data")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["widget_type"] == "jellyfin"
|
||||
assert data["data"] is None
|
||||
assert data["error"] is not None
|
||||
assert "Jellyfin" in data["error"] or "machine" in data["error"].lower()
|
||||
assert "disabled" in response.json()["error"]
|
||||
|
||||
|
||||
def test_fetch_widget_data_not_found(client):
|
||||
response = client.get("/api/widgets/instances/does-not-exist/data")
|
||||
assert response.status_code == 404
|
||||
def test_fetch_widget_not_found(client):
|
||||
assert client.get("/api/widgets/instances/missing/data").status_code == 404
|
||||
|
||||
|
||||
def test_fetch_widget_data_unhandled_exception_returns_500(client):
|
||||
response = client.post(
|
||||
"/api/widgets/instances",
|
||||
json={
|
||||
"addon_id": "core",
|
||||
"widget_type": "static",
|
||||
"title": "Note",
|
||||
"config": {"text": "x"},
|
||||
},
|
||||
)
|
||||
widget_id = response.json()["id"]
|
||||
# ---------------------------------------------------------------------------
|
||||
# Adapter unit tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class _ExplodingAdapter:
|
||||
source_type = "static"
|
||||
|
||||
async def fetch(self, config):
|
||||
raise RuntimeError("boom")
|
||||
@pytest.mark.asyncio
|
||||
async def test_grafana_adapter_builds_url():
|
||||
adapter = GrafanaWidgetSource()
|
||||
service = ServiceRecord(id="s", service_type="grafana", name="g", config={"base_url": "http://g:3000"})
|
||||
result = await adapter.fetch(service, "link", {"dashboard_uid": "ov"})
|
||||
assert result["url"] == "http://g:3000/d/ov"
|
||||
result = await adapter.fetch(service, "link", {"dashboard_uid": "ov", "panel_id": 4})
|
||||
assert result["url"] == "http://g:3000/d/ov?viewPanel=4"
|
||||
|
||||
with patch("media_library_viewer_api.routers.widgets.get_source_adapter", return_value=_ExplodingAdapter()):
|
||||
response = client.get(f"/api/widgets/instances/{widget_id}/data")
|
||||
|
||||
assert response.status_code == 500
|
||||
@pytest.mark.asyncio
|
||||
async def test_grafana_adapter_missing_service():
|
||||
adapter = GrafanaWidgetSource()
|
||||
result = await adapter.fetch(None, "link", {"dashboard_uid": "ov"})
|
||||
assert "error" in result
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_static_adapter():
|
||||
adapter = StaticWidgetSource()
|
||||
result = await adapter.fetch({"text": "hello"})
|
||||
assert result == {"text": "hello"}
|
||||
result = await adapter.fetch(None, "static", {"text": "hi"})
|
||||
assert result == {"text": "hi"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_grafana_adapter():
|
||||
adapter = GrafanaWidgetSource()
|
||||
result = await adapter.fetch({"dashboard_uid": "overview", "panel_id": 2})
|
||||
assert result["url"] == "http://grafana:3000/d/overview?viewPanel=2"
|
||||
|
||||
result = await adapter.fetch({"dashboard_uid": "overview"})
|
||||
assert result["url"] == "http://grafana:3000/d/overview"
|
||||
async def test_backups_adapter(client):
|
||||
store = app.dependency_overrides[get_settings_store]()
|
||||
with patch("media_library_viewer_api.widgets.sources.get_settings_store", return_value=store):
|
||||
adapter = BackupsWidgetSource()
|
||||
result = await adapter.fetch(None, "backups", {})
|
||||
assert "total_jobs" in result
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ssh_task_adapter_timeout(tmp_path):
|
||||
store = SettingsStore(tmp_path / "settings.sqlite")
|
||||
store.ensure_defaults()
|
||||
|
||||
# Create a local machine and a simple shell task.
|
||||
machine = store.list_machines()[0]
|
||||
task = store.upsert_task(
|
||||
{
|
||||
"name": "slow-task",
|
||||
"task_type": "shell",
|
||||
"content": "echo hello",
|
||||
"enabled": True,
|
||||
"default_machine_id": machine["id"],
|
||||
}
|
||||
)
|
||||
async def test_ssh_task_adapter_missing_service():
|
||||
from media_library_viewer_api.widgets.sources import SshTaskWidgetSource
|
||||
|
||||
adapter = SshTaskWidgetSource()
|
||||
with patch(
|
||||
"media_library_viewer_api.widgets.sources.get_settings_store",
|
||||
return_value=store,
|
||||
), patch(
|
||||
"media_library_viewer_api.widgets.sources.asyncio.wait_for",
|
||||
side_effect=asyncio.TimeoutError,
|
||||
):
|
||||
result = await adapter.fetch({"task_id": task["id"]})
|
||||
|
||||
result = await adapter.fetch(None, "task_output", {"task_id": "t1"})
|
||||
assert "error" in result
|
||||
assert "timed out" in result["error"].lower()
|
||||
|
||||
|
||||
def test_source_registry_closed():
|
||||
assert set(SOURCE_REGISTRY.keys()) == {
|
||||
"jellyfin",
|
||||
"backups",
|
||||
"grafana",
|
||||
"prometheus",
|
||||
"ssh_task",
|
||||
"static",
|
||||
}
|
||||
@pytest.mark.asyncio
|
||||
async def test_ssh_task_adapter_records_history_on_run(client):
|
||||
store = app.dependency_overrides[get_settings_store]()
|
||||
# Save a task and an ssh_tasks service instance.
|
||||
task = store.upsert_task(
|
||||
{
|
||||
"name": "echo",
|
||||
"task_type": "shell",
|
||||
"content": "echo hi",
|
||||
"enabled": True,
|
||||
"default_machine_id": "",
|
||||
}
|
||||
)
|
||||
service = store.upsert_service(
|
||||
{"service_type": "ssh_tasks", "name": "box", "config": {"host": "h", "username": "u"}, "enabled": True}
|
||||
)
|
||||
|
||||
fake_result = SimpleNamespace(exit_status=0, stdout="hi\n", stderr="")
|
||||
fake_client = SimpleNamespace(run=lambda *a, **k: fake_result)
|
||||
|
||||
from media_library_viewer_api.widgets.sources import SshTaskWidgetSource
|
||||
|
||||
adapter = SshTaskWidgetSource()
|
||||
service_record = ServiceRecord(
|
||||
id=service["id"], service_type="ssh_tasks", name="box", config={"host": "h", "username": "u"}
|
||||
)
|
||||
with patch("media_library_viewer_api.widgets.sources.get_settings_store", return_value=store), patch(
|
||||
"media_library_viewer_api.widgets.sources._build_ssh_client", return_value=fake_client
|
||||
):
|
||||
result = await adapter.fetch(service_record, "task_output", {"task_id": task["id"]})
|
||||
|
||||
assert result["exit_status"] == 0
|
||||
runs = store.list_service_task_runs(service_id=service["id"])
|
||||
assert len(runs) == 1
|
||||
assert runs[0]["status"] == "success"
|
||||
|
||||
Reference in New Issue
Block a user