Files
devplacepy/tests/api/admin/workspaces/data.py
T
retoorandClaude Sonnet 5.5 1a9201cb91 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>
2026-11-15 01:50:50 +01:00

103 lines
3.0 KiB
Python

# 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)