107 lines
2.8 KiB
Python
Raw Normal View History

2026-07-26 16:45:38 +02:00
# retoor <retoor@molodetz.nl>
import asyncio
from devplacepy.database import invalidate_admins_cache
from devplacepy.services.devii.tasks.context import in_task_run, task_run_scope
from devplacepy.services.devii.tasks.guards import nesting_allowed
from devplacepy.utils import generate_uid
SIGNUP_AFTER_PRIMARY_ADMIN = "2099-01-01T00:00:00"
2026-07-26 16:45:38 +02:00
from tests.conftest import run_async
def _account(local_db, role):
uid = generate_uid()
local_db["users"].insert(
{
"uid": uid,
"username": f"ctx-{uid[-10:]}",
Add the trust and safety subsystem and the App Store compliance work 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.
2026-08-09 00:18:20 +02:00
"terms_version": "1",
2026-07-26 16:45:38 +02:00
"role": role,
"deleted_at": None,
"created_at": SIGNUP_AFTER_PRIMARY_ADMIN,
2026-07-26 16:45:38 +02:00
}
)
invalidate_admins_cache()
return uid
def test_outside_a_task_run_the_flag_is_off():
assert in_task_run() is False
def test_scope_sets_and_restores_the_flag():
assert in_task_run() is False
with task_run_scope():
assert in_task_run() is True
assert in_task_run() is False
def test_scope_restores_the_flag_after_an_exception():
try:
with task_run_scope():
raise RuntimeError("boom")
except RuntimeError:
pass
assert in_task_run() is False
def test_nested_scopes_restore_correctly():
with task_run_scope():
with task_run_scope():
assert in_task_run() is True
assert in_task_run() is True
assert in_task_run() is False
def test_the_flag_reaches_work_spawned_inside_the_run():
seen = []
async def inner():
seen.append(in_task_run())
async def run():
with task_run_scope():
await inner()
task = asyncio.create_task(inner())
await task
run_async(run())
assert seen == [True, True]
def test_a_concurrent_turn_outside_the_run_never_sees_the_flag():
seen = {}
async def scheduled(gate):
with task_run_scope():
seen["inside"] = in_task_run()
gate.set()
await asyncio.sleep(0.05)
seen["inside_after"] = in_task_run()
async def interactive(gate):
await gate.wait()
seen["outside"] = in_task_run()
async def run():
gate = asyncio.Event()
await asyncio.gather(scheduled(gate), interactive(gate))
run_async(run())
assert seen["inside"] is True
assert seen["inside_after"] is True
assert seen["outside"] is False
def test_members_may_not_nest_but_administrators_may(local_db):
member = _account(local_db, "Member")
admin = _account(local_db, "Admin")
assert nesting_allowed(member) is True
assert nesting_allowed(admin) is True
with task_run_scope():
assert nesting_allowed(member) is False
assert nesting_allowed(admin) is True
assert nesting_allowed(member) is True