forked from retoor/devplacepy
Admin pages: one shared specification, builders, fragments, caps and live refresh
Adds admin_sections registry, shared admin partials, WorkspaceList, admin CLAUDE.md spec, capped node/flag listings, and api/unit tests enforcing the spec. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,3 +39,34 @@ def test_admin_ai_usage_data_returns_json(app_server, seeded_db):
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert isinstance(data, dict)
|
||||
|
||||
|
||||
def test_admin_ai_usage_first_response_is_rendered(app_server, seeded_db):
|
||||
admin = _admin_aiusage(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/ai-usage")
|
||||
assert r.status_code == 200
|
||||
assert "Loading metrics" not in r.text
|
||||
assert 'id="ai-usage-root"' in r.text
|
||||
assert "Window:" in r.text
|
||||
|
||||
|
||||
def test_admin_ai_usage_window_param_is_honored_and_clamped(app_server, seeded_db):
|
||||
admin = _admin_aiusage(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/ai-usage/data?hours=999999&top_n=999", headers=JSON_aiusage)
|
||||
assert r.status_code == 200
|
||||
assert r.json()["window_hours"] <= 168
|
||||
page = admin.get(f"{BASE_URL}/admin/ai-usage?hours=1")
|
||||
assert '<option value="1" selected>' in page.text
|
||||
|
||||
|
||||
def test_admin_ai_usage_fragment_is_html(app_server, seeded_db):
|
||||
admin = _admin_aiusage(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/ai-usage/fragment?hours=24")
|
||||
assert r.status_code == 200
|
||||
assert "text/html" in r.headers["content-type"]
|
||||
assert "Window:" in r.text
|
||||
|
||||
|
||||
def test_admin_ai_usage_fragment_requires_admin(app_server):
|
||||
r = requests.get(f"{BASE_URL}/admin/ai-usage/fragment", allow_redirects=False)
|
||||
assert r.status_code in (302, 303, 401, 403)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from devplacepy.services.backup import store
|
||||
|
||||
HTML = {"Accept": "text/html"}
|
||||
JSON = {"Accept": "application/json"}
|
||||
|
||||
|
||||
def _admin(seeded_db):
|
||||
refresh_snapshot()
|
||||
key = get_table("users").find_one(username="alice_test")["api_key"]
|
||||
session = requests.Session()
|
||||
session.headers.update({"X-API-KEY": key})
|
||||
return session
|
||||
|
||||
|
||||
def test_backups_page_is_server_rendered(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
schedule_uid = store.create_schedule(
|
||||
name="Pages schedule",
|
||||
target="database",
|
||||
kind="interval",
|
||||
every_seconds=3600,
|
||||
cron="",
|
||||
keep_last=3,
|
||||
created_by="pages-test",
|
||||
next_run_at="",
|
||||
)
|
||||
backup_uid = store.create_backup(
|
||||
target="database", created_by="pages-test", job_uid="pagesjob"
|
||||
)
|
||||
refresh_snapshot()
|
||||
r = admin.get(f"{BASE_URL}/admin/backups", headers=HTML)
|
||||
assert r.status_code == 200
|
||||
assert "Loading" not in r.text
|
||||
assert 'data-live="storage"' in r.text
|
||||
assert 'data-live="backups"' in r.text
|
||||
assert 'data-live="schedules"' in r.text
|
||||
assert f'data-uid="{backup_uid}"' in r.text
|
||||
assert f'data-uid="{schedule_uid}"' in r.text
|
||||
assert "Pages schedule" in r.text
|
||||
assert "<time" in r.text
|
||||
|
||||
|
||||
def test_backups_data_matches_page_json(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
page = admin.get(f"{BASE_URL}/admin/backups", headers=JSON).json()
|
||||
data = admin.get(f"{BASE_URL}/admin/backups/data", headers=JSON).json()
|
||||
assert {b["uid"] for b in page["backups"]} == {b["uid"] for b in data["backups"]}
|
||||
assert page["backups_total"] >= len(page["backups"])
|
||||
assert data["backups_total"] >= len(data["backups"])
|
||||
@@ -91,6 +91,7 @@ def test_add_learns_and_shows_detail(client, admin, fake_world):
|
||||
listing = client.get("/admin/communities", headers=admin["headers"])
|
||||
assert listing.status_code == 200
|
||||
assert listing.json()["items"][0]["slug"] == "forum-test"
|
||||
assert listing.json()["pagination"]["total"] >= 1
|
||||
detail = client.get("/admin/communities/forum-test", headers=admin["headers"])
|
||||
assert detail.status_code == 200
|
||||
body = detail.json()
|
||||
|
||||
@@ -16,3 +16,37 @@ def test_containers_admin_data_denied_for_non_admin(app_server, seeded_db):
|
||||
)
|
||||
r = s.get(f"{BASE_URL}/admin/containers/data", allow_redirects=False)
|
||||
assert r.status_code in (302, 303, 403)
|
||||
|
||||
|
||||
def test_containers_admin_data_is_paginated(app_server, seeded_db):
|
||||
from devplacepy.database import refresh_snapshot
|
||||
from devplacepy.services.containers import store
|
||||
|
||||
refresh_snapshot()
|
||||
admin = get_table("users").find_one(username=seeded_db["alice"]["username"])
|
||||
uids = []
|
||||
try:
|
||||
for index in range(27):
|
||||
row = store.create_instance(
|
||||
{
|
||||
"project_uid": "proj-admin-page",
|
||||
"name": f"cm-page-{index}",
|
||||
"status": "stopped",
|
||||
"desired_state": "stopped",
|
||||
}
|
||||
)
|
||||
uids.append(row["uid"])
|
||||
headers = {"Accept": "application/json", "X-API-KEY": admin["api_key"]}
|
||||
first = requests.get(f"{BASE_URL}/admin/containers/data", headers=headers).json()
|
||||
assert first["pagination"]["per_page"] == 25
|
||||
assert len(first["instances"]) <= 25
|
||||
second = requests.get(
|
||||
f"{BASE_URL}/admin/containers/data", headers=headers, params={"page": 2}
|
||||
).json()
|
||||
assert not {i["uid"] for i in first["instances"]} & {
|
||||
i["uid"] for i in second["instances"]
|
||||
}
|
||||
finally:
|
||||
table = get_table("instances")
|
||||
for uid in uids:
|
||||
table.delete(uid=uid)
|
||||
|
||||
@@ -182,3 +182,26 @@ def test_devii_tasks_shows_the_owner_24h_usage(seeded_db):
|
||||
assert entry["quota"]["runs_used"] >= 1
|
||||
assert entry["quota"]["runs_limit"] == 100
|
||||
assert entry["quota"]["creates_used"] >= 1
|
||||
|
||||
|
||||
def test_devii_tasks_list_is_paginated(seeded_db):
|
||||
owner = _admin_uid(seeded_db)
|
||||
uids = [_seed_task(owner) for _ in range(27)]
|
||||
admin = _admin_dt(seeded_db)
|
||||
first = admin.get(f"{BASE_URL}/admin/devii-tasks", headers=JSON_dt).json()
|
||||
assert first["pagination"]["per_page"] == 25
|
||||
assert first["pagination"]["total"] >= 27
|
||||
assert len(first["items"]) == 25
|
||||
second = admin.get(
|
||||
f"{BASE_URL}/admin/devii-tasks", headers=JSON_dt, params={"page": 2}
|
||||
).json()
|
||||
assert second["items"]
|
||||
assert not {i["uid"] for i in first["items"]} & {i["uid"] for i in second["items"]}
|
||||
assert uids
|
||||
|
||||
|
||||
def test_devii_tasks_page_is_server_rendered_with_admin_empty_classes(seeded_db):
|
||||
admin = _admin_dt(seeded_db)
|
||||
html = admin.get(f"{BASE_URL}/admin/devii-tasks").text
|
||||
assert "empty-state" not in html
|
||||
assert 'class="admin-table-wrap"' in html
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import re
|
||||
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
|
||||
JSON = {"Accept": "application/json"}
|
||||
HTML = {"Accept": "text/html"}
|
||||
|
||||
|
||||
def _admin(seeded_db):
|
||||
refresh_snapshot()
|
||||
key = get_table("users").find_one(username="alice_test")["api_key"]
|
||||
session = requests.Session()
|
||||
session.headers.update({"X-API-KEY": key})
|
||||
return session
|
||||
|
||||
|
||||
def test_game_admin_json_face(seeded_db):
|
||||
r = _admin(seeded_db).get(f"{BASE_URL}/admin/game", headers=JSON)
|
||||
assert r.status_code == 200, r.text[:300]
|
||||
assert "era_active" in r.json()
|
||||
|
||||
|
||||
def test_game_admin_uses_the_admin_vocabulary(seeded_db):
|
||||
r = _admin(seeded_db).get(f"{BASE_URL}/admin/game", headers=HTML)
|
||||
assert r.status_code == 200
|
||||
assert "admin-btn" in r.text
|
||||
content = r.text.split('<main class="admin-content">', 1)[1].split("</main>", 1)[0]
|
||||
tokens = {name for value in re.findall(r'class="([^"]*)"', content) for name in value.split()}
|
||||
assert not tokens & {"btn", "btn-primary", "btn-danger", "hint-text"}
|
||||
@@ -0,0 +1,251 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import http.server
|
||||
import json
|
||||
import socket
|
||||
import socketserver
|
||||
import threading
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from devplacepy.database.settings import internal_gateway_key
|
||||
from tests.api.admin.gateway.index import (
|
||||
_unique_gateway,
|
||||
admin_session,
|
||||
)
|
||||
from tests.conftest import BASE_URL
|
||||
|
||||
CHAT_URL = f"{BASE_URL}/openai/v1/chat/completions"
|
||||
NOTICE_MARKER = "tripped its circuit breaker"
|
||||
SYNC_SECONDS = 1.5
|
||||
NOTICE_WAIT_SECONDS = 10
|
||||
OK_BODY = {
|
||||
"id": "x",
|
||||
"model": "m",
|
||||
"choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}],
|
||||
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
||||
}
|
||||
|
||||
|
||||
class _Upstream:
|
||||
def __init__(self, status, payload):
|
||||
self.calls = []
|
||||
calls = self.calls
|
||||
body = json.dumps(payload).encode()
|
||||
code = status
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
calls.append(json.loads(self.rfile.read(length) or b"{}"))
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, *args):
|
||||
pass
|
||||
|
||||
sock = socket.socket()
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
self.port = sock.getsockname()[1]
|
||||
sock.close()
|
||||
self.httpd = socketserver.ThreadingTCPServer(("127.0.0.1", self.port), Handler)
|
||||
threading.Thread(target=self.httpd.serve_forever, daemon=True).start()
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
return f"127.0.0.1:{self.port}"
|
||||
|
||||
def close(self):
|
||||
self.httpd.shutdown()
|
||||
self.httpd.server_close()
|
||||
|
||||
|
||||
def _internal_headers():
|
||||
refresh_snapshot()
|
||||
return {"Authorization": f"Bearer {internal_gateway_key()}", "Content-Type": "application/json"}
|
||||
|
||||
|
||||
def _route(admin, upstream, prefix, target=None):
|
||||
provider = _unique_gateway(f"{prefix}p").lower()
|
||||
source = _unique_gateway(f"{prefix}m").lower()
|
||||
target = target or f"vendor/{prefix}"
|
||||
admin.post(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
json={"name": provider, "base_url": f"http://{upstream.host}/v1/chat/completions", "api_key": "sk-test"},
|
||||
)
|
||||
created = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={"source_model": source, "provider": provider, "target_model": target, "kind": "chat"},
|
||||
)
|
||||
assert created.status_code == 200, created.text[:300]
|
||||
return provider, source
|
||||
|
||||
|
||||
def _cleanup(admin, provider, source):
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/models/{source}")
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/providers/{provider}")
|
||||
|
||||
|
||||
def _chat(source):
|
||||
return requests.post(
|
||||
CHAT_URL,
|
||||
headers=_internal_headers(),
|
||||
json={"model": source, "messages": [{"role": "user", "content": "hi"}]},
|
||||
timeout=60,
|
||||
)
|
||||
|
||||
|
||||
def _admin_notices(host):
|
||||
refresh_snapshot()
|
||||
rows = get_table("notifications").find(type="system")
|
||||
return [r for r in rows if NOTICE_MARKER in (r.get("message") or "") and host in r["message"]]
|
||||
|
||||
|
||||
def _wait_for_notices(host):
|
||||
deadline = time.time() + NOTICE_WAIT_SECONDS
|
||||
while time.time() < deadline:
|
||||
found = _admin_notices(host)
|
||||
if found:
|
||||
return found
|
||||
time.sleep(0.5)
|
||||
return []
|
||||
|
||||
|
||||
def test_first_upstream_error_opens_the_target_and_the_second_call_never_leaves(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
upstream = _Upstream(500, {"error": {"message": "provider exploded"}})
|
||||
provider, source = _route(admin, upstream, "trip")
|
||||
try:
|
||||
first = _chat(source)
|
||||
assert first.status_code >= 500
|
||||
sent_after_first = len(upstream.calls)
|
||||
assert sent_after_first >= 1
|
||||
second = _chat(source)
|
||||
assert second.status_code == 503
|
||||
assert second.json()["error"]["type"] == "circuit_open"
|
||||
assert len(upstream.calls) == sent_after_first
|
||||
finally:
|
||||
_cleanup(admin, provider, source)
|
||||
upstream.close()
|
||||
|
||||
|
||||
def test_every_admin_gets_one_detailed_notification_per_incident(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
upstream = _Upstream(500, {"error": {"message": "provider exploded"}})
|
||||
provider, source = _route(admin, upstream, "notify")
|
||||
try:
|
||||
_chat(source)
|
||||
_chat(source)
|
||||
_chat(source)
|
||||
notices = _wait_for_notices(upstream.host)
|
||||
assert notices, "no system notification reached the admins"
|
||||
admins = {u["uid"] for u in get_table("users").find(role="Admin")}
|
||||
per_admin = {}
|
||||
for row in notices:
|
||||
per_admin[row["user_uid"]] = per_admin.get(row["user_uid"], 0) + 1
|
||||
assert set(per_admin) == admins
|
||||
assert set(per_admin.values()) == {1}
|
||||
message = notices[0]["message"]
|
||||
assert "vendor/notify" in message
|
||||
assert "Error class: upstream_error" in message
|
||||
assert "HTTP status: 500" in message
|
||||
assert "provider exploded" in message
|
||||
assert "sk-test" not in message
|
||||
assert notices[0]["target_url"] == "/admin/services/openai"
|
||||
finally:
|
||||
_cleanup(admin, provider, source)
|
||||
upstream.close()
|
||||
|
||||
|
||||
def test_a_failing_target_does_not_block_a_healthy_one(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
bad = _Upstream(500, {"error": {"message": "down"}})
|
||||
good = _Upstream(200, OK_BODY)
|
||||
bad_provider, bad_source = _route(admin, bad, "isobad")
|
||||
good_provider, good_source = _route(admin, good, "isogood")
|
||||
try:
|
||||
_chat(bad_source)
|
||||
assert _chat(bad_source).status_code == 503
|
||||
healthy = _chat(good_source)
|
||||
assert healthy.status_code == 200
|
||||
assert healthy.json()["choices"][0]["message"]["content"] == "ok"
|
||||
assert len(good.calls) == 1
|
||||
finally:
|
||||
_cleanup(admin, bad_provider, bad_source)
|
||||
_cleanup(admin, good_provider, good_source)
|
||||
bad.close()
|
||||
good.close()
|
||||
|
||||
|
||||
def test_a_malformed_request_trips_once_and_is_not_resent(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
upstream = _Upstream(400, {"error": {"message": "bad schema"}})
|
||||
provider, source = _route(admin, upstream, "badreq")
|
||||
try:
|
||||
first = _chat(source)
|
||||
assert first.status_code == 400
|
||||
sent = len(upstream.calls)
|
||||
assert sent == 1
|
||||
assert _chat(source).status_code == 503
|
||||
assert len(upstream.calls) == sent
|
||||
finally:
|
||||
_cleanup(admin, provider, source)
|
||||
upstream.close()
|
||||
|
||||
|
||||
def test_a_shape_fixable_400_does_not_open_the_target(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
upstream = _Upstream(
|
||||
400, {"issues": [{"code": "unrecognized_keys", "keys": ["thinking"]}]}
|
||||
)
|
||||
provider, source = _route(admin, upstream, "shape")
|
||||
try:
|
||||
_chat(source)
|
||||
sent = len(upstream.calls)
|
||||
assert sent >= 2
|
||||
again = _chat(source)
|
||||
assert again.status_code == 400
|
||||
assert len(upstream.calls) > sent
|
||||
finally:
|
||||
_cleanup(admin, provider, source)
|
||||
upstream.close()
|
||||
|
||||
|
||||
def test_the_breaker_is_per_model_not_per_upstream(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
first = _Upstream(500, {"error": {"message": "down"}})
|
||||
second = _Upstream(200, OK_BODY)
|
||||
model = _unique_gateway("vendor/shared").lower()
|
||||
first_provider, first_source = _route(admin, first, "permodela", target=model)
|
||||
second_provider, second_source = _route(admin, second, "permodelb", target=model)
|
||||
try:
|
||||
_chat(first_source)
|
||||
blocked = _chat(second_source)
|
||||
assert blocked.status_code == 503
|
||||
assert blocked.json()["error"]["type"] == "circuit_open"
|
||||
assert second.calls == []
|
||||
finally:
|
||||
_cleanup(admin, first_provider, first_source)
|
||||
_cleanup(admin, second_provider, second_source)
|
||||
first.close()
|
||||
second.close()
|
||||
|
||||
|
||||
def test_a_healthy_target_stays_closed_and_raises_no_notification(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
upstream = _Upstream(200, OK_BODY)
|
||||
provider, source = _route(admin, upstream, "calm")
|
||||
try:
|
||||
for _ in range(3):
|
||||
assert _chat(source).status_code == 200
|
||||
assert len(upstream.calls) == 3
|
||||
time.sleep(SYNC_SECONDS)
|
||||
assert _admin_notices(upstream.host) == []
|
||||
finally:
|
||||
_cleanup(admin, provider, source)
|
||||
upstream.close()
|
||||
@@ -80,3 +80,54 @@ def test_stats_model_detail_for_an_unknown_model(seeded_db):
|
||||
data = response.json()
|
||||
assert data["summary"]["total_requests"] == 0
|
||||
assert data["health"] is None
|
||||
|
||||
|
||||
def test_stats_tab_first_response_is_rendered(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
response = admin.get(
|
||||
f"{BASE_URL}/admin/gateway?tab=stats", headers={"Accept": "text/html"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "Loading..." not in response.text
|
||||
assert 'id="gw-stats-initial"' in response.text
|
||||
assert 'data-slot="tiles"' in response.text
|
||||
assert "/vendor/chartjs/chart.umd.min.js" in response.text
|
||||
assert "js/vendor/chart.js" not in response.text
|
||||
|
||||
|
||||
def test_stats_tab_honors_range_param(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
response = admin.get(
|
||||
f"{BASE_URL}/admin/gateway?tab=stats&range=7d", headers={"Accept": "text/html"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert '<option value="7d" selected>' in response.text
|
||||
|
||||
|
||||
def test_stats_tab_falls_back_on_unknown_range(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
response = admin.get(
|
||||
f"{BASE_URL}/admin/gateway?tab=stats&range=nonsense", headers={"Accept": "text/html"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert '<option value="24h" selected>' in response.text
|
||||
|
||||
|
||||
def test_stats_fragment_renders_slots(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
response = admin.get(f"{BASE_URL}/admin/gateway/stats/fragment?range=24h")
|
||||
assert response.status_code == 200
|
||||
for slot in ("tiles", "models", "failures", "note"):
|
||||
assert f'data-slot="{slot}"' in response.text
|
||||
assert 'id="gw-stats-initial"' in response.text
|
||||
|
||||
|
||||
def test_stats_fragment_rejects_unknown_range(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
response = admin.get(f"{BASE_URL}/admin/gateway/stats/fragment?range=nonsense")
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_stats_fragment_requires_admin(seeded_db):
|
||||
response = requests.get(f"{BASE_URL}/admin/gateway/stats/fragment", allow_redirects=False)
|
||||
assert response.status_code in (302, 303, 401, 403)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
|
||||
JSON = {"Accept": "application/json"}
|
||||
HTML = {"Accept": "text/html"}
|
||||
|
||||
|
||||
def _admin(seeded_db):
|
||||
refresh_snapshot()
|
||||
key = get_table("users").find_one(username="alice_test")["api_key"]
|
||||
session = requests.Session()
|
||||
session.headers.update({"X-API-KEY": key})
|
||||
return session
|
||||
|
||||
|
||||
def test_planning_page_json_face(seeded_db):
|
||||
r = _admin(seeded_db).get(f"{BASE_URL}/admin/issues/planning", headers=JSON)
|
||||
assert r.status_code == 200, r.text[:300]
|
||||
body = r.json()
|
||||
assert "configured" in body
|
||||
assert isinstance(body["tickets"], list)
|
||||
|
||||
|
||||
def test_planning_page_uses_the_admin_vocabulary(seeded_db):
|
||||
r = _admin(seeded_db).get(f"{BASE_URL}/admin/issues/planning", headers=HTML)
|
||||
assert r.status_code == 200
|
||||
assert "<h1" not in r.text.split('class="admin-content"', 1)[1]
|
||||
assert "empty-state" not in r.text
|
||||
assert "admin-header" not in r.text
|
||||
assert 'aria-current="page"' in r.text
|
||||
@@ -34,6 +34,8 @@ def test_admin_overview_shape(client, admin):
|
||||
listed = {row["uid"]: row for row in body["devices"]}
|
||||
assert listed[device["uid"]]["owner_username"] == owner["username"]
|
||||
assert len(body["devices"]) <= 100
|
||||
assert body["devices_total"] >= len(body["devices"])
|
||||
assert body["flags_total"] >= len(body["flags"])
|
||||
assert "secret_hash" not in listed[device["uid"]]
|
||||
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ def _all_tool_names():
|
||||
|
||||
def test_devii_service_page_has_tools_tab(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/services/devii")
|
||||
r = admin.get(f"{BASE_URL}/admin/services/devii", headers={"Accept": "text/html"})
|
||||
assert r.status_code == 200
|
||||
assert 'data-tab="tools"' in r.text
|
||||
assert "devii-tools-form" in r.text
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
|
||||
JSON = {"Accept": "application/json"}
|
||||
HTML = {"Accept": "text/html"}
|
||||
|
||||
|
||||
def _admin(seeded_db):
|
||||
refresh_snapshot()
|
||||
key = get_table("users").find_one(username="alice_test")["api_key"]
|
||||
session = requests.Session()
|
||||
session.headers.update({"X-API-KEY": key})
|
||||
return session
|
||||
|
||||
|
||||
def test_services_page_json_face(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/services", headers=JSON)
|
||||
assert r.status_code == 200, r.text[:300]
|
||||
body = r.json()
|
||||
names = {svc["name"] for svc in body["services"]}
|
||||
assert "seo_meta" in names
|
||||
assert body["services_total"] >= len(body["services"])
|
||||
|
||||
|
||||
def test_services_page_is_server_rendered(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/services", headers=HTML)
|
||||
assert r.status_code == 200
|
||||
assert 'data-service="seo_meta"' in r.text
|
||||
assert "Loading" not in r.text
|
||||
content = r.text.split('<main class="admin-content">', 1)[1].split("</main>", 1)[0]
|
||||
assert "btn-primary" not in content.replace("admin-btn-primary", "")
|
||||
|
||||
|
||||
def test_services_data_matches_page_json(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
page = admin.get(f"{BASE_URL}/admin/services", headers=JSON).json()["services"]
|
||||
data = admin.get(f"{BASE_URL}/admin/services/data", headers=JSON).json()["services"]
|
||||
assert {s["name"] for s in page} == {s["name"] for s in data}
|
||||
|
||||
|
||||
def test_service_detail_json_face(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/services/seo_meta", headers=JSON)
|
||||
assert r.status_code == 200, r.text[:300]
|
||||
assert r.json()["service"]["name"] == "seo_meta"
|
||||
|
||||
|
||||
def test_service_detail_renders_metrics_on_the_server(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/services/seo_meta", headers=HTML)
|
||||
assert r.status_code == 200
|
||||
assert 'class="metric-card"' in r.text
|
||||
assert "service-btn" not in r.text
|
||||
assert "hint-text" not in r.text
|
||||
|
||||
|
||||
def test_service_detail_unknown_is_404(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/services/does_not_exist", headers=JSON)
|
||||
assert r.status_code == 404
|
||||
@@ -0,0 +1,57 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import re
|
||||
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
|
||||
HTML = {"Accept": "text/html"}
|
||||
JSON = {"Accept": "application/json"}
|
||||
|
||||
|
||||
def _admin(seeded_db):
|
||||
refresh_snapshot()
|
||||
key = get_table("users").find_one(username="alice_test")["api_key"]
|
||||
session = requests.Session()
|
||||
session.headers.update({"X-API-KEY": key})
|
||||
return session
|
||||
|
||||
|
||||
def test_statistics_page_is_server_rendered(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/statistics", headers=HTML)
|
||||
assert r.status_code == 200
|
||||
assert "Loading" not in r.text
|
||||
assert 'class="metric-card"' in r.text
|
||||
assert 'id="statistics-initial"' in r.text
|
||||
assert "<time" in r.text
|
||||
|
||||
|
||||
def test_statistics_tab_and_window_navigate(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
r = admin.get(f"{BASE_URL}/admin/statistics?tab=members&hours=24&compare=0", headers=HTML)
|
||||
assert r.status_code == 200, re.sub(r"\s+", " ", re.sub(r"<(script|style).*?</\1>", "", r.text, flags=re.S))[-500:]
|
||||
assert 'name="tab" value="members"' in r.text
|
||||
assert '<option value="24" selected>' in r.text
|
||||
assert '<option value="0" selected>No comparison</option>' in r.text
|
||||
|
||||
|
||||
def test_statistics_data_matches_page_json(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
page = admin.get(f"{BASE_URL}/admin/statistics?tab=content", headers=JSON).json()
|
||||
data = admin.get(f"{BASE_URL}/admin/statistics/data?tab=content", headers=JSON).json()
|
||||
assert page["initial"]["tab"] == data["tab"] == "content"
|
||||
assert [c["key"] for c in page["initial"]["cards"]] == [c["key"] for c in data["cards"]]
|
||||
assert all(c["display"] for c in data["cards"])
|
||||
|
||||
|
||||
def test_statistics_params_are_clamped(seeded_db):
|
||||
admin = _admin(seeded_db)
|
||||
data = admin.get(
|
||||
f"{BASE_URL}/admin/statistics/data?tab=overview&hours=99999999&top_n=100000",
|
||||
headers=JSON,
|
||||
).json()
|
||||
assert data["window_hours"] == 2160
|
||||
unknown = admin.get(f"{BASE_URL}/admin/statistics/data?tab=nope", headers=JSON).json()
|
||||
assert unknown["tab"] == "overview"
|
||||
@@ -0,0 +1,102 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
|
||||
from devplacepy.database import get_table
|
||||
from devplacepy.services.containers import store
|
||||
from tests.conftest import BASE_URL
|
||||
|
||||
HTTP_TIMEOUT = 30
|
||||
JSON = {"Accept": "application/json"}
|
||||
PAGE_SIZE = 25
|
||||
|
||||
|
||||
def _user(username: str) -> dict:
|
||||
from devplacepy.database import refresh_snapshot
|
||||
|
||||
refresh_snapshot()
|
||||
return get_table("users").find_one(username=username)
|
||||
|
||||
|
||||
def _headers(user: dict) -> dict:
|
||||
return {**JSON, "X-API-KEY": user["api_key"]}
|
||||
|
||||
|
||||
def _seed(count: int, owner_uid: str) -> list[str]:
|
||||
uids = []
|
||||
for index in range(count):
|
||||
row = store.create_instance(
|
||||
{
|
||||
"project_uid": "proj-admin-ws-page",
|
||||
"name": f"ws-page-{index}",
|
||||
"status": "stopped",
|
||||
"desired_state": "stopped",
|
||||
"is_workspace": 1,
|
||||
"workspace_owner_uid": owner_uid,
|
||||
}
|
||||
)
|
||||
uids.append(row["uid"])
|
||||
return uids
|
||||
|
||||
|
||||
def _drop(uids: list[str]) -> None:
|
||||
table = get_table("instances")
|
||||
for uid in uids:
|
||||
table.delete(uid=uid)
|
||||
|
||||
|
||||
def test_workspaces_data_is_refused_for_a_member(app_server, seeded_db):
|
||||
member = _user("bob_test")
|
||||
response = requests.get(
|
||||
f"{BASE_URL}/admin/workspaces/data",
|
||||
headers=_headers(member),
|
||||
timeout=HTTP_TIMEOUT,
|
||||
allow_redirects=False,
|
||||
)
|
||||
assert response.status_code in (302, 303, 401, 403)
|
||||
|
||||
|
||||
def test_workspaces_data_is_paginated_and_matches_the_page(app_server, seeded_db):
|
||||
admin = _user("alice_test")
|
||||
owner = _user("bob_test")
|
||||
uids = _seed(PAGE_SIZE + 2, owner["uid"])
|
||||
try:
|
||||
first = requests.get(
|
||||
f"{BASE_URL}/admin/workspaces/data",
|
||||
headers=_headers(admin),
|
||||
timeout=HTTP_TIMEOUT,
|
||||
).json()
|
||||
assert {"workspaces", "flags", "flags_total", "pagination"} <= set(first)
|
||||
assert first["pagination"]["per_page"] == PAGE_SIZE
|
||||
assert first["pagination"]["total"] >= PAGE_SIZE + 2
|
||||
assert len(first["workspaces"]) == PAGE_SIZE
|
||||
second = requests.get(
|
||||
f"{BASE_URL}/admin/workspaces/data",
|
||||
headers=_headers(admin),
|
||||
params={"page": 2},
|
||||
timeout=HTTP_TIMEOUT,
|
||||
).json()
|
||||
assert second["workspaces"]
|
||||
assert not {w["uid"] for w in first["workspaces"]} & {
|
||||
w["uid"] for w in second["workspaces"]
|
||||
}
|
||||
finally:
|
||||
_drop(uids)
|
||||
|
||||
|
||||
def test_workspaces_page_is_server_rendered_with_pagination(app_server, seeded_db):
|
||||
admin = _user("alice_test")
|
||||
owner = _user("bob_test")
|
||||
uids = _seed(PAGE_SIZE + 1, owner["uid"])
|
||||
try:
|
||||
html = requests.get(
|
||||
f"{BASE_URL}/admin/workspaces",
|
||||
headers={"Accept": "text/html", "X-API-KEY": admin["api_key"]},
|
||||
timeout=HTTP_TIMEOUT,
|
||||
).text
|
||||
assert "Loading" not in html
|
||||
assert 'data-workspace-uid="' in html
|
||||
assert 'scope="col">Name</th>' in html
|
||||
assert 'class="pagination"' in html
|
||||
finally:
|
||||
_drop(uids)
|
||||
Reference in New Issue
Block a user