DevPlace CI / test (push) Failing after 1h5m29s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
396 lines
15 KiB
Python
396 lines
15 KiB
Python
# 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"
|