Files
siftlode/scripts/e2e_seed.py
T
peter 6d8b7be6d8 feat!: remove the shared demo account
The demo account — one shared user that whitelisted emails entered from
the login page, reset on a schedule — goes end to end (owner decision,
E34): demo.py and the reset job, users.is_demo and demo_whitelist
(migration 0098 deletes the demo user, its audit rows, the reset job's
override and notices, then drops the column and table; the downgrade
restores the empty schema), the /auth/demo entry, the admin whitelist and
reset, the Demo tab, the banner, the login page's "Try the demo", and
every is_demo branch in routes, userscope, scheduler and the UI.

require_human only ever refused the demo account, so its 87 uses become
current_user (owner-approved); require_write_scope keeps its own check.
The login page's "or" divider now shows only beside the Google button:
without the demo entry under it, an instance without Google would have
said "or" to nothing.

A contract test (removedFeatures) fails on the word anywhere in the
backend, frontend, e2e or scripts — seen red on a planted hit. Size
ceilings tightened for the modules that shrank; admin.py is back under
the limit.
2026-10-05 00:19:38 +02:00

193 lines
7.2 KiB
Python

"""Idempotent seed for the E2E characterization (golden-master) account.
Run INSIDE the api container — it needs the app's models + the argon2 hasher:
docker exec -i siftlode-api-1 python - < scripts/e2e_seed.py
Resets a dedicated test account to a known baseline every run, so the Playwright
golden-master net is deterministic and never touches a real account. Self-healing: a crashed
spec that left the account dirty is cleaned on the next run. Prints one JSON line describing
what it seeded (the wrapper / global-setup reads it).
Credentials come from env: E2E_EMAIL (default e2e@siftlode.local) and E2E_PASSWORD (REQUIRED, no
default — the ops wrapper exports it from the secret store); the account
lives only in the localdev DB.
"""
import json
import os
import sys
from datetime import datetime, timezone
sys.path.insert(0, "/app")
from sqlalchemy import delete, func, select
from app.db import SessionLocal
from app.models import (
AppState,
Channel,
Playlist,
PlaylistItem,
Subscription,
User,
Video,
VideoState,
)
from app.security import hash_password
EMAIL = os.environ.get("E2E_EMAIL", "e2e@siftlode.local")
PASSWORD = os.environ.get("E2E_PASSWORD", "")
if not PASSWORD:
sys.exit(
"e2e_seed: E2E_PASSWORD is not set (no default by design) — run it via `siftlode e2e-seed`"
)
N_SUBS = 3
def _now() -> datetime:
return datetime.now(timezone.utc)
def main() -> None:
db = SessionLocal()
try:
# 0. Mark the instance configured so the first-run setup wizard is skipped. A prod→dev
# reseed (siftlode seed-dev-from-prod) TRUNCATEs app_state — it's excluded from the
# data allow-list, being local/operational state — which would otherwise drop the box
# back into "Setup needed" and block login/UAT. Since e2e_seed always leaves a loginable
# admin below, the instance is legitimately set up. Idempotent (mirrors state._row +
# state.mark_configured). The running api process caches `configured` one-way from False,
# so it re-reads the DB and promotes on the next request — no restart needed.
app_state = db.get(AppState, 1)
if app_state is None:
app_state = AppState(id=1, sync_paused=False)
db.add(app_state)
app_state.configured = True
app_state.setup_token_hash = None
db.commit()
# 1. Upsert the account and always reset it to a clean, loginable baseline
# (email_verified + is_active + not suspended + password set).
user = db.scalar(select(User).where(User.email == EMAIL))
if user is None:
user = User(email=EMAIL)
db.add(user)
user.password_hash = hash_password(PASSWORD)
user.email_verified = True
user.is_active = True
user.is_suspended = False
user.google_sub = None
# Admin so the E2E can reach the admin-only pages (scheduler / config / users / audit) and
# thus the render ternary's admin branches — the primary real user (npeter83) is admin too.
user.role = "admin"
user.display_name = "E2E Tester"
user.preferences = {"language": "en"}
db.commit()
# 2. Subscriptions: the top-N organic channels by catalog size — deterministic, and
# >1 so a channel filter visibly narrows the feed. Reset each run.
top = db.execute(
select(Video.channel_id, func.count().label("n"))
.join(Channel, Channel.id == Video.channel_id)
.where(Channel.from_search.is_(False), Channel.from_explore.is_(False))
.group_by(Video.channel_id)
.order_by(func.count().desc())
.limit(N_SUBS)
).all()
channel_ids = [row[0] for row in top]
db.execute(delete(Subscription).where(Subscription.user_id == user.id))
for cid in channel_ids:
db.add(Subscription(user_id=user.id, channel_id=cid, subscribed_at=_now()))
db.commit()
# 3. Deterministic watch state (reset each run):
# - one in-progress video (drives the resume bar + "in progress" filter) from the
# first channel — status stays "new" so it still counts as unwatched;
# - one WATCHED video from the second channel, so the "watched" show-filter is a
# real, non-empty narrowing of the feed (and the "all" vs "unwatched" split exists).
db.execute(delete(VideoState).where(VideoState.user_id == user.id))
def _newest(channel_id: str, exclude: set[str]) -> Video | None:
conds = [
Video.channel_id == channel_id,
Video.duration_seconds.is_not(None),
Video.live_status == "none",
]
if exclude:
conds.append(Video.id.not_in(exclude))
return db.scalar(
select(Video).where(*conds).order_by(Video.published_at.desc()).limit(1)
)
used: set[str] = set()
in_progress = None
watched = None
if channel_ids:
vid = _newest(channel_ids[0], used)
if vid is not None:
db.add(
VideoState(
user_id=user.id,
video_id=vid.id,
status="new",
position_seconds=42,
progress_updated_at=_now(),
)
)
in_progress = vid.id
used.add(vid.id)
if len(channel_ids) > 1:
wv = _newest(channel_ids[1], used)
if wv is not None:
db.add(
VideoState(
user_id=user.id,
video_id=wv.id,
status="watched",
watched_at=_now(),
)
)
watched = wv.id
used.add(wv.id)
# 4. One "saved" video (Watch-later membership drives the saved marker) from the last
# subscribed channel. Reset the built-in playlist's items each run.
wl = db.scalar(
select(Playlist).where(Playlist.user_id == user.id, Playlist.kind == "watch_later")
)
if wl is None:
wl = Playlist(user_id=user.id, name="Watch later", kind="watch_later")
db.add(wl)
db.commit()
db.execute(delete(PlaylistItem).where(PlaylistItem.playlist_id == wl.id))
saved = None
if channel_ids:
svid = _newest(channel_ids[-1], used)
if svid is not None:
db.add(PlaylistItem(playlist_id=wl.id, video_id=svid.id, position=0))
saved = svid.id
db.commit()
# T201: this script's stdout IS its interface — the e2e runner parses this JSON.
print( # noqa: T201
json.dumps(
{
"ok": True,
"user_id": user.id,
"email": EMAIL,
"channels": channel_ids,
"in_progress": in_progress,
"watched": watched,
"saved": saved,
}
)
)
finally:
db.close()
if __name__ == "__main__":
main()