forked from retoor/devplacepy
Implements the moderation and consent obligations a social platform carries, so the web version and any client that speaks to it enforce the same rules. Moderation core (services/moderation/, database/moderation.py): a reportable target registry, the content filter and its choke points, the report queue with atomic resolution, enforcement actions, consent tracking, maturity gating, and account deletion with a grace window. Surfaces: POST /reports plus the member report list, /admin/moderation and the per-report admin view, /workspaces, terms acceptance at /auth/terms, consent and account deletion under /profile, the report button and dialog partials, the maturity gate, and the moderation stylesheet and ReportDialog client. Every user-generated surface stays reportable by construction: new content tables are registered in REPORTABLE_TARGETS or listed in UNREPORTABLE_TABLES with a reason, and the registry test fails the suite on anything left unclassified. Docs: community guidelines, content moderation, intellectual property, privacy, terms, contact, and the admin-only moderation operations page, plus the moderation API group and the Devii moderation actions. Compliance record: applecomp.md is the requirement register, applechanges.md the gap analysis against this codebase, and appleimpl.md the implementation design they resolve to. Tests cover the report flow, admin moderation, consent, account deletion, terms acceptance, workspaces, and the registry invariant across the unit, api, and e2e tiers.
137 lines
4.7 KiB
Python
137 lines
4.7 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import io
|
|
import uuid
|
|
import requests
|
|
from PIL import Image
|
|
from tests.conftest import BASE_URL
|
|
from devplacepy.database import get_table
|
|
JSON_media = {"Accept": "application/json"}
|
|
def _png_bytes_media(color=(200, 30, 30)):
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (8, 8), color).save(buf, "PNG")
|
|
return buf.getvalue()
|
|
def _signup_media(prefix="media"):
|
|
s = requests.Session()
|
|
name = f"{prefix}_{uuid.uuid4().hex[:10]}"
|
|
s.post(
|
|
f"{BASE_URL}/auth/signup",
|
|
data={
|
|
"username": name,
|
|
"email": f"{name}@test.devplace",
|
|
"password": "secret123",
|
|
"confirm_password": "secret123",
|
|
"birth_date": "1990-01-01",
|
|
"accept_terms": "1",
|
|
},
|
|
allow_redirects=True,
|
|
)
|
|
return s, name
|
|
def _login_media(seeded_db, who):
|
|
s = requests.Session()
|
|
creds = seeded_db[who]
|
|
s.post(
|
|
f"{BASE_URL}/auth/login",
|
|
data={"email": creds["email"], "password": creds["password"]},
|
|
allow_redirects=True,
|
|
)
|
|
return s
|
|
def _upload_media(s, name="pic.png", content=None, mime="image/png"):
|
|
files = {"file": (name, content if content is not None else _png_bytes_media(), mime)}
|
|
r = s.post(f"{BASE_URL}/uploads/upload", files=files)
|
|
assert r.status_code == 201, r.text
|
|
return r.json()["uid"]
|
|
def _create_post_media(s, attachment_uids, title="media post"):
|
|
r = s.post(
|
|
f"{BASE_URL}/posts/create",
|
|
data={
|
|
"content": "A post that carries media for the gallery tests.",
|
|
"title": title,
|
|
"topic": "random",
|
|
"attachment_uids": attachment_uids,
|
|
},
|
|
allow_redirects=True,
|
|
)
|
|
assert r.status_code == 200, r.text[:300]
|
|
assert "/posts/" in r.url, r.url
|
|
return r.url
|
|
def _create_project_media(s, attachment_uids, title="Media Project"):
|
|
r = s.post(
|
|
f"{BASE_URL}/projects/create",
|
|
data={
|
|
"title": title,
|
|
"description": "Project with media for gallery tests.",
|
|
"project_type": "software",
|
|
"platforms": "linux",
|
|
"status": "In Development",
|
|
"attachment_uids": attachment_uids,
|
|
},
|
|
allow_redirects=True,
|
|
)
|
|
assert r.status_code == 200, r.text[:300]
|
|
assert "/projects/" in r.url, r.url
|
|
return r.url
|
|
def _media_json(session, username):
|
|
r = session.get(f"{BASE_URL}/profile/{username}?tab=media", headers=JSON_media)
|
|
assert r.status_code == 200, r.text[:300]
|
|
return r.json()
|
|
def _media_uids(session, username):
|
|
return [m["uid"] for m in _media_json(session, username)["media"]]
|
|
def _seed_media_via_browser(page, title="ui media"):
|
|
up = page.request.post(
|
|
f"{BASE_URL}/uploads/upload",
|
|
multipart={
|
|
"file": {
|
|
"name": "pic.png",
|
|
"mimeType": "image/png",
|
|
"buffer": _png_bytes_media(),
|
|
}
|
|
},
|
|
)
|
|
assert up.ok, up.text()
|
|
uid = up.json()["uid"]
|
|
post = page.request.post(
|
|
f"{BASE_URL}/posts/create",
|
|
form={
|
|
"content": "media for the ui gallery test",
|
|
"title": title,
|
|
"topic": "random",
|
|
"attachment_uids": uid,
|
|
},
|
|
)
|
|
assert post.ok, post.text()
|
|
return uid
|
|
def _media_uids_via_page(page, username):
|
|
resp = page.request.get(f"{BASE_URL}/profile/{username}?tab=media", headers=JSON_media)
|
|
return [m["uid"] for m in resp.json()["media"]]
|
|
|
|
|
|
def test_restore_requires_admin(seeded_db):
|
|
owner, name = _signup_media()
|
|
uid = _upload_media(owner)
|
|
_create_post_media(owner, uid)
|
|
owner.post(f"{BASE_URL}/media/{uid}/delete", headers=JSON_media)
|
|
# a plain member may not restore
|
|
member = _login_media(seeded_db, "bob")
|
|
r = member.post(f"{BASE_URL}/media/{uid}/restore", headers=JSON_media)
|
|
assert r.status_code in (302, 303, 403)
|
|
assert uid not in _media_uids(owner, name)
|
|
|
|
|
|
def test_admin_restore_brings_media_back(seeded_db):
|
|
owner, name = _signup_media()
|
|
uid = _upload_media(owner)
|
|
post_url = _create_post_media(owner, uid)
|
|
owner.post(f"{BASE_URL}/media/{uid}/delete", headers=JSON_media)
|
|
assert uid not in _media_uids(owner, name)
|
|
|
|
admin = _login_media(seeded_db, "alice")
|
|
r = admin.post(f"{BASE_URL}/media/{uid}/restore", headers=JSON_media)
|
|
assert r.status_code == 200
|
|
# reappears in the gallery, and its relation to the parent post is intact
|
|
# (delete never cleared target_type/target_uid), so it is back on the post too
|
|
restored = {m["uid"]: m for m in _media_json(owner, name)["media"]}
|
|
assert uid in restored
|
|
assert restored[uid]["target_type"] == "post"
|
|
assert post_url.endswith(restored[uid]["target_url"])
|