2026-06-13 16:32:33 +02:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
|
2026-06-19 10:06:09 +02:00
|
|
|
from devplacepy.database import (
|
|
|
|
|
get_table,
|
|
|
|
|
get_primary_admin_uid,
|
|
|
|
|
invalidate_admins_cache,
|
|
|
|
|
)
|
2026-06-09 18:48:08 +02:00
|
|
|
from devplacepy.utils import (
|
2026-06-09 20:02:50 +02:00
|
|
|
award_badge,
|
|
|
|
|
award_xp,
|
2026-06-12 05:37:12 +02:00
|
|
|
get_badge,
|
2026-06-09 20:02:50 +02:00
|
|
|
check_milestone_badges,
|
|
|
|
|
create_mention_notifications,
|
|
|
|
|
extract_mentions,
|
2026-06-09 18:48:08 +02:00
|
|
|
generate_uid,
|
2026-06-09 20:02:50 +02:00
|
|
|
hash_password,
|
2026-06-17 16:08:28 +02:00
|
|
|
is_primary_admin,
|
2026-06-09 20:02:50 +02:00
|
|
|
level_for_xp,
|
|
|
|
|
safe_next,
|
2026-06-09 18:48:08 +02:00
|
|
|
slugify,
|
2026-06-09 20:02:50 +02:00
|
|
|
strip_html,
|
2026-06-09 18:48:08 +02:00
|
|
|
time_ago,
|
2026-06-09 20:02:50 +02:00
|
|
|
verify_password,
|
2026-06-09 18:48:08 +02:00
|
|
|
)
|
2026-05-14 04:12:19 +02:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2026-06-13 16:32:33 +02:00
|
|
|
def _seed_user(role="Member", xp=0, level=1):
|
|
|
|
|
uid = generate_uid()
|
|
|
|
|
username = f"ut_{uid[:8]}"
|
|
|
|
|
get_table("users").insert(
|
|
|
|
|
{
|
|
|
|
|
"uid": uid,
|
|
|
|
|
"username": username,
|
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-06-13 16:32:33 +02:00
|
|
|
"email": f"{username}@t.dev",
|
|
|
|
|
"role": role,
|
|
|
|
|
"xp": xp,
|
|
|
|
|
"level": level,
|
|
|
|
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return uid, username
|
2026-05-11 03:14:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_hash_password_returns_string():
|
|
|
|
|
result = hash_password("test123")
|
|
|
|
|
assert isinstance(result, str)
|
|
|
|
|
assert len(result) > 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_verify_password_correct():
|
|
|
|
|
hashed = hash_password("correct-password")
|
|
|
|
|
assert verify_password("correct-password", hashed) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_verify_password_wrong():
|
|
|
|
|
hashed = hash_password("correct-password")
|
|
|
|
|
assert verify_password("wrong-password", hashed) is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_uid_is_unique():
|
|
|
|
|
uid1 = generate_uid()
|
|
|
|
|
uid2 = generate_uid()
|
|
|
|
|
assert uid1 != uid2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_uid_format():
|
|
|
|
|
uid = generate_uid()
|
|
|
|
|
assert isinstance(uid, str)
|
|
|
|
|
assert len(uid) == 36 # UUID v4 format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_slugify_basic():
|
|
|
|
|
assert slugify("Hello World") == "hello-world"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_slugify_special_chars():
|
|
|
|
|
assert slugify("Hello! World???") == "hello-world"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_slugify_multiple_dashes():
|
|
|
|
|
assert slugify("hello---world") == "hello-world"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_slugify_leading_trailing():
|
|
|
|
|
assert slugify("--hello--") == "hello"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_time_ago_just_now():
|
2026-05-14 04:12:19 +02:00
|
|
|
now = datetime.now(timezone.utc).isoformat()
|
2026-05-11 03:14:43 +02:00
|
|
|
result = time_ago(now)
|
|
|
|
|
assert result == "just now"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_time_ago_minutes():
|
2026-05-14 04:12:19 +02:00
|
|
|
dt = (datetime.now(timezone.utc) - timedelta(minutes=5)).isoformat()
|
2026-05-11 03:14:43 +02:00
|
|
|
result = time_ago(dt)
|
|
|
|
|
assert "m ago" in result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_time_ago_hours():
|
2026-05-14 04:12:19 +02:00
|
|
|
dt = (datetime.now(timezone.utc) - timedelta(hours=3)).isoformat()
|
2026-05-11 03:14:43 +02:00
|
|
|
result = time_ago(dt)
|
|
|
|
|
assert "h ago" in result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_time_ago_days():
|
2026-05-14 04:12:19 +02:00
|
|
|
dt = (datetime.now(timezone.utc) - timedelta(days=5)).isoformat()
|
2026-05-11 03:14:43 +02:00
|
|
|
result = time_ago(dt)
|
|
|
|
|
assert "d ago" in result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_time_ago_months():
|
2026-06-10 05:22:44 +02:00
|
|
|
dt = datetime.now(timezone.utc) - timedelta(days=60)
|
|
|
|
|
result = time_ago(dt.isoformat())
|
|
|
|
|
assert result == dt.strftime("%d/%m/%Y")
|
2026-05-11 03:14:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_time_ago_years():
|
2026-06-10 05:22:44 +02:00
|
|
|
dt = datetime.now(timezone.utc) - timedelta(days=400)
|
|
|
|
|
result = time_ago(dt.isoformat())
|
|
|
|
|
assert result == dt.strftime("%d/%m/%Y")
|
feat: add leaderboard router, gamification backfill, and shared content creation helpers
Introduce a new `/leaderboard` endpoint ranking top 50 members by total stars, wire it into the app router and documentation. Implement `_backfill_gamification()` in `database.py` to compute XP/levels for existing accounts from prior posts, comments, votes, and follows. Extract `is_owner()`, `create_content_item()`, and `detail_context()` into `content.py` to centralize content creation, reward awarding, mention notifications, and attachment linking. Update comment creation/deletion in `comments.py` to use `award_rewards()` with `XP_COMMENT` and the new `is_owner()` helper. Add shared template partials documentation for vote bars, star buttons, post headers, and topic selectors to `AGENTS.md`.
2026-05-30 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_level_for_xp_boundaries():
|
|
|
|
|
assert level_for_xp(0) == 1
|
|
|
|
|
assert level_for_xp(99) == 1
|
|
|
|
|
assert level_for_xp(100) == 2
|
|
|
|
|
assert level_for_xp(250) == 3
|
|
|
|
|
assert level_for_xp(450) == 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_level_for_xp_negative():
|
|
|
|
|
assert level_for_xp(-50) == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_badge_info_known():
|
2026-06-12 05:37:12 +02:00
|
|
|
meta = get_badge("Star Author")
|
feat: add leaderboard router, gamification backfill, and shared content creation helpers
Introduce a new `/leaderboard` endpoint ranking top 50 members by total stars, wire it into the app router and documentation. Implement `_backfill_gamification()` in `database.py` to compute XP/levels for existing accounts from prior posts, comments, votes, and follows. Extract `is_owner()`, `create_content_item()`, and `detail_context()` into `content.py` to centralize content creation, reward awarding, mention notifications, and attachment linking. Update comment creation/deletion in `comments.py` to use `award_rewards()` with `XP_COMMENT` and the new `is_owner()` helper. Add shared template partials documentation for vote bars, star buttons, post headers, and topic selectors to `AGENTS.md`.
2026-05-30 20:16:39 +02:00
|
|
|
assert meta["icon"]
|
|
|
|
|
assert meta["description"] == "Earned 100 stars"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_badge_info_unknown_fallback():
|
2026-06-12 05:37:12 +02:00
|
|
|
meta = get_badge("Nonexistent Badge")
|
feat: add leaderboard router, gamification backfill, and shared content creation helpers
Introduce a new `/leaderboard` endpoint ranking top 50 members by total stars, wire it into the app router and documentation. Implement `_backfill_gamification()` in `database.py` to compute XP/levels for existing accounts from prior posts, comments, votes, and follows. Extract `is_owner()`, `create_content_item()`, and `detail_context()` into `content.py` to centralize content creation, reward awarding, mention notifications, and attachment linking. Update comment creation/deletion in `comments.py` to use `award_rewards()` with `XP_COMMENT` and the new `is_owner()` helper. Add shared template partials documentation for vote bars, star buttons, post headers, and topic selectors to `AGENTS.md`.
2026-05-30 20:16:39 +02:00
|
|
|
assert meta["icon"]
|
|
|
|
|
assert meta["description"] == "Nonexistent Badge"
|
2026-06-05 20:33:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_safe_next_allows_internal_rejects_external():
|
|
|
|
|
assert safe_next("/gists") == "/gists"
|
|
|
|
|
assert safe_next("//evil.com") == "/feed"
|
|
|
|
|
assert safe_next("http://evil.com") == "/feed"
|
|
|
|
|
assert safe_next(None) == "/feed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_strip_html_removes_tags_and_unescapes():
|
|
|
|
|
assert strip_html("<b>Hi</b> & bye") == "Hi & bye"
|
|
|
|
|
assert strip_html("") == ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_mentions_finds_usernames():
|
|
|
|
|
mentions = extract_mentions("hi @alice and (@bob_1), mail a@b.com")
|
|
|
|
|
assert mentions == ["alice", "bob_1"]
|
|
|
|
|
|
|
|
|
|
|
2026-06-17 16:08:28 +02:00
|
|
|
def _seed_user_at(role, created_at):
|
|
|
|
|
uid = generate_uid()
|
|
|
|
|
get_table("users").insert(
|
|
|
|
|
{
|
|
|
|
|
"uid": uid,
|
|
|
|
|
"username": f"pa_{uid[:8]}",
|
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-06-17 16:08:28 +02:00
|
|
|
"email": f"pa_{uid[:8]}@t.dev",
|
|
|
|
|
"role": role,
|
|
|
|
|
"created_at": created_at.isoformat(),
|
2026-07-26 19:58:18 +02:00
|
|
|
# every real account is created active; the primary administrator must be
|
|
|
|
|
# an account that can actually authenticate
|
|
|
|
|
"is_active": True,
|
|
|
|
|
"deleted_at": None,
|
2026-06-17 16:08:28 +02:00
|
|
|
}
|
|
|
|
|
)
|
2026-06-19 10:06:09 +02:00
|
|
|
invalidate_admins_cache()
|
2026-06-17 16:08:28 +02:00
|
|
|
return get_table("users").find_one(uid=uid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _demote_existing_admins():
|
|
|
|
|
existing = [r["uid"] for r in get_table("users").find(role="Admin")]
|
|
|
|
|
for uid in existing:
|
|
|
|
|
get_table("users").update({"uid": uid, "role": "Member"}, ["uid"])
|
2026-06-19 10:06:09 +02:00
|
|
|
invalidate_admins_cache()
|
2026-06-17 16:08:28 +02:00
|
|
|
return existing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _restore_admins(uids):
|
|
|
|
|
for uid in uids:
|
|
|
|
|
get_table("users").update({"uid": uid, "role": "Admin"}, ["uid"])
|
2026-06-19 10:06:09 +02:00
|
|
|
invalidate_admins_cache()
|
2026-06-17 16:08:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _purge(*rows):
|
|
|
|
|
for row in rows:
|
|
|
|
|
get_table("users").delete(uid=row["uid"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_primary_admin_is_earliest_admin(local_db):
|
|
|
|
|
restore = _demote_existing_admins()
|
|
|
|
|
member = admin_first = admin_second = None
|
|
|
|
|
try:
|
|
|
|
|
base = datetime.now(timezone.utc)
|
|
|
|
|
member = _seed_user_at("Member", base)
|
|
|
|
|
admin_first = _seed_user_at("Admin", base + timedelta(seconds=1))
|
|
|
|
|
admin_second = _seed_user_at("Admin", base + timedelta(seconds=2))
|
|
|
|
|
|
|
|
|
|
assert get_primary_admin_uid() == admin_first["uid"]
|
|
|
|
|
assert is_primary_admin(admin_first) is True
|
|
|
|
|
assert is_primary_admin(admin_second) is False
|
|
|
|
|
assert is_primary_admin(member) is False
|
|
|
|
|
assert is_primary_admin(None) is False
|
|
|
|
|
finally:
|
|
|
|
|
_purge(*[r for r in (member, admin_first, admin_second) if r])
|
|
|
|
|
_restore_admins(restore)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_primary_admin_reassigns_when_founder_demoted(local_db):
|
|
|
|
|
restore = _demote_existing_admins()
|
|
|
|
|
admin_first = admin_second = None
|
|
|
|
|
try:
|
|
|
|
|
base = datetime.now(timezone.utc)
|
|
|
|
|
admin_first = _seed_user_at("Admin", base + timedelta(seconds=1))
|
|
|
|
|
admin_second = _seed_user_at("Admin", base + timedelta(seconds=2))
|
|
|
|
|
assert get_primary_admin_uid() == admin_first["uid"]
|
|
|
|
|
|
|
|
|
|
get_table("users").update(
|
|
|
|
|
{"uid": admin_first["uid"], "role": "Member"}, ["uid"]
|
|
|
|
|
)
|
2026-06-19 10:06:09 +02:00
|
|
|
invalidate_admins_cache()
|
2026-06-17 16:08:28 +02:00
|
|
|
assert get_primary_admin_uid() == admin_second["uid"]
|
|
|
|
|
assert (
|
|
|
|
|
is_primary_admin(get_table("users").find_one(uid=admin_second["uid"]))
|
|
|
|
|
is True
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
is_primary_admin(get_table("users").find_one(uid=admin_first["uid"]))
|
|
|
|
|
is False
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
_purge(*[r for r in (admin_first, admin_second) if r])
|
|
|
|
|
_restore_admins(restore)
|
|
|
|
|
|
|
|
|
|
|
2026-06-05 20:33:35 +02:00
|
|
|
def test_award_badge_is_idempotent(local_db):
|
|
|
|
|
uid, _ = _seed_user()
|
|
|
|
|
assert award_badge(uid, "First Post") is True
|
|
|
|
|
assert award_badge(uid, "First Post") is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_award_xp_levels_up_and_notifies(local_db):
|
|
|
|
|
uid, _ = _seed_user(xp=95, level=1)
|
|
|
|
|
result = award_xp(uid, 10)
|
|
|
|
|
assert result["xp"] == 105
|
|
|
|
|
assert result["level"] == 2
|
|
|
|
|
assert result["leveled_up"] is True
|
|
|
|
|
assert get_table("notifications").count(user_uid=uid, type="level") == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_milestone_badges_awards_prolific(local_db):
|
|
|
|
|
uid, _ = _seed_user()
|
|
|
|
|
posts = get_table("posts")
|
|
|
|
|
for index in range(10):
|
|
|
|
|
post_uid = generate_uid()
|
2026-06-09 18:48:08 +02:00
|
|
|
posts.insert(
|
|
|
|
|
{
|
2026-06-14 16:46:36 +02:00
|
|
|
"deleted_at": None,
|
|
|
|
|
"deleted_by": None,
|
2026-06-09 18:48:08 +02:00
|
|
|
"uid": post_uid,
|
|
|
|
|
"user_uid": uid,
|
|
|
|
|
"slug": f"{post_uid[:8]}-p",
|
|
|
|
|
"title": None,
|
|
|
|
|
"content": f"milestone post {index}",
|
|
|
|
|
"topic": "random",
|
|
|
|
|
"project_uid": None,
|
|
|
|
|
"image": None,
|
|
|
|
|
"stars": 0,
|
|
|
|
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-06-05 20:33:35 +02:00
|
|
|
awarded = check_milestone_badges(uid)
|
|
|
|
|
assert "Prolific" in awarded
|
|
|
|
|
assert get_table("badges").find_one(user_uid=uid, badge_name="Prolific") is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_mention_notifications_targets_known_users(local_db):
|
|
|
|
|
actor_uid, actor_name = _seed_user()
|
|
|
|
|
target_uid, target_name = _seed_user()
|
|
|
|
|
create_mention_notifications(
|
|
|
|
|
f"hey @{target_name} and @{actor_name} and @ghost_zzz",
|
2026-06-09 18:48:08 +02:00
|
|
|
actor_uid,
|
|
|
|
|
"/posts/x",
|
2026-06-05 20:33:35 +02:00
|
|
|
)
|
|
|
|
|
assert get_table("notifications").count(user_uid=target_uid, type="mention") == 1
|
|
|
|
|
assert get_table("notifications").count(user_uid=actor_uid, type="mention") == 0
|