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:
2026-11-05 17:59:21 +01:00
co-authored by Claude Sonnet 5.5
parent bc11475f29
commit 05d6633204
154 changed files with 6394 additions and 289 deletions
+1 -1
View File
@@ -154,7 +154,7 @@ The rule generalizes: if the production function reads a random-ish input or a t
| `Http.getJson` returns HTML / `JSON.parse` throws | The helper sends `Accept: application/json`; a content-negotiated route (`respond(..., model=...)`) needs it. Don't hand-roll `fetch()` without that header against a negotiated route |
| Page renders but data empty / 500 only after a CLI test ran earlier | A partial insert created the table with a reduced schema. Ensure the full column set in `init_db()` (see "Dataset rules -> `init_db()` MUST create every queried column") |
| `no such column: X` 500 in the server log | Same root cause: a queried/indexed column is missing because the table was created lazily by a partial insert. Add the column to the `init_db()` ensure-block |
| Action button click times out ("element is not visible") | The button moved into the `.project-actions-overflow` menu. Open it first: click `.project-actions-more`, then `.context-menu-item:has-text('<Label>')` (see `tests/e2e/projects/index.py`) |
| Action button click times out ("element is not visible") | The button moved into the `dp-action-menu` popover. Use the shared helpers in `tests/e2e/ui/components.py` (`_open_menu`, `_pick`): they click `[data-action-menu-trigger]` and wait for `[data-action-menu-panel]` to match `:popover-open`, then click the `[data-menu-action]` element (see `tests/e2e/projects/files.py`) |
| `ImportError: cannot import name X from devplacepy.utils` in a test | The util was renamed (e.g. `badge_info` -> `get_badge`). Update the test import to the current name; grep `templating.py` globals for the new name |
| Admin page heading assertion fails | Admin pages use `<h2>` inside `.admin-toolbar`, not `<h1>` - assert `text=` or `.admin-toolbar h2:has-text(...)` |
| `test_auth_matrix` reports `422` instead of `401`/`403` | A POST route's form params are undocumented in `docs_api.py`, so the matrix sends an empty body and FastAPI validation (422) fires before the auth guard. Document every required form param so auth is reached and tested |
+137
View File
@@ -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}")
+167
View File
@@ -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"]
)
+395
View File
@@ -0,0 +1,395 @@
# retoor <retoor@molodetz.nl>
import time
import pytest
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot
JSON = {"Accept": "application/json"}
KINDS = ("post", "gist", "project")
AREA = {"post": "posts", "gist": "gists", "project": "projects"}
LISTING = {"post": "/feed", "gist": "/gists", "project": "/projects"}
_counter = [0]
def _unique(prefix):
_counter[0] += 1
return f"{prefix}{int(time.time() * 1000)}{_counter[0]}"
def _signup():
name = _unique("vis")
session = requests.Session()
session.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
refresh_snapshot()
key = get_table("users").find_one(username=name)["api_key"]
session.headers.update({"X-API-KEY": key})
return session
@pytest.fixture(scope="module")
def owner(app_server):
return _signup()
@pytest.fixture(scope="module")
def stranger(app_server):
return _signup()
@pytest.fixture(scope="module")
def admin(seeded_db):
refresh_snapshot()
session = requests.Session()
session.headers.update(
{"X-API-KEY": get_table("users").find_one(username="alice_test")["api_key"]}
)
return session
def _create(session, kind, token, visibility=None):
extra = {} if visibility is None else {"visibility": visibility}
if kind == "post":
path, data = "/posts/create", {"content": f"visibility post body {token}", "title": token}
elif kind == "gist":
path, data = "/gists/create", {"title": token, "source_code": "print(1)"}
else:
path, data = "/projects/create", {"title": token, "description": f"project {token}"}
response = session.post(
f"{BASE_URL}{path}", data={**data, **extra}, headers=JSON, allow_redirects=False
)
return response
def _make(session, kind, visibility=None):
token = _unique(f"tok{kind}")
response = _create(session, kind, token, visibility)
assert response.status_code == 200, response.text[:300]
payload = response.json()["data"]
return {"token": token, "slug": payload["slug"], "uid": payload["uid"], "url": payload["url"]}
def _set(session, kind, item, value):
path, data = _edit_payload(kind, item["token"], value)
return session.post(
f"{BASE_URL}{path.format(slug=item['slug'])}", data=data, headers=JSON
)
def _listed_uids(kind, token, session=None):
getter = session.get if session is not None else requests.get
response = getter(f"{BASE_URL}{LISTING[kind]}", params={"search": token}, headers=JSON)
assert response.status_code == 200, response.text[:300]
data = response.json()
rows = data[AREA[kind]]
return {row[kind]["uid"] if kind in row else row["uid"] for row in rows}
def _detail(kind, item, session=None):
getter = session.get if session is not None else requests.get
return getter(f"{BASE_URL}{item['url']}", headers=JSON, allow_redirects=False)
@pytest.mark.parametrize("kind", KINDS)
def test_default_visibility_is_published(owner, kind):
item = _make(owner, kind)
detail = _detail(kind, item, owner)
assert detail.status_code == 200
assert detail.json()[kind]["visibility"] == "published"
assert item["uid"] in _listed_uids(kind, item["token"])
@pytest.mark.parametrize("kind", KINDS)
@pytest.mark.parametrize("value", ["hidden", "offline", "draft"])
def test_create_with_visibility(owner, kind, value):
item = _make(owner, kind, value)
assert _detail(kind, item, owner).json()[kind]["visibility"] == value
assert item["uid"] not in _listed_uids(kind, item["token"])
@pytest.mark.parametrize("kind", KINDS)
def test_create_rejects_unknown_visibility(owner, kind):
response = _create(owner, kind, _unique("bad"), "secret")
assert response.status_code in (400, 422)
@pytest.mark.parametrize("kind", KINDS)
def test_toggle_cycle_updates_listing_and_detail(owner, stranger, kind):
item = _make(owner, kind)
for value in ("hidden", "offline", "draft", "published"):
response = _set(owner, kind, item, value)
assert response.status_code == 200, response.text[:300]
assert _detail(kind, item, owner).json()[kind]["visibility"] == value
listed = item["uid"] in _listed_uids(kind, item["token"])
assert listed == (value == "published")
reachable = _detail(kind, item, stranger).status_code == 200
assert reachable == (value in ("published", "hidden"))
@pytest.mark.parametrize("kind", KINDS)
def test_hidden_is_open_by_link_for_everyone_but_noindex(owner, kind):
item = _make(owner, kind, "hidden")
guest = requests.get(f"{BASE_URL}{item['url']}", allow_redirects=False)
assert guest.status_code == 200
assert "noindex" in guest.text
assert item["uid"] not in _listed_uids(kind, item["token"])
@pytest.mark.parametrize("kind", KINDS)
@pytest.mark.parametrize("value", ["offline", "draft"])
def test_offline_and_draft_are_404_for_guests_and_strangers(owner, stranger, kind, value):
item = _make(owner, kind, value)
assert _detail(kind, item).status_code == 404
assert _detail(kind, item, stranger).status_code == 404
assert _detail(kind, item, owner).status_code == 200
@pytest.mark.parametrize("kind", KINDS)
def test_admin_can_open_offline_item(owner, admin, kind):
item = _make(owner, kind, "offline")
assert _detail(kind, item, admin).status_code == 200
@pytest.mark.parametrize("kind", KINDS)
def test_only_owner_can_change_visibility(owner, stranger, admin, kind):
item = _make(owner, kind)
assert _set(stranger, kind, item, "offline").status_code == 403
assert _set(admin, kind, item, "offline").status_code == 403
assert _detail(kind, item, owner).json()[kind]["visibility"] == "published"
@pytest.mark.parametrize("kind", KINDS)
def test_guest_cannot_change_visibility(owner, kind):
item = _make(owner, kind)
path, data = _edit_payload(kind, item["token"], "offline")
response = requests.post(
f"{BASE_URL}{path.format(slug=item['slug'])}", data=data, headers=JSON
)
assert response.status_code in (401, 403)
assert _detail(kind, item, owner).json()[kind]["visibility"] == "published"
@pytest.mark.parametrize("kind", KINDS)
def test_invalid_value_is_refused(owner, kind):
item = _make(owner, kind)
response = _set(owner, kind, item, "nonsense")
assert response.status_code in (400, 422)
assert _detail(kind, item, owner).json()[kind]["visibility"] == "published"
@pytest.mark.parametrize("kind", KINDS)
def test_missing_item_visibility_change_is_denied(owner, kind):
response = _set(owner, kind, {"slug": "does-not-exist", "token": "x"}, "hidden")
assert response.status_code == 403
def _edit_payload(kind, token, visibility):
if kind == "post":
return "/posts/edit/{slug}", {"content": f"edited body for {token}", "title": token, "visibility": visibility}
if kind == "gist":
return "/gists/edit/{slug}", {"title": token, "source_code": "print(2)", "visibility": visibility}
return "/projects/edit/{slug}", {"title": token, "description": f"edited {token}", "visibility": visibility}
@pytest.mark.parametrize("kind", KINDS)
def test_edit_form_changes_visibility(owner, kind):
item = _make(owner, kind)
path, data = _edit_payload(kind, item["token"], "hidden")
response = owner.post(f"{BASE_URL}{path.format(slug=item['slug'])}", data=data, headers=JSON)
assert response.status_code == 200, response.text[:300]
assert _detail(kind, item, owner).json()[kind]["visibility"] == "hidden"
assert item["uid"] not in _listed_uids(kind, item["token"])
@pytest.mark.parametrize("kind", KINDS)
def test_edit_without_visibility_keeps_current_value(owner, kind):
item = _make(owner, kind, "offline")
path, data = _edit_payload(kind, item["token"], "")
data.pop("visibility")
response = owner.post(f"{BASE_URL}{path.format(slug=item['slug'])}", data=data, headers=JSON)
assert response.status_code == 200, response.text[:300]
assert _detail(kind, item, owner).json()[kind]["visibility"] == "offline"
@pytest.mark.parametrize("kind", ("gist", "project"))
def test_owner_sees_unlisted_items_on_own_listing_only(owner, stranger, kind):
item = _make(owner, kind, "offline")
uid = get_table("users").find_one(username=_owner_name(owner))["uid"]
own = owner.get(
f"{BASE_URL}{LISTING[kind]}", params={"search": item["token"], "user_uid": uid}, headers=JSON
)
assert own.status_code == 200
rows = own.json()[AREA[kind]]
found = {row[kind]["uid"] if kind in row else row["uid"] for row in rows}
assert item["uid"] in found
other = stranger.get(
f"{BASE_URL}{LISTING[kind]}", params={"search": item["token"], "user_uid": uid}, headers=JSON
)
rows = other.json()[AREA[kind]]
assert item["uid"] not in {row[kind]["uid"] if kind in row else row["uid"] for row in rows}
def _owner_name(session):
refresh_snapshot()
key = session.headers["X-API-KEY"]
return get_table("users").find_one(api_key=key)["username"]
def test_unlisted_post_leaves_profile_for_visitors(owner, stranger):
item = _make(owner, "post", "hidden")
name = _owner_name(owner)
visitor = stranger.get(f"{BASE_URL}/profile/{name}", params={"tab": "posts"}, headers=JSON)
assert visitor.status_code == 200
assert item["uid"] not in {row["post"]["uid"] for row in visitor.json()["posts"]}
mine = owner.get(f"{BASE_URL}/profile/{name}", params={"tab": "posts"}, headers=JSON)
assert item["uid"] in {row["post"]["uid"] for row in mine.json()["posts"]}
@pytest.mark.parametrize("kind", KINDS)
def test_unlisted_items_are_absent_from_sitemap(owner, kind):
visible = _make(owner, kind)
hidden = _make(owner, kind, "hidden")
offline = _make(owner, kind, "offline")
sitemap = requests.get(f"{BASE_URL}/sitemap.xml").text
assert visible["slug"] in sitemap
assert hidden["slug"] not in sitemap
assert offline["slug"] not in sitemap
def test_offline_post_refuses_engagement_from_strangers(owner, stranger):
item = _make(owner, "post", "offline")
comment = stranger.post(
f"{BASE_URL}/comments/create",
data={"content": "should not land", "target_uid": item["uid"], "target_type": "post"},
headers=JSON,
)
assert comment.status_code == 404
vote = stranger.post(f"{BASE_URL}/votes/post/{item['uid']}", data={"value": 1}, headers=JSON)
assert vote.status_code == 404
reaction = stranger.post(
f"{BASE_URL}/reactions/post/{item['uid']}", data={"emoji": "\U0001F44D"}, headers=JSON
)
assert reaction.status_code == 404
bookmark = stranger.post(f"{BASE_URL}/bookmarks/post/{item['uid']}", headers=JSON)
assert bookmark.status_code == 404
def test_offline_post_still_accepts_engagement_from_owner(owner):
item = _make(owner, "post", "offline")
comment = owner.post(
f"{BASE_URL}/comments/create",
data={"content": "owner note", "target_uid": item["uid"], "target_type": "post"},
headers=JSON,
)
assert comment.status_code == 200
def test_hidden_post_accepts_engagement_from_strangers(owner, stranger):
item = _make(owner, "post", "hidden")
comment = stranger.post(
f"{BASE_URL}/comments/create",
data={"content": "still reachable by link", "target_uid": item["uid"], "target_type": "post"},
headers=JSON,
)
assert comment.status_code == 200
def test_offline_post_leaves_next_post_and_related_lists(owner):
item = _make(owner, "post", "offline")
detail = requests.get(f"{BASE_URL}/posts/{item['slug']}", headers=JSON)
assert detail.status_code == 404
def test_audit_log_records_visibility_change(owner, admin):
item = _make(owner, "post")
assert _set(owner, "post", item, "offline").status_code == 200
response = admin.get(
f"{BASE_URL}/admin/audit-log", params={"event_key": "post.visibility.set"}, headers=JSON
)
assert response.status_code == 200
entries = response.json()["entries"]
assert any(
entry.get("target_uid") == item["uid"] and entry.get("new_value") == "offline"
for entry in entries
)
def test_denied_change_is_audited(owner, stranger, admin):
item = _make(owner, "gist")
assert _set(stranger, "gist", item, "offline").status_code == 403
response = admin.get(
f"{BASE_URL}/admin/audit-log", params={"event_key": "gist.edit"}, headers=JSON
)
entries = response.json()["entries"]
assert any(
entry.get("target_uid") == item["uid"] and entry.get("result") == "denied"
for entry in entries
)
def test_devrant_feed_hides_unlisted_rants(owner):
item = _make(owner, "post", "hidden")
response = requests.get(f"{BASE_URL}/api/devrant/rants", params={"app": 3, "limit": 50})
assert response.status_code == 200
assert item["token"] not in response.text
LEGACY_CREATE = {
"post": ("/posts/create", {"content": "legacy client post body text"}),
"gist": ("/gists/create", {"title": "legacy gist", "source_code": "print(1)"}),
"project": ("/projects/create", {"title": "legacy project", "description": "legacy"}),
}
@pytest.mark.parametrize("kind", KINDS)
def test_legacy_form_create_without_the_field_is_published(owner, kind):
path, data = LEGACY_CREATE[kind]
response = owner.post(f"{BASE_URL}{path}", data=data, headers=JSON)
assert response.status_code == 200, response.text[:300]
url = response.json()["data"]["url"]
detail = owner.get(f"{BASE_URL}{url}", headers=JSON)
assert detail.json()[kind]["visibility"] == "published"
@pytest.mark.parametrize("kind", KINDS)
def test_legacy_json_create_without_the_field_is_published(owner, kind):
path, data = LEGACY_CREATE[kind]
response = owner.post(f"{BASE_URL}{path}", json=data, headers=JSON)
assert response.status_code == 200, response.text[:300]
url = response.json()["data"]["url"]
assert owner.get(f"{BASE_URL}{url}", headers=JSON).json()[kind]["visibility"] == "published"
@pytest.mark.parametrize("kind", KINDS)
def test_legacy_json_create_with_the_field_is_honoured(owner, kind):
path, data = LEGACY_CREATE[kind]
response = owner.post(
f"{BASE_URL}{path}", json={**data, "visibility": "hidden"}, headers=JSON
)
assert response.status_code == 200, response.text[:300]
url = response.json()["data"]["url"]
assert owner.get(f"{BASE_URL}{url}", headers=JSON).json()[kind]["visibility"] == "hidden"
@pytest.mark.parametrize("kind", KINDS)
def test_legacy_json_edit_without_the_field_keeps_the_value(owner, kind):
item = _make(owner, kind, "hidden")
path, data = _edit_payload(kind, item["token"], "")
data.pop("visibility")
response = owner.post(
f"{BASE_URL}{path.format(slug=item['slug'])}", json=data, headers=JSON
)
assert response.status_code == 200, response.text[:300]
assert _detail(kind, item, owner).json()[kind]["visibility"] == "hidden"
+248
View File
@@ -0,0 +1,248 @@
# retoor <retoor@molodetz.nl>
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import pytest
from starlette.testclient import TestClient
from devplacepy.database import get_table, init_db, set_setting
from devplacepy.routers import giphy as giphy_router
from devplacepy.services.giphy import runtime
from devplacepy.services.giphy.fake import FakeGiphyClient
from devplacepy.utils import create_session
JSON = {"Accept": "application/json"}
_seq = [0]
GIF_BYTES = (
b"GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff"
b"\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00"
b"\x01\x00\x00\x02\x02\x44\x01\x00\x3b"
)
SETTINGS = {
"giphy_api_key": "test-giphy-key",
"giphy_base_url": "https://api.giphy.test/v1/gifs",
"giphy_rating": "pg",
"max_upload_size_mb": "10",
"allowed_file_types": "",
"max_attachments_per_resource": "10",
}
@pytest.fixture(autouse=True)
def _init_db_giphy():
init_db()
yield
@pytest.fixture
def giphy_env():
fake = FakeGiphyClient()
runtime.set_client(fake)
giphy_router.limiter.reset()
for key, value in SETTINGS.items():
set_setting(key, value)
yield fake
runtime.set_client(None)
for key in ("giphy_api_key", "giphy_base_url", "giphy_rating"):
set_setting(key, "")
@pytest.fixture
def remote_gif():
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "image/gif")
self.send_header("Content-Length", str(len(GIF_BYTES)))
self.end_headers()
self.wfile.write(GIF_BYTES)
def log_message(self, *args):
return
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
yield f"http://127.0.0.1:{server.server_port}/giphy.gif"
server.shutdown()
server.server_close()
def _user():
_seq[0] += 1
uid = f"giphy-user-{_seq[0]}"
get_table("users").insert(
{
"uid": uid,
"username": f"giphytest{_seq[0]}",
"terms_version": "1",
"role": "member",
"xp": 0,
"level": 1,
"is_active": True,
}
)
return uid
def _client(uid=None):
import devplacepy.main as m
client = TestClient(m.app)
if uid:
client.cookies.set("session", create_session(uid))
return client
def test_status_requires_auth():
response = _client().get("/giphy/status", headers=JSON)
assert response.status_code == 401
def test_status_configured(giphy_env):
response = _client(_user()).get("/giphy/status", headers=JSON)
assert response.status_code == 200, response.text
assert response.json() == {"configured": True}
def test_status_unconfigured():
runtime.set_client(FakeGiphyClient(configured=False))
try:
set_setting("giphy_api_key", "")
client = _client(_user())
assert client.get("/giphy/status", headers=JSON).json() == {"configured": False}
assert client.get("/giphy/trending", headers=JSON).status_code == 503
assert (
client.get("/giphy/search", params={"q": "cat"}, headers=JSON).status_code == 503
)
finally:
runtime.set_client(None)
def test_search_and_trending(giphy_env):
client = _client(_user())
response = client.get("/giphy/trending", headers=JSON)
assert response.status_code == 200, response.text
assert [item["id"] for item in response.json()["results"]] == ["fake1", "fake2"]
response = client.get("/giphy/search", params={"q": "dance"}, headers=JSON)
assert response.status_code == 200, response.text
assert response.json()["query"] == "dance"
assert [item["id"] for item in response.json()["results"]] == ["fake2"]
assert giphy_env.queries == ["dance"]
response = client.get("/giphy/search", params={"q": " "}, headers=JSON)
assert response.status_code == 400
def test_attach(giphy_env, remote_gif, monkeypatch):
import devplacepy.attachments as attachments
import devplacepy.net_guard as net_guard
async def _no_guard(url, **kwargs):
return url
async def _no_host_guard(host):
return host
monkeypatch.setattr(attachments, "_guard_public_url", _no_guard)
monkeypatch.setattr(net_guard, "guard_public_url", _no_guard)
monkeypatch.setattr(net_guard, "guard_public_host", _no_host_guard)
giphy_env.gifs[0]["images"]["original"]["url"] = remote_gif
client = _client(_user())
response = client.post(
"/giphy/attach", json={"gif_id": "fake1", "title": "Fake happy"}, headers=JSON
)
assert response.status_code == 201, response.text
payload = response.json()
assert payload["uid"]
assert payload["mime_type"] == "image/gif"
assert payload["original_filename"] == "Fake happy.gif"
row = get_table("attachments").find_one(uid=payload["uid"])
assert row is not None
response = client.post("/giphy/attach", json={"gif_id": "missing"}, headers=JSON)
assert response.status_code == 404
response = client.post("/giphy/attach", json={"gif_id": ""}, headers=JSON)
assert response.status_code == 422
def test_every_route_requires_a_signed_in_user():
client = _client()
assert client.get("/giphy/search", params={"q": "cat"}, headers=JSON).status_code == 401
assert client.get("/giphy/trending", headers=JSON).status_code == 401
assert client.post("/giphy/attach", json={"gif_id": "fake1"}, headers=JSON).status_code == 401
def test_errors_use_the_shared_error_shape(giphy_env):
client = _client(_user())
response = client.get("/giphy/search", params={"q": " "}, headers=JSON)
assert response.status_code == 400
assert response.json() == {"error": {"status": 400, "message": "q is required"}}
response = client.post("/giphy/attach", json={"gif_id": "missing"}, headers=JSON)
assert response.status_code == 404
assert response.json()["error"]["message"] == "GIF not found"
def test_search_is_rate_limited_per_user(giphy_env):
limiter = giphy_router.limiter
first, second = _client(_user()), _client(_user())
statuses = [first.get("/giphy/trending", headers=JSON).status_code for _ in range(limiter.max_calls + 3)]
assert statuses == [200] * limiter.max_calls + [429] * 3
refused = first.get("/giphy/search", params={"q": "dance"}, headers=JSON)
assert refused.status_code == 429
assert refused.headers["Retry-After"] == str(int(limiter.window_seconds))
assert refused.json()["error"]["status"] == 429
assert second.get("/giphy/trending", headers=JSON).status_code == 200
def test_oversized_limit_is_clamped(giphy_env):
response = _client(_user()).get("/giphy/trending", params={"limit": 100000}, headers=JSON)
assert response.status_code == 200
def test_attach_refuses_when_gifs_are_not_an_allowed_type(giphy_env):
set_setting("allowed_file_types", ".png,.jpg")
try:
response = _client(_user()).post("/giphy/attach", json={"gif_id": "fake1"}, headers=JSON)
assert response.status_code == 415, response.text
finally:
set_setting("allowed_file_types", "")
def test_attach_is_audited_and_the_record_is_the_upload_shape(giphy_env, remote_gif, monkeypatch):
import devplacepy.attachments as attachments
async def _no_guard(url, **kwargs):
return url
monkeypatch.setattr(attachments, "_guard_public_url", _no_guard)
import devplacepy.net_guard as net_guard
monkeypatch.setattr(net_guard, "guard_public_url", _no_guard)
giphy_env.gifs[1]["images"]["original"]["url"] = remote_gif
owner = _user()
response = _client(owner).post("/giphy/attach", json={"gif_id": "fake2"}, headers=JSON)
assert response.status_code == 201, response.text
payload = response.json()
for key in ("uid", "url", "mime_type", "original_filename", "file_size", "is_image", "is_video"):
assert key in payload
assert payload["is_image"] is True
assert payload["url"].endswith(".gif")
event = get_table("audit_log").find_one(event_key="attachment.giphy", target_uid=payload["uid"])
assert event is not None
assert event["actor_uid"] == owner
def test_attach_surfaces_an_unreachable_gif_as_an_error(giphy_env):
giphy_env.gifs[0]["images"]["original"]["url"] = "http://127.0.0.1:9/never.gif"
giphy_env.gifs[0]["images"]["fixed_width"]["url"] = "http://127.0.0.1:9/never.gif"
response = _client(_user()).post("/giphy/attach", json={"gif_id": "fake1"}, headers=JSON)
assert response.status_code in (400, 502)
assert "message" in response.json()["error"]
+122
View File
@@ -0,0 +1,122 @@
# retoor <retoor@molodetz.nl>
import time
import pytest
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot, set_setting
JSON = {"Accept": "application/json"}
@pytest.fixture(scope="module", autouse=True)
def _delete_settings(app_server):
for key, value in {
"rate_limit_per_minute": "1000000",
"rate_limit_window_seconds": "60",
"registration_open": "1",
"maintenance_mode": "0",
}.items():
set_setting(key, value)
yield
def _login(seeded_db, who):
session = requests.Session()
creds = seeded_db[who]
session.post(
f"{BASE_URL}/auth/login",
data={"email": creds["email"], "password": creds["password"]},
allow_redirects=True,
)
return session
def _uid(username):
refresh_snapshot()
return get_table("users").find_one(username=username)["uid"]
def _send(session, receiver_uid, text):
response = session.post(
f"{BASE_URL}/messages/send",
data={"receiver_uid": receiver_uid, "content": text},
headers=JSON,
)
assert response.status_code == 200, response.text
return response.json()["data"]["uid"]
def _thread_text(session, with_uid):
response = session.get(f"{BASE_URL}/messages", params={"with_uid": with_uid}, headers=JSON)
assert response.status_code == 200, response.text
return [m["message"]["content"] for m in response.json()["messages"]]
def test_admin_deletes_own_message(seeded_db):
alice = _login(seeded_db, "alice")
bob = _login(seeded_db, "bob")
bob_uid = _uid("bob_test")
text = f"admin own delete {int(time.time() * 1000)}"
uid = _send(alice, bob_uid, text)
assert text in _thread_text(alice, bob_uid)
response = alice.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
assert response.status_code == 200, response.text
assert response.json()["data"]["deleted_uid"] == uid
assert text not in _thread_text(alice, bob_uid)
assert text not in _thread_text(bob, _uid("alice_test"))
refresh_snapshot()
row = get_table("messages").find_one(uid=uid)
assert row["deleted_at"] and row["deleted_by"] == _uid("alice_test")
def test_member_cannot_delete_own_message(seeded_db):
alice = _login(seeded_db, "alice")
bob = _login(seeded_db, "bob")
alice_uid = _uid("alice_test")
text = f"member cannot delete {int(time.time() * 1000)}"
uid = _send(bob, alice_uid, text)
response = bob.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
assert response.status_code == 403
assert text in _thread_text(alice, _uid("bob_test"))
def test_admin_cannot_delete_other_users_message(seeded_db):
alice = _login(seeded_db, "alice")
bob = _login(seeded_db, "bob")
alice_uid = _uid("alice_test")
text = f"admin cannot delete theirs {int(time.time() * 1000)}"
uid = _send(bob, alice_uid, text)
response = alice.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
assert response.status_code == 403
assert text in _thread_text(bob, alice_uid)
def test_delete_unknown_message_is_404(seeded_db):
alice = _login(seeded_db, "alice")
response = alice.post(f"{BASE_URL}/messages/does-not-exist/delete", headers=JSON)
assert response.status_code == 404
def test_delete_requires_login(app_server):
response = requests.post(
f"{BASE_URL}/messages/anything/delete", headers=JSON, allow_redirects=False
)
assert response.status_code in (401, 303)
def test_deleted_message_leaves_conversation_preview(seeded_db):
alice = _login(seeded_db, "alice")
bob_uid = _uid("bob_test")
text = f"preview delete {int(time.time() * 1000)}"
uid = _send(alice, bob_uid, text)
alice.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
response = alice.get(f"{BASE_URL}/messages/conversations")
previews = [c["last_message"] for c in response.json()["conversations"]]
assert text not in previews
+163
View File
@@ -136,3 +136,166 @@ def test_model_form_submits_selected_model_from_dropdown(page, seeded_db):
httpd.shutdown()
requests.delete(f"{BASE_URL}/admin/gateway/models/{source}", headers=auth)
requests.delete(f"{BASE_URL}/admin/gateway/providers/{provider}", headers=auth)
def _fake_probe_upstream():
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
sock.close()
class Handler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
self.rfile.read(length)
if self.path.endswith("/images/generations"):
body = json.dumps(
{"data": [{"url": "https://img.example/h.png"}]}
).encode()
else:
body = json.dumps(
{"choices": [{"message": {"content": "OK"}}]}
).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
httpd = socketserver.TCPServer(("127.0.0.1", port), Handler)
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
return httpd, port
def test_model_grid_health_badges_and_test_buttons(page, seeded_db):
user = seeded_db["alice"]
_promote_to_admin(user["username"])
key = _admin_api_key(user["username"])
auth = {"X-API-KEY": key}
httpd, port = _fake_probe_upstream()
live_provider = f"e2ehealth{port}"
dead_provider = f"e2edead{port}"
live_source = f"e2elive{port}"
dead_source = f"e2edead{port}"
try:
requests.post(
f"{BASE_URL}/admin/gateway/providers",
json={
"name": live_provider,
"base_url": f"http://127.0.0.1:{port}/v1/chat/completions",
},
headers=auth,
)
requests.post(
f"{BASE_URL}/admin/gateway/providers",
json={
"name": dead_provider,
"base_url": "http://127.0.0.1:9/v1/chat/completions",
},
headers=auth,
)
requests.post(
f"{BASE_URL}/admin/gateway/models",
json={
"source_model": live_source,
"provider": live_provider,
"target_model": "vendor/live",
"kind": "chat",
},
headers=auth,
)
requests.post(
f"{BASE_URL}/admin/gateway/models",
json={
"source_model": dead_source,
"provider": dead_provider,
"target_model": "vendor/dead",
"kind": "chat",
},
headers=auth,
)
assert (
requests.post(
f"{BASE_URL}/admin/gateway/models/{live_source}/test", headers=auth
).status_code
== 200
)
assert (
requests.post(
f"{BASE_URL}/admin/gateway/models/{dead_source}/test", headers=auth
).status_code
== 200
)
login_user(page, user)
page.goto(f"{BASE_URL}/admin/gateway?tab=models", wait_until="domcontentloaded")
grid = page.locator("table.admin-table")
grid.wait_for(state="visible")
assert page.get_by_role("button", name="Test all", exact=True).is_visible()
live_row = grid.locator("tr", has_text=live_source)
dead_row = grid.locator("tr", has_text=dead_source)
assert live_row.get_by_role("button", name="Test", exact=True).is_visible()
assert live_row.locator(".gw-badge-ok").is_visible()
assert dead_row.locator(".gw-badge-bad").is_visible()
assert "connection_error" in (dead_row.inner_text() or "")
assert "gw-row-bad" in (dead_row.get_attribute("class") or "")
finally:
httpd.shutdown()
requests.delete(f"{BASE_URL}/admin/gateway/models/{live_source}", headers=auth)
requests.delete(f"{BASE_URL}/admin/gateway/models/{dead_source}", headers=auth)
requests.delete(
f"{BASE_URL}/admin/gateway/providers/{live_provider}", headers=auth
)
requests.delete(
f"{BASE_URL}/admin/gateway/providers/{dead_provider}", headers=auth
)
def test_image_test_opens_result_modal(page, seeded_db):
user = seeded_db["alice"]
_promote_to_admin(user["username"])
key = _admin_api_key(user["username"])
auth = {"X-API-KEY": key}
httpd, port = _fake_probe_upstream()
provider = f"e2eimg{port}"
source = f"e2eimgsrc{port}"
try:
requests.post(
f"{BASE_URL}/admin/gateway/providers",
json={
"name": provider,
"base_url": f"http://127.0.0.1:{port}/v1/chat/completions",
},
headers=auth,
)
requests.post(
f"{BASE_URL}/admin/gateway/models",
json={
"source_model": source,
"provider": provider,
"target_model": "vendor/img",
"kind": "image",
},
headers=auth,
)
login_user(page, user)
page.goto(f"{BASE_URL}/admin/gateway?tab=models", wait_until="domcontentloaded")
row = page.locator("table.admin-table tr", has_text=source)
row.wait_for(state="visible")
row.get_by_role("button", name="Test", exact=True).click()
modal = page.locator("#gw-test-image-modal.visible")
modal.wait_for(state="visible", timeout=30000)
assert modal.locator(".gw-test-image").get_attribute("src") == (
"https://img.example/h.png"
)
assert source in (modal.inner_text() or "")
finally:
httpd.shutdown()
requests.delete(f"{BASE_URL}/admin/gateway/models/{source}", headers=auth)
requests.delete(f"{BASE_URL}/admin/gateway/providers/{provider}", headers=auth)
+17
View File
@@ -160,3 +160,20 @@ def test_admin_news_featured_toggle(alice):
page.locator(toggle_sel).first.get_attribute("class") or ""
)
assert is_active != was_active
def test_admin_news_visibility_select_offers_every_status_and_saves(alice):
page, _ = alice
seed_admin_news()
page.goto(f"{BASE_URL}/admin/news", wait_until="domcontentloaded")
select = page.locator("form[action*='/visibility'] select").nth(1)
assert select.locator("option").all_text_contents() == ["Published", "Hidden", "Offline"]
with page.expect_navigation(wait_until="domcontentloaded"):
select.select_option("hidden")
refresh_snapshot()
statuses = sorted(r["status"] for r in get_table("news").find(deleted_at=None))
assert "hidden" in statuses
page.goto(f"{BASE_URL}/admin/news", wait_until="domcontentloaded")
selects = page.locator("form[action*='/visibility'] select")
values = [selects.nth(i).input_value() for i in range(selects.count())]
assert "hidden" in values
+262
View File
@@ -0,0 +1,262 @@
# retoor <retoor@molodetz.nl>
import http.server
import json
import threading
import time
from contextlib import contextmanager
from devplacepy.database import get_setting, set_setting
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS
from tests.e2e.components.title import _create_post
GIF_BYTES = (
b"GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff"
b"\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00"
b"\x01\x00\x00\x02\x02\x44\x01\x00\x3b"
)
SETTING_KEYS = ("giphy_api_key", "giphy_base_url", "giphy_rating")
TITLES = {"gif1": "happy dance", "gif2": "sad cat", "gif3": "dance party"}
PICKER = "dp-gif-picker .gif-toggle-btn"
PANEL = "dp-gif-picker .gif-picker-wrapper"
ITEM = "dp-gif-picker .gif-picker-item"
def _gif_item(port, gif_id):
media = f"http://127.0.0.1:{port}/media/{gif_id}.gif"
return {
"id": gif_id,
"title": TITLES[gif_id],
"url": f"https://giphy.test/gifs/{gif_id}",
"images": {
"fixed_width": {"url": media, "width": "200", "height": "150", "size": "10"},
"original": {"url": media, "width": "480", "height": "360", "size": "10"},
},
}
def _fake_giphy():
holder = {}
class Handler(http.server.BaseHTTPRequestHandler):
def _send(self, status, body, content_type):
self.send_response(status)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
port = holder["server"].server_port
path, _, query = self.path.partition("?")
params = dict(pair.split("=", 1) for pair in query.split("&") if "=" in pair)
if path.startswith("/media/"):
return self._send(200, GIF_BYTES, "image/gif")
ids = list(TITLES)
if path.endswith("/search"):
needle = params.get("q", "").replace("+", " ").lower()
ids = [i for i in ids if needle in TITLES[i]]
if path.endswith("/search") or path.endswith("/trending"):
body = {"data": [_gif_item(port, i) for i in ids]}
else:
body = {"data": _gif_item(port, path.rsplit("/", 1)[-1])}
self._send(200, json.dumps(body).encode(), "application/json")
def log_message(self, *args):
return
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
holder["server"] = server
threading.Thread(target=server.serve_forever, daemon=True).start()
return server
@contextmanager
def _giphy(key="e2e-giphy-key"):
server = _fake_giphy()
originals = {name: get_setting(name, "") for name in SETTING_KEYS}
try:
set_setting("giphy_api_key", key)
set_setting("giphy_base_url", f"http://127.0.0.1:{server.server_port}/v1/gifs")
set_setting("giphy_rating", "pg")
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
yield server
finally:
for name, value in originals.items():
set_setting(name, value)
server.shutdown()
server.server_close()
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
def _stub_attach(page):
uploaded = page.request.post(
f"{BASE_URL}/uploads/upload",
multipart={"file": {"name": "party.gif", "mimeType": "image/gif", "buffer": GIF_BYTES}},
headers={"Accept": "application/json"},
)
assert uploaded.ok, uploaded.text()
record = uploaded.json()
calls = []
def fulfil(route):
calls.append(route.request.post_data_json)
route.fulfill(status=201, content_type="application/json", body=json.dumps(record))
page.route("**/giphy/attach", fulfil)
return record, calls
def _open_post_with_comment_form(page):
_create_post(page, "A post that will receive a GIF comment.", "Giphy host post")
page.locator(".comment-form textarea").first.wait_for(state="visible")
def test_picker_is_hidden_when_giphy_is_not_configured(alice):
page, _ = alice
with _giphy(key=""):
_open_post_with_comment_form(page)
page.locator("dp-gif-picker").first.wait_for(state="attached")
page.wait_for_function("() => document.querySelector('dp-gif-picker').hidden === true")
assert not page.locator(PICKER).first.is_visible()
def test_trending_search_and_dismissal(alice):
page, _ = alice
with _giphy():
_open_post_with_comment_form(page)
page.locator(PICKER).first.click()
page.locator(PANEL).first.wait_for(state="visible")
page.wait_for_function("() => document.querySelectorAll('.gif-picker-item').length === 3")
assert "Powered by GIPHY" in page.locator(".gif-picker-credit").first.text_content()
page.locator(".gif-picker-search").first.fill("dance")
page.wait_for_function("() => document.querySelectorAll('.gif-picker-item').length === 2")
page.keyboard.press("Escape")
page.locator(PANEL).first.wait_for(state="hidden")
page.locator(PICKER).first.click()
page.locator(PANEL).first.wait_for(state="visible")
page.locator(".page-title, h1").first.click()
page.locator(PANEL).first.wait_for(state="hidden")
def test_empty_search_says_so(alice):
page, _ = alice
with _giphy():
_open_post_with_comment_form(page)
page.locator(PICKER).first.click()
page.locator(".gif-picker-search").first.fill("zzzz-no-such-gif")
status = page.locator(".gif-picker-status").first
status.wait_for(state="visible")
page.wait_for_function("() => document.querySelector('.gif-picker-status').textContent === 'No GIFs found'")
assert page.locator(ITEM).count() == 0
def test_picked_gif_becomes_an_attachment_not_markup(alice):
page, _ = alice
with _giphy():
_open_post_with_comment_form(page)
record, calls = _stub_attach(page)
form = page.locator(".comment-form").first
form.locator("textarea").fill("nice one")
page.locator(PICKER).first.click()
page.locator(ITEM).first.wait_for(state="visible")
page.locator(ITEM).first.click()
page.locator(PANEL).first.wait_for(state="hidden")
assert calls == [{"gif_id": "gif1", "title": "happy dance"}]
assert form.locator("input[name=attachment_uids]").input_value() == record["uid"]
assert form.locator("textarea").input_value() == "nice one"
form.locator(".comment-form-submit").click()
page.wait_for_load_state("domcontentloaded")
page.locator(".comment img[src$='.gif']").first.wait_for(state="visible")
assert page.locator(".comment img[src$='.gif']").count() == 1
def test_a_second_pick_adds_a_second_attachment(alice):
page, _ = alice
with _giphy():
_open_post_with_comment_form(page)
record, _ = _stub_attach(page)
form = page.locator(".comment-form").first
for _round in range(2):
page.locator(PICKER).first.click()
page.locator(ITEM).first.wait_for(state="visible")
page.locator(ITEM).first.click()
page.locator(PANEL).first.wait_for(state="hidden")
assert form.locator("input[name=attachment_uids]").input_value() == record["uid"]
def test_composer_has_the_picker_beside_the_attachment_button(alice):
page, _ = alice
with _giphy():
page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
page.locator(".feed-fab").first.wait_for(state="visible", timeout=10000)
page.locator(".feed-fab").first.click()
actions = page.locator("#create-post-modal .composer-attachments-actions")
actions.wait_for(state="visible")
actions.locator(".gif-toggle-btn").wait_for(state="visible")
assert actions.locator("dp-upload").count() == 1
record, _ = _stub_attach(page)
actions.locator(".gif-toggle-btn").click()
page.locator(ITEM).first.wait_for(state="visible")
page.locator(ITEM).first.click()
page.wait_for_function(
"(uid) => document.querySelector('#create-post-modal input[name=attachment_uids]').value === uid",
arg=record["uid"],
)
def test_message_input_has_the_picker(alice, bob):
page, _ = alice
_, bob_user = bob
with _giphy():
page.goto(f"{BASE_URL}/messages?search={bob_user['username']}", wait_until="domcontentloaded")
area = page.locator(".messages-input-area")
area.wait_for(state="visible")
area.locator(".gif-toggle-btn").wait_for(state="visible")
record, _ = _stub_attach(page)
area.locator(".gif-toggle-btn").click()
page.locator(ITEM).first.wait_for(state="visible")
page.locator(ITEM).first.click()
page.wait_for_function(
"(uid) => document.querySelector('.messages-input-area input[name=attachment_uids]').value === uid",
arg=record["uid"],
)
def test_panel_fits_a_small_phone(alice):
page, _ = alice
page.set_viewport_size({"width": 360, "height": 640})
with _giphy():
_open_post_with_comment_form(page)
page.locator(PICKER).first.click()
panel = page.locator(PANEL).first
panel.wait_for(state="visible")
page.locator(ITEM).first.wait_for(state="visible")
box = panel.bounding_box()
assert box["x"] >= 0
assert box["x"] + box["width"] <= 360
assert box["y"] >= 0
assert box["y"] + box["height"] <= 640
def test_message_panel_fits_a_small_phone(alice, bob):
page, _ = alice
_, bob_user = bob
page.set_viewport_size({"width": 360, "height": 640})
with _giphy():
page.goto(f"{BASE_URL}/messages?search={bob_user['username']}", wait_until="domcontentloaded")
area = page.locator(".messages-input-area")
area.wait_for(state="visible")
area.locator(".gif-toggle-btn").click()
panel = page.locator(PANEL).first
panel.wait_for(state="visible")
page.locator(ITEM).first.wait_for(state="visible")
box = panel.bounding_box()
assert box["x"] >= 0
assert box["x"] + box["width"] <= 360
assert box["y"] >= 0
+78
View File
@@ -558,3 +558,81 @@ def test_gists_long_code_line_does_not_widen_layout(page, app_server):
card.wait_for(state="visible")
assert card.locator("pre").count() == 1
assert_no_horizontal_overflow(page, ".gists-layout")
def _create_gist_with_visibility(page, title, visibility):
page.goto(f"{BASE_URL}/gists", wait_until="domcontentloaded")
page.locator("#create-gist-btn").wait_for(state="visible", timeout=10000)
page.locator("#create-gist-btn").click()
page.fill("#gist-title", title)
page.fill("#gist-description", "visibility e2e")
_set_cm_value(page, "print('visibility')")
if visibility:
page.select_option("#gist-visibility", visibility)
page.locator("button.btn-primary:has-text('Create Gist')").click()
page.wait_for_url(f"{BASE_URL}/gists/*", wait_until="domcontentloaded")
return page.url
def _gists_list_has(page, title):
page.goto(f"{BASE_URL}/gists?search={title}", wait_until="domcontentloaded")
return page.locator(f"text={title}").count() > 0
def test_gist_create_form_offers_the_three_visibility_choices(alice):
page, _ = alice
page.goto(f"{BASE_URL}/gists", wait_until="domcontentloaded")
page.locator("#create-gist-btn").click()
options = page.locator("#gist-visibility option")
assert options.all_text_contents() == ["Published", "Hidden", "Offline"]
expect(page.locator("#gist-visibility")).to_have_value("published")
def test_gist_created_hidden_leaves_the_listing(alice):
page, _ = alice
title = f"VisGistHidden{int(time.time() * 1000)}"
_create_gist_with_visibility(page, title, "hidden")
assert not _gists_list_has(page, title)
def _edit_gist_visibility(page, url, value):
page.goto(url, wait_until="domcontentloaded")
page.locator("[data-modal='edit-gist-modal']").click()
page.select_option("#edit-gist-visibility", value)
page.locator("#edit-gist-modal button.btn-primary").click()
page.wait_for_url(url, wait_until="domcontentloaded")
def test_gist_edit_form_toggles_visibility_and_others_lose_access(alice, bob):
page, _ = alice
other, _ = bob
title = f"VisGistToggle{int(time.time() * 1000)}"
url = _create_gist_with_visibility(page, title, None)
assert _gists_list_has(page, title)
_edit_gist_visibility(page, url, "offline")
assert not _gists_list_has(page, title)
other.goto(url, wait_until="domcontentloaded")
assert other.locator(f"text={title}").count() == 0
_edit_gist_visibility(page, url, "published")
assert _gists_list_has(page, title)
other.goto(url, wait_until="domcontentloaded")
expect(other.locator(f"text={title}").first).to_be_visible()
def test_gist_edit_modal_changes_visibility(alice):
page, _ = alice
title = f"VisGistEdit{int(time.time() * 1000)}"
url = _create_gist_with_visibility(page, title, None)
page.locator("[data-modal='edit-gist-modal']").click()
expect(page.locator("#edit-gist-visibility")).to_have_value("published")
page.select_option("#edit-gist-visibility", "hidden")
page.locator("#edit-gist-modal button.btn-primary").click()
page.wait_for_url(url, wait_until="domcontentloaded")
assert not _gists_list_has(page, title)
def test_gist_visibility_field_fits_a_phone(mobile_page):
page, _ = mobile_page
title = f"VisGistPhone{int(time.time() * 1000)}"
_create_gist_with_visibility(page, title, "hidden")
page.locator("[data-modal='edit-gist-modal']").click()
expect(page.locator("#edit-gist-visibility")).to_be_visible()
assert_no_horizontal_overflow(page, ".gist-detail-page")
+27
View File
@@ -183,6 +183,33 @@ def test_send_message_appears_in_thread(alice):
page.locator(f".message-bubble:has-text('{msg}')").first.wait_for(state="visible")
def test_admin_deletes_own_message_in_chat(alice):
page, _ = alice
bob = get_table("users").find_one(username="bob_test")
page.goto(
f"{BASE_URL}/messages?with_uid={bob['uid']}", wait_until="domcontentloaded"
)
msg = f"Delete me {int(time.time() * 1000)}"
page.fill("textarea[name='content']", msg)
page.locator(".messages-send-btn").click()
bubble = page.locator(f".message-bubble.mine:has-text('{msg}')").first
bubble.wait_for(state="visible")
bubble.locator(".message-delete-btn").wait_for(state="attached")
bubble.locator(".message-delete-btn").click()
page.locator(".dialog-overlay.visible .dialog-confirm").click()
expect(page.locator(f".message-bubble:has-text('{msg}')")).to_have_count(0)
def test_member_sees_no_delete_control_in_chat(bob):
page, _ = bob
alice = get_table("users").find_one(username="alice_test")
page.goto(
f"{BASE_URL}/messages?with_uid={alice['uid']}", wait_until="domcontentloaded"
)
page.locator(".messages-thread").wait_for(state="visible")
expect(page.locator(".message-delete-btn")).to_have_count(0)
def test_paste_image_attaches_in_chat(alice):
page, _ = alice
bob = get_table("users").find_one(username="bob_test")
+105
View File
@@ -0,0 +1,105 @@
# retoor <retoor@molodetz.nl>
import time
from playwright.sync_api import expect
from tests.conftest import BASE_URL, assert_no_horizontal_overflow
def _unique():
return f"vispost{int(time.time() * 1000)}"
def _compose(page, token, visibility=None):
page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
page.locator(".feed-fab").first.wait_for(state="visible", timeout=10000)
page.locator(".feed-fab").first.click()
page.fill("#post-content", f"visibility e2e body {token}")
if visibility:
page.select_option("#composer-visibility", visibility)
page.locator("#create-post-modal button.btn-primary:has-text('Post')").click()
page.wait_for_url(f"{BASE_URL}/posts/*", wait_until="domcontentloaded")
return page.url
def _edit_visibility(page, url, value):
page.goto(url, wait_until="domcontentloaded")
page.locator("[data-modal='edit-post-modal']").click()
page.select_option("#edit-post-visibility", value)
page.locator("#edit-post-form button.btn-primary").click()
page.wait_for_url(url, wait_until="domcontentloaded")
def _feed_has(page, token):
page.goto(f"{BASE_URL}/feed?search={token}", wait_until="domcontentloaded")
return page.locator(f"text={token}").count() > 0
def test_new_post_defaults_to_published(alice):
page, _ = alice
token = _unique()
url = _compose(page, token)
page.locator("[data-modal='edit-post-modal']").click()
expect(page.locator("#edit-post-visibility")).to_have_value("published")
assert _feed_has(page, token)
page.goto(url, wait_until="domcontentloaded")
def test_composer_offers_the_three_user_choices(alice):
page, _ = alice
page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
page.locator(".feed-fab").first.click()
options = page.locator("#composer-visibility option")
assert options.all_text_contents() == ["Published", "Hidden", "Offline"]
expect(page.locator("#composer-visibility")).to_have_value("published")
def test_create_hidden_post_leaves_the_feed(alice):
page, _ = alice
token = _unique()
url = _compose(page, token, "hidden")
page.locator("[data-modal='edit-post-modal']").click()
expect(page.locator("#edit-post-visibility")).to_have_value("hidden")
assert not _feed_has(page, token)
page.goto(url, wait_until="domcontentloaded")
expect(page.locator(f"text={token}").first).to_be_visible()
def test_edit_form_toggles_visibility_and_the_feed_follows(alice):
page, _ = alice
token = _unique()
url = _compose(page, token)
assert _feed_has(page, token)
_edit_visibility(page, url, "offline")
assert not _feed_has(page, token)
_edit_visibility(page, url, "published")
assert _feed_has(page, token)
def test_offline_post_is_not_shown_to_other_members(alice, bob):
page, _ = alice
other, _ = bob
token = _unique()
url = _compose(page, token)
_edit_visibility(page, url, "offline")
other.goto(url, wait_until="domcontentloaded")
assert other.locator(f"text={token}").count() == 0
_edit_visibility(page, url, "hidden")
other.goto(url, wait_until="domcontentloaded")
expect(other.locator(f"text={token}").first).to_be_visible()
assert not _feed_has(other, token)
def test_edit_form_lists_the_three_choices(alice):
page, _ = alice
_compose(page, _unique())
page.locator("[data-modal='edit-post-modal']").click()
options = page.locator("#edit-post-visibility option")
assert options.all_text_contents() == ["Published", "Hidden", "Offline"]
def test_visibility_field_fits_a_phone(mobile_page):
page, _ = mobile_page
_compose(page, _unique(), "hidden")
page.locator("[data-modal='edit-post-modal']").click()
expect(page.locator("#edit-post-visibility")).to_be_visible()
assert_no_horizontal_overflow(page, ".post-page")
+41 -12
View File
@@ -15,10 +15,39 @@ def _confirm(page):
page.locator(".dialog-overlay.visible .dialog-confirm").click()
TRIGGER = "[data-action-menu-trigger]"
def _open_menu(page):
# aria-expanded can lag behind the popover (a dialog closing, a light
# dismiss), so the open state that counts is whether the panel shows.
page.wait_for_function(
"() => document.querySelector('[data-action-menu-panel]') !== null"
" || document.querySelector('[data-action-menu-trigger]') === null"
)
panel = page.locator("[data-action-menu-panel]")
if panel.count() and panel.evaluate("(el) => el.matches(':popover-open')"):
return
page.click(TRIGGER)
page.wait_for_function(
"() => document.querySelector('[data-action-menu-panel]').matches(':popover-open')"
)
def _has(page, selector):
_open_menu(page)
return page.is_visible(selector)
def _pick(page, selector):
_open_menu(page)
page.click(selector)
def _reset_block(page):
page.goto(f"{BASE_URL}/profile/bob_test", wait_until="domcontentloaded")
if page.is_visible(UNBLOCK):
page.click(UNBLOCK)
if _has(page, UNBLOCK):
_pick(page, UNBLOCK)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
@@ -64,30 +93,30 @@ def _seed_post(owner_uid, marker):
def test_block_and_unblock_buttons_toggle(alice):
page, _ = alice
_reset_block(page)
assert page.is_visible(BLOCK)
assert _has(page, BLOCK)
page.click(BLOCK)
_pick(page, BLOCK)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
assert page.is_visible(UNBLOCK)
assert _has(page, UNBLOCK)
page.click(UNBLOCK)
_pick(page, UNBLOCK)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
assert page.is_visible(BLOCK)
assert _has(page, BLOCK)
def test_block_button_opens_confirmation_dialog(alice):
page, _ = alice
_reset_block(page)
page.click(BLOCK)
_pick(page, BLOCK)
expect(page.locator(".dialog-overlay.visible")).to_be_visible()
expect(page.locator(".dialog-overlay.visible .dialog-message")).to_contain_text(
"Block"
)
page.locator(".dialog-overlay.visible .dialog-cancel").click()
expect(page.locator(".dialog-overlay")).not_to_be_visible()
assert page.is_visible(BLOCK)
assert _has(page, BLOCK)
def test_blocked_authors_post_hidden_from_feed(alice):
@@ -151,14 +180,14 @@ def test_blocked_users_profile_still_shows_their_content(alice):
_seed_post(bob_uid, marker)
_reset_block(page)
page.click(BLOCK)
_pick(page, BLOCK)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
page.goto(f"{BASE_URL}/profile/bob_test", wait_until="domcontentloaded")
assert page.is_visible(UNBLOCK)
assert _has(page, UNBLOCK)
assert marker in page.content()
page.click(UNBLOCK)
_pick(page, UNBLOCK)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
+38 -9
View File
@@ -11,10 +11,39 @@ def _confirm(page):
page.locator(".dialog-overlay.visible .dialog-confirm").click()
TRIGGER = "[data-action-menu-trigger]"
def _open_menu(page):
# aria-expanded can lag behind the popover (a dialog closing, a light
# dismiss), so the open state that counts is whether the panel shows.
page.wait_for_function(
"() => document.querySelector('[data-action-menu-panel]') !== null"
" || document.querySelector('[data-action-menu-trigger]') === null"
)
panel = page.locator("[data-action-menu-panel]")
if panel.count() and panel.evaluate("(el) => el.matches(':popover-open')"):
return
page.click(TRIGGER)
page.wait_for_function(
"() => document.querySelector('[data-action-menu-panel]').matches(':popover-open')"
)
def _has(page, selector):
_open_menu(page)
return page.is_visible(selector)
def _pick(page, selector):
_open_menu(page)
page.click(selector)
def _reset_mute(page):
page.goto(f"{BASE_URL}/profile/bob_test", wait_until="domcontentloaded")
if page.is_visible(UNMUTE):
page.click(UNMUTE)
if _has(page, UNMUTE):
_pick(page, UNMUTE)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
@@ -22,27 +51,27 @@ def _reset_mute(page):
def test_mute_and_unmute_buttons_toggle(alice):
page, _ = alice
_reset_mute(page)
assert page.is_visible(MUTE)
assert _has(page, MUTE)
page.click(MUTE)
_pick(page, MUTE)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
assert page.is_visible(UNMUTE)
assert _has(page, UNMUTE)
page.click(UNMUTE)
_pick(page, UNMUTE)
_confirm(page)
page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
assert page.is_visible(MUTE)
assert _has(page, MUTE)
def test_mute_button_opens_confirmation_dialog(alice):
page, _ = alice
_reset_mute(page)
page.click(MUTE)
_pick(page, MUTE)
expect(page.locator(".dialog-overlay.visible")).to_be_visible()
expect(page.locator(".dialog-overlay.visible .dialog-message")).to_contain_text(
"Mute"
)
page.locator(".dialog-overlay.visible .dialog-cancel").click()
expect(page.locator(".dialog-overlay")).not_to_be_visible()
assert page.is_visible(MUTE)
assert _has(page, MUTE)
+17 -25
View File
@@ -7,6 +7,7 @@ from playwright.sync_api import expect
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS, assert_share_copies
from devplacepy.database import get_table, set_setting
from devplacepy.utils import make_combined_slug
from tests.e2e.ui.components import _menu_item, _open_menu, _pick
def _goto_expect_404(page, url):
@@ -317,8 +318,7 @@ def _open_project_with_delete(page, title):
slug = proj_url.rstrip("/").split("/")[-1]
return proj_url, slug
def _click_delete(page):
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Delete')").click()
_pick(page, "Delete")
import asyncio
import io
import json
@@ -503,8 +503,7 @@ def test_delete_own_project(alice):
page, _ = alice
_create_project_projects(page, "Deletable Project XYZ")
proj_url = page.url
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Delete')").click()
_pick(page, "Delete")
page.locator(".dialog-overlay.visible .dialog-confirm").click()
page.wait_for_url(f"{BASE_URL}/projects", wait_until="domcontentloaded")
_goto_expect_404(page, proj_url)
@@ -513,15 +512,14 @@ def test_delete_own_project(alice):
def test_project_edit_button(alice):
page, _ = alice
_create_project_projects(page, "Editable Project ABC")
page.locator(".project-actions-more").click()
expect(page.locator(".context-menu-item:has-text('Edit')")).to_be_visible()
_open_menu(page)
expect(_menu_item(page, "Edit")).to_be_visible()
def test_project_edit_modal_prefills(alice):
page, _ = alice
_create_project_projects(page, "Edit Modal Project")
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Edit')").click()
_pick(page, "Edit")
assert page.is_visible("h3:has-text('Edit Project')")
expect(page.locator("#edit-project-title")).to_have_value("Edit Modal Project")
expect(page.locator("#platforms-tags .platform-tag:has-text('PC')")).to_be_visible()
@@ -531,8 +529,7 @@ def test_project_edit_submit(alice):
page, _ = alice
_create_project_projects(page, "Original Project Title")
proj_url = page.url
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Edit')").click()
_pick(page, "Edit")
page.fill("#edit-project-title", "Edited Project Title")
page.fill("#edit-project-description", "Edited project description body")
page.click("button:has-text('Save Changes')")
@@ -545,8 +542,7 @@ def test_project_edit_submit(alice):
def test_project_edit_status_change(alice):
page, _ = alice
_create_project_projects(page, "Status Change Project")
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Edit')").click()
_pick(page, "Edit")
page.check("#edit-project-modal input[value='Released']")
page.click("button:has-text('Save Changes')")
expect(page.locator(".project-status:has-text('Released')")).to_be_visible()
@@ -558,11 +554,9 @@ def test_project_edit_hidden_for_non_owner(alice, bob):
proj_url = page.url
bob_page, _ = bob
bob_page.goto(proj_url, wait_until="domcontentloaded")
bob_page.locator(".project-actions-more").click()
expect(
bob_page.locator(".context-menu-item:has-text('Download zip')")
).to_be_visible()
expect(bob_page.locator(".context-menu-item:has-text('Edit')")).to_have_count(0)
_open_menu(bob_page)
expect(_menu_item(bob_page, "Download zip")).to_be_visible()
expect(_menu_item(bob_page, "Edit")).to_have_count(0)
def test_project_detail_share_button(alice):
@@ -836,8 +830,7 @@ def test_project_make_private(alice):
page, _ = alice
_create_project_projects(page, "PrivateToggleProject")
slug = page.url.rstrip("/").split("/")[-1]
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Make private')").click()
_pick(page, "Make private")
page.locator(".dialog-overlay.visible .dialog-confirm").click()
page.wait_for_url(f"**/projects/{slug}", wait_until="domcontentloaded")
expect(page.locator(".badge-type:has-text('Private')")).to_be_visible()
@@ -847,8 +840,7 @@ def test_project_make_readonly(alice):
page, _ = alice
_create_project_projects(page, "ReadonlyToggleProject")
slug = page.url.rstrip("/").split("/")[-1]
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Make read-only')").click()
_pick(page, "Make read-only")
page.locator(".dialog-overlay.visible .dialog-confirm").click()
page.wait_for_url(f"**/projects/{slug}", wait_until="domcontentloaded")
expect(page.locator(".badge-type:has-text('Read-only')")).to_be_visible()
@@ -871,8 +863,8 @@ def test_detail_download_button_downloads(app_server, page):
_write_zip_download(key, slug, "README.md", "# hi")
_login_zip_download(page, name)
page.goto(f"{BASE_URL}/projects/{slug}", wait_until="domcontentloaded")
page.locator(".project-actions-more").click()
btn = page.locator(".context-menu-item:has-text('Download zip')")
_open_menu(page)
btn = _menu_item(page, "Download zip")
btn.wait_for(state="visible")
with page.expect_download(timeout=30000) as dl_info:
btn.click()
@@ -1014,5 +1006,5 @@ def test_containers_menu_hidden_for_non_owner_admin_on_member_private_project(pa
assert get_primary_admin_uid() != admin_uid
login_user(page, {"email": f"{admin_name}@t.dev", "password": "secret123"})
page.goto(f"{BASE_URL}/projects/{slug}", wait_until="domcontentloaded")
page.locator(".project-actions-more").click()
expect(page.locator(".context-menu-item:has-text('Containers')")).to_have_count(0)
_open_menu(page)
expect(_menu_item(page, "Containers")).to_have_count(0)
+87
View File
@@ -0,0 +1,87 @@
# retoor <retoor@molodetz.nl>
import time
from playwright.sync_api import expect
from tests.conftest import BASE_URL, assert_no_horizontal_overflow
def _create(page, title, visibility=None):
page.goto(f"{BASE_URL}/projects", wait_until="domcontentloaded")
page.locator("#create-project-btn").click()
page.fill("#title", title)
page.fill("#description", "visibility e2e project")
if visibility:
page.select_option("#project-visibility", visibility)
page.click("button:has-text('Create Project')")
page.wait_for_url(f"{BASE_URL}/projects/*", wait_until="domcontentloaded")
return page.url
def _open_edit(page):
page.locator("[data-menu-label='Edit']").first.evaluate("el => el.click()")
def _edit_visibility(page, url, value):
page.goto(url, wait_until="domcontentloaded")
_open_edit(page)
page.select_option("#edit-project-visibility", value)
page.locator("#edit-project-modal button.btn-primary").click()
page.wait_for_url(url, wait_until="domcontentloaded")
def _listed(page, title):
page.goto(f"{BASE_URL}/projects?search={title}", wait_until="domcontentloaded")
return page.locator(f"text={title}").count() > 0
def _title(prefix):
return f"{prefix}{int(time.time() * 1000)}"
def test_project_create_form_offers_the_three_visibility_choices(alice):
page, _ = alice
page.goto(f"{BASE_URL}/projects", wait_until="domcontentloaded")
page.locator("#create-project-btn").click()
options = page.locator("#project-visibility option")
assert options.all_text_contents() == ["Published", "Hidden", "Offline"]
expect(page.locator("#project-visibility")).to_have_value("published")
def test_project_created_offline_is_only_visible_to_its_owner(alice, bob):
page, _ = alice
other, _ = bob
title = _title("VisProjOffline")
url = _create(page, title, "offline")
assert not _listed(other, title)
other.goto(url, wait_until="domcontentloaded")
assert other.locator(f"h1:has-text('{title}')").count() == 0
page.goto(url, wait_until="domcontentloaded")
expect(page.locator(f"h1:has-text('{title}')").first).to_be_visible()
def test_project_edit_form_toggles_visibility(alice, bob):
page, _ = alice
other, _ = bob
title = _title("VisProjToggle")
url = _create(page, title)
assert _listed(other, title)
_edit_visibility(page, url, "hidden")
assert not _listed(other, title)
other.goto(url, wait_until="domcontentloaded")
expect(other.locator(f"h1:has-text('{title}')").first).to_be_visible()
_edit_visibility(page, url, "published")
assert _listed(other, title)
def test_project_edit_form_shows_the_current_status(alice):
page, _ = alice
url = _create(page, _title("VisProjCurrent"), "hidden")
page.goto(url, wait_until="domcontentloaded")
_open_edit(page)
expect(page.locator("#edit-project-visibility")).to_have_value("hidden")
def test_project_visibility_field_fits_a_phone(mobile_page):
page, _ = mobile_page
_create(page, _title("VisProjPhone"), "hidden")
assert_no_horizontal_overflow(page, ".project-page")
+5 -4
View File
@@ -14,6 +14,7 @@ from devplacepy.services.containers import store
from devplacepy.services.containers.workspace import flags, naming
from devplacepy.utils import make_combined_slug
from tests.conftest import BASE_URL
from tests.e2e.ui.components import _menu_item, _open_menu
EDITOR_CONTAINER_PORT = 8443
TRANSITION_TIMEOUT_MS = 8000
@@ -110,8 +111,8 @@ def test_project_page_hides_the_workspace_entry_point_from_a_non_owner(bob):
page.goto(
f"{BASE_URL}/projects/{project['slug']}", wait_until="domcontentloaded"
)
page.locator(".project-actions-more").click()
assert not page.locator(".context-menu-item:has-text('Workspace')").count()
_open_menu(page)
assert not _menu_item(page, "Workspace").count()
def test_direct_vscode_button_is_absent_while_the_workspace_is_stopped(alice):
@@ -148,8 +149,8 @@ def test_project_page_hides_the_workspace_entry_point_when_disabled(alice):
page.goto(
f"{BASE_URL}/projects/{project['slug']}", wait_until="domcontentloaded"
)
page.locator(".project-actions-more").click()
assert not page.locator(".context-menu-item:has-text('Workspace')").count()
_open_menu(page)
assert not _menu_item(page, "Workspace").count()
finally:
set_setting("workspace_enabled", "1")
assert _await_workspace_flag(project["slug"], row["api_key"], True)
+1 -1
View File
@@ -178,7 +178,7 @@ def test_landing_page_loads(page, app_server):
def test_landing_tagline(page, app_server):
page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
tagline = page.text_content("p")
tagline = page.text_content(".landing-hero p")
assert "Track industry shifts" in tagline
+33 -2
View File
@@ -7,9 +7,40 @@ def _open_project_with_delete(page, title):
proj_url = _make_project_ui(page, title)
slug = proj_url.rstrip("/").split("/")[-1]
return proj_url, slug
TRIGGER = "[data-action-menu-trigger]"
PANEL = "[data-action-menu-panel]"
def _open_menu(page):
# aria-expanded can lag behind the popover, so the open state that counts
# is whether the panel shows.
page.wait_for_function(
"() => document.querySelector('[data-action-menu-panel]') !== null"
" || document.querySelector('[data-action-menu-trigger]') === null"
)
panel = page.locator(PANEL)
if panel.count() and panel.evaluate("(el) => el.matches(':popover-open')"):
return
page.click(TRIGGER)
page.wait_for_function(
"() => document.querySelector('[data-action-menu-panel]').matches(':popover-open')"
)
def _menu_item(page, label):
return page.locator(
f"{PANEL} [data-menu-action]:has-text('{label}'), "
f"{PANEL} [data-menu-action][data-menu-label='{label}']"
)
def _pick(page, label):
_open_menu(page)
_menu_item(page, label).first.click()
def _click_delete(page):
page.locator(".project-actions-more").click()
page.locator(".context-menu-item:has-text('Delete')").click()
_pick(page, "Delete")
def test_data_confirm_opens_custom_dialog(alice):
+247
View File
@@ -0,0 +1,247 @@
# retoor <retoor@molodetz.nl>
import pytest
import dataset
from pydantic import ValidationError
from devplacepy.database import visibility as visibility_module
from devplacepy.database import (
DEFAULT_VISIBILITY,
LISTED_VALUES,
OPEN_VALUES,
SOFT_DELETE_TABLES,
TARGET_TABLES,
VISIBILITY_CHOICES,
VISIBILITY_COLUMNS,
VISIBILITY_HINTS,
VISIBILITY_LABELS,
VISIBILITY_VALUES,
is_listed,
is_open,
item_visibility,
listed_clause,
listed_sql,
normalize_visibility,
visibility_label,
visibility_options,
)
from devplacepy.models import (
GistEditForm,
GistForm,
PostEditForm,
PostForm,
ProjectEditForm,
ProjectForm,
VisibilityForm,
)
from devplacepy.schemas.content import GistOut, PostOut, ProjectOut
@pytest.fixture
def scratch(monkeypatch):
memory = dataset.connect("sqlite:///:memory:")
monkeypatch.setattr(visibility_module, "db", memory)
monkeypatch.setattr(visibility_module, "get_table", lambda name: memory[name])
yield memory
memory.close()
def test_the_four_statuses_and_the_default():
assert VISIBILITY_VALUES == ("published", "hidden", "draft", "offline")
assert DEFAULT_VISIBILITY == "published"
assert VISIBILITY_CHOICES == ("published", "hidden", "offline")
assert set(VISIBILITY_CHOICES) <= set(VISIBILITY_VALUES)
def test_listed_is_a_subset_of_open():
assert set(LISTED_VALUES) <= set(OPEN_VALUES)
assert LISTED_VALUES == ("published",)
assert OPEN_VALUES == ("published", "hidden")
def test_labels_and_hints_cover_every_value():
for value in VISIBILITY_VALUES:
assert VISIBILITY_LABELS[value]
assert VISIBILITY_HINTS[value]
assert visibility_label(value) == VISIBILITY_LABELS[value]
def test_options_expose_only_user_choices_by_default():
assert [o["value"] for o in visibility_options()] == list(VISIBILITY_CHOICES)
assert [o["value"] for o in visibility_options(VISIBILITY_VALUES)] == list(
VISIBILITY_VALUES
)
@pytest.mark.parametrize("raw", [None, "", " ", "nonsense", 0, "PUBLISHED "])
def test_normalize_falls_back_to_published_or_lowercases(raw):
expected = "published"
assert normalize_visibility(raw) == expected
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_normalize_is_idempotent(value):
assert normalize_visibility(normalize_visibility(value)) == value
assert normalize_visibility(value.upper()) == value
def test_every_visibility_table_is_a_soft_delete_table_with_a_target():
for table in VISIBILITY_COLUMNS:
assert table in SOFT_DELETE_TABLES
assert set(TARGET_TABLES.values()) == set(VISIBILITY_COLUMNS)
def test_news_uses_its_existing_status_column():
assert VISIBILITY_COLUMNS["news"] == "status"
for table in ("posts", "gists", "projects"):
assert VISIBILITY_COLUMNS[table] == "visibility"
@pytest.mark.parametrize("table", sorted(VISIBILITY_COLUMNS))
def test_null_and_unknown_rows_read_as_published(table):
column = VISIBILITY_COLUMNS[table]
assert item_visibility(table, {}) == "published"
assert item_visibility(table, {column: None}) == "published"
assert item_visibility(table, {column: "weird"}) == "published"
assert is_listed(table, {column: None})
assert is_open(table, {column: None})
@pytest.mark.parametrize("table", sorted(VISIBILITY_COLUMNS))
@pytest.mark.parametrize(
"value,listed,open_",
[
("published", True, True),
("hidden", False, True),
("offline", False, False),
("draft", False, False),
],
)
def test_listed_and_open_truth_table(table, value, listed, open_):
row = {VISIBILITY_COLUMNS[table]: value}
assert is_listed(table, row) is listed
assert is_open(table, row) is open_
def test_untracked_tables_are_always_open_and_listed():
assert item_visibility("quizzes", {"visibility": "offline"}) == "published"
assert is_listed("quizzes", {"visibility": "offline"})
assert listed_sql("quizzes") == "1 = 1"
def test_listed_sql_uses_coalesce_so_null_rows_stay_visible(scratch):
scratch["posts"].insert({"uid": "x", "visibility": "published"})
scratch["news"].insert({"uid": "y", "status": "published"})
assert "COALESCE(p.visibility, 'published') = 'published'" == listed_sql(
"posts", "p"
)
assert listed_sql("news") == "COALESCE(status, 'published') = 'published'"
def test_listed_clause_is_none_for_tables_without_a_visibility_column(scratch):
scratch["sessions"].insert({"uid": "x"})
assert listed_clause(scratch["sessions"]) is None
def test_listed_clause_filters_rows(scratch):
table = scratch["posts"]
table.insert(
{
"uid": "vis-unit-published",
"user_uid": "owner-1",
"visibility": "published",
"deleted_at": None,
}
)
table.insert(
{
"uid": "vis-unit-legacy",
"user_uid": "owner-1",
"visibility": None,
"deleted_at": None,
}
)
for value in ("hidden", "offline", "draft"):
table.insert(
{
"uid": f"vis-unit-{value}",
"user_uid": "owner-1",
"visibility": value,
"deleted_at": None,
}
)
wanted = {"vis-unit-published", "vis-unit-legacy"}
clause = listed_clause(table)
found = {r["uid"] for r in table.find(clause) if r["uid"].startswith("vis-unit-")}
assert found == wanted
own = {
r["uid"]
for r in table.find(listed_clause(table, own_uid="owner-1"))
if r["uid"].startswith("vis-unit-")
}
assert own == {f"vis-unit-{v}" for v in VISIBILITY_VALUES} | {"vis-unit-legacy"}
stranger = {
r["uid"]
for r in table.find(listed_clause(table, own_uid="someone-else"))
if r["uid"].startswith("vis-unit-")
}
assert stranger == wanted
CREATE_FORMS = (
(PostForm, {"content": "x" * 12}),
(GistForm, {"title": "t", "source_code": "c"}),
(ProjectForm, {"title": "t", "description": "d"}),
)
EDIT_FORMS = (
(PostEditForm, {"content": "x" * 12}),
(GistEditForm, {"title": "t", "source_code": "c"}),
(ProjectEditForm, {"title": "t", "description": "d"}),
)
@pytest.mark.parametrize("form,base", CREATE_FORMS)
def test_create_forms_default_to_published(form, base):
assert form(**base).visibility == "published"
assert form(**base, visibility="").visibility == "published"
@pytest.mark.parametrize("form,base", CREATE_FORMS)
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_create_forms_accept_every_status(form, base, value):
assert form(**base, visibility=value.upper()).visibility == value
@pytest.mark.parametrize("form,base", EDIT_FORMS)
def test_edit_forms_leave_visibility_unset_by_default(form, base):
assert form(**base).visibility is None
assert form(**base, visibility="").visibility is None
@pytest.mark.parametrize("form,base", EDIT_FORMS)
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_edit_forms_accept_every_status(form, base, value):
assert form(**base, visibility=value).visibility == value
@pytest.mark.parametrize("form,base", CREATE_FORMS + EDIT_FORMS)
def test_forms_reject_unknown_status(form, base):
with pytest.raises(ValidationError):
form(**base, visibility="secret")
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_visibility_form_accepts_every_status(value):
assert VisibilityForm(value=value).value == value
def test_visibility_form_rejects_unknown_status():
with pytest.raises(ValidationError):
VisibilityForm(value="secret")
@pytest.mark.parametrize("schema", [PostOut, GistOut, ProjectOut])
def test_output_schemas_always_report_a_status(schema):
assert schema(uid="1").visibility == "published"
assert schema(uid="1", visibility=None).visibility == "published"
assert schema(uid="1", visibility="offline").visibility == "offline"
+37
View File
@@ -0,0 +1,37 @@
# retoor <retoor@molodetz.nl>
import pytest
from devplacepy.docs_api import API_GROUPS
ENDPOINTS = {e["id"]: e for g in API_GROUPS for e in g["endpoints"]}
WRITES = ("posts-create", "posts-edit", "gists-create", "gists-edit", "projects-create", "projects-edit")
def _field(endpoint_id):
return next(p for p in ENDPOINTS[endpoint_id]["params"] if p["name"] == "visibility")
@pytest.mark.parametrize("endpoint_id", WRITES)
def test_visibility_is_documented_optional_and_enumerated(endpoint_id):
field = _field(endpoint_id)
assert field["required"] is False
assert field["location"] == "form"
assert field["options"] == ["published", "hidden", "offline", "draft"]
@pytest.mark.parametrize("endpoint_id", ("posts-create", "gists-create", "projects-create"))
def test_create_docs_state_the_default(endpoint_id):
assert "published" in _field(endpoint_id)["description"]
assert "when omitted the item is published" in _field(endpoint_id)["description"]
@pytest.mark.parametrize("endpoint_id", ("posts-edit", "gists-edit", "projects-edit"))
def test_edit_docs_state_that_omitting_keeps_the_value(endpoint_id):
assert "when omitted the current value is kept" in _field(endpoint_id)["description"]
def test_admin_news_visibility_endpoint_is_documented_as_admin_only():
endpoint = ENDPOINTS["admin-news-visibility"]
assert endpoint["auth"] == "admin"
assert any(p["name"] == "value" and p["required"] for p in endpoint["params"])
+15
View File
@@ -56,3 +56,18 @@ def test_is_auth_error_detects_rclone_html_401():
)
assert offload._is_auth_error("didn't find section in config file")
assert not offload._is_auth_error("connection timed out")
def test_run_cycle_skips_clearly_when_rclone_binary_missing(monkeypatch):
monkeypatch.setattr(offload.config, "RCLONE_BIN", "/nonexistent/rclone-test-binary")
async def explode(*args, **kwargs):
raise AssertionError("rclone must not be spawned when the binary is missing")
monkeypatch.setattr(offload, "_run_rclone", explode)
logs = []
result = run_async(offload.run_cycle(keep_local=5, keep_remote=5, log=logs.append))
assert result == {"uploaded": 0, "purged_local": 0, "removed_remote": 0}
assert len(logs) == 1
assert "rclone binary not found" in logs[0]
assert "backup_offload_enabled" in logs[0]
+23
View File
@@ -1,7 +1,10 @@
# retoor <retoor@molodetz.nl>
import asyncio
from tests.conftest import run_async
from devplacepy.services.dbapi.progress import hub
from devplacepy.services.dbapi.service import DbApiJobService
@@ -30,3 +33,23 @@ def test_process_rejects_non_select(local_db):
finally:
svc.cleanup(job)
assert raised
def test_stream_publishes_progress_to_subscriber(local_db):
svc = DbApiJobService()
uid = "unit-dbquery-progress"
queue = hub.subscribe(uid)
try:
rows, truncated = run_async(svc._stream(uid, "SELECT 1 AS n", 5000))
assert rows == [{"n": 1}]
assert truncated is False
frames = []
while True:
try:
frames.append(queue.get_nowait())
except asyncio.QueueEmpty:
break
assert any(frame.get("type") == "progress" for frame in frames)
finally:
hub.unsubscribe(uid, queue)
hub.clear(uid)
+1
View File
@@ -0,0 +1 @@
# retoor <retoor@molodetz.nl>
+190
View File
@@ -0,0 +1,190 @@
# retoor <retoor@molodetz.nl>
import httpx
import pytest
from tests.conftest import run_async
from devplacepy.services.giphy import client as client_module
from devplacepy.services.giphy.client import (
MAX_LIMIT,
GiphyClient,
GiphyError,
clear_cache,
normalize_gif,
)
from devplacepy.services.giphy.config import GiphyConfig
CONFIG = GiphyConfig(api_key="secret-key", base_url="https://api.giphy.test/v1/gifs", rating="pg")
def _item(gif_id="abc", title=" Happy dance "):
return {
"id": gif_id,
"title": title,
"url": f"https://giphy.test/gifs/{gif_id}",
"images": {
"fixed_width": {"url": "https://m.giphy.test/200.gif", "width": "200", "height": "150", "size": "9"},
"original": {"url": "https://m.giphy.test/o.gif", "width": "480", "height": "360", "size": "99"},
},
}
class _Recorder:
def __init__(self, handler):
self.calls = []
self.handler = handler
def factory(self, **kwargs):
def respond(request):
self.calls.append(request)
return self.handler(request)
return httpx.AsyncClient(transport=httpx.MockTransport(respond))
@pytest.fixture(autouse=True)
def _fresh_cache():
clear_cache()
yield
clear_cache()
def _patch(monkeypatch, handler):
recorder = _Recorder(handler)
monkeypatch.setattr(client_module.stealth, "stealth_async_client", recorder.factory)
return recorder
def test_normalize_gif_picks_preview_and_original():
gif = normalize_gif(_item())
assert gif["id"] == "abc"
assert gif["title"] == "Happy dance"
assert gif["preview"] == {"url": "https://m.giphy.test/200.gif", "width": 200, "height": 150, "size": 9}
assert gif["original"]["url"] == "https://m.giphy.test/o.gif"
def test_normalize_gif_survives_missing_images():
gif = normalize_gif({"id": 7})
assert gif == {"id": "7", "title": "", "url": "", "preview": {}, "original": {}}
def test_normalize_gif_falls_back_when_a_rendition_is_missing():
item = _item()
del item["images"]["fixed_width"]
assert normalize_gif(item)["preview"]["url"] == "https://m.giphy.test/o.gif"
def test_search_sends_key_rating_and_clamped_limit(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": [_item()]}))
gifs = await GiphyClient(CONFIG).search("cat", limit=999)
assert [gif["id"] for gif in gifs] == ["abc"]
request = recorder.calls[0]
assert request.url.path == "/v1/gifs/search"
params = dict(request.url.params)
assert params["api_key"] == "secret-key"
assert params["rating"] == "pg"
assert params["q"] == "cat"
assert params["limit"] == str(MAX_LIMIT)
run_async(body())
def test_limit_has_a_floor_of_one(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": []}))
await GiphyClient(CONFIG).trending(limit=-5)
assert dict(recorder.calls[0].url.params)["limit"] == "1"
run_async(body())
def test_identical_requests_hit_the_upstream_once(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": [_item()]}))
client = GiphyClient(CONFIG)
for _ in range(5):
await client.search("cat")
assert len(recorder.calls) == 1
await client.search("dog")
assert len(recorder.calls) == 2
run_async(body())
def test_cache_is_shared_across_api_keys_but_not_across_ratings(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": []}))
await GiphyClient(CONFIG).search("cat")
await GiphyClient(GiphyConfig("other-key", CONFIG.base_url, "pg")).search("cat")
assert len(recorder.calls) == 1
await GiphyClient(GiphyConfig("secret-key", CONFIG.base_url, "r")).search("cat")
assert len(recorder.calls) == 2
run_async(body())
def test_errors_are_not_cached(monkeypatch):
async def body():
answers = iter([httpx.Response(500, text="boom"), httpx.Response(200, json={"data": []})])
recorder = _patch(monkeypatch, lambda request: next(answers))
client = GiphyClient(CONFIG)
with pytest.raises(GiphyError) as caught:
await client.trending()
assert caught.value.status == 500
assert await client.trending() == []
assert len(recorder.calls) == 2
run_async(body())
def test_transport_failure_becomes_a_giphy_error(monkeypatch):
async def body():
def refuse(request):
raise httpx.ConnectError("no route", request=request)
_patch(monkeypatch, refuse)
with pytest.raises(GiphyError) as caught:
await GiphyClient(CONFIG).trending()
assert caught.value.status == 0
run_async(body())
def test_unconfigured_client_refuses_without_a_request(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={}))
client = GiphyClient(GiphyConfig("", CONFIG.base_url, "pg"))
for call in (client.trending(), client.search("cat"), client.get_gif("abc")):
with pytest.raises(GiphyError) as caught:
await call
assert caught.value.status == 503
assert recorder.calls == []
run_async(body())
def test_get_gif_not_found_and_empty_id(monkeypatch):
async def body():
_patch(monkeypatch, lambda request: httpx.Response(200, json={"data": {}}))
client = GiphyClient(CONFIG)
with pytest.raises(GiphyError) as missing:
await client.get_gif("nope")
assert missing.value.status == 404
with pytest.raises(GiphyError) as empty:
await client.get_gif("")
assert empty.value.status == 400
run_async(body())
def test_get_gif_returns_the_normalized_item(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": _item("xyz")}))
gif = await GiphyClient(CONFIG).get_gif("xyz")
assert gif["id"] == "xyz"
assert recorder.calls[0].url.path == "/v1/gifs/xyz"
run_async(body())
+84
View File
@@ -0,0 +1,84 @@
# retoor <retoor@molodetz.nl>
from devplacepy.services.giphy import limits
from devplacepy.services.giphy.limits import TtlCache, UserRateLimiter
def test_cache_returns_what_was_stored():
cache = TtlCache()
cache.put("a", {"x": 1})
assert cache.get("a") == {"x": 1}
assert cache.get("missing") is None
def test_cache_expires_entries(monkeypatch):
clock = [1000.0]
monkeypatch.setattr(limits.time, "monotonic", lambda: clock[0])
cache = TtlCache(ttl_seconds=10)
cache.put("a", 1)
clock[0] += 9
assert cache.get("a") == 1
clock[0] += 2
assert cache.get("a") is None
assert len(cache) == 0
def test_cache_never_exceeds_its_entry_cap():
cache = TtlCache(max_entries=8)
for index in range(500):
cache.put(index, index)
assert len(cache) <= 8
assert cache.get(499) == 499
assert cache.get(0) is None
def test_cache_evicts_the_least_recently_used():
cache = TtlCache(max_entries=2)
cache.put("a", 1)
cache.put("b", 2)
assert cache.get("a") == 1
cache.put("c", 3)
assert cache.get("b") is None
assert cache.get("a") == 1
def test_limiter_allows_exactly_the_cap_then_refuses():
limiter = UserRateLimiter(max_calls=5, window_seconds=60)
results = [limiter.allow("u1") for _ in range(8)]
assert results == [True] * 5 + [False] * 3
def test_limiter_is_per_user():
limiter = UserRateLimiter(max_calls=1, window_seconds=60)
assert limiter.allow("u1") is True
assert limiter.allow("u1") is False
assert limiter.allow("u2") is True
def test_limiter_window_slides(monkeypatch):
clock = [0.0]
monkeypatch.setattr(limits.time, "monotonic", lambda: clock[0])
limiter = UserRateLimiter(max_calls=2, window_seconds=60)
assert limiter.allow("u") and limiter.allow("u")
assert limiter.allow("u") is False
clock[0] += 61
assert limiter.allow("u") is True
def test_limiter_bounds_the_number_of_tracked_users():
limiter = UserRateLimiter(max_calls=1, window_seconds=60, max_tracked=16)
for index in range(1000):
limiter.allow(f"user-{index}")
assert len(limiter._calls) <= 16
def test_refused_calls_do_not_extend_the_window(monkeypatch):
clock = [0.0]
monkeypatch.setattr(limits.time, "monotonic", lambda: clock[0])
limiter = UserRateLimiter(max_calls=1, window_seconds=60)
assert limiter.allow("u") is True
for _ in range(50):
clock[0] += 1
assert limiter.allow("u") is False
clock[0] += 11
assert limiter.allow("u") is True
+80
View File
@@ -0,0 +1,80 @@
# retoor <retoor@molodetz.nl>
import pytest
from tests.conftest import run_async
from devplacepy.database import set_setting
from devplacepy.services.giphy import runtime
from devplacepy.services.giphy.client import GiphyError
from devplacepy.services.giphy.config import CONFIG_FIELDS, giphy_config
from devplacepy.services.giphy.fake import FakeGiphyClient
from devplacepy.services.giphy.service import GiphyService
@pytest.fixture
def settings(local_db):
yield
for key in ("giphy_api_key", "giphy_base_url", "giphy_rating"):
set_setting(key, "")
runtime.set_client(None)
def test_config_fields_declare_the_secret_key_and_rating_choices():
fields = {field.key: field for field in CONFIG_FIELDS}
assert fields["giphy_api_key"].secret is True
assert fields["giphy_api_key"].default == ""
assert fields["giphy_rating"].options == ["g", "pg", "pg-13", "r"]
assert GiphyService.config_fields is CONFIG_FIELDS
assert GiphyService.default_enabled is False
def test_config_reads_settings_and_applies_defaults(settings):
assert giphy_config().is_configured is False
set_setting("giphy_api_key", "k")
set_setting("giphy_rating", "")
config = giphy_config()
assert config.is_configured is True
assert config.rating == "pg"
assert config.base_url.startswith("https://api.giphy.com")
def test_run_once_is_quiet_without_a_key(settings):
async def body():
service = GiphyService()
runtime.set_client(FakeGiphyClient())
await service.run_once()
run_async(body())
def test_run_once_probes_trending_when_configured(settings):
async def body():
set_setting("giphy_api_key", "k")
calls = []
class Probe(FakeGiphyClient):
async def trending(self, limit=24):
calls.append(limit)
return await super().trending(limit)
runtime.set_client(Probe())
await GiphyService().run_once()
assert calls == [1]
run_async(body())
def test_run_once_survives_an_upstream_failure(settings):
async def body():
set_setting("giphy_api_key", "k")
class Broken(FakeGiphyClient):
async def trending(self, limit=24):
raise GiphyError("down", status=502)
runtime.set_client(Broken())
await GiphyService().run_once()
run_async(body())
@@ -3006,3 +3006,207 @@ def test_responses_protocol_default_provider(local_db, monkeypatch):
assert resp.status_code == 200
payload = json.loads(bytes(resp.body).decode())
assert payload["object"] == "chat.completion"
EMULATED_CALL_TEXT_openai_gateway = (
"Sure.\n<tool_call>\n"
'{"name": "bash", "arguments": {"command": "ls"}}\n'
"</tool_call>"
)
BASH_TOOL_openai_gateway = {
"type": "function",
"function": {
"name": "bash",
"description": "Run a shell command",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
}
def _emulated_obj_openai_gateway(model, text):
obj = _resp_obj_openai_gateway(model)
obj["output"][0]["content"][0]["text"] = text
return obj
class FakeEmulatedClient_openai_gateway(FakeResponsesClient_openai_gateway):
text = EMULATED_CALL_TEXT_openai_gateway
async def send(self, request, stream=False):
self.calls.append((request.url, request.json_body))
model = (request.json_body or {}).get("model", "")
obj = _emulated_obj_openai_gateway(model, self.text)
events = [{"type": "response.created", "response": {"id": "resp_1"}}]
for i in range(0, len(self.text), 4):
events.append(
{"type": "response.output_text.delta", "delta": self.text[i : i + 4]}
)
events.append({"type": "response.completed", "response": obj})
return FakeResponsesResp_openai_gateway(payload=obj, events=events)
def _setup_emulated_route_openai_gateway():
from devplacepy.services.openai_gateway import routing
routing.provider_store.set(
routing.ProviderIn(
name="gwmeta",
base_url="https://api.meta.ai/v1/responses",
api_key="sk-test",
protocol="responses",
)
)
routing.model_store.set(
routing.ModelRouteIn(
source_model="meta-chat",
provider="gwmeta",
target_model="muse-spark-1.3-contributor",
kind="chat",
tools_mode="emulated",
)
)
def _emulated_chat_openai_gateway(monkeypatch, body, **tools):
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmulatedClient_openai_gateway)
_setup_emulated_route_openai_gateway()
svc = GatewayService()
cfg = svc.effective_config()
cfg["gateway_vision_enabled"] = False
rt = svc.runtime()
resp = run_async(rt.handle_chat(body, cfg, ("guest", "test"), "test", "default"))
return rt, resp
def test_emulated_tools_meta_responses_nonstream(local_db, monkeypatch):
body = _chat_body_openai_gateway(tools=[BASH_TOOL_openai_gateway], tool_choice="auto")
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
url, sent = rt._client.calls[-1]
assert str(url) == "https://api.meta.ai/v1/responses"
assert "tools" not in sent and "tool_choice" not in sent
assert "<tools>" in sent["instructions"] and '"bash"' in sent["instructions"]
assert resp.status_code == 200
message = json.loads(bytes(resp.body).decode())["choices"][0]
assert message["finish_reason"] == "tool_calls"
call = message["message"]["tool_calls"][0]
assert call["function"]["name"] == "bash"
assert json.loads(call["function"]["arguments"]) == {"command": "ls"}
assert message["message"]["content"] == "Sure."
finally:
_teardown_responses_route_openai_gateway()
def test_emulated_tools_meta_responses_stream(local_db, monkeypatch):
body = _chat_body_openai_gateway(
stream=True, tools=[BASH_TOOL_openai_gateway]
)
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
async def drain():
out = []
async for chunk in resp.body_iterator:
out.append(chunk if isinstance(chunk, str) else chunk.decode())
return "".join(out)
raw = run_async(drain())
chunks = [
json.loads(line[5:])
for line in raw.splitlines()
if line.startswith("data:") and "[DONE]" not in line
]
text = "".join(
c["choices"][0]["delta"].get("content", "")
for c in chunks
if c.get("choices")
)
calls = [
tc
for c in chunks
for choice in c.get("choices") or []
for tc in choice["delta"].get("tool_calls") or []
]
assert "<tool_call>" not in text and text.strip() == "Sure."
assert len(calls) == 1 and calls[0]["function"]["name"] == "bash"
finishes = [
c["choices"][0]["finish_reason"]
for c in chunks
if c.get("choices") and c["choices"][0].get("finish_reason")
]
assert finishes[-1] == "tool_calls"
assert raw.rstrip().endswith("[DONE]")
finally:
_teardown_responses_route_openai_gateway()
def test_emulated_tools_convert_history_for_the_meta_transpiler(local_db, monkeypatch):
history = [
{"role": "user", "content": "go"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "bash", "arguments": '{"command": "ls"}'},
}
],
},
{"role": "tool", "tool_call_id": "call_1", "content": "a.txt"},
]
body = _chat_body_openai_gateway(messages=history, tools=[BASH_TOOL_openai_gateway])
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
_, sent = rt._client.calls[-1]
assert resp.status_code == 200
assert all(item.get("type") == "message" for item in sent["input"])
joined = json.dumps(sent["input"])
assert "<tool_call>" in joined and "<tool_response>" in joined and "a.txt" in joined
assert "function_call" not in joined
finally:
_teardown_responses_route_openai_gateway()
def test_emulated_tools_over_the_cap_is_a_400_with_no_upstream_call(local_db, monkeypatch):
from devplacepy.services.openai_gateway import tool_emulation
body = _chat_body_openai_gateway(
tools=[BASH_TOOL_openai_gateway] * (tool_emulation.MAX_TOOLS + 1)
)
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
assert resp.status_code == 400
assert rt._client.calls == []
finally:
_teardown_responses_route_openai_gateway()
def test_native_route_keeps_forwarding_tools(local_db, monkeypatch):
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway)
FakeResponsesClient_openai_gateway.behavior = "ok"
_setup_responses_route_openai_gateway()
try:
svc = GatewayService()
cfg = svc.effective_config()
cfg["gateway_vision_enabled"] = False
rt = svc.runtime()
run_async(
rt.handle_chat(
_chat_body_openai_gateway(tools=[BASH_TOOL_openai_gateway]),
cfg,
("guest", "test"),
"test",
"default",
)
)
_, sent = rt._client.calls[-1]
assert "tools" in sent
assert "<tools>" not in sent["instructions"]
finally:
_teardown_responses_route_openai_gateway()
@@ -0,0 +1,286 @@
# retoor <retoor@molodetz.nl>
import json
import pytest
from devplacepy.services.openai_gateway import tool_emulation as te
BASH = {
"type": "function",
"function": {
"name": "bash",
"description": "Run a shell command",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
}
def _payload(**over):
body = {
"model": "m",
"messages": [
{"role": "system", "content": "Be terse."},
{"role": "user", "content": "list files"},
],
"tools": [BASH],
}
body.update(over)
return body
def _block(name="bash", arguments=None):
arguments = {"command": "ls"} if arguments is None else arguments
return f'{te.CALL_OPEN}\n{json.dumps({"name": name, "arguments": arguments})}\n{te.CALL_CLOSE}'
def test_request_strips_native_fields_and_injects_catalog():
out = te.emulate_request(_payload(tool_choice="auto", parallel_tool_calls=False))
for key in te.REMOVED_PAYLOAD_KEYS:
assert key not in out
system = out["messages"][0]["content"]
assert system.startswith("Be terse.")
assert '"name": "bash"' in system
assert te.CALL_OPEN in system
assert "at most one tool call" in system
assert out["stop"] == [te.RESPONSE_OPEN]
def test_request_does_not_mutate_the_input():
original = _payload()
snapshot = json.dumps(original, sort_keys=True)
te.emulate_request(original)
assert json.dumps(original, sort_keys=True) == snapshot
def test_system_message_is_created_when_absent():
out = te.emulate_request(_payload(messages=[{"role": "user", "content": "hi"}]))
assert out["messages"][0]["role"] == "system"
assert "<tools>" in out["messages"][0]["content"]
def test_tool_choice_none_leaves_the_catalog_out():
out = te.emulate_request(_payload(tool_choice="none"))
assert "<tools>" not in out["messages"][0]["content"]
def test_tool_choice_required_and_named_add_a_directive():
required = te.emulate_request(_payload(tool_choice="required"))
assert "MUST call at least one tool" in required["messages"][0]["content"]
named = te.emulate_request(
_payload(tool_choice={"type": "function", "function": {"name": "bash"}})
)
assert 'MUST call the tool "bash"' in named["messages"][0]["content"]
def test_history_roundtrips_into_the_same_format_the_model_emits():
history = [
{"role": "user", "content": "go"},
{
"role": "assistant",
"content": "ok",
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "bash", "arguments": '{"command": "ls"}'},
},
{
"id": "call_2",
"type": "function",
"function": {"name": "bash", "arguments": '{"command": "pwd"}'},
},
],
},
{"role": "tool", "tool_call_id": "call_1", "content": "a.txt"},
{"role": "tool", "tool_call_id": "call_2", "content": "/tmp"},
]
out = te.emulate_request(_payload(messages=history))
roles = [m["role"] for m in out["messages"]]
assert roles == ["system", "user", "assistant", "user"]
assistant = out["messages"][2]
assert "tool_calls" not in assistant
assert assistant["content"].count(te.CALL_OPEN) == 2
results = out["messages"][3]["content"]
assert results.count(te.RESPONSE_OPEN) == 2
assert '"name": "bash"' in results and "a.txt" in results
def test_caps_reject_loudly():
with pytest.raises(te.ToolEmulationError):
te.emulate_request(_payload(tools=[BASH] * (te.MAX_TOOLS + 1)))
huge = json.loads(json.dumps(BASH))
huge["function"]["description"] = "x" * (te.MAX_CATALOG_CHARS + 1)
with pytest.raises(te.ToolEmulationError):
te.emulate_request(_payload(tools=[huge]))
with pytest.raises(te.ToolEmulationError):
te.emulate_request(_payload(tools=[{"type": "retrieval"}]))
def test_existing_stop_sequences_are_kept_and_capped():
out = te.emulate_request(_payload(stop="END"))
assert out["stop"] == ["END", te.RESPONSE_OPEN]
full = te.emulate_request(_payload(stop=["a", "b", "c", "d"]))
assert full["stop"] == ["a", "b", "c", "d"]
def test_request_without_tools_or_tool_history_is_untouched():
plain = {"model": "m", "messages": [{"role": "user", "content": "hi"}]}
assert te.emulate_request(plain) is plain
def test_parse_extracts_calls_and_remaining_text():
declared = te.declared_tools(_payload())
text = "Sure.\n" + _block() + "\n" + _block(arguments={"command": "pwd"})
remaining, calls, warnings = te.parse_tool_calls(text, declared)
assert remaining == "Sure."
assert [json.loads(c["function"]["arguments"])["command"] for c in calls] == ["ls", "pwd"]
assert all(c["id"].startswith("call_") and c["type"] == "function" for c in calls)
assert warnings == []
def test_parse_is_tolerant_of_fences_arrays_and_unterminated_blocks():
declared = te.declared_tools(_payload())
fenced = f'{te.CALL_OPEN}\n```json\n{{"name": "bash", "arguments": {{"command": "ls"}}}}\n```\n{te.CALL_CLOSE}'
assert len(te.parse_tool_calls(fenced, declared)[1]) == 1
array = f'{te.CALL_OPEN}[{{"name":"bash","arguments":{{"command":"a"}}}},{{"name":"bash","arguments":{{"command":"b"}}}}]{te.CALL_CLOSE}'
assert len(te.parse_tool_calls(array, declared)[1]) == 2
unterminated = f'{te.CALL_OPEN}{{"name": "bash", "arguments": {{"command": "ls"}}}}'
assert len(te.parse_tool_calls(unterminated, declared)[1]) == 1
string_args = f'{te.CALL_OPEN}{{"name":"bash","arguments":"{{\\"command\\": \\"ls\\"}}"}}{te.CALL_CLOSE}'
assert len(te.parse_tool_calls(string_args, declared)[1]) == 1
def test_parse_rejects_invalid_calls_loudly_and_keeps_the_text():
declared = te.declared_tools(_payload())
for bad in (
_block(name="nope"),
_block(arguments={}),
f"{te.CALL_OPEN}not json{te.CALL_CLOSE}",
):
remaining, calls, warnings = te.parse_tool_calls(bad, declared)
assert calls == []
assert warnings
assert te.CALL_OPEN in remaining
def test_parallel_and_per_turn_caps():
declared = te.declared_tools(_payload())
text = "".join(_block() for _ in range(3))
_, calls, warnings = te.parse_tool_calls(text, declared, allow_parallel=False)
assert len(calls) == 1 and warnings
many = "".join(_block() for _ in range(te.MAX_CALLS_PER_TURN + 3))
assert len(te.parse_tool_calls(many, declared)[1]) == te.MAX_CALLS_PER_TURN
def test_convert_completion_builds_openai_tool_calls():
declared = te.declared_tools(_payload())
completion = {"choices": [{"message": {"role": "assistant", "content": "x\n" + _block()}, "finish_reason": "stop"}]}
out, warnings = te.convert_completion(completion, declared)
choice = out["choices"][0]
assert choice["finish_reason"] == "tool_calls"
assert choice["message"]["content"] == "x"
assert choice["message"]["tool_calls"][0]["function"]["name"] == "bash"
assert warnings == []
def test_convert_completion_with_only_a_call_has_null_content():
declared = te.declared_tools(_payload())
completion = {"choices": [{"message": {"content": _block()}, "finish_reason": "stop"}]}
out, _ = te.convert_completion(completion, declared)
assert out["choices"][0]["message"]["content"] is None
def _chunk(content=None, finish=None, **extra):
delta = {} if content is None else {"content": content}
return {
"id": "c",
"object": "chat.completion.chunk",
"model": "m",
"choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
**extra,
}
def _run_stream(pieces, declared=None, finish="stop", allow_parallel=True):
stream = te.ToolCallStream(declared or te.declared_tools(_payload()), allow_parallel)
out = []
for piece in pieces:
out.extend(stream.feed(_chunk(piece)))
out.extend(stream.feed(_chunk(None, finish)))
return out
def _texts(chunks):
return "".join(
c["choices"][0]["delta"].get("content", "") for c in chunks if c.get("choices")
)
def _calls(chunks):
found = []
for c in chunks:
for choice in c.get("choices") or []:
found.extend(choice["delta"].get("tool_calls") or [])
return found
def test_stream_passes_plain_text_through_unchanged():
out = _run_stream(["hel", "lo ", "world"])
assert _texts(out) == "hello world"
assert _calls(out) == []
assert out[-1]["choices"][0]["finish_reason"] == "stop"
def test_stream_converts_a_call_split_across_arbitrary_chunk_boundaries():
full = "Sure.\n" + _block()
for size in (1, 2, 3, 5, 7, 50):
pieces = [full[i : i + size] for i in range(0, len(full), size)]
out = _run_stream(pieces)
assert _texts(out).strip() == "Sure."
calls = _calls(out)
assert len(calls) == 1
assert calls[0]["function"]["name"] == "bash"
assert json.loads(calls[0]["function"]["arguments"]) == {"command": "ls"}
assert calls[0]["index"] == 0
assert out[-1]["choices"][0]["finish_reason"] == "tool_calls"
def test_stream_never_leaks_call_markup_into_content():
out = _run_stream(["a <tool", "_call>", '{"name":"bash","arguments":{"command":"ls"}}', "</tool_call>"])
assert te.CALL_OPEN not in _texts(out)
def test_stream_emits_a_lookalike_prefix_that_never_became_a_call():
out = _run_stream(["use <tool", " carefully"])
assert _texts(out) == "use <tool carefully"
def test_stream_invalid_call_is_flushed_as_text_with_a_warning():
stream = te.ToolCallStream(te.declared_tools(_payload()))
out = []
for piece in [_block(name="nope")]:
out.extend(stream.feed(_chunk(piece)))
out.extend(stream.feed(_chunk(None, "stop")))
assert _calls(out) == []
assert te.CALL_OPEN in _texts(out)
assert stream.warnings
def test_stream_without_a_finish_chunk_flushes_on_finish():
stream = te.ToolCallStream(te.declared_tools(_payload()))
out = stream.feed(_chunk(_block()))
out.extend(stream.finish())
assert len(_calls(out)) == 1
def test_stream_keeps_usage_chunks_and_other_deltas():
stream = te.ToolCallStream(te.declared_tools(_payload()))
usage = {"id": "c", "choices": [], "usage": {"total_tokens": 3}}
assert stream.feed(usage) == [usage]
out = stream.feed({"id": "c", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}]})
assert out[0]["choices"][0]["delta"] == {"role": "assistant"}