forked from retoor/devplacepy
Render markdown in gist and project sidebar titles; add content visibility and GIF picker
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -331,6 +331,71 @@ def test_model_edit_page_404_for_missing_model(seeded_db):
|
||||
)
|
||||
|
||||
|
||||
def test_model_test_marks_unreachable_route_bad(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
provider = _unique_gateway("testprov").lower()
|
||||
source = _unique_gateway("testroute")
|
||||
|
||||
created_provider = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
json={
|
||||
"name": provider,
|
||||
"base_url": "http://127.0.0.1:9/v1/chat/completions",
|
||||
"api_key": "sk-test",
|
||||
"is_active": True,
|
||||
},
|
||||
)
|
||||
assert created_provider.status_code == 200, created_provider.text[:300]
|
||||
created_route = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={
|
||||
"source_model": source,
|
||||
"provider": provider,
|
||||
"target_model": "vendor/dead",
|
||||
"kind": "chat",
|
||||
},
|
||||
)
|
||||
assert created_route.status_code == 200, created_route.text[:300]
|
||||
try:
|
||||
tested = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models/{source}/test",
|
||||
allow_redirects=False,
|
||||
)
|
||||
assert tested.status_code == 302
|
||||
assert tested.headers["location"].startswith("/admin/gateway?tab=models")
|
||||
listed = admin.get(f"{BASE_URL}/admin/gateway/models").json()
|
||||
row = next(m for m in listed["models"] if m["source_model"] == source)
|
||||
assert row["health_status"] == "bad"
|
||||
assert row["health_code"] == "connection_error"
|
||||
assert row["health_checked_at"]
|
||||
finally:
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/models/{source}")
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/providers/{provider}")
|
||||
|
||||
|
||||
def test_model_test_pages_require_admin_and_known_route(seeded_db):
|
||||
assert (
|
||||
requests.post(
|
||||
f"{BASE_URL}/admin/gateway/models/x/test",
|
||||
headers=JSON_gateway,
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 401
|
||||
)
|
||||
key = member_key()
|
||||
assert (
|
||||
requests.post(
|
||||
f"{BASE_URL}/admin/gateway/models/x/test",
|
||||
headers={**JSON_gateway, "X-API-KEY": key},
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
admin = admin_session(seeded_db)
|
||||
missing = _unique_gateway("ghosttest")
|
||||
assert admin.post(f"{BASE_URL}/admin/gateway/models/{missing}/test").status_code == 404
|
||||
|
||||
|
||||
def test_model_route_validation(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
missing_target = admin.post(
|
||||
@@ -349,3 +414,75 @@ def test_model_route_validation(seeded_db):
|
||||
},
|
||||
)
|
||||
assert bad_kind.status_code == 400
|
||||
|
||||
|
||||
def test_model_route_tools_mode_round_trip_and_validation(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
source = _unique_gateway("tools-mode")
|
||||
|
||||
default = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={"source_model": source, "target_model": "vendor/t", "kind": "chat"},
|
||||
)
|
||||
assert default.status_code == 200, default.text[:300]
|
||||
assert default.json()["model"]["tools_mode"] == "native"
|
||||
|
||||
emulated = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={
|
||||
"source_model": source,
|
||||
"target_model": "vendor/t",
|
||||
"kind": "chat",
|
||||
"tools_mode": "emulated",
|
||||
},
|
||||
)
|
||||
assert emulated.status_code == 200, emulated.text[:300]
|
||||
assert emulated.json()["model"]["tools_mode"] == "emulated"
|
||||
listed = admin.get(f"{BASE_URL}/admin/gateway/models").json()
|
||||
row = next(m for m in listed["models"] if m["source_model"] == source)
|
||||
assert row["tools_mode"] == "emulated"
|
||||
|
||||
invalid = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={
|
||||
"source_model": source,
|
||||
"target_model": "vendor/t",
|
||||
"kind": "chat",
|
||||
"tools_mode": "magic",
|
||||
},
|
||||
)
|
||||
assert invalid.json().get("ok") is not True
|
||||
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/models/{source}")
|
||||
|
||||
|
||||
def test_model_route_edit_page_renders_html_and_shows_tools_mode(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
source = _unique_gateway("edit-page")
|
||||
created = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={
|
||||
"source_model": source,
|
||||
"target_model": "vendor/e",
|
||||
"kind": "chat",
|
||||
"tools_mode": "emulated",
|
||||
},
|
||||
)
|
||||
assert created.status_code == 200, created.text[:300]
|
||||
|
||||
page = admin.get(
|
||||
f"{BASE_URL}/admin/gateway/models/{source}/edit",
|
||||
headers={"Accept": "text/html"},
|
||||
)
|
||||
assert page.status_code == 200, page.text[:500]
|
||||
assert 'name="tools_mode"' in page.text
|
||||
assert '<option value="emulated" selected>' in page.text
|
||||
|
||||
index = admin.get(
|
||||
f"{BASE_URL}/admin/gateway?tab=models", headers={"Accept": "text/html"}
|
||||
)
|
||||
assert index.status_code == 200
|
||||
assert f"/admin/gateway/models/{source}/edit" in index.text
|
||||
assert "tools: emulated" in index.text
|
||||
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/models/{source}")
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
import pytest
|
||||
import requests
|
||||
from tests.conftest import BASE_URL
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from devplacepy.utils import generate_uid, make_combined_slug
|
||||
|
||||
JSON = {"Accept": "application/json"}
|
||||
_counter = [0]
|
||||
|
||||
|
||||
def _seed_article(status="published"):
|
||||
_counter[0] += 1
|
||||
uid = generate_uid()
|
||||
title = f"Visibility News {int(time.time() * 1000)}{_counter[0]}"
|
||||
slug = make_combined_slug(title, uid)
|
||||
get_table("news").insert(
|
||||
{
|
||||
"deleted_at": None,
|
||||
"deleted_by": None,
|
||||
"uid": uid,
|
||||
"slug": slug,
|
||||
"title": title,
|
||||
"description": "Seeded article for visibility tests.",
|
||||
"content": "Body.",
|
||||
"url": "https://example.com/visibility",
|
||||
"source_name": "Example",
|
||||
"status": status,
|
||||
"synced_at": datetime.now(timezone.utc).isoformat(),
|
||||
"show_on_landing": 0,
|
||||
"grade": 9,
|
||||
}
|
||||
)
|
||||
return {"uid": uid, "slug": slug, "title": title}
|
||||
|
||||
|
||||
def _session(username):
|
||||
refresh_snapshot()
|
||||
session = requests.Session()
|
||||
session.headers.update(
|
||||
{"X-API-KEY": get_table("users").find_one(username=username)["api_key"]}
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def admin(seeded_db):
|
||||
return _session("alice_test")
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def member(seeded_db):
|
||||
return _session("bob_test")
|
||||
|
||||
|
||||
def _set(session, uid, value):
|
||||
return session.post(
|
||||
f"{BASE_URL}/admin/news/{uid}/visibility", data={"value": value}, headers=JSON
|
||||
)
|
||||
|
||||
|
||||
def _set_ok(session, uid, value):
|
||||
response = _set(session, uid, value)
|
||||
assert response.status_code == 200, response.text[:200]
|
||||
return response
|
||||
|
||||
|
||||
def _listed(slug_or_title):
|
||||
response = requests.get(f"{BASE_URL}/news", headers=JSON)
|
||||
assert response.status_code == 200
|
||||
return slug_or_title in {
|
||||
row["article"]["title"] for row in response.json()["articles"]
|
||||
}
|
||||
|
||||
|
||||
def _read(uid):
|
||||
refresh_snapshot()
|
||||
return get_table("news").find_one(uid=uid)
|
||||
|
||||
|
||||
def test_published_article_is_listed_and_open(app_server):
|
||||
article = _seed_article()
|
||||
assert _listed(article["title"])
|
||||
assert requests.get(f"{BASE_URL}/news/{article['slug']}").status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["hidden", "offline", "draft"])
|
||||
def test_admin_sets_visibility_and_listing_follows(admin, value):
|
||||
article = _seed_article()
|
||||
response = _set(admin, article["uid"], value)
|
||||
assert response.status_code == 200, response.text[:300]
|
||||
assert response.json()["data"]["visibility"] == value
|
||||
assert _read(article["uid"])["status"] == value
|
||||
assert not _listed(article["title"])
|
||||
|
||||
|
||||
def test_hidden_article_opens_by_link_with_noindex(admin):
|
||||
article = _seed_article()
|
||||
_set_ok(admin, article["uid"], "hidden")
|
||||
page = requests.get(f"{BASE_URL}/news/{article['slug']}")
|
||||
assert page.status_code == 200
|
||||
assert "noindex" in page.text
|
||||
assert article["slug"] not in requests.get(f"{BASE_URL}/sitemap.xml").text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["offline", "draft"])
|
||||
def test_offline_and_draft_articles_are_admin_only(admin, member, value):
|
||||
article = _seed_article()
|
||||
_set_ok(admin, article["uid"], value)
|
||||
assert requests.get(f"{BASE_URL}/news/{article['slug']}", headers=JSON).status_code == 404
|
||||
assert member.get(f"{BASE_URL}/news/{article['slug']}", headers=JSON).status_code == 404
|
||||
assert admin.get(f"{BASE_URL}/news/{article['slug']}").status_code == 200
|
||||
|
||||
|
||||
def test_article_can_be_published_again(admin):
|
||||
article = _seed_article()
|
||||
_set_ok(admin, article["uid"], "offline")
|
||||
assert _set(admin, article["uid"], "published").status_code == 200
|
||||
assert _listed(article["title"])
|
||||
assert requests.get(f"{BASE_URL}/news/{article['slug']}").status_code == 200
|
||||
|
||||
|
||||
def test_member_cannot_change_article_visibility(member):
|
||||
article = _seed_article()
|
||||
response = _set(member, article["uid"], "offline")
|
||||
assert response.status_code == 403
|
||||
assert _read(article["uid"])["status"] == "published"
|
||||
|
||||
|
||||
def test_guest_cannot_change_article_visibility(app_server):
|
||||
article = _seed_article()
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/admin/news/{article['uid']}/visibility",
|
||||
data={"value": "offline"},
|
||||
headers=JSON,
|
||||
)
|
||||
assert response.status_code in (401, 403)
|
||||
|
||||
|
||||
def test_invalid_value_is_refused(admin):
|
||||
article = _seed_article()
|
||||
response = _set(admin, article["uid"], "nonsense")
|
||||
assert response.status_code in (400, 422)
|
||||
assert _read(article["uid"])["status"] == "published"
|
||||
|
||||
|
||||
def test_publish_toggle_still_switches_draft_and_published(admin):
|
||||
article = _seed_article()
|
||||
admin.post(f"{BASE_URL}/admin/news/{article['uid']}/publish", headers=JSON)
|
||||
assert _read(article["uid"])["status"] == "draft"
|
||||
admin.post(f"{BASE_URL}/admin/news/{article['uid']}/publish", headers=JSON)
|
||||
assert _read(article["uid"])["status"] == "published"
|
||||
|
||||
|
||||
def test_news_visibility_change_is_audited(admin):
|
||||
article = _seed_article()
|
||||
_set_ok(admin, article["uid"], "offline")
|
||||
response = admin.get(
|
||||
f"{BASE_URL}/admin/audit-log", params={"event_key": "news.visibility.set"}, headers=JSON
|
||||
)
|
||||
assert any(
|
||||
entry.get("target_uid") == article["uid"] and entry.get("new_value") == "offline"
|
||||
for entry in response.json()["entries"]
|
||||
)
|
||||
Reference in New Issue
Block a user