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.
113 lines
3.1 KiB
Python
113 lines
3.1 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import asyncio
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
import requests
|
|
import websockets
|
|
|
|
from tests.conftest import BASE_URL, PORT
|
|
from devplacepy.database import get_table, refresh_snapshot, set_setting
|
|
|
|
WS_URL = f"ws://127.0.0.1:{PORT}/messages/ws"
|
|
JSON = {"Accept": "application/json"}
|
|
_counter = [0]
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
def _wsticket_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 _unique(prefix="wst"):
|
|
_counter[0] += 1
|
|
return f"{prefix}{int(time.time() * 1000)}{_counter[0]}"
|
|
|
|
|
|
def _signup():
|
|
name = _unique()
|
|
s = requests.Session()
|
|
s.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,
|
|
)
|
|
return s, name
|
|
|
|
|
|
def _uid(name):
|
|
refresh_snapshot()
|
|
return get_table("users").find_one(username=name)["uid"]
|
|
|
|
|
|
async def _recv_until(ws, frame_type, timeout=5.0):
|
|
deadline = asyncio.get_event_loop().time() + timeout
|
|
while True:
|
|
remaining = deadline - asyncio.get_event_loop().time()
|
|
if remaining <= 0:
|
|
raise AssertionError(f"timed out waiting for {frame_type}")
|
|
raw = await asyncio.wait_for(ws.recv(), timeout=remaining)
|
|
frame = json.loads(raw)
|
|
if frame.get("type") == frame_type:
|
|
return frame
|
|
|
|
|
|
def test_ws_ticket_requires_auth_401_for_json(app_server):
|
|
r = requests.post(
|
|
f"{BASE_URL}/messages/ws-ticket", headers=JSON, allow_redirects=False
|
|
)
|
|
assert r.status_code == 401
|
|
|
|
|
|
def test_ws_ticket_requires_auth_redirects_browser(app_server):
|
|
r = requests.post(f"{BASE_URL}/messages/ws-ticket", allow_redirects=False)
|
|
assert r.status_code == 303
|
|
|
|
|
|
def test_ws_ticket_issued_for_logged_in_session(app_server):
|
|
session, _ = _signup()
|
|
r = session.post(f"{BASE_URL}/messages/ws-ticket", headers=JSON)
|
|
assert r.status_code == 200, r.text[:300]
|
|
data = r.json()
|
|
assert data["ticket"]
|
|
assert data["expires_in"] == 30
|
|
|
|
|
|
def test_ws_ticket_single_use_and_authenticates_owner(app_server):
|
|
session, name = _signup()
|
|
owner_uid = _uid(name)
|
|
|
|
ticket = session.post(f"{BASE_URL}/messages/ws-ticket", headers=JSON).json()[
|
|
"ticket"
|
|
]
|
|
|
|
async def first_connect():
|
|
async with websockets.connect(f"{WS_URL}?ticket={ticket}") as ws:
|
|
ready = await _recv_until(ws, "ready")
|
|
assert ready["user_uid"] == owner_uid
|
|
|
|
asyncio.run(first_connect())
|
|
|
|
async def second_connect():
|
|
async with websockets.connect(f"{WS_URL}?ticket={ticket}") as ws:
|
|
with pytest.raises(websockets.exceptions.ConnectionClosed):
|
|
await asyncio.wait_for(ws.recv(), timeout=3.0)
|
|
|
|
asyncio.run(second_connect())
|