mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
feat: local E2E stack (fake workers + test API) + email-code sign-in (v0.275.0) (#1165)
## Summary - **Local stack:** `scripts/local-stack.sh up --frontend` runs the **real** backend on the Firestore + GCS emulators with `FAKE_WORKERS=1`. In-process fake workers move jobs through their real states in seconds, using tiny real media, so web Playwright and mobile Maestro full-journey tests can run offline against the same endpoints. - **Email-code sign-in** (nomad-mobile spec §4.1): `POST /api/users/auth/send-code` + `verify-code`. The code logic is shared with kjbox via `LoginCodeStore`, and sends are throttled per email and per IP. - **Bug fix found by the new suite:** `confirm-duration` moved jobs to `separating_stage1`, from which they can never reach review. It now resumes at `downloading`. No prod job had taken this path yet. ## Changes - `backend/services/fake_workers/`: dispatch seam in `WorkerService.trigger_*`, plus fake search, download, checkout and outbox, scenarios, and per-user config. The real stem upload, transcode, backing-vocals analysis and preview render are reused. Every check is strict (`is True`), so a MagicMock settings object never enables the fakes, and startup **refuses FAKE_WORKERS in production**. - `/api/test/*` router (mounted only in that mode): `status`, `scenario`, `fake-config`, `emails`, `reset`, plus the fake Stripe checkout page. Paying it runs the real webhook handler. - `frontend/e2e/local-stack/` (9 specs, ~50s): - sign-up → make → review → instrumental → download - buy and cancel credits - duration confirm, failed, complete and review states - code sign-in and lockout - New required CI job `local-stack-e2e`. - Docs: TESTING.md § Local stack, DEVELOPMENT.md, API.md, README, LESSONS-LEARNED. - `MAX_SIGNUPS_PER_IP` can now be set by env. Only the local stack raises it; production keeps the default of 2. ## Testing - [x] Backend unit: 6175 passed - [x] Emulator: 152 passed - [x] Frontend Jest: 1706 passed - [x] Local-stack Playwright: 9/9 passed (CI mode) ## Review - [x] CodeRabbit CLI review completed locally. 1 minor finding declined: it suggested swallowing infrastructure errors in send-code as fake "sent" responses, which would hide real outages from users. @coderabbitai ignore --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01AGuxtgoscF6RJGtpjHPg3W --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
eeb08578c9
commit
c277bc8ee3
84 files changed
+4698
-204
No files matched your search
+115
-1
@@ -772,6 +772,118 @@ jobs:
|
||||
path: backend/htmlcov/
|
||||
retention-days: 7
|
||||
|
||||
local-stack-e2e:
|
||||
name: "Local Stack - Full-Journey E2E"
|
||||
runs-on: ubuntu-latest
|
||||
needs: changes
|
||||
# The real backend on the Firestore + GCS emulators with fake workers
|
||||
# (scripts/local-stack.sh), driven through the real web UI by
|
||||
# frontend/e2e/local-stack. Fully offline: no secrets, no real services.
|
||||
if: github.ref != 'refs/heads/main' && (needs.changes.outputs.backend == 'true' || needs.changes.outputs.frontend == 'true' || needs.changes.outputs.deps == 'true')
|
||||
timeout-minutes: 40
|
||||
steps:
|
||||
- name: Free disk space (only if low)
|
||||
run: |
|
||||
df -h /
|
||||
AVAIL_GB=$(df -BG --output=avail / | tail -1 | tr -dc '0-9')
|
||||
if [ "$AVAIL_GB" -lt 30 ]; then
|
||||
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc \
|
||||
/usr/local/.ghcup /opt/hostedtoolcache/CodeQL
|
||||
df -h /
|
||||
fi
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: false
|
||||
|
||||
- name: Set up Python 3.13
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.13"
|
||||
|
||||
- name: Install Poetry
|
||||
uses: snok/install-poetry@v1
|
||||
with:
|
||||
virtualenvs-create: true
|
||||
virtualenvs-in-project: true
|
||||
|
||||
- name: Cache Poetry dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: .venv
|
||||
key: venv-backend-hosted-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
||||
restore-keys: |
|
||||
venv-backend-hosted-${{ runner.os }}-py3.13-
|
||||
|
||||
- name: Set up Java 21 (required by Firestore emulator)
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: "temurin"
|
||||
java-version: "21"
|
||||
|
||||
- name: Install Firestore emulator + ffmpeg
|
||||
run: |
|
||||
if ! grep -rq "packages.cloud.google.com" /etc/apt/sources.list.d/ 2>/dev/null; then
|
||||
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | \
|
||||
sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list
|
||||
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
|
||||
sudo gpg --dearmor --yes -o /usr/share/keyrings/cloud.google.gpg
|
||||
fi
|
||||
# Same "Hash Sum mismatch" retry as backend-emulator-tests.
|
||||
for attempt in 1 2 3 4 5; do
|
||||
sudo rm -rf /var/lib/apt/lists/*
|
||||
if sudo apt-get update -y && \
|
||||
sudo apt-get install -y google-cloud-cli-firestore-emulator ffmpeg; then
|
||||
break
|
||||
fi
|
||||
echo "apt install attempt ${attempt} failed; retrying in 15s..."
|
||||
sleep 15
|
||||
done
|
||||
dpkg -s google-cloud-cli-firestore-emulator >/dev/null 2>&1
|
||||
ffmpeg -version | head -1
|
||||
|
||||
- name: Install backend dependencies
|
||||
run: poetry install
|
||||
|
||||
- name: Pull GCS emulator image
|
||||
run: docker pull fsouza/fake-gcs-server:latest
|
||||
|
||||
- name: Setup Node.js with caching
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
cache: 'npm'
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install frontend dependencies
|
||||
working-directory: frontend
|
||||
run: npm ci --legacy-peer-deps
|
||||
|
||||
- name: Install Playwright browsers
|
||||
working-directory: frontend
|
||||
run: npx playwright install --with-deps chromium
|
||||
|
||||
- name: Run local-stack E2E
|
||||
working-directory: frontend
|
||||
run: |
|
||||
chmod +x ../scripts/*.sh
|
||||
npx playwright test -c playwright.local-stack.config.ts --reporter=list
|
||||
|
||||
- name: Backend log (on failure)
|
||||
if: failure()
|
||||
run: tail -300 .local-stack/backend.log || true
|
||||
|
||||
- name: Upload Playwright report
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: playwright-local-stack-report
|
||||
path: |
|
||||
frontend/playwright-report-local-stack/
|
||||
frontend/test-results/
|
||||
retention-days: 7
|
||||
|
||||
backend-lint:
|
||||
name: "Backend - Code Quality"
|
||||
runs-on: ubuntu-latest
|
||||
@@ -805,7 +917,8 @@ jobs:
|
||||
# frontend-lint IS required - catches React hooks violations that cause runtime crashes
|
||||
# frontend-e2e-happy-path is required ONLY when 'e2e-happy-path' label is present
|
||||
# frontend-e2e-full is optional (requires 'e2e' label) so not enforced here
|
||||
needs: [frontend-lint, frontend-unit-tests, frontend-e2e-smoke, frontend-build, package-unit-tests, package-integration-tests, backend-unit-tests, backend-emulator-tests, backend-lint, frontend-e2e-happy-path, translation-check]
|
||||
# local-stack-e2e IS required when backend/frontend changed (offline, deterministic)
|
||||
needs: [frontend-lint, frontend-unit-tests, frontend-e2e-smoke, frontend-build, package-unit-tests, package-integration-tests, backend-unit-tests, backend-emulator-tests, local-stack-e2e, backend-lint, frontend-e2e-happy-path, translation-check]
|
||||
if: github.ref != 'refs/heads/main' && always()
|
||||
steps:
|
||||
- name: Check results
|
||||
@@ -820,6 +933,7 @@ jobs:
|
||||
"${{ needs.package-integration-tests.result }}"
|
||||
"${{ needs.backend-unit-tests.result }}"
|
||||
"${{ needs.backend-emulator-tests.result }}"
|
||||
"${{ needs.local-stack-e2e.result }}"
|
||||
"${{ needs.backend-lint.result }}"
|
||||
)
|
||||
for result in "${results[@]}"; do
|
||||
|
||||
@@ -18,6 +18,7 @@ tests/outputs
|
||||
|
||||
# Cloud Infra
|
||||
.emulator-logs
|
||||
.local-stack
|
||||
|
||||
# Next.js / React Frontend (./frontend)
|
||||
node_modules/
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Makefile for karaoke-gen project
|
||||
# Run `make help` to see available commands
|
||||
|
||||
.PHONY: help install install-backend install-frontend build-frontend dev-install test test-unit test-backend test-e2e test-frontend test-all lint clean emulators-start emulators-stop eval-custom-lyrics
|
||||
.PHONY: help install install-backend install-frontend build-frontend dev-install test test-unit test-backend test-e2e test-frontend test-all lint clean emulators-start emulators-stop local-stack-up local-stack-down test-local-stack eval-custom-lyrics
|
||||
|
||||
# Per-stage timeout in seconds (default: 10 minutes)
|
||||
# Prevents hangs from stale dev servers, broken emulators, etc.
|
||||
@@ -124,6 +124,17 @@ emulators-stop:
|
||||
@echo "=== Stopping GCP emulators ==="
|
||||
./scripts/stop-emulators.sh
|
||||
|
||||
# Local E2E stack: real backend on emulators + fake workers (see docs/TESTING.md)
|
||||
local-stack-up:
|
||||
./scripts/local-stack.sh up --frontend
|
||||
|
||||
local-stack-down:
|
||||
./scripts/local-stack.sh down
|
||||
|
||||
# Full-journey Playwright suite against the local stack (starts it if needed)
|
||||
test-local-stack: install-frontend
|
||||
cd frontend && timeout $(TEST_TIMEOUT) npm run test:e2e:local-stack
|
||||
|
||||
# Clean up temporary files
|
||||
clean:
|
||||
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
|
||||
|
||||
@@ -3345,14 +3345,15 @@ async def confirm_duration(
|
||||
#
|
||||
# This is the idempotency guard: if a duplicate confirm POST arrives (e.g. a
|
||||
# double-click) the status guard at step 2 will now 409 immediately, preventing
|
||||
# a second pair of worker launches. We transition to SEPARATING_STAGE1 —
|
||||
# the normal next state for both the audio and lyrics parallel tracks — which is
|
||||
# a valid transition from AWAITING_DURATION_CONFIRM per STATE_TRANSITIONS.
|
||||
# The audio and lyrics workers update their own track states from here on, the
|
||||
# same as the normal convergence path.
|
||||
# a second pair of worker launches. We go back to DOWNLOADING — the prep-phase
|
||||
# status every other path runs the parallel audio + lyrics workers in. It must
|
||||
# be a PREP_PHASE_STATUSES member: the lyrics worker only advances a job to
|
||||
# screens/review from there. (This used to be SEPARATING_STAGE1, which can only
|
||||
# move to SEPARATING_STAGE2/FAILED, so every confirmed job stalled before
|
||||
# review. Found by the local-stack E2E, 2026-10-15.)
|
||||
job_manager.transition_to_state(
|
||||
job_id,
|
||||
new_status=JobStatus.SEPARATING_STAGE1,
|
||||
new_status=JobStatus.DOWNLOADING,
|
||||
progress=20,
|
||||
message="Cost confirmed, processing",
|
||||
)
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
"""gen email-code sign-in: ``POST /api/users/auth/send-code`` + ``verify-code``.
|
||||
|
||||
An alternative to the magic link for clients that can't follow an emailed link
|
||||
back into the same session (the mobile app, a second device, local E2E). The
|
||||
email carries BOTH a 6-digit code and a regular magic link; either signs in.
|
||||
|
||||
- POST /api/users/auth/send-code {email, locale?, device_fingerprint?,
|
||||
referral_code?, attribution?} → 202 {"status": "sent"}
|
||||
- POST /api/users/auth/verify-code {email, code} → VerifyMagicLinkResponse
|
||||
|
||||
Code mechanics come from :class:`LoginCodeStore` (shared with kjbox): HMAC-hashed
|
||||
at rest with a server pepper, 10-minute TTL, 5 verify attempts then 429,
|
||||
single-use marker, and per-email + per-IP rolling-hour send throttles checked in
|
||||
the same Firestore transaction as the code write.
|
||||
|
||||
Abuse checks mirror ``POST /auth/magic-link``: disposable domain → 422 (unless a
|
||||
tenant explicitly allowlisted the address), blocked email / IP → pretend
|
||||
success, new-user per-IP/fingerprint signup cap → pretend success, tenant
|
||||
email-domain restriction → pretend success, no email provider in production →
|
||||
503. Pretend-success responses are indistinguishable from a real send.
|
||||
|
||||
Account creation: the email's magic link is minted with the normal
|
||||
``UserService.create_magic_link`` machinery, which (exactly like the magic-link
|
||||
endpoint) creates the account at send time, recording signup IP, device
|
||||
fingerprint and first-touch attribution. That keeps magic-link parity (same
|
||||
signup caps, same audit fields) instead of inventing a lazily-minted link.
|
||||
verify-code still re-creates the account from the code record if it's gone.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import logging
|
||||
import threading
|
||||
from datetime import datetime
|
||||
from typing import Annotated, Dict, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from pydantic import BaseModel, Field, StringConstraints
|
||||
|
||||
from backend.api.routes.kjbox import _normalize_email, _split_locale
|
||||
from backend.api.routes.users import (
|
||||
_build_user_public,
|
||||
_mask_email,
|
||||
_precompute_credit_eval,
|
||||
complete_verified_login,
|
||||
)
|
||||
from backend.config import get_settings, is_production
|
||||
from backend.i18n import get_full_locale_from_request, get_locale_from_request, t
|
||||
from backend.models.user import VerifyMagicLinkResponse
|
||||
from backend.services.email_service import EmailService, get_email_service
|
||||
from backend.services.email_validation_service import get_email_validation_service
|
||||
from backend.services.login_code_store import (
|
||||
CODE_EXPIRY_MINUTES,
|
||||
CodeThrottled,
|
||||
LoginCodeStore,
|
||||
TransactionContention,
|
||||
VerifyOutcome,
|
||||
)
|
||||
from backend.services.user_service import MAGIC_LINKS_COLLECTION, UserService, get_user_service
|
||||
from backend.utils.request_helpers import get_client_ip
|
||||
from backend.utils.test_data import is_test_email
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
|
||||
LOGIN_CODES_COLLECTION = "login_codes"
|
||||
LOGIN_CODE_USES_COLLECTION = "login_code_uses"
|
||||
LOGIN_CODE_IP_COLLECTION = "login_code_ip_sends"
|
||||
|
||||
# Used only outside production when neither LOGIN_CODE_PEPPER nor an admin token
|
||||
# is configured (local dev / unit tests). Never used in production.
|
||||
_DEV_PEPPER = "gen-login-code-dev-pepper"
|
||||
|
||||
|
||||
def _resolve_pepper() -> Optional[str]:
|
||||
"""HMAC pepper for stored codes.
|
||||
|
||||
Order: ``LOGIN_CODE_PEPPER`` → sha256("gen-login-code-pepper:" + first admin
|
||||
token) → (non-production only) a fixed dev pepper. Returns None in production
|
||||
with nothing configured (endpoints then 503). Rotating the first admin token
|
||||
without setting LOGIN_CODE_PEPPER invalidates outstanding codes (≤10 min).
|
||||
"""
|
||||
settings = get_settings()
|
||||
if settings.login_code_pepper:
|
||||
return settings.login_code_pepper
|
||||
tokens = [tok.strip() for tok in (settings.admin_tokens or "").split(",") if tok.strip()]
|
||||
if tokens:
|
||||
return hashlib.sha256(f"gen-login-code-pepper:{tokens[0]}".encode("utf-8")).hexdigest()
|
||||
if not is_production():
|
||||
return _DEV_PEPPER
|
||||
return None
|
||||
|
||||
|
||||
def get_login_code_store(user_service: UserService = Depends(get_user_service)) -> LoginCodeStore:
|
||||
pepper = _resolve_pepper()
|
||||
if not pepper:
|
||||
logger.error("login-code: no LOGIN_CODE_PEPPER / admin token configured in production")
|
||||
raise HTTPException(status_code=503, detail="not configured")
|
||||
return LoginCodeStore(
|
||||
user_service.db,
|
||||
pepper,
|
||||
LOGIN_CODES_COLLECTION,
|
||||
LOGIN_CODE_USES_COLLECTION,
|
||||
ip_collection=LOGIN_CODE_IP_COLLECTION,
|
||||
)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- models
|
||||
|
||||
class SendLoginCodeRequest(BaseModel):
|
||||
email: str
|
||||
locale: Optional[str] = None
|
||||
device_fingerprint: Optional[str] = Field(default=None, max_length=256)
|
||||
referral_code: Optional[str] = Field(default=None, max_length=64)
|
||||
# First-touch attribution, same shape/limits as SendMagicLinkRequest.attribution.
|
||||
attribution: Optional[
|
||||
Dict[Annotated[str, StringConstraints(max_length=32)], Optional[Annotated[str, StringConstraints(max_length=500)]]]
|
||||
] = Field(default=None, max_length=16)
|
||||
|
||||
|
||||
class SendLoginCodeResponse(BaseModel):
|
||||
status: str
|
||||
|
||||
|
||||
class VerifyLoginCodeRequest(BaseModel):
|
||||
email: str
|
||||
code: str = Field(max_length=32)
|
||||
|
||||
|
||||
def _sent() -> SendLoginCodeResponse:
|
||||
return SendLoginCodeResponse(status="sent")
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ endpoints
|
||||
|
||||
@router.post("/auth/send-code", response_model=SendLoginCodeResponse, status_code=202)
|
||||
def send_login_code(
|
||||
body: SendLoginCodeRequest,
|
||||
http_request: Request,
|
||||
store: LoginCodeStore = Depends(get_login_code_store),
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
email_service: EmailService = Depends(get_email_service),
|
||||
):
|
||||
"""Email a 6-digit sign-in code (plus a magic link) to ``email``."""
|
||||
from backend.middleware.tenant import get_tenant_config_from_request, get_tenant_from_request
|
||||
|
||||
settings = get_settings()
|
||||
email = _normalize_email(body.email)
|
||||
email_locale, ui_locale = _split_locale(body.locale)
|
||||
has_accept_language = bool(http_request.headers.get("accept-language"))
|
||||
locale = email_locale or get_locale_from_request(http_request)
|
||||
ui_locale = ui_locale or get_full_locale_from_request(http_request)
|
||||
# Locale persisted on the user at verify: only an explicit choice (body or
|
||||
# Accept-Language), never the "en" fallback — that would clobber a stored one.
|
||||
persist_locale = email_locale or (locale if has_accept_language else None)
|
||||
|
||||
tenant_id = get_tenant_from_request(http_request)
|
||||
tenant_config = get_tenant_config_from_request(http_request)
|
||||
tenant_vouched = bool(tenant_config and tenant_config.is_explicitly_allowlisted(email))
|
||||
|
||||
email_validation = get_email_validation_service()
|
||||
if not tenant_vouched and email_validation.is_disposable_domain(email):
|
||||
logger.warning(f"login-code: blocked disposable email {_mask_email(email)}")
|
||||
raise HTTPException(status_code=422, detail=t(locale, "users.disposableEmailBlocked"))
|
||||
if email_validation.is_email_blocked(email):
|
||||
logger.warning(f"login-code: blocked email {_mask_email(email)} — pretending success")
|
||||
return _sent()
|
||||
ip_address = get_client_ip(http_request)
|
||||
if ip_address and email_validation.is_ip_blocked(ip_address):
|
||||
logger.warning(f"login-code: blocked IP {ip_address} — pretending success")
|
||||
return _sent()
|
||||
|
||||
device_fingerprint = body.device_fingerprint
|
||||
existing_user = user_service.get_user(email)
|
||||
if existing_user is None and user_service.is_signup_rate_limited(
|
||||
ip_address=ip_address, device_fingerprint=device_fingerprint,
|
||||
):
|
||||
logger.warning(f"login-code: signup rate limit hit: IP={ip_address} for {_mask_email(email)}")
|
||||
return _sent()
|
||||
|
||||
if not email_service.is_configured() and is_production():
|
||||
logger.error("login-code: email service not configured - cannot send codes")
|
||||
raise HTTPException(status_code=503, detail=t(locale, "users.emailServiceNotConfigured"))
|
||||
|
||||
if tenant_config and not tenant_config.is_email_allowed(email):
|
||||
logger.warning(f"login-code: email not allowed for tenant {tenant_id}: {_mask_email(email)}")
|
||||
return _sent()
|
||||
|
||||
user_agent = http_request.headers.get("user-agent")
|
||||
try:
|
||||
issued = store.issue_code(
|
||||
email,
|
||||
max_per_hour=settings.login_codes_per_email_per_hour,
|
||||
needs_credit_eval=(
|
||||
not (existing_user and getattr(existing_user, "welcome_credits_granted", False))
|
||||
and not is_test_email(email)
|
||||
and not tenant_id
|
||||
),
|
||||
fields={
|
||||
"locale": persist_locale,
|
||||
"email_locale": locale,
|
||||
"ui_locale": ui_locale,
|
||||
"ip_address": ip_address,
|
||||
"user_agent": user_agent,
|
||||
"device_fingerprint": device_fingerprint,
|
||||
"tenant_id": tenant_id,
|
||||
"referral_code": body.referral_code,
|
||||
},
|
||||
throttle_ip=ip_address,
|
||||
max_per_ip_per_hour=settings.login_codes_per_ip_per_hour,
|
||||
)
|
||||
except (CodeThrottled, TransactionContention):
|
||||
# Contention on one email's code doc = a burst of parallel sends to the
|
||||
# same address, which is exactly what the throttle exists to stop.
|
||||
raise HTTPException(status_code=429, detail="too_many_codes")
|
||||
|
||||
# Same machinery as POST /auth/magic-link (creates the account at send time,
|
||||
# recording signup IP / fingerprint / attribution) — see module docstring.
|
||||
magic_link = user_service.create_magic_link(
|
||||
email,
|
||||
ip_address=ip_address,
|
||||
user_agent=user_agent,
|
||||
tenant_id=tenant_id,
|
||||
device_fingerprint=device_fingerprint,
|
||||
referral_code=body.referral_code,
|
||||
attribution=body.attribution,
|
||||
)
|
||||
magic_link_url = email_service.build_magic_link_url(
|
||||
magic_link.token,
|
||||
locale=locale,
|
||||
tenant_frontend_url=tenant_config.get_frontend_url() if tenant_config else None,
|
||||
referral_code=body.referral_code,
|
||||
)
|
||||
sent = email_service.send_login_code(
|
||||
email,
|
||||
issued.code,
|
||||
magic_link_url=magic_link_url,
|
||||
expiry_minutes=CODE_EXPIRY_MINUTES,
|
||||
locale=locale,
|
||||
sender_email=tenant_config.get_sender_email() if tenant_config else None,
|
||||
tenant_name=tenant_config.name if tenant_config else None,
|
||||
)
|
||||
if not sent:
|
||||
if getattr(email_service, "last_send_suppressed", False):
|
||||
logger.info(f"login-code: recipient {_mask_email(email)} is Postmark-suppressed")
|
||||
else:
|
||||
logger.error(f"login-code: failed to send code email to {_mask_email(email)}")
|
||||
# Don't reveal failure (anti-enumeration).
|
||||
|
||||
# Warm the welcome-credit AI evaluation while the user reads their email, on
|
||||
# both sign-in docs (code record + magic link) so either path is instant.
|
||||
if issued.needs_credit_eval:
|
||||
threading.Thread(
|
||||
target=_precompute_credit_eval,
|
||||
args=(issued.doc_id, email, LOGIN_CODES_COLLECTION, ((MAGIC_LINKS_COLLECTION, magic_link.token),)),
|
||||
daemon=True,
|
||||
).start()
|
||||
|
||||
return _sent()
|
||||
|
||||
|
||||
@router.post("/auth/verify-code", response_model=VerifyMagicLinkResponse)
|
||||
def verify_login_code(
|
||||
body: VerifyLoginCodeRequest,
|
||||
http_request: Request,
|
||||
store: LoginCodeStore = Depends(get_login_code_store),
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
email_service: EmailService = Depends(get_email_service),
|
||||
):
|
||||
"""Exchange an emailed code for a session (same response as GET /auth/verify)."""
|
||||
email = _normalize_email(body.email)
|
||||
|
||||
outcome, record = store.verify_code(email, body.code)
|
||||
if outcome == VerifyOutcome.TOO_MANY_ATTEMPTS:
|
||||
raise HTTPException(status_code=429, detail="too_many_attempts")
|
||||
if outcome == VerifyOutcome.INVALID:
|
||||
raise HTTPException(status_code=401, detail="invalid_code")
|
||||
if outcome != VerifyOutcome.OK or record is None:
|
||||
raise HTTPException(status_code=401, detail="expired")
|
||||
|
||||
tenant_id = record.get("tenant_id")
|
||||
user = user_service.get_user(email)
|
||||
if user is None:
|
||||
# Normally created at send time by create_magic_link; recreate from the
|
||||
# code record (same signup audit fields) if it was removed meanwhile.
|
||||
user = user_service.get_or_create_user(
|
||||
email,
|
||||
tenant_id=tenant_id,
|
||||
signup_ip=record.get("ip_address"),
|
||||
device_fingerprint=record.get("device_fingerprint"),
|
||||
)
|
||||
# Same first-login definition as magic-link verify (checked before last_login_at is set).
|
||||
is_first_login = user.total_jobs_created == 0 and not user.last_login_at
|
||||
user = user_service.update_user(
|
||||
email, email_verified=True, last_login_at=datetime.utcnow(),
|
||||
) or user
|
||||
|
||||
precomputed_eval = {
|
||||
k: record.get(k)
|
||||
for k in ("credit_eval_decision", "credit_eval_reasoning", "credit_eval_error")
|
||||
if record.get(k) is not None
|
||||
} or None
|
||||
persist_locale = record.get("locale")
|
||||
email_locale = persist_locale or record.get("email_locale") or user.locale or "en"
|
||||
|
||||
# Tenant portal sign-ins never touch consumer credits (as in magic-link verify).
|
||||
result = complete_verified_login(
|
||||
user,
|
||||
user_service=user_service,
|
||||
email_service=email_service,
|
||||
is_first_login=is_first_login,
|
||||
precomputed_eval=precomputed_eval,
|
||||
grant_welcome_credit=not tenant_id,
|
||||
referral_code=(http_request.headers.get("x-referral-code") or record.get("referral_code"))
|
||||
if not tenant_id else None,
|
||||
locale=persist_locale,
|
||||
ui_locale=record.get("ui_locale"),
|
||||
ip_address=get_client_ip(http_request),
|
||||
user_agent=http_request.headers.get("user-agent"),
|
||||
tenant_id=tenant_id,
|
||||
device_fingerprint=record.get("device_fingerprint"),
|
||||
email_locale=email_locale,
|
||||
)
|
||||
logger.info(f"login-code: verified {_mask_email(email)} (credit_status={result.credit_status})")
|
||||
|
||||
tenant_subdomain = None
|
||||
if tenant_id:
|
||||
from backend.services.tenant_service import get_tenant_service
|
||||
tenant_cfg = get_tenant_service().get_tenant_config(tenant_id)
|
||||
if tenant_cfg:
|
||||
tenant_subdomain = tenant_cfg.subdomain
|
||||
|
||||
return VerifyMagicLinkResponse(
|
||||
status="success",
|
||||
session_token=result.session.token,
|
||||
user=_build_user_public(result.user),
|
||||
message=t(email_locale, "users.magicLinkSuccess"),
|
||||
tenant_subdomain=tenant_subdomain,
|
||||
credits_granted=result.credits_granted,
|
||||
credit_status=result.credit_status,
|
||||
)
|
||||
@@ -0,0 +1,169 @@
|
||||
"""Test-only API for the local E2E stack (``/api/test/*``).
|
||||
|
||||
Mounted ONLY when FAKE_WORKERS is on (``backend/main.py``), which startup
|
||||
refuses in production, and unauthenticated by design: the stack runs on
|
||||
emulators on a developer machine or CI runner. Web Playwright and mobile Maestro
|
||||
tests use it to reset state, seed scenarios, tune the fake workers, read
|
||||
captured email and complete fake Stripe checkouts.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.config import get_settings
|
||||
from backend.services import fake_workers
|
||||
from backend.services.fake_workers import checkout as fake_checkout
|
||||
from backend.services.fake_workers.config import clear_fake_config, get_fake_config, set_fake_config
|
||||
from backend.services.fake_workers.outbox import list_emails
|
||||
from backend.services.fake_workers.scenarios import SCENARIOS, ScenarioError, run_scenario
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/test", tags=["test-support"])
|
||||
|
||||
# Seeded by scripts/seed-local-data.py; kept across resets.
|
||||
PRESERVED_BLOB_PREFIXES = ("themes/",)
|
||||
|
||||
|
||||
class ScenarioRequest(BaseModel):
|
||||
name: str
|
||||
email: Optional[str] = None
|
||||
credits: Optional[int] = None
|
||||
artist: Optional[str] = None
|
||||
title: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
def status() -> Dict[str, Any]:
|
||||
return {
|
||||
"fake_workers": True,
|
||||
"active_workers": fake_workers.active_worker_count(),
|
||||
"config": get_fake_config().to_dict(),
|
||||
"scenarios": SCENARIOS,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/reset")
|
||||
def reset() -> Dict[str, Any]:
|
||||
"""Wipe every Firestore document and every bucket object except the seed (themes)."""
|
||||
emulator = os.getenv("FIRESTORE_EMULATOR_HOST")
|
||||
if not emulator:
|
||||
raise HTTPException(status_code=409, detail="reset only runs against the Firestore emulator")
|
||||
project = get_settings().google_cloud_project
|
||||
resp = httpx.delete(
|
||||
f"http://{emulator}/emulator/v1/projects/{project}/databases/(default)/documents", timeout=30,
|
||||
)
|
||||
if resp.status_code >= 300:
|
||||
raise HTTPException(status_code=502, detail=f"emulator reset failed: {resp.status_code} {resp.text}")
|
||||
|
||||
from backend.services.storage_service import StorageService
|
||||
|
||||
bucket = StorageService().bucket
|
||||
deleted = 0
|
||||
for blob in list(bucket.list_blobs()):
|
||||
if not blob.name.startswith(PRESERVED_BLOB_PREFIXES):
|
||||
blob.delete()
|
||||
deleted += 1
|
||||
clear_fake_config()
|
||||
return {"status": "reset", "blobs_deleted": deleted}
|
||||
|
||||
|
||||
@router.get("/fake-config")
|
||||
def read_config(email: Optional[str] = None) -> Dict[str, Any]:
|
||||
return get_fake_config(email=email).to_dict()
|
||||
|
||||
|
||||
@router.put("/fake-config")
|
||||
def update_config(updates: Dict[str, Any], email: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""Merge ``updates`` into the global knobs, or into ``?email=``'s per-user overrides."""
|
||||
try:
|
||||
return set_fake_config(updates, email=email).to_dict()
|
||||
except (ValueError, TypeError) as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/scenario")
|
||||
def scenario(body: ScenarioRequest) -> Dict[str, Any]:
|
||||
try:
|
||||
return run_scenario(body.name, email=body.email, credits=body.credits, artist=body.artist, title=body.title)
|
||||
except ScenarioError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/emails")
|
||||
def emails(to: Optional[str] = None, limit: int = 20) -> Dict[str, Any]:
|
||||
return {"emails": list_emails(to, limit=limit)}
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ fake Stripe
|
||||
|
||||
_CHECKOUT_PAGE = """<!doctype html>
|
||||
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Fake Checkout</title>
|
||||
<style>body{{font-family:system-ui,sans-serif;max-width:420px;margin:48px auto;padding:0 16px}}
|
||||
button{{font-size:18px;padding:12px 20px;margin:8px 8px 0 0;border-radius:8px;border:0}}
|
||||
#pay{{background:#635bff;color:#fff}}#cancel{{background:#eee}}</style></head>
|
||||
<body><h1>Fake Checkout</h1>
|
||||
<p data-testid="fake-checkout-product">{product}</p>
|
||||
<p data-testid="fake-checkout-amount">${amount}</p>
|
||||
<p>{email}</p>
|
||||
<form method="post" action="{base}/api/test/fake-checkout/{sid}/pay" style="display:inline">
|
||||
<button id="pay" data-testid="fake-checkout-pay" type="submit">Pay</button></form>
|
||||
<form method="post" action="{base}/api/test/fake-checkout/{sid}/cancel" style="display:inline">
|
||||
<button id="cancel" data-testid="fake-checkout-cancel" type="submit">Cancel</button></form>
|
||||
</body></html>"""
|
||||
|
||||
|
||||
def _checkout_or_404(session_id: str) -> Dict[str, Any]:
|
||||
record = fake_checkout.get_checkout(session_id)
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="unknown fake checkout session")
|
||||
return record
|
||||
|
||||
|
||||
@router.get("/fake-checkout/{session_id}", response_class=HTMLResponse)
|
||||
def checkout_page(session_id: str) -> HTMLResponse:
|
||||
record = _checkout_or_404(session_id)
|
||||
return HTMLResponse(_CHECKOUT_PAGE.format(
|
||||
product=html.escape(record.get("product_name") or ""),
|
||||
amount=f"{(record.get('amount_cents') or 0) / 100:.2f}",
|
||||
email=html.escape(record.get("customer_email") or ""),
|
||||
base=fake_checkout.public_api_url(),
|
||||
sid=html.escape(session_id),
|
||||
))
|
||||
|
||||
|
||||
@router.post("/fake-checkout/{session_id}/pay")
|
||||
async def checkout_pay(session_id: str):
|
||||
"""Complete the session through the same handler as the real Stripe webhook."""
|
||||
from backend.api.routes.internal import TestWebhookRequest, test_webhook
|
||||
|
||||
record = _checkout_or_404(session_id)
|
||||
result = await test_webhook(
|
||||
TestWebhookRequest(
|
||||
event_type="checkout.session.completed",
|
||||
session_id=session_id,
|
||||
customer_email=record.get("customer_email") or "",
|
||||
metadata=record.get("metadata") or {},
|
||||
),
|
||||
auth_data=None,
|
||||
)
|
||||
if result.status not in ("processed", "already_processed"):
|
||||
raise HTTPException(status_code=500, detail=f"fake checkout completion failed: {result.message}")
|
||||
fake_checkout.mark_checkout(session_id, "complete")
|
||||
return RedirectResponse(fake_checkout.resolve_url(record.get("success_url"), session_id), status_code=303)
|
||||
|
||||
|
||||
@router.post("/fake-checkout/{session_id}/cancel")
|
||||
def checkout_cancel(session_id: str):
|
||||
record = _checkout_or_404(session_id)
|
||||
fake_checkout.mark_checkout(session_id, "cancelled")
|
||||
return RedirectResponse(fake_checkout.resolve_url(record.get("cancel_url"), session_id), status_code=303)
|
||||
@@ -194,13 +194,20 @@ class LogoutResponse(BaseModel):
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def _precompute_credit_eval(token: str, email: str, collection: Optional[str] = None) -> None:
|
||||
def _precompute_credit_eval(
|
||||
token: str,
|
||||
email: str,
|
||||
collection: Optional[str] = None,
|
||||
also_store_on: Tuple[Tuple[str, str], ...] = (),
|
||||
) -> None:
|
||||
"""Run credit evaluation in background and store result on the sign-in doc.
|
||||
|
||||
Called in a background thread after sending a magic link (or a kjbox emailed
|
||||
Called in a background thread after sending a magic link (or an emailed
|
||||
code — pass ``collection`` + that doc's id as ``token``) for new users. If the
|
||||
user verifies before this finishes, verification falls back to inline
|
||||
evaluation (same as before this optimization).
|
||||
evaluation (same as before this optimization). ``also_store_on`` lists extra
|
||||
(collection, doc_id) sign-in docs to receive the same result (the login-code
|
||||
email carries both a code and a magic link; either may be used to sign in).
|
||||
"""
|
||||
try:
|
||||
from backend.services.credit_evaluation_service import get_credit_evaluation_service
|
||||
@@ -210,11 +217,13 @@ def _precompute_credit_eval(token: str, email: str, collection: Optional[str] =
|
||||
evaluation = eval_service.evaluate(email, "welcome")
|
||||
|
||||
user_service = get_user_service()
|
||||
user_service.db.collection(collection or MAGIC_LINKS_COLLECTION).document(token).update({
|
||||
result = {
|
||||
"credit_eval_decision": evaluation.decision,
|
||||
"credit_eval_reasoning": evaluation.reasoning,
|
||||
"credit_eval_error": evaluation.error,
|
||||
})
|
||||
}
|
||||
for coll, doc_id in ((collection or MAGIC_LINKS_COLLECTION, token), *also_store_on):
|
||||
user_service.db.collection(coll).document(doc_id).update(result)
|
||||
logger.info(f"Pre-computed credit eval for {_mask_email(email)}: {evaluation.decision}")
|
||||
except Exception:
|
||||
logger.exception(f"Background credit eval failed for {_mask_email(email)} — verify will compute inline")
|
||||
@@ -951,7 +960,7 @@ def create_checkout(
|
||||
Returns a URL to redirect the user to Stripe's hosted checkout page.
|
||||
No authentication required - email is provided in the request.
|
||||
"""
|
||||
if not stripe_service.is_configured():
|
||||
if not stripe_service.can_checkout():
|
||||
raise HTTPException(status_code=503, detail="Payment processing is not available")
|
||||
|
||||
# Check for referral discount
|
||||
@@ -1010,7 +1019,7 @@ def create_made_for_you_checkout(
|
||||
$15 with 24-hour delivery guarantee.
|
||||
No authentication required - customer email is provided in the request.
|
||||
"""
|
||||
if not stripe_service.is_configured():
|
||||
if not stripe_service.can_checkout():
|
||||
raise HTTPException(status_code=503, detail="Payment processing is not available")
|
||||
|
||||
success, checkout_url, message = stripe_service.create_made_for_you_checkout_session(
|
||||
|
||||
@@ -340,6 +340,14 @@ class Settings(BaseSettings):
|
||||
# Max "free at the show" credits kjbox may grant one user per rolling 24h.
|
||||
kjbox_show_credits_per_user_per_24h: int = int(os.getenv("KJBOX_SHOW_CREDITS_PER_USER_PER_24H", "5"))
|
||||
|
||||
# gen email-code sign-in (/api/users/auth/send-code + verify-code).
|
||||
# HMAC pepper for stored codes; when empty it's derived from the first admin
|
||||
# token (see backend/api/routes/login_code.py:_resolve_pepper).
|
||||
login_code_pepper: str = os.getenv("LOGIN_CODE_PEPPER", "")
|
||||
# Max emailed sign-in codes per address / per client IP per rolling hour.
|
||||
login_codes_per_email_per_hour: int = int(os.getenv("LOGIN_CODES_PER_EMAIL_PER_HOUR", "5"))
|
||||
login_codes_per_ip_per_hour: int = int(os.getenv("LOGIN_CODES_PER_IP_PER_HOUR", "20"))
|
||||
|
||||
# Private (non-published) track distribution settings
|
||||
default_private_dropbox_path: Optional[str] = os.getenv("DEFAULT_PRIVATE_DROPBOX_PATH")
|
||||
default_private_brand_prefix: Optional[str] = os.getenv("DEFAULT_PRIVATE_BRAND_PREFIX", "NOMADNP")
|
||||
@@ -366,6 +374,12 @@ class Settings(BaseSettings):
|
||||
youtube_quota_safety_margin: int = int(os.getenv("YOUTUBE_QUOTA_SAFETY_MARGIN", "500"))
|
||||
# E2E test bypass key for rate limiting (set via secret in production)
|
||||
e2e_bypass_key: str = os.getenv("E2E_BYPASS_KEY", "")
|
||||
# Local-stack E2E mode (scripts/local-stack.sh): workers are replaced by in-process
|
||||
# fakes that walk jobs through their real states with canned media, the
|
||||
# /api/test/* router is mounted, and external lookups (Secret Manager, DeBounce,
|
||||
# ip-api, encoding VM) are skipped. Startup refuses it in production.
|
||||
# See backend/services/fake_workers/ and docs/TESTING.md § Local stack.
|
||||
fake_workers: bool = os.getenv("FAKE_WORKERS", "").lower() in ("1", "true", "yes")
|
||||
# Canonical YouTube description template. Placeholders ({title}, {artist},
|
||||
# {artist_hashtag}, {brand_code}) are filled by
|
||||
# backend.services.youtube_description.render_youtube_description, which is the
|
||||
@@ -495,6 +509,10 @@ class Settings(BaseSettings):
|
||||
self._secret_cache[secret_id] = env_value
|
||||
return env_value
|
||||
|
||||
# Local stack runs offline on emulators: never reach for Secret Manager.
|
||||
if self.fake_workers is True and not is_production():
|
||||
return None
|
||||
|
||||
# Try Secret Manager (production mode)
|
||||
if not self.google_cloud_project:
|
||||
logger.warning(f"Cannot fetch secret {secret_id}: No GCP project configured")
|
||||
@@ -566,6 +584,11 @@ def validate_production_config() -> None:
|
||||
"""
|
||||
if not is_production():
|
||||
return
|
||||
if settings.fake_workers is True:
|
||||
raise RuntimeError(
|
||||
"FAKE_WORKERS is set in production. It replaces every worker with a fake "
|
||||
"and mounts the unauthenticated /api/test/* router. Refusing to start."
|
||||
)
|
||||
missing = [v for v in _REQUIRED_PRODUCTION_ENV_VARS if not (os.environ.get(v) or "").strip()]
|
||||
if missing:
|
||||
raise RuntimeError(
|
||||
|
||||
@@ -13,6 +13,7 @@ from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from backend.config import settings, validate_production_config
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
from backend.api.routes import health, jobs, internal, file_upload, review, auth, audio_search, themes, users, admin, tenant, tenant_admin, tenant_bulk, tenant_theme, rate_limits, push, catalog, encoding_worker, client_errors, client_events, bulk, parse_titles
|
||||
from backend.services.tracing import setup_tracing, instrument_app, get_current_trace_id
|
||||
from backend.services.structured_logging import setup_structured_logging
|
||||
@@ -133,6 +134,11 @@ def _run_background_warmup():
|
||||
except Exception as e:
|
||||
logger.warning(f"Langfuse preload failed (will initialize lazily): {e}")
|
||||
|
||||
if fake_workers_enabled(settings):
|
||||
# Local stack: no encoding VM and no OAuth credentials to validate.
|
||||
logger.info(f"Background warmup complete in {time.time() - warmup_start:.2f}s (fake workers)")
|
||||
return
|
||||
|
||||
# 4. Encoding service singleton — its first construction imports
|
||||
# google.cloud.compute_v1 (~10s on a fresh instance). Build it here, off the
|
||||
# event loop, before /api/health/detailed or a preview encode needs it.
|
||||
@@ -332,10 +338,16 @@ from backend.api.routes import karaokehunt
|
||||
app.include_router(karaokehunt.router, prefix="/api") # Retired KaraokeHunt app request interceptor
|
||||
from backend.api.routes import kjbox
|
||||
app.include_router(kjbox.router, prefix="/api") # kjbox partner API: singer email-code sign-in + show credits
|
||||
from backend.api.routes import login_code
|
||||
app.include_router(login_code.router, prefix="/api") # gen email-code sign-in (/users/auth/send-code, verify-code)
|
||||
app.include_router(tenant.router) # Tenant/white-label configuration (no /api prefix, router has it)
|
||||
app.include_router(tenant_admin.router) # Admin tenant provisioning (router has /api prefix)
|
||||
app.include_router(tenant_bulk.router) # Tenant bulk-upload filename analysis (router has /api prefix)
|
||||
app.include_router(tenant_theme.router) # Tenant self-service theme editor (router has /api prefix)
|
||||
if fake_workers_enabled(settings):
|
||||
# Local E2E stack only (startup refuses FAKE_WORKERS in production).
|
||||
from backend.api.routes import test_support
|
||||
app.include_router(test_support.router, prefix="/api")
|
||||
|
||||
|
||||
# Exception handlers
|
||||
|
||||
@@ -128,6 +128,8 @@ STATE_TRANSITIONS = {
|
||||
|
||||
# Duration cost confirmation (BLOCKING) - user confirms before heavy processing begins
|
||||
JobStatus.AWAITING_DURATION_CONFIRM: [
|
||||
# DOWNLOADING = confirmed, back to the prep phase (see jobs.confirm_duration).
|
||||
JobStatus.DOWNLOADING,
|
||||
JobStatus.SEPARATING_STAGE1, JobStatus.TRANSCRIBING,
|
||||
JobStatus.GENERATING_SCREENS, JobStatus.AWAITING_AUDIO_EDIT,
|
||||
JobStatus.FAILED, JobStatus.CANCELLED,
|
||||
|
||||
@@ -36,6 +36,8 @@ from karaoke_gen.audio_fetcher import (
|
||||
)
|
||||
|
||||
from .flacfetch_client import get_flacfetch_client, FlacfetchServiceError
|
||||
from backend.config import get_settings
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -854,5 +856,11 @@ def get_audio_search_service() -> AudioSearchService:
|
||||
"""Get the singleton AudioSearchService instance."""
|
||||
global _audio_search_service
|
||||
if _audio_search_service is None:
|
||||
_audio_search_service = AudioSearchService()
|
||||
if fake_workers_enabled(get_settings()):
|
||||
# Local stack: deterministic offline results (see fake_workers/search.py).
|
||||
from backend.services.fake_workers.search import FakeAudioSearchService
|
||||
|
||||
_audio_search_service = FakeAudioSearchService()
|
||||
else:
|
||||
_audio_search_service = AudioSearchService()
|
||||
return _audio_search_service
|
||||
@@ -25,6 +25,7 @@ from zoneinfo import ZoneInfo
|
||||
import requests
|
||||
|
||||
from backend.config import get_settings, is_production
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
from backend.i18n import t, get_locale_prefix
|
||||
from karaoke_gen.utils import sanitize_filename
|
||||
|
||||
@@ -133,6 +134,12 @@ class ConsoleEmailProvider(EmailProvider):
|
||||
logger.info("-" * 60)
|
||||
logger.info(text_content or html_content)
|
||||
logger.info("=" * 60)
|
||||
if fake_workers_enabled():
|
||||
# Local stack: keep a copy so E2E tests can read magic links / codes
|
||||
# (GET /api/test/emails).
|
||||
from backend.services.fake_workers.outbox import record_email
|
||||
|
||||
record_email(to_email, subject, html_content, text_content)
|
||||
return True
|
||||
|
||||
|
||||
@@ -663,6 +670,21 @@ class EmailService:
|
||||
else:
|
||||
collection.add(doc)
|
||||
|
||||
def build_magic_link_url(
|
||||
self,
|
||||
token: str,
|
||||
locale: str = "en",
|
||||
tenant_frontend_url: Optional[str] = None,
|
||||
referral_code: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Frontend URL that verifies a magic-link ``token`` (shared by the magic
|
||||
link and login-code emails)."""
|
||||
base_url = tenant_frontend_url or self.frontend_url
|
||||
url = f"{base_url}{get_locale_prefix(locale)}/auth/verify?token={token}"
|
||||
if referral_code:
|
||||
url += f"&ref={referral_code}"
|
||||
return url
|
||||
|
||||
def send_magic_link(
|
||||
self,
|
||||
email: str,
|
||||
@@ -686,10 +708,9 @@ class EmailService:
|
||||
Returns:
|
||||
True if email was sent successfully
|
||||
"""
|
||||
base_url = tenant_frontend_url or self.frontend_url
|
||||
magic_link_url = f"{base_url}{get_locale_prefix(locale)}/auth/verify?token={token}"
|
||||
if referral_code:
|
||||
magic_link_url += f"&ref={referral_code}"
|
||||
magic_link_url = self.build_magic_link_url(
|
||||
token, locale=locale, tenant_frontend_url=tenant_frontend_url, referral_code=referral_code,
|
||||
)
|
||||
|
||||
brand_name = tenant_name or "Nomad Karaoke"
|
||||
subject = t(locale, "emails.magicLink.subject", brand_name=brand_name)
|
||||
@@ -830,6 +851,98 @@ class EmailService:
|
||||
|
||||
return self._log_and_send(email, subject, html_content, text_content)
|
||||
|
||||
def send_login_code(
|
||||
self,
|
||||
email: str,
|
||||
code: str,
|
||||
magic_link_url: str,
|
||||
expiry_minutes: int,
|
||||
locale: str = "en",
|
||||
sender_email: Optional[str] = None,
|
||||
tenant_name: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Send a gen sign-in email carrying BOTH a 6-digit code and a magic link.
|
||||
|
||||
The code suits a user signing in on another device / inside an app (type
|
||||
it in); the link is one-click sign-in on the device that reads the email.
|
||||
Either works and both are single-use (the code expires after
|
||||
``expiry_minutes``; the link after 24 hours).
|
||||
|
||||
Returns:
|
||||
True if email was sent successfully
|
||||
"""
|
||||
brand_name = tenant_name or "Nomad Karaoke"
|
||||
subject = t(locale, "emails.loginCode.subject", brand_name=brand_name, code=code)
|
||||
safe_code = html.escape(code)
|
||||
safe_url = html.escape(magic_link_url, quote=True)
|
||||
|
||||
extra_styles = f"""
|
||||
.code-box {{
|
||||
background-color: #fdf2f8;
|
||||
border: 2px solid {self.BRAND_PRIMARY};
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
margin: 24px 0;
|
||||
}}
|
||||
.code {{
|
||||
font-size: 40px;
|
||||
font-weight: 700;
|
||||
letter-spacing: 10px;
|
||||
font-family: 'SF Mono', Menlo, Consolas, monospace;
|
||||
color: #111827;
|
||||
}}
|
||||
"""
|
||||
|
||||
content = f"""
|
||||
<p>{t(locale, "emails.loginCode.greeting")}</p>
|
||||
|
||||
<p>{t(locale, "emails.loginCode.intro")}</p>
|
||||
|
||||
<div class="code-box">
|
||||
<div class="code">{safe_code}</div>
|
||||
</div>
|
||||
|
||||
<p>{t(locale, "emails.loginCode.expiry", minutes=expiry_minutes)}</p>
|
||||
|
||||
<p>{t(locale, "emails.loginCode.orLink")}</p>
|
||||
|
||||
<p style="text-align: center;">
|
||||
<a href="{safe_url}" class="button">{t(locale, "emails.loginCode.signInButton")}</a>
|
||||
</p>
|
||||
|
||||
<p style="font-size: 14px; color: #666;">{t(locale, "emails.loginCode.linkExpiry")}</p>
|
||||
|
||||
<p>{t(locale, "emails.loginCode.ignoreNote")}</p>
|
||||
"""
|
||||
|
||||
html_content = self._build_email_html(content, extra_styles, locale=locale)
|
||||
|
||||
text_content = f"""
|
||||
{t(locale, "emails.loginCode.greeting")}
|
||||
|
||||
{t(locale, "emails.loginCode.intro")}
|
||||
|
||||
{code}
|
||||
|
||||
{t(locale, "emails.loginCode.expiry", minutes=expiry_minutes)}
|
||||
|
||||
{t(locale, "emails.loginCode.orLink")}
|
||||
{magic_link_url}
|
||||
|
||||
{t(locale, "emails.loginCode.linkExpiry")}
|
||||
|
||||
{t(locale, "emails.loginCode.ignoreNote")}
|
||||
|
||||
---
|
||||
© {self._get_year()} {brand_name}
|
||||
"""
|
||||
|
||||
return self._log_and_send(
|
||||
email, subject, html_content, text_content, from_email_override=sender_email
|
||||
)
|
||||
|
||||
def send_credits_added(self, email: str, credits: int, total_credits: int, locale: str = "en") -> bool:
|
||||
"""
|
||||
Send notification when credits are added to account.
|
||||
|
||||
@@ -24,6 +24,7 @@ import httpx
|
||||
from google.cloud import firestore
|
||||
|
||||
from backend.config import settings
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -281,6 +282,11 @@ class EmailValidationService:
|
||||
if domain in config.get("allowlisted_domains", set()):
|
||||
return False
|
||||
|
||||
# Local stack (FAKE_WORKERS) is offline: the Firestore blocklist above is
|
||||
# the whole check, never the external APIs below.
|
||||
if fake_workers_enabled(settings):
|
||||
return False
|
||||
|
||||
# Tier 2: Recently verified clean? Skip API calls
|
||||
if self._is_domain_verified_clean(domain, config):
|
||||
return False
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Fake workers for the local E2E stack (``FAKE_WORKERS=1``, never in production).
|
||||
|
||||
``WorkerService.trigger_*`` hands every dispatch to :func:`dispatch` when the
|
||||
setting is on. Each fake runs on its own thread with its own event loop, so it
|
||||
outlives the request (or short-lived loop) that triggered it, just as a real
|
||||
Cloud Run Job / Cloud Task would. See ``pipeline.py`` for what each one writes
|
||||
and docs/TESTING.md § "Local stack" for how tests drive it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import threading
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_threads: set = set()
|
||||
_threads_lock = threading.Lock()
|
||||
|
||||
|
||||
def fake_workers_enabled(settings=None) -> bool:
|
||||
"""True only when FAKE_WORKERS is really on (strict, so a MagicMock settings in tests is off)."""
|
||||
if settings is None:
|
||||
from backend.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
return getattr(settings, "fake_workers", False) is True
|
||||
|
||||
|
||||
def dispatch(kind: str, job_id: str) -> bool:
|
||||
"""Start fake worker ``kind`` for ``job_id`` in the background. Returns True (dispatched)."""
|
||||
from backend.services.fake_workers.pipeline import RUNNERS, run
|
||||
|
||||
if kind not in RUNNERS:
|
||||
raise ValueError(f"Unknown fake worker kind: {kind}")
|
||||
|
||||
def _target():
|
||||
try:
|
||||
asyncio.run(run(kind, job_id))
|
||||
finally:
|
||||
with _threads_lock:
|
||||
_threads.discard(threading.current_thread())
|
||||
|
||||
thread = threading.Thread(target=_target, name=f"fake-{kind}-{job_id[:8]}", daemon=True)
|
||||
with _threads_lock:
|
||||
_threads.add(thread)
|
||||
thread.start()
|
||||
logger.info(f"[job:{job_id}] fake worker dispatched: {kind}")
|
||||
return True
|
||||
|
||||
|
||||
def active_worker_count() -> int:
|
||||
"""Fake workers still running (the test API reports it so tests can wait for idle)."""
|
||||
with _threads_lock:
|
||||
return sum(1 for t in _threads if t.is_alive())
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Fake Stripe Checkout for the local stack.
|
||||
|
||||
``StripeService`` calls :func:`create_fake_checkout` instead of
|
||||
``stripe.checkout.Session.create`` when Stripe isn't configured and
|
||||
FAKE_WORKERS is on. The returned URL is a tiny "checkout" page served by
|
||||
``/api/test/fake-checkout/{id}``; paying it runs the same completion handler as
|
||||
the real webhook (via ``/api/internal/test-webhook``'s logic), then redirects to
|
||||
the session's success URL, just as Stripe would.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import secrets
|
||||
from datetime import datetime, timezone
|
||||
from types import SimpleNamespace
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
CHECKOUTS_COLLECTION = "local_stack_checkouts"
|
||||
# Must start with "e2e-test-" so the shared test-webhook handler accepts it.
|
||||
SESSION_PREFIX = "e2e-test-local-"
|
||||
|
||||
|
||||
def public_api_url() -> str:
|
||||
"""Base URL browsers / devices use to reach this API (LOCAL_STACK_API_URL)."""
|
||||
return os.getenv("LOCAL_STACK_API_URL", f"http://127.0.0.1:{os.getenv('PORT', '8000')}").rstrip("/")
|
||||
|
||||
|
||||
def _db():
|
||||
from backend.services.firestore_service import FirestoreService
|
||||
|
||||
return FirestoreService().db
|
||||
|
||||
|
||||
def _amount_cents(session_params: Dict[str, Any]) -> int:
|
||||
return sum(
|
||||
(item.get("price_data") or {}).get("unit_amount", 0) * item.get("quantity", 1)
|
||||
for item in session_params.get("line_items") or []
|
||||
)
|
||||
|
||||
|
||||
def create_fake_checkout(session_params: Dict[str, Any]) -> SimpleNamespace:
|
||||
session_id = SESSION_PREFIX + secrets.token_hex(8)
|
||||
line_items = session_params.get("line_items") or [{}]
|
||||
_db().collection(CHECKOUTS_COLLECTION).document(session_id).set({
|
||||
"session_id": session_id,
|
||||
"customer_email": session_params.get("customer_email"),
|
||||
"metadata": session_params.get("metadata") or {},
|
||||
"success_url": session_params.get("success_url"),
|
||||
"cancel_url": session_params.get("cancel_url"),
|
||||
"product_name": ((line_items[0].get("price_data") or {}).get("product_data") or {}).get("name"),
|
||||
"amount_cents": _amount_cents(session_params),
|
||||
"status": "open",
|
||||
"created_at": datetime.now(timezone.utc),
|
||||
})
|
||||
return SimpleNamespace(id=session_id, url=f"{public_api_url()}/api/test/fake-checkout/{session_id}")
|
||||
|
||||
|
||||
def get_checkout(session_id: str) -> Optional[Dict[str, Any]]:
|
||||
snap = _db().collection(CHECKOUTS_COLLECTION).document(session_id).get()
|
||||
return snap.to_dict() if snap.exists else None
|
||||
|
||||
|
||||
def mark_checkout(session_id: str, status: str) -> None:
|
||||
_db().collection(CHECKOUTS_COLLECTION).document(session_id).update({"status": status})
|
||||
|
||||
|
||||
def resolve_url(url: Optional[str], session_id: str) -> str:
|
||||
return (url or "/").replace("{CHECKOUT_SESSION_ID}", session_id)
|
||||
@@ -0,0 +1,114 @@
|
||||
"""Runtime knobs for the fake workers, stored in Firestore so every API process sees them.
|
||||
|
||||
Tests change them through ``PUT /api/test/fake-config`` (see
|
||||
``backend/api/routes/test_support.py``) and reset them with ``POST /api/test/reset``.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import asdict, dataclass, fields
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_COLLECTION = "local_stack"
|
||||
CONFIG_DOC = "fake_config"
|
||||
|
||||
# Worker kinds the fakes implement. ``fail_at`` must be one of these.
|
||||
WORKER_KINDS = ("audio_download", "audio", "lyrics", "screens", "render_video", "video")
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeConfig:
|
||||
# Seconds each fake worker waits before doing its work, so a UI (or test) can
|
||||
# observe the intermediate states. 0 makes the whole pipeline near-instant.
|
||||
step_delay_seconds: float = 0.5
|
||||
# True: the fake screens worker auto-approves the review (skips straight to
|
||||
# render) unless the job asked for review_mode="always_review". False: every job
|
||||
# parks at AWAITING_REVIEW for a human (the default, exercises the review UI).
|
||||
auto_approve: bool = False
|
||||
# Fail the job inside this worker (one of WORKER_KINDS), or None.
|
||||
fail_at: Optional[str] = None
|
||||
# Length of the generated song audio. Longer than the credits charged for the
|
||||
# job sends it to AWAITING_DURATION_CONFIRM (the real duration reconciliation).
|
||||
audio_duration_seconds: int = 30
|
||||
# Number of results the fake audio search returns (0 = "no results").
|
||||
search_result_count: int = 3
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Optional[Dict[str, Any]]) -> "FakeConfig":
|
||||
known = {f.name for f in fields(cls)}
|
||||
return cls(**{k: v for k, v in (data or {}).items() if k in known})
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
def validate(self) -> None:
|
||||
if self.fail_at is not None and self.fail_at not in WORKER_KINDS:
|
||||
raise ValueError(f"fail_at must be one of {WORKER_KINDS} or null")
|
||||
if self.step_delay_seconds < 0 or self.step_delay_seconds > 60:
|
||||
raise ValueError("step_delay_seconds must be between 0 and 60")
|
||||
if not 5 <= self.audio_duration_seconds <= 1800:
|
||||
raise ValueError("audio_duration_seconds must be between 5 and 1800")
|
||||
if not 0 <= self.search_result_count <= 3:
|
||||
raise ValueError("search_result_count must be between 0 and 3")
|
||||
|
||||
|
||||
USER_CONFIG_COLLECTION = "local_stack_user_config"
|
||||
|
||||
|
||||
def _default_db():
|
||||
from backend.services.firestore_service import FirestoreService
|
||||
|
||||
return FirestoreService().db
|
||||
|
||||
|
||||
def _doc(db, email: Optional[str] = None):
|
||||
if email:
|
||||
return db.collection(USER_CONFIG_COLLECTION).document(email.strip().lower())
|
||||
return db.collection(CONFIG_COLLECTION).document(CONFIG_DOC)
|
||||
|
||||
|
||||
def _read(db, email: Optional[str] = None) -> Dict[str, Any]:
|
||||
snap = _doc(db, email).get()
|
||||
return (snap.to_dict() or {}) if snap.exists else {}
|
||||
|
||||
|
||||
def get_fake_config(db=None, email: Optional[str] = None) -> FakeConfig:
|
||||
"""Defaults, overlaid with the global config, overlaid with ``email``'s overrides.
|
||||
|
||||
Per-user overrides let tests that each use their own user run in parallel
|
||||
against one stack without fighting over the global knobs.
|
||||
"""
|
||||
db = db or _default_db()
|
||||
try:
|
||||
merged = _read(db)
|
||||
if email:
|
||||
merged.update(_read(db, email))
|
||||
return FakeConfig.from_dict(merged)
|
||||
except Exception as e: # emulator hiccup: fall back to defaults, never stall a job
|
||||
logger.warning(f"fake_workers: could not read config, using defaults: {e}")
|
||||
return FakeConfig()
|
||||
|
||||
|
||||
def set_fake_config(updates: Dict[str, Any], db=None, email: Optional[str] = None) -> FakeConfig:
|
||||
"""Merge ``updates`` into the global config (or ``email``'s overrides) and return the effective config."""
|
||||
db = db or _default_db()
|
||||
known = {f.name for f in fields(FakeConfig)}
|
||||
unknown = set(updates) - known
|
||||
if unknown:
|
||||
raise ValueError(f"Unknown fake config keys: {sorted(unknown)}")
|
||||
stored = _read(db, email)
|
||||
stored.update(updates)
|
||||
base = get_fake_config(db).to_dict() if email else {}
|
||||
FakeConfig.from_dict({**base, **stored}).validate()
|
||||
_doc(db, email).set(stored)
|
||||
return get_fake_config(db, email)
|
||||
|
||||
|
||||
def clear_fake_config(db=None) -> None:
|
||||
"""Drop the global config and every per-user override (POST /api/test/reset)."""
|
||||
db = db or _default_db()
|
||||
_doc(db).delete()
|
||||
for snap in db.collection(USER_CONFIG_COLLECTION).stream():
|
||||
snap.reference.delete()
|
||||
@@ -0,0 +1,128 @@
|
||||
"""A fake but schema-valid ``corrections.json`` for the fake lyrics worker.
|
||||
|
||||
Built from the real ``CorrectionResult`` dataclasses, so if the schema the review
|
||||
UI depends on changes, the fake changes with it (and its unit test catches
|
||||
anything ``from_dict`` can no longer read).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from karaoke_gen.lyrics_transcriber.types import (
|
||||
AnchorSequence,
|
||||
CorrectionResult,
|
||||
LyricsData,
|
||||
LyricsMetadata,
|
||||
LyricsSegment,
|
||||
Word,
|
||||
)
|
||||
|
||||
FAKE_LINES = (
|
||||
"This is a fake song for testing",
|
||||
"Every word arrives right on time",
|
||||
"Sing along with the local stack",
|
||||
"Nothing here was ever transcribed",
|
||||
"Workers pretend and the jobs move on",
|
||||
"Until the video is done",
|
||||
)
|
||||
|
||||
REFERENCE_SOURCE = "lrclib"
|
||||
LEAD_IN_SECONDS = 2.0
|
||||
|
||||
|
||||
def _segments(prefix: str, duration: float) -> List[LyricsSegment]:
|
||||
"""Spread FAKE_LINES evenly over the song, one word slot per word."""
|
||||
usable = max(duration - LEAD_IN_SECONDS - 1.0, len(FAKE_LINES))
|
||||
line_len = usable / len(FAKE_LINES)
|
||||
segments = []
|
||||
for i, line in enumerate(FAKE_LINES):
|
||||
words_text = line.split()
|
||||
start = LEAD_IN_SECONDS + i * line_len
|
||||
slot = (line_len * 0.9) / len(words_text)
|
||||
words = [
|
||||
Word(
|
||||
id=f"{prefix}w{i}_{j}",
|
||||
text=text,
|
||||
start_time=round(start + j * slot, 3),
|
||||
end_time=round(start + (j + 1) * slot - 0.05, 3),
|
||||
confidence=0.99,
|
||||
)
|
||||
for j, text in enumerate(words_text)
|
||||
]
|
||||
segments.append(
|
||||
LyricsSegment(
|
||||
id=f"{prefix}s{i}",
|
||||
text=line,
|
||||
words=words,
|
||||
start_time=words[0].start_time,
|
||||
end_time=words[-1].end_time,
|
||||
)
|
||||
)
|
||||
return segments
|
||||
|
||||
|
||||
def build_corrections(artist: str, title: str, duration: float) -> Dict[str, Any]:
|
||||
"""Return a ``CorrectionResult.to_dict()`` whose transcription matches its reference."""
|
||||
transcribed = _segments("t", duration)
|
||||
reference = _segments("r", duration)
|
||||
reference_lyrics = {
|
||||
REFERENCE_SOURCE: LyricsData(
|
||||
segments=reference,
|
||||
metadata=LyricsMetadata(
|
||||
source=REFERENCE_SOURCE,
|
||||
track_name=title,
|
||||
artist_names=artist,
|
||||
duration_ms=int(duration * 1000),
|
||||
is_synced=True,
|
||||
lyrics_provider=REFERENCE_SOURCE,
|
||||
lyrics_provider_id="fake",
|
||||
),
|
||||
source=REFERENCE_SOURCE,
|
||||
)
|
||||
}
|
||||
t_ids = [w.id for s in transcribed for w in s.words]
|
||||
r_ids = [w.id for s in reference for w in s.words]
|
||||
anchors = [
|
||||
AnchorSequence(
|
||||
id="fake-anchor-0",
|
||||
transcribed_word_ids=t_ids,
|
||||
transcription_position=0,
|
||||
reference_positions={REFERENCE_SOURCE: 0},
|
||||
reference_word_ids={REFERENCE_SOURCE: r_ids},
|
||||
confidence=1.0,
|
||||
)
|
||||
]
|
||||
result = CorrectionResult(
|
||||
original_segments=transcribed,
|
||||
corrected_segments=transcribed,
|
||||
corrections=[],
|
||||
corrections_made=0,
|
||||
confidence=1.0,
|
||||
reference_lyrics=reference_lyrics,
|
||||
anchor_sequences=anchors,
|
||||
gap_sequences=[],
|
||||
resized_segments=transcribed,
|
||||
metadata={
|
||||
"correction_type": "none",
|
||||
"reason": "fake_workers",
|
||||
"anchor_sequences_count": len(anchors),
|
||||
"gap_sequences_count": 0,
|
||||
"total_words": len(t_ids),
|
||||
"correction_ratio": 0.0,
|
||||
"available_handlers": [],
|
||||
"enabled_handlers": [],
|
||||
"agentic_routing": "disabled",
|
||||
},
|
||||
correction_steps=[],
|
||||
word_id_map={},
|
||||
segment_id_map={},
|
||||
)
|
||||
return result.to_dict()
|
||||
|
||||
|
||||
def build_lrc(corrections: Dict[str, Any]) -> str:
|
||||
lines = []
|
||||
for seg in corrections["corrected_segments"]:
|
||||
m, s = divmod(seg["start_time"], 60)
|
||||
lines.append(f"[{int(m):02d}:{s:05.2f}]{seg['text']}")
|
||||
return "\n".join(lines) + "\n"
|
||||
@@ -0,0 +1,150 @@
|
||||
"""Tiny, real media files for the fake workers.
|
||||
|
||||
Everything downstream of a worker (ffprobe duration reconciliation, the review
|
||||
waveform / OGG transcode, backing-vocals analysis, the local preview render,
|
||||
downloads) reads real files, so the fakes write real ones — just small: mono
|
||||
22.05 kHz tones and 320x180 video. Files are generated once per duration with
|
||||
ffmpeg and cached in a temp dir for the life of the process.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
import zipfile
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SAMPLE_RATE = 22050
|
||||
VIDEO_SIZE = "320x180"
|
||||
|
||||
_lock = threading.Lock()
|
||||
_cache: Dict[int, "MediaSet"] = {}
|
||||
_root = tempfile.mkdtemp(prefix="fake_worker_media_")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MediaSet:
|
||||
duration: int
|
||||
input_audio: str
|
||||
instrumental_clean: str
|
||||
instrumental_with_backing: str
|
||||
vocals: str
|
||||
lead_vocals: str
|
||||
backing_vocals: str
|
||||
title_png: str
|
||||
title_jpg: str
|
||||
end_png: str
|
||||
end_jpg: str
|
||||
with_vocals_mkv: str
|
||||
final_mp4: str
|
||||
final_mkv: str
|
||||
cdg_zip: str
|
||||
txt_zip: str
|
||||
|
||||
|
||||
def _ffmpeg(*args: str) -> None:
|
||||
cmd = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", *args]
|
||||
subprocess.run(cmd, check=True, capture_output=True, timeout=300)
|
||||
|
||||
|
||||
def _tone(path: str, freq: int, duration: int, volume: float = 0.3) -> None:
|
||||
_ffmpeg(
|
||||
"-f", "lavfi", "-i", f"sine=frequency={freq}:sample_rate={SAMPLE_RATE}:duration={duration}",
|
||||
"-af", f"volume={volume}", "-ac", "1", path,
|
||||
)
|
||||
|
||||
|
||||
def _silence(path: str, duration: int) -> None:
|
||||
_ffmpeg(
|
||||
"-f", "lavfi", "-i", f"anullsrc=r={SAMPLE_RATE}:cl=mono", "-t", str(duration), path,
|
||||
)
|
||||
|
||||
|
||||
def _mix(path: str, *inputs: str) -> None:
|
||||
args = []
|
||||
for i in inputs:
|
||||
args += ["-i", i]
|
||||
_ffmpeg(*args, "-filter_complex", f"amix=inputs={len(inputs)}:normalize=0", "-ac", "1", path)
|
||||
|
||||
|
||||
def _screen(png_path: str, jpg_path: str, label: str) -> None:
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
img = Image.new("RGB", (640, 360), (24, 16, 48))
|
||||
ImageDraw.Draw(img).text((24, 160), label, fill=(255, 122, 204))
|
||||
img.save(png_path)
|
||||
img.save(jpg_path, quality=80)
|
||||
|
||||
|
||||
def _video(path: str, audio: str, duration: int, codec_args: list) -> None:
|
||||
_ffmpeg(
|
||||
"-f", "lavfi", "-i", f"color=c=0x181030:s={VIDEO_SIZE}:d={duration}:r=10",
|
||||
"-i", audio, "-shortest", "-c:v", "libx264", "-preset", "ultrafast",
|
||||
"-pix_fmt", "yuv420p", *codec_args, path,
|
||||
)
|
||||
|
||||
|
||||
def _zip(path: str, members: Dict[str, bytes]) -> None:
|
||||
with zipfile.ZipFile(path, "w") as zf:
|
||||
for name, data in members.items():
|
||||
zf.writestr(name, data)
|
||||
|
||||
|
||||
def _build(duration: int) -> MediaSet:
|
||||
d = os.path.join(_root, str(duration))
|
||||
os.makedirs(d, exist_ok=True)
|
||||
p = lambda name: os.path.join(d, name) # noqa: E731
|
||||
|
||||
# "Instrumental" = low tone, "lead vocals" = high tone, backing vocals = silence,
|
||||
# so the real backing-vocals analysis recommends the clean instrumental.
|
||||
_tone(p("instrumental_clean.flac"), 220, duration)
|
||||
_tone(p("lead_vocals.flac"), 660, duration, volume=0.2)
|
||||
_silence(p("backing_vocals.flac"), duration)
|
||||
_mix(p("vocals_clean.flac"), p("lead_vocals.flac"), p("backing_vocals.flac"))
|
||||
_mix(p("instrumental_with_backing.flac"), p("instrumental_clean.flac"), p("backing_vocals.flac"))
|
||||
_mix(p("input.flac"), p("instrumental_clean.flac"), p("vocals_clean.flac"))
|
||||
|
||||
_screen(p("title.png"), p("title.jpg"), "Fake title screen")
|
||||
_screen(p("end.png"), p("end.jpg"), "Fake end screen")
|
||||
|
||||
_video(p("with_vocals.mkv"), p("input.flac"), duration, ["-c:a", "flac"])
|
||||
_video(p("final.mp4"), p("instrumental_clean.flac"), duration, ["-c:a", "aac", "-b:a", "64k"])
|
||||
_video(p("final.mkv"), p("instrumental_clean.flac"), duration, ["-c:a", "flac"])
|
||||
|
||||
_zip(p("cdg.zip"), {"karaoke.cdg": b"\0" * 2400, "karaoke.mp3": b""})
|
||||
_zip(p("txt.zip"), {"karaoke.txt": b"fake lyrics\n", "karaoke.mp3": b""})
|
||||
|
||||
return MediaSet(
|
||||
duration=duration,
|
||||
input_audio=p("input.flac"),
|
||||
instrumental_clean=p("instrumental_clean.flac"),
|
||||
instrumental_with_backing=p("instrumental_with_backing.flac"),
|
||||
vocals=p("vocals_clean.flac"),
|
||||
lead_vocals=p("lead_vocals.flac"),
|
||||
backing_vocals=p("backing_vocals.flac"),
|
||||
title_png=p("title.png"),
|
||||
title_jpg=p("title.jpg"),
|
||||
end_png=p("end.png"),
|
||||
end_jpg=p("end.jpg"),
|
||||
with_vocals_mkv=p("with_vocals.mkv"),
|
||||
final_mp4=p("final.mp4"),
|
||||
final_mkv=p("final.mkv"),
|
||||
cdg_zip=p("cdg.zip"),
|
||||
txt_zip=p("txt.zip"),
|
||||
)
|
||||
|
||||
|
||||
def get_media(duration: int) -> MediaSet:
|
||||
"""Return (building once) the media set for a song of ``duration`` seconds."""
|
||||
with _lock:
|
||||
media = _cache.get(duration)
|
||||
if media is None:
|
||||
logger.info(f"fake_workers: generating {duration}s media set in {_root}")
|
||||
media = _build(duration)
|
||||
_cache[duration] = media
|
||||
return media
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Captured outbound email for the local stack (the console email provider writes here).
|
||||
|
||||
Tests read it through ``GET /api/test/emails`` to follow magic links and codes,
|
||||
the way production E2E reads a testmail.app inbox.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
OUTBOX_COLLECTION = "local_stack_outbox"
|
||||
|
||||
_URL_RE = re.compile(r"https?://[^\s\"'<>]+")
|
||||
# A 6-digit sign-in code: a standalone run of exactly six digits (not part of a
|
||||
# hex colour like #123456 or a longer number).
|
||||
_CODE_RE = re.compile(r"(?<![#\w])(\d{6})(?!\w)")
|
||||
_STYLE_RE = re.compile(r"<(style|script)[^>]*>.*?</\1>", re.S | re.I)
|
||||
_TAG_RE = re.compile(r"<[^>]+>")
|
||||
|
||||
|
||||
def _visible_text(text_content: Optional[str], html_content: Optional[str]) -> str:
|
||||
if text_content:
|
||||
return text_content
|
||||
return html.unescape(_TAG_RE.sub(" ", _STYLE_RE.sub(" ", html_content or "")))
|
||||
|
||||
|
||||
def _db():
|
||||
from backend.services.firestore_service import FirestoreService
|
||||
|
||||
return FirestoreService().db
|
||||
|
||||
|
||||
def record_email(to_email: str, subject: str, html_content: str, text_content: Optional[str]) -> None:
|
||||
text = text_content or ""
|
||||
links = list(dict.fromkeys(
|
||||
html.unescape(u) for u in _URL_RE.findall(f"{text}\n{html_content or ''}")
|
||||
))
|
||||
try:
|
||||
_db().collection(OUTBOX_COLLECTION).add({
|
||||
"to": (to_email or "").strip().lower(),
|
||||
"subject": subject,
|
||||
"text": text_content,
|
||||
"html": html_content,
|
||||
"links": links,
|
||||
"codes": list(dict.fromkeys(_CODE_RE.findall(_visible_text(text_content, html_content)))),
|
||||
"created_at": datetime.now(timezone.utc),
|
||||
})
|
||||
except Exception as e: # capture is best-effort; never break a send
|
||||
logger.warning(f"local stack outbox: could not record email to {to_email}: {e}")
|
||||
|
||||
|
||||
def list_emails(to_email: Optional[str] = None, limit: int = 20) -> List[Dict[str, Any]]:
|
||||
"""Newest first. Filtering happens in memory (the outbox is small and test-only)."""
|
||||
docs = [d.to_dict() | {"id": d.id} for d in _db().collection(OUTBOX_COLLECTION).stream()]
|
||||
if to_email:
|
||||
docs = [d for d in docs if d.get("to") == to_email.strip().lower()]
|
||||
docs.sort(key=lambda d: d.get("created_at") or datetime.min.replace(tzinfo=timezone.utc), reverse=True)
|
||||
for d in docs:
|
||||
if isinstance(d.get("created_at"), datetime):
|
||||
d["created_at"] = d["created_at"].isoformat()
|
||||
return docs[:limit]
|
||||
@@ -0,0 +1,348 @@
|
||||
"""The fake workers: walk a job through its real states with canned media.
|
||||
|
||||
Each coroutine mirrors what the real worker writes (status transitions through
|
||||
``JobManager``, ``file_urls``, ``state_data``, ``processing_metadata``) and
|
||||
triggers the next stage through ``WorkerService`` exactly like the real one, so
|
||||
everything outside the workers (API routes, review UI, downloads, emails) runs
|
||||
for real. Where a real helper works offline it is reused rather than copied:
|
||||
the audio download runs the real ``process_audio_download`` (only the fetch
|
||||
from the provider is faked), and the fake audio worker calls the real stem
|
||||
upload, review-stem transcode and backing-vocals analysis.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
from backend.models.job import JobStatus
|
||||
from backend.services.fake_workers.config import FakeConfig, get_fake_config
|
||||
from backend.services.fake_workers.lyrics import build_corrections, build_lrc
|
||||
from backend.services.fake_workers.media import MediaSet, get_media
|
||||
from backend.services.job_manager import JobManager
|
||||
from backend.services.storage_service import StorageService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
FAKE_MODEL_NAMES = {
|
||||
"clean_instrumental_model": "fake_clean_model.ckpt",
|
||||
"backing_vocals_models": ["fake_backing_model.ckpt"],
|
||||
"other_stems_models": [],
|
||||
}
|
||||
|
||||
|
||||
class FakeWorkerFailure(Exception):
|
||||
"""Raised inside a fake worker when FakeConfig.fail_at targets it."""
|
||||
|
||||
|
||||
def _job_or_none(job_manager: JobManager, job_id: str):
|
||||
job = job_manager.get_job(job_id)
|
||||
if job is None:
|
||||
logger.warning(f"[job:{job_id}] fake worker: job not found")
|
||||
return job
|
||||
|
||||
|
||||
def _config_for_job(job_id: str) -> FakeConfig:
|
||||
job = JobManager().get_job(job_id)
|
||||
return get_fake_config(email=getattr(job, "user_email", None) if job else None)
|
||||
|
||||
|
||||
async def _begin(kind: str, job_id: str) -> FakeConfig:
|
||||
config = await asyncio.to_thread(_config_for_job, job_id)
|
||||
logger.info(f"[job:{job_id}] WORKER_START worker={kind} fake=true")
|
||||
if config.step_delay_seconds:
|
||||
await asyncio.sleep(config.step_delay_seconds)
|
||||
if config.fail_at == kind:
|
||||
raise FakeWorkerFailure(f"Fake {kind} worker failed (fail_at={kind})")
|
||||
return config
|
||||
|
||||
|
||||
def _media_for(config: FakeConfig) -> MediaSet:
|
||||
return get_media(int(config.audio_duration_seconds))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- audio download
|
||||
|
||||
async def fake_download_audio(job_id: str, selected: dict, storage: StorageService) -> tuple[str, str]:
|
||||
"""Stand-in for ``audio_download_worker._download_audio``: "fetch" the canned song."""
|
||||
config = await asyncio.to_thread(_config_for_job, job_id)
|
||||
media = await asyncio.to_thread(_media_for, config)
|
||||
artist = (selected or {}).get("artist") or "Fake Artist"
|
||||
title = (selected or {}).get("title") or "Fake Title"
|
||||
filename = f"{artist} - {title}.flac".replace("/", "_")
|
||||
gcs_path = f"uploads/{job_id}/audio/{filename}"
|
||||
await asyncio.to_thread(storage.upload_file, media.input_audio, gcs_path)
|
||||
return gcs_path, filename
|
||||
|
||||
|
||||
async def run_audio_download(job_id: str) -> bool:
|
||||
await _begin("audio_download", job_id)
|
||||
from backend.workers.audio_download_worker import process_audio_download
|
||||
|
||||
return await process_audio_download(job_id)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- audio separation
|
||||
|
||||
async def run_audio(job_id: str) -> bool:
|
||||
config = await _begin("audio", job_id)
|
||||
from backend.workers.audio_worker import _transcode_review_stems, upload_separation_results
|
||||
from backend.workers.screens_worker import _analyze_backing_vocals
|
||||
from backend.workers.worker_logging import create_job_logger
|
||||
|
||||
job_manager, storage = JobManager(), StorageService()
|
||||
if not _job_or_none(job_manager, job_id):
|
||||
return False
|
||||
job_log = create_job_logger(job_id, "audio")
|
||||
media = await asyncio.to_thread(_media_for, config)
|
||||
|
||||
job_manager.update_state_data(job_id, "audio_progress", {
|
||||
"stage": "separating", "progress": 30, "message": "Separating audio (fake)",
|
||||
})
|
||||
separation_result = {
|
||||
"clean_instrumental": {"instrumental": media.instrumental_clean, "vocals": media.vocals},
|
||||
"backing_vocals": {
|
||||
"fake_backing_model.ckpt": {
|
||||
"lead_vocals": media.lead_vocals,
|
||||
"backing_vocals": media.backing_vocals,
|
||||
}
|
||||
},
|
||||
"combined_instrumentals": {"fake_backing_model.ckpt": media.instrumental_with_backing},
|
||||
}
|
||||
job_manager.update_state_data(job_id, "model_names", FAKE_MODEL_NAMES)
|
||||
await upload_separation_results(job_id, separation_result, storage, job_manager)
|
||||
await _transcode_review_stems(job_id, job_manager, storage, job_log)
|
||||
job_manager.update_processing_metadata(job_id, "separation", {
|
||||
"provider": "fake_workers",
|
||||
"clean_model": FAKE_MODEL_NAMES["clean_instrumental_model"],
|
||||
"backing_models": FAKE_MODEL_NAMES["backing_vocals_models"],
|
||||
"other_models": [],
|
||||
"stems_generated": list(separation_result.keys()),
|
||||
"duration_seconds": 0.0,
|
||||
})
|
||||
try:
|
||||
await _analyze_backing_vocals(job_id, job_manager, storage, job_log)
|
||||
except Exception as e: # non-fatal, same as the real worker
|
||||
logger.warning(f"[job:{job_id}] fake audio: backing vocals analysis failed: {e}")
|
||||
|
||||
job_manager.mark_audio_complete(job_id)
|
||||
await job_manager.advance_to_screens_if_ready(job_id)
|
||||
return True
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- lyrics
|
||||
|
||||
async def run_lyrics(job_id: str) -> bool:
|
||||
config = await _begin("lyrics", job_id)
|
||||
job_manager, storage = JobManager(), StorageService()
|
||||
job = _job_or_none(job_manager, job_id)
|
||||
if not job:
|
||||
return False
|
||||
|
||||
corrections = build_corrections(job.artist or "Fake Artist", job.title or "Fake Title",
|
||||
float(config.audio_duration_seconds))
|
||||
with tempfile.TemporaryDirectory(prefix=f"fake_lyrics_{job_id}_") as tmp:
|
||||
corrections_path = os.path.join(tmp, "corrections.json")
|
||||
with open(corrections_path, "w") as f:
|
||||
json.dump(corrections, f)
|
||||
lrc_path = os.path.join(tmp, "karaoke.lrc")
|
||||
with open(lrc_path, "w") as f:
|
||||
f.write(build_lrc(corrections))
|
||||
url = await asyncio.to_thread(storage.upload_file, corrections_path, f"jobs/{job_id}/lyrics/corrections.json")
|
||||
job_manager.update_file_url(job_id, "lyrics", "corrections", url)
|
||||
url = await asyncio.to_thread(storage.upload_file, lrc_path, f"jobs/{job_id}/lyrics/karaoke.lrc")
|
||||
job_manager.update_file_url(job_id, "lyrics", "lrc", url)
|
||||
|
||||
job_manager.update_state_data(job_id, "lyrics_metadata", {
|
||||
"segment_count": len(corrections["corrected_segments"]),
|
||||
"has_corrections": True,
|
||||
"ready_for_review": True,
|
||||
"has_countdown_padding": False,
|
||||
"countdown_padding_seconds": 0.0,
|
||||
})
|
||||
job_manager.update_processing_metadata(job_id, "transcription", {
|
||||
"provider": "fake_workers", "reference_sources": ["lrclib"],
|
||||
})
|
||||
job_manager.mark_lyrics_complete(job_id)
|
||||
await job_manager.advance_to_screens_if_ready(job_id)
|
||||
return True
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- screens
|
||||
|
||||
async def run_screens(job_id: str) -> bool:
|
||||
config = await _begin("screens", job_id)
|
||||
from backend.workers.screens_worker import (
|
||||
_SCREENS_ENTRY_STATUSES,
|
||||
_transcode_review_audio,
|
||||
_upload_screens,
|
||||
)
|
||||
from backend.workers.worker_logging import create_job_logger
|
||||
|
||||
job_manager, storage = JobManager(), StorageService()
|
||||
job = _job_or_none(job_manager, job_id)
|
||||
if not job:
|
||||
return False
|
||||
if job.status not in _SCREENS_ENTRY_STATUSES:
|
||||
logger.info(f"[job:{job_id}] fake screens: duplicate dispatch (status={job.status}), skipping")
|
||||
return True
|
||||
if not job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.GENERATING_SCREENS, progress=50,
|
||||
message="Generating title and end screens", raise_on_invalid=False,
|
||||
):
|
||||
return True
|
||||
|
||||
media = await asyncio.to_thread(_media_for, config)
|
||||
with tempfile.TemporaryDirectory(prefix=f"fake_screens_{job_id}_") as tmp:
|
||||
paths = {}
|
||||
for screen in ("title", "end"):
|
||||
for ext in ("png", "jpg"):
|
||||
src = getattr(media, f"{screen}_{ext}")
|
||||
dst = os.path.join(tmp, f"{screen}.{ext}")
|
||||
with open(src, "rb") as r, open(dst, "wb") as w:
|
||||
w.write(r.read())
|
||||
paths[screen] = os.path.join(tmp, f"{screen}.png")
|
||||
await _upload_screens(job_id, job_manager, storage, paths["title"], paths["end"])
|
||||
job_manager.update_state_data(job_id, "screens_included", {"title": True, "end": True})
|
||||
await _transcode_review_audio(job_id, job_manager, create_job_logger(job_id, "screens"))
|
||||
|
||||
if config.auto_approve and getattr(job, "review_mode", None) != "always_review":
|
||||
job_manager.update_state_data(job_id, "instrumental_selection", "clean")
|
||||
job_manager.update_processing_metadata(job_id, "auto_approval", {
|
||||
"outcome": "auto_completed", "source": "fake_workers",
|
||||
})
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.REVIEW_COMPLETE, progress=60,
|
||||
message="Review auto-approved (fake workers)",
|
||||
)
|
||||
from backend.services.worker_service import get_worker_service
|
||||
|
||||
await get_worker_service().trigger_render_video_worker(job_id)
|
||||
else:
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.AWAITING_REVIEW, progress=55,
|
||||
message="Ready for review. Please review lyrics and select your instrumental.",
|
||||
)
|
||||
job_manager.update_state_data(job_id, "screens_progress", {"stage": "complete"})
|
||||
return True
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- render (post-review)
|
||||
|
||||
async def run_render_video(job_id: str) -> bool:
|
||||
config = await _begin("render_video", job_id)
|
||||
job_manager, storage = JobManager(), StorageService()
|
||||
job = _job_or_none(job_manager, job_id)
|
||||
if not job:
|
||||
return False
|
||||
if job.status != JobStatus.REVIEW_COMPLETE:
|
||||
logger.info(f"[job:{job_id}] fake render: status={job.status}, not REVIEW_COMPLETE; skipping")
|
||||
return True
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.RENDERING_VIDEO, progress=65,
|
||||
message="Rendering karaoke video with your corrections",
|
||||
)
|
||||
media = await asyncio.to_thread(_media_for, config)
|
||||
url = await asyncio.to_thread(storage.upload_file, media.with_vocals_mkv, f"jobs/{job_id}/videos/with_vocals.mkv")
|
||||
job_manager.update_file_url(job_id, "videos", "with_vocals", url)
|
||||
with tempfile.TemporaryDirectory(prefix=f"fake_render_{job_id}_") as tmp:
|
||||
for key, name, body in (
|
||||
("ass", "karaoke.ass", "[Script Info]\nTitle: fake\n"),
|
||||
("corrected_txt", "karaoke.txt", "fake lyrics\n"),
|
||||
):
|
||||
path = os.path.join(tmp, name)
|
||||
with open(path, "w") as f:
|
||||
f.write(body)
|
||||
url = await asyncio.to_thread(storage.upload_file, path, f"jobs/{job_id}/lyrics/{name}")
|
||||
job_manager.update_file_url(job_id, "lyrics", key, url)
|
||||
job_manager.update_state_data(job_id, "render_progress", {"stage": "complete"})
|
||||
|
||||
if getattr(job, "prep_only", False):
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.PREP_COMPLETE, progress=100,
|
||||
message="Prep complete (fake workers)",
|
||||
)
|
||||
return True
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.INSTRUMENTAL_SELECTED, progress=68,
|
||||
message="Render complete, starting final encoding",
|
||||
)
|
||||
from backend.services.worker_service import get_worker_service
|
||||
|
||||
return await get_worker_service().trigger_video_worker(job_id)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- final video
|
||||
|
||||
async def run_video(job_id: str) -> bool:
|
||||
config = await _begin("video", job_id)
|
||||
job_manager, storage = JobManager(), StorageService()
|
||||
job = _job_or_none(job_manager, job_id)
|
||||
if not job:
|
||||
return False
|
||||
started = time.time()
|
||||
media = await asyncio.to_thread(_media_for, config)
|
||||
|
||||
for status, progress, message in (
|
||||
(JobStatus.GENERATING_VIDEO, 70, "Preparing files for video generation"),
|
||||
(JobStatus.ENCODING, 80, "Encoding videos (fake)"),
|
||||
(JobStatus.PACKAGING, 92, "Packaging outputs (fake)"),
|
||||
):
|
||||
job_manager.transition_to_state(job_id=job_id, new_status=status, progress=progress, message=message)
|
||||
if config.step_delay_seconds:
|
||||
await asyncio.sleep(config.step_delay_seconds / 2)
|
||||
|
||||
uploads = (
|
||||
(media.final_mp4, "finals", "lossless_4k_mp4"),
|
||||
(media.final_mkv, "finals", "lossless_4k_mkv"),
|
||||
(media.final_mp4, "finals", "lossy_4k_mp4"),
|
||||
(media.final_mp4, "finals", "lossy_720p_mp4"),
|
||||
(media.final_mp4, "finals", "with_vocals_mp4"),
|
||||
(media.cdg_zip, "packages", "cdg_zip"),
|
||||
(media.txt_zip, "packages", "txt_zip"),
|
||||
)
|
||||
for local, category, key in uploads:
|
||||
ext = os.path.splitext(local)[1]
|
||||
url = await asyncio.to_thread(storage.upload_file, local, f"jobs/{job_id}/{category}/{key}{ext}")
|
||||
job_manager.update_file_url(job_id, category, key, url)
|
||||
|
||||
job_manager.update_state_data(job_id, "video_progress", {"stage": "complete"})
|
||||
job_manager.update_processing_metadata(job_id, "timing.video_worker_seconds", round(time.time() - started, 1))
|
||||
job_manager.transition_to_state(
|
||||
job_id=job_id, new_status=JobStatus.COMPLETE, progress=100,
|
||||
message="Karaoke generation complete!",
|
||||
timeline_metadata={"action": "completed", "brand_code": None, "fake_workers": True},
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
RUNNERS = {
|
||||
"audio_download": run_audio_download,
|
||||
"audio": run_audio,
|
||||
"lyrics": run_lyrics,
|
||||
"screens": run_screens,
|
||||
"render_video": run_render_video,
|
||||
"video": run_video,
|
||||
}
|
||||
|
||||
|
||||
async def run(kind: str, job_id: str) -> bool:
|
||||
"""Run one fake worker, failing the job (like a real worker crash) on error."""
|
||||
started = time.time()
|
||||
try:
|
||||
ok = await RUNNERS[kind](job_id)
|
||||
logger.info(
|
||||
f"[job:{job_id}] WORKER_END worker={kind} fake=true status={'success' if ok else 'noop'} "
|
||||
f"duration={time.time() - started:.1f}s"
|
||||
)
|
||||
return ok
|
||||
except Exception as e:
|
||||
logger.error(f"[job:{job_id}] WORKER_END worker={kind} fake=true status=error error={e}", exc_info=True)
|
||||
try:
|
||||
JobManager().fail_job(job_id, f"{kind} failed: {e}", error_details={"stage": kind, "fake_workers": True})
|
||||
except Exception:
|
||||
logger.exception(f"[job:{job_id}] fake worker: could not mark job failed")
|
||||
return False
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Named starting states for local-stack tests (``POST /api/test/scenario``).
|
||||
|
||||
A scenario creates a fresh signed-in user and, optionally, a job parked in a
|
||||
given state. Jobs are made through the real HTTP routes (standalone search,
|
||||
then create-from-search with the user's own session token), then walked by the
|
||||
fake workers with zero delay and per-user overrides. So a seeded
|
||||
``awaiting_review`` job is exactly what a real user would have at that point,
|
||||
not a hand-built document.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
import time
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from backend.models.job import JobStatus
|
||||
from backend.services.fake_workers.checkout import public_api_url
|
||||
from backend.services.fake_workers.config import set_fake_config
|
||||
|
||||
# target -> (per-user fake config overrides, status the job must reach)
|
||||
JOB_TARGETS: Dict[str, tuple] = {
|
||||
"awaiting_review": ({"step_delay_seconds": 0, "auto_approve": False}, JobStatus.AWAITING_REVIEW),
|
||||
"complete": ({"step_delay_seconds": 0, "auto_approve": True}, JobStatus.COMPLETE),
|
||||
"failed": ({"step_delay_seconds": 0, "fail_at": "lyrics"}, JobStatus.FAILED),
|
||||
# Search reports the GLOBAL duration (30 s by default, so 1 credit is charged);
|
||||
# the "downloaded" audio is 11 min (2 credits), so the real reconciliation pauses it.
|
||||
"awaiting_duration_confirm": (
|
||||
{"step_delay_seconds": 0, "audio_duration_seconds": 660},
|
||||
JobStatus.AWAITING_DURATION_CONFIRM,
|
||||
),
|
||||
}
|
||||
|
||||
DEFAULT_ARTIST = "Local Stack"
|
||||
DEFAULT_TITLE = "Fake Song"
|
||||
JOB_TIMEOUT_SECONDS = 120
|
||||
|
||||
|
||||
class ScenarioError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _user_service():
|
||||
from backend.services.user_service import get_user_service
|
||||
|
||||
return get_user_service()
|
||||
|
||||
|
||||
def create_user(email: Optional[str] = None, credits: int = 2) -> Dict[str, Any]:
|
||||
"""A verified user who already had their welcome credit, holding ``credits`` credits."""
|
||||
from datetime import datetime
|
||||
|
||||
email = (email or f"e2e-{secrets.token_hex(4)}@example.com").strip().lower()
|
||||
users = _user_service()
|
||||
user = users.get_or_create_user(email, signup_source="local_stack")
|
||||
users.update_user(email, email_verified=True, welcome_credits_granted=True, last_login_at=datetime.utcnow())
|
||||
delta = credits - (user.credits or 0)
|
||||
if delta > 0:
|
||||
users.add_credits(email=email, amount=delta, reason="local_stack_seed")
|
||||
elif delta < 0: # re-seeding an existing test user: set the balance outright
|
||||
users.update_user(email, credits=credits)
|
||||
session = users.create_session(email, ip_address="127.0.0.1", user_agent="local-stack-scenario")
|
||||
return {"email": email, "session_token": session.token, "credits": users.check_credits(email)}
|
||||
|
||||
|
||||
def _api(token: str) -> httpx.Client:
|
||||
return httpx.Client(
|
||||
base_url=public_api_url(), headers={"Authorization": f"Bearer {token}"}, timeout=60,
|
||||
)
|
||||
|
||||
|
||||
def create_job(user: Dict[str, Any], target: str, artist: str = DEFAULT_ARTIST, title: str = DEFAULT_TITLE) -> str:
|
||||
"""Create a job for ``user`` through the real routes and wait until it reaches ``target``."""
|
||||
if target not in JOB_TARGETS:
|
||||
raise ScenarioError(f"Unknown job target {target!r}; one of {sorted(JOB_TARGETS)}")
|
||||
overrides, wanted = JOB_TARGETS[target]
|
||||
set_fake_config(overrides, email=user["email"])
|
||||
|
||||
with _api(user["session_token"]) as api:
|
||||
search = api.post("/api/audio-search/search-standalone", json={"artist": artist, "title": title})
|
||||
if search.status_code != 200:
|
||||
raise ScenarioError(f"search-standalone failed: {search.status_code} {search.text}")
|
||||
created = api.post("/api/jobs/create-from-search", json={
|
||||
"search_session_id": search.json()["search_session_id"],
|
||||
"selection_index": 0,
|
||||
"artist": artist,
|
||||
"title": title,
|
||||
"review_mode": "always_review" if target == "awaiting_review" else "auto",
|
||||
})
|
||||
if created.status_code != 200:
|
||||
raise ScenarioError(f"create-from-search failed: {created.status_code} {created.text}")
|
||||
job_id = created.json()["job_id"]
|
||||
|
||||
from backend.services.job_manager import JobManager
|
||||
|
||||
job_manager = JobManager()
|
||||
deadline = time.time() + JOB_TIMEOUT_SECONDS
|
||||
status = None
|
||||
while time.time() < deadline:
|
||||
job = job_manager.get_job(job_id)
|
||||
status = job.status if job else None
|
||||
if status == wanted:
|
||||
return job_id
|
||||
if status == JobStatus.FAILED and wanted != JobStatus.FAILED:
|
||||
raise ScenarioError(f"job {job_id} failed before reaching {target}: {job.error_message}")
|
||||
time.sleep(0.25)
|
||||
raise ScenarioError(f"job {job_id} did not reach {target} within {JOB_TIMEOUT_SECONDS}s (status={status})")
|
||||
|
||||
|
||||
def run_scenario(name: str, email: Optional[str] = None, credits: Optional[int] = None,
|
||||
artist: Optional[str] = None, title: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""``user`` or ``job_<target>`` (e.g. ``job_awaiting_review``)."""
|
||||
if name == "user":
|
||||
return create_user(email, 2 if credits is None else credits)
|
||||
if name.startswith("job_"):
|
||||
target = name[len("job_"):]
|
||||
if target not in JOB_TARGETS:
|
||||
raise ScenarioError(f"Unknown scenario {name!r}")
|
||||
user = create_user(email, 2 if credits is None else credits)
|
||||
job_id = create_job(user, target, artist or DEFAULT_ARTIST, title or DEFAULT_TITLE)
|
||||
return {**user, "credits": _user_service().check_credits(user["email"]), "job_id": job_id}
|
||||
raise ScenarioError(f"Unknown scenario {name!r}")
|
||||
|
||||
|
||||
SCENARIOS = ["user"] + [f"job_{t}" for t in JOB_TARGETS]
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Deterministic audio search for the local stack (no flacfetch, no YouTube).
|
||||
|
||||
Returns up to three results for any artist/title, in the shapes the web UI
|
||||
ranks: a well-seeded lossless torrent (the "BEST CHOICE"), a Spotify match and a
|
||||
YouTube upload. Downloads are served by ``pipeline.fake_download_audio``, keyed
|
||||
on nothing but the job, so every provider "works".
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from typing import List
|
||||
|
||||
from karaoke_gen.audio_fetcher import AudioSearchResult, NoResultsError
|
||||
|
||||
|
||||
def _source_id(provider: str, artist: str, title: str) -> str:
|
||||
return hashlib.sha1(f"{provider}:{artist}:{title}".lower().encode()).hexdigest()[:16]
|
||||
|
||||
|
||||
class FakeAudioSearchService:
|
||||
"""Drop-in for ``AudioSearchService`` (the methods the API routes call)."""
|
||||
|
||||
last_remote_search_id = None
|
||||
|
||||
def search(self, artist: str, title: str) -> List[AudioSearchResult]:
|
||||
from backend.services.fake_workers.config import get_fake_config
|
||||
|
||||
config = get_fake_config()
|
||||
duration = int(config.audio_duration_seconds)
|
||||
candidates = [
|
||||
AudioSearchResult(
|
||||
title=title, artist=artist, provider="RED",
|
||||
url=None, duration=duration, quality="FLAC 16bit CD",
|
||||
source_id=_source_id("RED", artist, title), seeders=120,
|
||||
target_file=f"01 - {title}.flac",
|
||||
raw_result={
|
||||
"year": 2001, "label": "Fake Records", "release_type": "Album",
|
||||
"is_lossless": True, "seeders": 120, "match_score": 1.0,
|
||||
"quality_str": "FLAC 16bit CD", "formatted_duration": f"{duration // 60}:{duration % 60:02d}",
|
||||
"quality_data": {"format": "FLAC", "bit_depth": 16, "media": "CD"},
|
||||
},
|
||||
),
|
||||
AudioSearchResult(
|
||||
title=title, artist=artist, provider="Spotify",
|
||||
url=f"https://open.spotify.com/track/{_source_id('Spotify', artist, title)}",
|
||||
duration=duration, quality="OGG 320kbps",
|
||||
source_id=_source_id("Spotify", artist, title),
|
||||
raw_result={"is_lossless": False, "quality_str": "OGG 320kbps", "match_score": 0.95},
|
||||
),
|
||||
AudioSearchResult(
|
||||
title=f"{artist} - {title} (Official Video)", artist=artist, provider="YouTube",
|
||||
url=f"https://www.youtube.com/watch?v={_source_id('YouTube', artist, title)[:11]}",
|
||||
duration=duration, quality="opus 160kbps",
|
||||
source_id=_source_id("YouTube", artist, title)[:11],
|
||||
raw_result={"is_lossless": False, "channel": f"{artist}VEVO", "view_count": 1000000},
|
||||
),
|
||||
][: config.search_result_count]
|
||||
if not candidates:
|
||||
raise NoResultsError(f"No results for {artist} - {title} (fake search)")
|
||||
for i, result in enumerate(candidates):
|
||||
result.index = i
|
||||
return candidates
|
||||
|
||||
async def search_async(self, artist: str, title: str) -> List[AudioSearchResult]:
|
||||
import asyncio
|
||||
|
||||
return await asyncio.to_thread(self.search, artist, title)
|
||||
|
||||
def select_best(self, results: List[AudioSearchResult]) -> int:
|
||||
return 0
|
||||
@@ -27,19 +27,31 @@ concurrent requests can't slip past them.
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
import logging
|
||||
import secrets
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any, Optional
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from google.api_core import exceptions as google_exceptions
|
||||
from google.cloud import firestore
|
||||
from google.cloud.firestore_v1 import FieldFilter, Increment
|
||||
from google.cloud import firestore # noqa: F401 (tests patch kps.firestore.transactional)
|
||||
from google.cloud.firestore_v1 import FieldFilter
|
||||
|
||||
from backend.models.user import CreditTransaction
|
||||
from backend.services.login_code_store import ( # noqa: F401 (re-exported for callers/tests)
|
||||
CODE_EXPIRY_MINUTES,
|
||||
CODE_LENGTH,
|
||||
MAX_VERIFY_ATTEMPTS,
|
||||
TRANSACTION_MAX_ATTEMPTS,
|
||||
CodeThrottled,
|
||||
IssuedCode,
|
||||
LoginCodeStore,
|
||||
TransactionContention,
|
||||
VerifyOutcome,
|
||||
_aware,
|
||||
_now,
|
||||
code_doc_id,
|
||||
run_firestore_transaction,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -49,58 +61,15 @@ SIGNUPS_COLLECTION = "kjbox_signups"
|
||||
SHOW_CREDITS_COLLECTION = "kjbox_show_credits"
|
||||
SHOW_CREDIT_USERS_COLLECTION = "kjbox_show_credit_users"
|
||||
|
||||
CODE_LENGTH = 6
|
||||
CODE_EXPIRY_MINUTES = 10
|
||||
MAX_VERIFY_ATTEMPTS = 5
|
||||
SHOW_CREDIT_REASON = "kjbox show credit"
|
||||
|
||||
TRANSACTION_MAX_ATTEMPTS = 10
|
||||
|
||||
_THROTTLE_WINDOW = timedelta(hours=1)
|
||||
_DAY = timedelta(hours=24)
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def _aware(dt: Any) -> Optional[datetime]:
|
||||
"""Normalize Firestore/ISO timestamps to tz-aware UTC datetimes."""
|
||||
if dt is None:
|
||||
return None
|
||||
if isinstance(dt, str):
|
||||
try:
|
||||
dt = datetime.fromisoformat(dt)
|
||||
except ValueError:
|
||||
return None
|
||||
if not isinstance(dt, datetime):
|
||||
return None
|
||||
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def _sha256(value: str) -> str:
|
||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def code_doc_id(email: str) -> str:
|
||||
return _sha256(email.strip().lower())
|
||||
|
||||
|
||||
@dataclass
|
||||
class IssuedCode:
|
||||
code: str
|
||||
doc_id: str
|
||||
needs_credit_eval: bool
|
||||
|
||||
|
||||
class TransactionContention(Exception):
|
||||
"""A Firestore transaction gave up after repeated contention (nothing written)."""
|
||||
|
||||
|
||||
class CodeThrottled(Exception):
|
||||
"""Per-email code throttle hit (raised out of the issue transaction)."""
|
||||
|
||||
|
||||
class ShowCreditCapReached(Exception):
|
||||
"""Per-user rolling-24h show-credit cap hit."""
|
||||
|
||||
@@ -111,54 +80,47 @@ class ShowCreditResult:
|
||||
credits: int
|
||||
|
||||
|
||||
class VerifyOutcome:
|
||||
OK = "ok"
|
||||
EXPIRED = "expired"
|
||||
INVALID = "invalid_code"
|
||||
TOO_MANY_ATTEMPTS = "too_many_attempts"
|
||||
|
||||
|
||||
class KjboxPartnerService:
|
||||
"""Code issuance/verification, signup cap, and show-credit bookkeeping."""
|
||||
"""Code issuance/verification, signup cap, and show-credit bookkeeping.
|
||||
|
||||
Code issue/verify is delegated to :class:`LoginCodeStore` (shared with gen's
|
||||
own email-code sign-in) over the kjbox-specific collections.
|
||||
"""
|
||||
|
||||
def __init__(self, db, pepper: str, transaction_runner=None):
|
||||
self.db = db
|
||||
# The partner secret doubles as the HMAC pepper: a Firestore read alone
|
||||
# (backups, exports) can't brute-force the 10^6 code space offline.
|
||||
self._pepper = pepper.encode("utf-8")
|
||||
# Injectable for unit tests (in-memory Firestore fake); production runs
|
||||
# ``fn(transaction)`` under @firestore.transactional (retried on contention).
|
||||
self._transaction_runner = transaction_runner
|
||||
# The partner secret doubles as the HMAC pepper. The store routes its
|
||||
# transactions through self._run_transaction (looked up per call, so a
|
||||
# class-level patch in tests applies to code issuance too).
|
||||
self._codes = LoginCodeStore(
|
||||
db,
|
||||
pepper,
|
||||
LOGIN_CODES_COLLECTION,
|
||||
LOGIN_CODE_USES_COLLECTION,
|
||||
transaction_runner=lambda fn: self._run_transaction(fn),
|
||||
)
|
||||
|
||||
def _run_transaction(self, fn):
|
||||
if self._transaction_runner is not None:
|
||||
return self._transaction_runner(fn)
|
||||
try:
|
||||
return firestore.transactional(fn)(self.db.transaction(max_attempts=TRANSACTION_MAX_ATTEMPTS))
|
||||
except ValueError as exc:
|
||||
# google-cloud-firestore gives up with ValueError("Failed to commit
|
||||
# transaction in N attempts.") under heavy contention. Nothing was
|
||||
# written, so callers can safely treat it as "busy, retry".
|
||||
if "Failed to commit transaction" in str(exc):
|
||||
raise TransactionContention() from exc
|
||||
raise
|
||||
return run_firestore_transaction(self.db, fn)
|
||||
|
||||
# ------------------------------------------------------------------ codes
|
||||
|
||||
def _hash_code(self, code_id: str, code: str) -> str:
|
||||
return hmac.new(self._pepper, f"{code_id}:{code}".encode("utf-8"), hashlib.sha256).hexdigest()
|
||||
return self._codes.hash_code(code_id, code)
|
||||
|
||||
def _code_ref(self, email: str):
|
||||
return self.db.collection(LOGIN_CODES_COLLECTION).document(code_doc_id(email))
|
||||
return self._codes.code_ref(email)
|
||||
|
||||
def get_code_record(self, email: str) -> Optional[dict]:
|
||||
snap = self._code_ref(email).get()
|
||||
return snap.to_dict() if snap.exists else None
|
||||
return self._codes.get_code_record(email)
|
||||
|
||||
def recent_send_times(self, record: Optional[dict], now: Optional[datetime] = None) -> list:
|
||||
now = now or _now()
|
||||
times = [_aware(t) for t in (record or {}).get("send_times") or []]
|
||||
return [t for t in times if t and now - t < _THROTTLE_WINDOW]
|
||||
return self._codes.recent_send_times(record, now)
|
||||
|
||||
def issue_code(
|
||||
self,
|
||||
@@ -172,97 +134,27 @@ class KjboxPartnerService:
|
||||
ip_address: Optional[str],
|
||||
user_agent: Optional[str],
|
||||
) -> IssuedCode:
|
||||
"""Mint a fresh code for ``email``, replacing (invalidating) any prior one.
|
||||
"""Mint a fresh code for ``email`` (see :meth:`LoginCodeStore.issue_code`).
|
||||
|
||||
The throttle check, the send_times append and the code write happen in ONE
|
||||
transaction, so parallel requests can't exceed ``max_per_hour`` sends
|
||||
(raises :class:`CodeThrottled`). Only an HMAC of the code is stored.
|
||||
``merge=True`` keeps any precomputed welcome-credit evaluation across
|
||||
re-sends. Venue/locale are kept for attribution when the email verifies.
|
||||
Venue/locale are kept on the record for attribution when the email verifies.
|
||||
Raises :class:`CodeThrottled` past ``max_per_hour`` sends.
|
||||
"""
|
||||
email = email.strip().lower()
|
||||
ref = self._code_ref(email)
|
||||
|
||||
def _txn(transaction):
|
||||
now = _now()
|
||||
snap = ref.get(transaction=transaction)
|
||||
record = snap.to_dict() if snap.exists else None
|
||||
recent = self.recent_send_times(record, now)
|
||||
if len(recent) >= max_per_hour:
|
||||
raise CodeThrottled()
|
||||
code = f"{secrets.randbelow(10 ** CODE_LENGTH):0{CODE_LENGTH}d}"
|
||||
code_id = secrets.token_urlsafe(16)
|
||||
transaction.set(
|
||||
ref,
|
||||
{
|
||||
"email": email,
|
||||
"code_id": code_id,
|
||||
"code_hash": self._hash_code(code_id, code),
|
||||
"created_at": now,
|
||||
"expires_at": now + timedelta(minutes=CODE_EXPIRY_MINUTES),
|
||||
"attempts": 0,
|
||||
"send_times": recent + [now],
|
||||
"locale": locale,
|
||||
"ui_locale": ui_locale,
|
||||
"venue": venue,
|
||||
"ip_address": ip_address,
|
||||
"user_agent": user_agent,
|
||||
},
|
||||
merge=True,
|
||||
)
|
||||
has_eval = bool((record or {}).get("credit_eval_decision"))
|
||||
return IssuedCode(
|
||||
code=code,
|
||||
doc_id=code_doc_id(email),
|
||||
needs_credit_eval=needs_credit_eval and not has_eval,
|
||||
)
|
||||
|
||||
return self._run_transaction(_txn)
|
||||
return self._codes.issue_code(
|
||||
email,
|
||||
max_per_hour=max_per_hour,
|
||||
needs_credit_eval=needs_credit_eval,
|
||||
fields={
|
||||
"locale": locale,
|
||||
"ui_locale": ui_locale,
|
||||
"venue": venue,
|
||||
"ip_address": ip_address,
|
||||
"user_agent": user_agent,
|
||||
},
|
||||
)
|
||||
|
||||
def verify_code(self, email: str, code: str) -> tuple[str, Optional[dict]]:
|
||||
"""Check ``code`` for ``email``. Returns (outcome, code_record).
|
||||
|
||||
Attempts are counted with an atomic server-side increment BEFORE the
|
||||
comparison, so parallel guesses can't exceed MAX_VERIFY_ATTEMPTS. A
|
||||
success is claimed via a create()-only marker so a code works once.
|
||||
"""
|
||||
email = email.strip().lower()
|
||||
ref = self._code_ref(email)
|
||||
snap = ref.get()
|
||||
record = snap.to_dict() if snap.exists else None
|
||||
if not record or not record.get("code_hash") or not record.get("code_id"):
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
|
||||
expires_at = _aware(record.get("expires_at"))
|
||||
if not expires_at or _now() > expires_at:
|
||||
ref.update({"code_hash": None})
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
|
||||
ref.update({"attempts": Increment(1)})
|
||||
after = ref.get().to_dict() or {}
|
||||
if after.get("code_id") != record["code_id"] or not after.get("code_hash"):
|
||||
# Superseded or burned between our read and the increment.
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
if int(after.get("attempts") or 0) > MAX_VERIFY_ATTEMPTS:
|
||||
ref.update({"code_hash": None})
|
||||
return VerifyOutcome.TOO_MANY_ATTEMPTS, None
|
||||
|
||||
candidate = (code or "").strip()
|
||||
expected = record["code_hash"]
|
||||
provided = self._hash_code(record["code_id"], candidate)
|
||||
if not (candidate.isdigit() and len(candidate) == CODE_LENGTH
|
||||
and hmac.compare_digest(provided, expected)):
|
||||
return VerifyOutcome.INVALID, None
|
||||
|
||||
try:
|
||||
self.db.collection(LOGIN_CODE_USES_COLLECTION).document(record["code_id"]).create(
|
||||
{"email": email, "used_at": _now()}
|
||||
)
|
||||
except google_exceptions.AlreadyExists:
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
|
||||
ref.update({"code_hash": None, "used_at": _now()})
|
||||
return VerifyOutcome.OK, after
|
||||
"""Check ``code`` for ``email``. Returns (outcome, code_record)."""
|
||||
return self._codes.verify_code(email, code)
|
||||
|
||||
# ----------------------------------------------------------- signup cap
|
||||
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
"""Reusable emailed sign-in code store (6-digit codes, HMAC-hashed at rest).
|
||||
|
||||
Shared by gen's own email-code sign-in (``/api/users/auth/send-code`` +
|
||||
``verify-code``, collections ``login_codes`` / ``login_code_uses``) and the kjbox
|
||||
partner API (``KjboxPartnerService``, collections ``kjbox_login_codes`` /
|
||||
``kjbox_login_code_uses``). Each caller picks its own collections, so the two
|
||||
flows never share codes or throttle state.
|
||||
|
||||
Per collection pair:
|
||||
- ``<codes_collection>`` one doc per email (id = sha256(email)); holds only an
|
||||
HMAC of the current code, its expiry + attempt counter,
|
||||
the recent send timestamps (per-email throttle), caller
|
||||
metadata (locale, IP, ...) and the precomputed
|
||||
welcome-credit evaluation.
|
||||
- ``<uses_collection>`` create()-only markers keyed by code_id — makes a code
|
||||
strictly single-use even under concurrent verifies.
|
||||
- ``<ip_collection>`` optional; one doc per sha256(client IP) with recent send
|
||||
timestamps (per-IP throttle). Only touched when the
|
||||
caller passes ``throttle_ip``.
|
||||
|
||||
Throttle decisions run inside ONE Firestore transaction together with the code
|
||||
write, so concurrent sends can't slip past either limit.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
import secrets
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from google.api_core import exceptions as google_exceptions
|
||||
from google.cloud import firestore
|
||||
from google.cloud.firestore_v1 import Increment
|
||||
|
||||
CODE_LENGTH = 6
|
||||
CODE_EXPIRY_MINUTES = 10
|
||||
MAX_VERIFY_ATTEMPTS = 5
|
||||
TRANSACTION_MAX_ATTEMPTS = 10
|
||||
|
||||
THROTTLE_WINDOW = timedelta(hours=1)
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def _aware(dt: Any) -> Optional[datetime]:
|
||||
"""Normalize Firestore/ISO timestamps to tz-aware UTC datetimes."""
|
||||
if dt is None:
|
||||
return None
|
||||
if isinstance(dt, str):
|
||||
try:
|
||||
dt = datetime.fromisoformat(dt)
|
||||
except ValueError:
|
||||
return None
|
||||
if not isinstance(dt, datetime):
|
||||
return None
|
||||
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def _sha256(value: str) -> str:
|
||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def code_doc_id(email: str) -> str:
|
||||
return _sha256(email.strip().lower())
|
||||
|
||||
|
||||
def recent_times(values, now: Optional[datetime] = None, window: timedelta = THROTTLE_WINDOW) -> list:
|
||||
"""Timestamps from ``values`` that fall inside the rolling ``window``."""
|
||||
now = now or _now()
|
||||
times = [_aware(t) for t in values or []]
|
||||
return [t for t in times if t and now - t < window]
|
||||
|
||||
|
||||
@dataclass
|
||||
class IssuedCode:
|
||||
code: str
|
||||
doc_id: str
|
||||
needs_credit_eval: bool
|
||||
|
||||
|
||||
class TransactionContention(Exception):
|
||||
"""A Firestore transaction gave up after repeated contention (nothing written)."""
|
||||
|
||||
|
||||
class CodeThrottled(Exception):
|
||||
"""Per-email (or per-IP) code throttle hit (raised out of the issue transaction)."""
|
||||
|
||||
|
||||
class VerifyOutcome:
|
||||
OK = "ok"
|
||||
EXPIRED = "expired"
|
||||
INVALID = "invalid_code"
|
||||
TOO_MANY_ATTEMPTS = "too_many_attempts"
|
||||
|
||||
|
||||
def run_firestore_transaction(db, fn: Callable):
|
||||
"""Run ``fn(transaction)`` under @firestore.transactional (retried on contention).
|
||||
|
||||
google-cloud-firestore gives up with ValueError("Failed to commit transaction
|
||||
in N attempts.") under heavy contention. Nothing was written, so it's mapped
|
||||
to :class:`TransactionContention` for callers to treat as "busy, retry".
|
||||
"""
|
||||
try:
|
||||
return firestore.transactional(fn)(db.transaction(max_attempts=TRANSACTION_MAX_ATTEMPTS))
|
||||
except ValueError as exc:
|
||||
if "Failed to commit transaction" in str(exc):
|
||||
raise TransactionContention() from exc
|
||||
raise
|
||||
|
||||
|
||||
class LoginCodeStore:
|
||||
"""Issue + verify emailed 6-digit sign-in codes in a given pair of collections."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
db,
|
||||
pepper: str,
|
||||
codes_collection: str,
|
||||
uses_collection: str,
|
||||
transaction_runner: Optional[Callable] = None,
|
||||
ip_collection: Optional[str] = None,
|
||||
):
|
||||
self.db = db
|
||||
# A server-side secret pepper: a Firestore read alone (backups, exports)
|
||||
# can't brute-force the 10^6 code space offline.
|
||||
self._pepper = pepper.encode("utf-8")
|
||||
self.codes_collection = codes_collection
|
||||
self.uses_collection = uses_collection
|
||||
self.ip_collection = ip_collection
|
||||
# Injectable for unit tests (in-memory Firestore fake) and so callers
|
||||
# (KjboxPartnerService) can route through their own runner.
|
||||
self._transaction_runner = transaction_runner
|
||||
|
||||
def _run_transaction(self, fn):
|
||||
if self._transaction_runner is not None:
|
||||
return self._transaction_runner(fn)
|
||||
return run_firestore_transaction(self.db, fn)
|
||||
|
||||
def hash_code(self, code_id: str, code: str) -> str:
|
||||
return hmac.new(self._pepper, f"{code_id}:{code}".encode("utf-8"), hashlib.sha256).hexdigest()
|
||||
|
||||
def code_ref(self, email: str):
|
||||
return self.db.collection(self.codes_collection).document(code_doc_id(email))
|
||||
|
||||
def _ip_ref(self, ip_address: str):
|
||||
return self.db.collection(self.ip_collection).document(_sha256(ip_address))
|
||||
|
||||
def get_code_record(self, email: str) -> Optional[dict]:
|
||||
snap = self.code_ref(email).get()
|
||||
return snap.to_dict() if snap.exists else None
|
||||
|
||||
def recent_send_times(self, record: Optional[dict], now: Optional[datetime] = None) -> list:
|
||||
return recent_times((record or {}).get("send_times"), now)
|
||||
|
||||
def issue_code(
|
||||
self,
|
||||
email: str,
|
||||
*,
|
||||
max_per_hour: int,
|
||||
needs_credit_eval: bool,
|
||||
fields: Optional[dict] = None,
|
||||
throttle_ip: Optional[str] = None,
|
||||
max_per_ip_per_hour: Optional[int] = None,
|
||||
) -> IssuedCode:
|
||||
"""Mint a fresh code for ``email``, replacing (invalidating) any prior one.
|
||||
|
||||
The throttle checks, the send_times appends and the code write happen in
|
||||
ONE transaction, so parallel requests can't exceed ``max_per_hour`` sends
|
||||
per email (nor ``max_per_ip_per_hour`` per ``throttle_ip`` when given) —
|
||||
raises :class:`CodeThrottled`. A throttled request records nothing.
|
||||
Only an HMAC of the code is stored. ``merge=True`` keeps any precomputed
|
||||
welcome-credit evaluation across re-sends. ``fields`` (locale, IP, venue,
|
||||
tenant, ...) are stored on the record and returned by verify_code.
|
||||
"""
|
||||
email = email.strip().lower()
|
||||
ref = self.code_ref(email)
|
||||
ip_ref = None
|
||||
if throttle_ip and max_per_ip_per_hour is not None:
|
||||
if not self.ip_collection:
|
||||
raise ValueError("throttle_ip requires ip_collection")
|
||||
ip_ref = self._ip_ref(throttle_ip)
|
||||
|
||||
def _txn(transaction):
|
||||
now = _now()
|
||||
# All reads before any write (Firestore transaction rule).
|
||||
snap = ref.get(transaction=transaction)
|
||||
ip_snap = ip_ref.get(transaction=transaction) if ip_ref is not None else None
|
||||
record = snap.to_dict() if snap.exists else None
|
||||
recent = self.recent_send_times(record, now)
|
||||
if len(recent) >= max_per_hour:
|
||||
raise CodeThrottled()
|
||||
ip_recent = None
|
||||
if ip_snap is not None:
|
||||
ip_recent = recent_times((ip_snap.to_dict() or {}).get("send_times") if ip_snap.exists else None, now)
|
||||
if len(ip_recent) >= max_per_ip_per_hour:
|
||||
raise CodeThrottled()
|
||||
|
||||
code = f"{secrets.randbelow(10 ** CODE_LENGTH):0{CODE_LENGTH}d}"
|
||||
code_id = secrets.token_urlsafe(16)
|
||||
transaction.set(
|
||||
ref,
|
||||
{
|
||||
**(fields or {}),
|
||||
"email": email,
|
||||
"code_id": code_id,
|
||||
"code_hash": self.hash_code(code_id, code),
|
||||
"created_at": now,
|
||||
"expires_at": now + timedelta(minutes=CODE_EXPIRY_MINUTES),
|
||||
"attempts": 0,
|
||||
"send_times": recent + [now],
|
||||
},
|
||||
merge=True,
|
||||
)
|
||||
if ip_ref is not None:
|
||||
# Only the IP hash is the doc id; the raw IP isn't stored here.
|
||||
transaction.set(ip_ref, {"send_times": ip_recent + [now], "updated_at": now})
|
||||
has_eval = bool((record or {}).get("credit_eval_decision"))
|
||||
return IssuedCode(
|
||||
code=code,
|
||||
doc_id=code_doc_id(email),
|
||||
needs_credit_eval=needs_credit_eval and not has_eval,
|
||||
)
|
||||
|
||||
return self._run_transaction(_txn)
|
||||
|
||||
def verify_code(self, email: str, code: str) -> tuple[str, Optional[dict]]:
|
||||
"""Check ``code`` for ``email``. Returns (outcome, code_record).
|
||||
|
||||
Attempts are counted with an atomic server-side increment BEFORE the
|
||||
comparison, so parallel guesses can't exceed MAX_VERIFY_ATTEMPTS. A
|
||||
success is claimed via a create()-only marker so a code works once.
|
||||
"""
|
||||
email = email.strip().lower()
|
||||
ref = self.code_ref(email)
|
||||
snap = ref.get()
|
||||
record = snap.to_dict() if snap.exists else None
|
||||
if not record or not record.get("code_hash") or not record.get("code_id"):
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
|
||||
expires_at = _aware(record.get("expires_at"))
|
||||
if not expires_at or _now() > expires_at:
|
||||
ref.update({"code_hash": None})
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
|
||||
ref.update({"attempts": Increment(1)})
|
||||
after = ref.get().to_dict() or {}
|
||||
if after.get("code_id") != record["code_id"] or not after.get("code_hash"):
|
||||
# Superseded or burned between our read and the increment.
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
if int(after.get("attempts") or 0) > MAX_VERIFY_ATTEMPTS:
|
||||
ref.update({"code_hash": None})
|
||||
return VerifyOutcome.TOO_MANY_ATTEMPTS, None
|
||||
|
||||
candidate = (code or "").strip()
|
||||
expected = record["code_hash"]
|
||||
provided = self.hash_code(record["code_id"], candidate)
|
||||
if not (candidate.isdigit() and len(candidate) == CODE_LENGTH
|
||||
and hmac.compare_digest(provided, expected)):
|
||||
return VerifyOutcome.INVALID, None
|
||||
|
||||
try:
|
||||
self.db.collection(self.uses_collection).document(record["code_id"]).create(
|
||||
{"email": email, "used_at": _now()}
|
||||
)
|
||||
except google_exceptions.AlreadyExists:
|
||||
return VerifyOutcome.EXPIRED, None
|
||||
|
||||
ref.update({"code_hash": None, "used_at": _now()})
|
||||
return VerifyOutcome.OK, after
|
||||
@@ -14,6 +14,7 @@ from typing import Optional, Dict, Any, Tuple
|
||||
import stripe
|
||||
|
||||
from backend.config import get_settings
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -102,6 +103,18 @@ class StripeService:
|
||||
"""Get available credit packages."""
|
||||
return CREDIT_PACKAGES
|
||||
|
||||
def can_checkout(self) -> bool:
|
||||
"""Checkout sessions can be created: real Stripe, or the local stack's fake checkout."""
|
||||
return self.is_configured() or fake_workers_enabled()
|
||||
|
||||
def _create_checkout(self, session_params: Dict[str, Any]):
|
||||
"""Create the Stripe Checkout session, or a local fake one on the local stack."""
|
||||
if not self.is_configured() and fake_workers_enabled():
|
||||
from backend.services.fake_workers.checkout import create_fake_checkout
|
||||
|
||||
return create_fake_checkout(session_params)
|
||||
return stripe.checkout.Session.create(**session_params)
|
||||
|
||||
def create_checkout_session(
|
||||
self,
|
||||
package_id: str,
|
||||
@@ -122,7 +135,7 @@ class StripeService:
|
||||
Returns:
|
||||
(success, checkout_url, message)
|
||||
"""
|
||||
if not self.is_configured():
|
||||
if not self.can_checkout():
|
||||
return False, None, "Payment processing is not configured"
|
||||
|
||||
package = CREDIT_PACKAGES.get(package_id)
|
||||
@@ -176,7 +189,7 @@ class StripeService:
|
||||
session_params['payment_method_configuration'] = self.payment_method_config
|
||||
|
||||
# Create checkout session
|
||||
session = stripe.checkout.Session.create(**session_params)
|
||||
session = self._create_checkout(session_params)
|
||||
|
||||
logger.info(f"Created checkout session {session.id} for {user_email}, package {package_id}")
|
||||
return True, session.url, "Checkout session created"
|
||||
@@ -219,7 +232,7 @@ class StripeService:
|
||||
Returns:
|
||||
(success, checkout_url, message)
|
||||
"""
|
||||
if not self.is_configured():
|
||||
if not self.can_checkout():
|
||||
return False, None, "Payment processing is not configured"
|
||||
|
||||
try:
|
||||
@@ -275,7 +288,7 @@ class StripeService:
|
||||
session_params['payment_method_configuration'] = self.payment_method_config
|
||||
|
||||
# Create checkout session
|
||||
session = stripe.checkout.Session.create(**session_params)
|
||||
session = self._create_checkout(session_params)
|
||||
|
||||
logger.info(
|
||||
f"Created made-for-you checkout session {session.id} for {customer_email}, "
|
||||
|
||||
@@ -586,7 +586,9 @@ class UserService:
|
||||
# =========================================================================
|
||||
|
||||
# Maximum new signups allowed per IP address in the rate limit window
|
||||
MAX_SIGNUPS_PER_IP = 2
|
||||
# (Env-overridable only so the local E2E stack, where every browser signs up
|
||||
# from 127.0.0.1, can raise it; production never sets it.)
|
||||
MAX_SIGNUPS_PER_IP = _safe_positive_int_env("MAX_SIGNUPS_PER_IP", 2)
|
||||
SIGNUP_RATE_LIMIT_HOURS = 24
|
||||
# Higher cap for requests-board sign-ins: a shared venue WiFi can register many
|
||||
# voters, but we still keep a deterministic ceiling against scripted abuse (each
|
||||
|
||||
@@ -25,6 +25,7 @@ from google.api_core import exceptions as gcp_exceptions
|
||||
from google.protobuf import duration_pb2
|
||||
|
||||
from backend.config import get_settings, is_production
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
from backend.services.tracing import inject_trace_context
|
||||
|
||||
|
||||
@@ -379,6 +380,12 @@ class WorkerService:
|
||||
)
|
||||
return False
|
||||
|
||||
def _dispatch_fake(self, kind: str, job_id: str) -> bool:
|
||||
"""FAKE_WORKERS (local stack): run the in-process fake instead of the real worker."""
|
||||
from backend.services.fake_workers import dispatch
|
||||
|
||||
return dispatch(kind, job_id)
|
||||
|
||||
# Convenience methods for specific workers
|
||||
# These provide a cleaner API and better IDE autocomplete
|
||||
|
||||
@@ -396,6 +403,8 @@ class WorkerService:
|
||||
The execution timeout is overridden to fit the torrent stall budget
|
||||
(longer for a user's "Keep trying" retry) — see audio_download_limits.
|
||||
"""
|
||||
if fake_workers_enabled(self.settings):
|
||||
return self._dispatch_fake("audio_download", job_id)
|
||||
from backend.services.audio_download_limits import task_timeout_seconds
|
||||
|
||||
return await self._trigger_worker_cloud_run_job(
|
||||
@@ -511,6 +520,8 @@ class WorkerService:
|
||||
|
||||
Uses Cloud Run Jobs with L4 GPU in us-east4 for direct audio separation.
|
||||
"""
|
||||
if fake_workers_enabled(self.settings):
|
||||
return self._dispatch_fake("audio", job_id)
|
||||
return await self._trigger_worker_cloud_run_job(
|
||||
job_id=job_id,
|
||||
cloud_run_job_name="audio-separation-job",
|
||||
@@ -527,6 +538,8 @@ class WorkerService:
|
||||
terminate instances mid-processing (e.g., job c94cc9d6) when no HTTP requests
|
||||
were active, even though the worker was still processing.
|
||||
"""
|
||||
if fake_workers_enabled(self.settings):
|
||||
return self._dispatch_fake("lyrics", job_id)
|
||||
return await self._trigger_worker_cloud_run_job(
|
||||
job_id=job_id,
|
||||
cloud_run_job_name="lyrics-transcription-job",
|
||||
@@ -536,6 +549,8 @@ class WorkerService:
|
||||
|
||||
async def trigger_screens_worker(self, job_id: str) -> bool:
|
||||
"""Trigger screen generation worker."""
|
||||
if fake_workers_enabled(self.settings):
|
||||
return self._dispatch_fake("screens", job_id)
|
||||
return await self.trigger_worker("screens", job_id)
|
||||
|
||||
async def trigger_auto_correct(self, job_id: str, timeout_seconds: int = 200) -> bool:
|
||||
@@ -625,6 +640,8 @@ class WorkerService:
|
||||
uses Cloud Run Jobs for execution (supports >30 min encoding).
|
||||
Otherwise, uses Cloud Tasks or direct HTTP.
|
||||
"""
|
||||
if fake_workers_enabled(self.settings):
|
||||
return self._dispatch_fake("video", job_id)
|
||||
await asyncio.to_thread(self._bump_worker_generation, job_id) # Firestore write
|
||||
self._start_encoding_worker_warmup(job_id)
|
||||
if self._use_cloud_tasks and self.settings.use_cloud_run_jobs_for_video:
|
||||
@@ -796,6 +813,8 @@ class WorkerService:
|
||||
rendering_video (incident 2026-09-13, job 41e06b90 — same class as
|
||||
incident 2026-03-08 which migrated the video worker to Cloud Run Jobs).
|
||||
"""
|
||||
if fake_workers_enabled(self.settings):
|
||||
return self._dispatch_fake("render_video", job_id)
|
||||
await asyncio.to_thread(self._bump_worker_generation, job_id) # Firestore write
|
||||
self._start_encoding_worker_warmup(job_id)
|
||||
if self._use_cloud_tasks and self.settings.use_cloud_run_jobs_for_render:
|
||||
|
||||
@@ -387,12 +387,12 @@ class TestConfirmDurationIdempotency:
|
||||
"""A second confirm POST must 409 because the job is no longer AWAITING_DURATION_CONFIRM.
|
||||
|
||||
After a successful confirm the endpoint calls transition_to_state →
|
||||
SEPARATING_STAGE1. The mock default returns True from transition_to_state;
|
||||
DOWNLOADING. The mock default returns True from transition_to_state;
|
||||
what matters is that the endpoint *issued* the transition so that a
|
||||
subsequent request (now seeing a different status) would be rejected.
|
||||
|
||||
We verify this by asserting that transition_to_state was called with
|
||||
new_status=SEPARATING_STAGE1 on the first confirm — the status guard at
|
||||
new_status=DOWNLOADING on the first confirm — the status guard at
|
||||
step 2 would reject a second request once the real Firestore document
|
||||
reflects the new status.
|
||||
"""
|
||||
@@ -408,8 +408,8 @@ class TestConfirmDurationIdempotency:
|
||||
# so a duplicate confirm would hit the status guard (409).
|
||||
jm.transition_to_state.assert_called_once()
|
||||
call_kwargs = jm.transition_to_state.call_args[1]
|
||||
assert call_kwargs.get("new_status") == JobStatus.SEPARATING_STAGE1, (
|
||||
"confirm_duration must transition to SEPARATING_STAGE1 before triggering "
|
||||
assert call_kwargs.get("new_status") == JobStatus.DOWNLOADING, (
|
||||
"confirm_duration must transition to DOWNLOADING before triggering "
|
||||
"workers so a duplicate POST is rejected by the status guard"
|
||||
)
|
||||
|
||||
@@ -441,7 +441,7 @@ class TestConfirmDurationIdempotency:
|
||||
# Job must still be transitioned out of the pause state
|
||||
jm.transition_to_state.assert_called_once()
|
||||
call_kwargs = jm.transition_to_state.call_args[1]
|
||||
assert call_kwargs.get("new_status") == JobStatus.SEPARATING_STAGE1
|
||||
assert call_kwargs.get("new_status") == JobStatus.DOWNLOADING
|
||||
|
||||
|
||||
class TestConfirmDurationErrorCases:
|
||||
|
||||
@@ -88,8 +88,11 @@ class TestCodes:
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
results = list(pool.map(attempt, range(12)))
|
||||
# Never more than the cap, and every success is recorded exactly once
|
||||
assert 1 <= sum(results) <= 5
|
||||
# Never more than the cap, and every success is recorded exactly once.
|
||||
# (Zero successes is allowed: on a slow CI runner all 12 contended
|
||||
# transactions can exhaust their retries against the emulator; the
|
||||
# sequential top-up below still proves the throttle.)
|
||||
assert 0 <= sum(results) <= 5
|
||||
assert len(svc.get_code_record(email)["send_times"]) == sum(results)
|
||||
# Sequential top-up reaches exactly the cap, then throttles
|
||||
while len(svc.get_code_record(email)["send_times"]) < 5:
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
"""
|
||||
Emulator integration tests for LoginCodeStore (gen email-code sign-in).
|
||||
|
||||
Runs the store against a REAL Firestore emulator with gen's own collections so
|
||||
the primitives behind the security properties are verified for real: the
|
||||
atomic Increment attempt counter, the create()-only single-use marker, and the
|
||||
per-email + per-IP throttles held inside one real transaction under
|
||||
concurrency. Run with: scripts/run-emulator-tests.sh
|
||||
"""
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import pytest
|
||||
from google.cloud import firestore
|
||||
|
||||
from backend.api.routes.login_code import (
|
||||
LOGIN_CODE_IP_COLLECTION,
|
||||
LOGIN_CODE_USES_COLLECTION,
|
||||
LOGIN_CODES_COLLECTION,
|
||||
)
|
||||
from backend.services.login_code_store import (
|
||||
MAX_VERIFY_ATTEMPTS,
|
||||
CodeThrottled,
|
||||
LoginCodeStore,
|
||||
TransactionContention,
|
||||
VerifyOutcome,
|
||||
)
|
||||
|
||||
from .conftest import emulators_running
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not emulators_running(),
|
||||
reason="GCP emulators not running. Start with: scripts/start-emulators.sh",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store():
|
||||
return LoginCodeStore(
|
||||
firestore.Client(), "emulator-pepper",
|
||||
LOGIN_CODES_COLLECTION, LOGIN_CODE_USES_COLLECTION, ip_collection=LOGIN_CODE_IP_COLLECTION,
|
||||
)
|
||||
|
||||
|
||||
def _email():
|
||||
return f"lc-{uuid.uuid4().hex[:8]}@example.com"
|
||||
|
||||
|
||||
def _ip():
|
||||
return f"ip-{uuid.uuid4().hex}" # only ever hashed into a doc id
|
||||
|
||||
|
||||
def _wrong(code):
|
||||
return f"{(int(code) + 1) % 1_000_000:06d}"
|
||||
|
||||
|
||||
def test_round_trip_single_use_and_attempt_cap(store):
|
||||
email = _email()
|
||||
issued = store.issue_code(email, max_per_hour=5, needs_credit_eval=False, fields={"locale": "es"})
|
||||
assert store.verify_code(email, _wrong(issued.code))[0] == VerifyOutcome.INVALID
|
||||
outcome, record = store.verify_code(email, issued.code)
|
||||
assert outcome == VerifyOutcome.OK and record["locale"] == "es"
|
||||
assert store.verify_code(email, issued.code)[0] == VerifyOutcome.EXPIRED
|
||||
|
||||
issued = store.issue_code(email, max_per_hour=5, needs_credit_eval=False)
|
||||
for _ in range(MAX_VERIFY_ATTEMPTS):
|
||||
assert store.verify_code(email, _wrong(issued.code))[0] == VerifyOutcome.INVALID
|
||||
assert store.verify_code(email, issued.code)[0] == VerifyOutcome.TOO_MANY_ATTEMPTS
|
||||
|
||||
|
||||
def test_per_ip_throttle_holds_under_concurrency(store):
|
||||
"""Per-IP check + append + code write are one transaction, across emails."""
|
||||
ip = _ip()
|
||||
|
||||
def attempt(_):
|
||||
try:
|
||||
store.issue_code(_email(), max_per_hour=5, needs_credit_eval=False,
|
||||
throttle_ip=ip, max_per_ip_per_hour=4)
|
||||
return True
|
||||
except (CodeThrottled, TransactionContention):
|
||||
return False
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
results = list(pool.map(attempt, range(10)))
|
||||
assert 1 <= sum(results) <= 4
|
||||
# Sequential top-up reaches exactly the cap, then throttles
|
||||
for _ in range(4 - sum(results)):
|
||||
store.issue_code(_email(), max_per_hour=5, needs_credit_eval=False,
|
||||
throttle_ip=ip, max_per_ip_per_hour=4)
|
||||
with pytest.raises(CodeThrottled):
|
||||
store.issue_code(_email(), max_per_hour=5, needs_credit_eval=False,
|
||||
throttle_ip=ip, max_per_ip_per_hour=4)
|
||||
@@ -275,7 +275,7 @@ class TestReconcileUpEndToEnd:
|
||||
# "Resumed" job: what get_job returns AFTER confirm (post-transition)
|
||||
resumed_job = _make_job(
|
||||
job_id=job_id,
|
||||
status=JobStatus.SEPARATING_STAGE1,
|
||||
status=JobStatus.DOWNLOADING,
|
||||
credits_charged=4,
|
||||
user_email=user_email,
|
||||
extra_state_data={
|
||||
@@ -448,10 +448,10 @@ class TestReconcileUpEndToEnd:
|
||||
"Expected pending_additional_credits=0 after confirm"
|
||||
)
|
||||
|
||||
def test_confirm_duration_transitions_to_separating_stage1(self, test_setup, mock_creds):
|
||||
def test_confirm_duration_transitions_to_downloading(self, test_setup, mock_creds):
|
||||
"""
|
||||
The job must be transitioned OUT of AWAITING_DURATION_CONFIRM to
|
||||
SEPARATING_STAGE1 before workers are triggered.
|
||||
DOWNLOADING before workers are triggered.
|
||||
"""
|
||||
setup = test_setup
|
||||
patches = [
|
||||
@@ -478,8 +478,8 @@ class TestReconcileUpEndToEnd:
|
||||
if (call_args.args and len(call_args.args) >= 2)
|
||||
else call_args.kwargs.get("new_status")
|
||||
)
|
||||
assert new_status == JobStatus.SEPARATING_STAGE1, (
|
||||
f"Expected transition to SEPARATING_STAGE1, got {new_status}"
|
||||
assert new_status == JobStatus.DOWNLOADING, (
|
||||
f"Expected transition to DOWNLOADING, got {new_status}"
|
||||
)
|
||||
|
||||
def test_confirm_duration_wrong_acknowledged_credits_returns_409(self, test_setup, mock_creds):
|
||||
|
||||
@@ -0,0 +1,401 @@
|
||||
"""Unit tests for the local-stack fake workers (backend/services/fake_workers)
|
||||
and the FAKE_WORKERS seams in real services.
|
||||
|
||||
The end-to-end behaviour (a job walking PENDING -> COMPLETE on the emulators)
|
||||
is covered by frontend/e2e/local-stack and backend/tests/emulator/test_fake_workers_pipeline.py.
|
||||
"""
|
||||
import asyncio
|
||||
import shutil
|
||||
import subprocess
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.services import fake_workers
|
||||
from backend.services.fake_workers import config as fw_config
|
||||
from backend.services.fake_workers.config import FakeConfig, get_fake_config, set_fake_config
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- in-memory Firestore
|
||||
|
||||
class _Snap:
|
||||
def __init__(self, ref, data):
|
||||
self.reference, self.id, self._data = ref, ref.id, data
|
||||
|
||||
@property
|
||||
def exists(self):
|
||||
return self._data is not None
|
||||
|
||||
def to_dict(self):
|
||||
return dict(self._data) if self._data is not None else None
|
||||
|
||||
|
||||
class _Doc:
|
||||
def __init__(self, store, coll, doc_id):
|
||||
self.store, self.coll, self.id = store, coll, doc_id
|
||||
|
||||
def get(self):
|
||||
return _Snap(self, self.store.get(self.coll, {}).get(self.id))
|
||||
|
||||
def set(self, data):
|
||||
self.store.setdefault(self.coll, {})[self.id] = dict(data)
|
||||
|
||||
def update(self, data):
|
||||
self.store.setdefault(self.coll, {}).setdefault(self.id, {}).update(data)
|
||||
|
||||
def delete(self):
|
||||
self.store.get(self.coll, {}).pop(self.id, None)
|
||||
|
||||
|
||||
class _Coll:
|
||||
def __init__(self, store, name):
|
||||
self.store, self.name = store, name
|
||||
|
||||
def document(self, doc_id):
|
||||
return _Doc(self.store, self.name, doc_id)
|
||||
|
||||
def add(self, data):
|
||||
doc_id = f"auto{len(self.store.get(self.name, {}))}"
|
||||
self.document(doc_id).set(data)
|
||||
|
||||
def stream(self):
|
||||
return [_Doc(self.store, self.name, k).get() for k in list(self.store.get(self.name, {}))]
|
||||
|
||||
|
||||
class FakeDB:
|
||||
def __init__(self):
|
||||
self.store = {}
|
||||
|
||||
def collection(self, name):
|
||||
return _Coll(self.store, name)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- the on/off switch
|
||||
|
||||
class TestFakeWorkersEnabled:
|
||||
def test_true_only_for_real_true(self):
|
||||
assert fake_workers.fake_workers_enabled(SimpleNamespace(fake_workers=True)) is True
|
||||
assert fake_workers.fake_workers_enabled(SimpleNamespace(fake_workers=False)) is False
|
||||
assert fake_workers.fake_workers_enabled(SimpleNamespace()) is False
|
||||
|
||||
def test_magicmock_settings_are_off(self):
|
||||
# Many tests patch settings with a MagicMock; its truthy attributes must
|
||||
# never turn the fakes on.
|
||||
assert fake_workers.fake_workers_enabled(MagicMock()) is False
|
||||
|
||||
def test_off_by_default_in_tests(self):
|
||||
assert fake_workers.fake_workers_enabled() is False
|
||||
|
||||
|
||||
class TestProductionSafety:
|
||||
def test_startup_refuses_fake_workers_in_production(self, monkeypatch):
|
||||
from backend import config
|
||||
|
||||
monkeypatch.setenv("ENVIRONMENT", "production")
|
||||
for var in config._REQUIRED_PRODUCTION_ENV_VARS:
|
||||
monkeypatch.setenv(var, "set")
|
||||
monkeypatch.setattr(config.settings, "fake_workers", True)
|
||||
with pytest.raises(RuntimeError, match="FAKE_WORKERS"):
|
||||
config.validate_production_config()
|
||||
|
||||
def test_production_without_fake_workers_still_starts(self, monkeypatch):
|
||||
from backend import config
|
||||
|
||||
monkeypatch.setenv("ENVIRONMENT", "production")
|
||||
for var in config._REQUIRED_PRODUCTION_ENV_VARS:
|
||||
monkeypatch.setenv(var, "set")
|
||||
monkeypatch.setattr(config.settings, "fake_workers", False)
|
||||
config.validate_production_config()
|
||||
|
||||
def test_get_secret_never_calls_secret_manager_in_fake_mode(self, monkeypatch):
|
||||
from backend.config import Settings
|
||||
|
||||
s = Settings()
|
||||
monkeypatch.setattr(s, "fake_workers", True)
|
||||
monkeypatch.delenv("SOME_LOCAL_SECRET", raising=False)
|
||||
with patch("google.cloud.secretmanager.SecretManagerServiceClient") as client:
|
||||
assert s.get_secret("some-local-secret") is None
|
||||
client.assert_not_called()
|
||||
|
||||
def test_get_secret_env_var_still_wins_in_fake_mode(self, monkeypatch):
|
||||
from backend.config import Settings
|
||||
|
||||
s = Settings()
|
||||
monkeypatch.setattr(s, "fake_workers", True)
|
||||
monkeypatch.setenv("SOME_ENV_SECRET", "from-env")
|
||||
assert s.get_secret("some-env-secret") == "from-env"
|
||||
|
||||
def test_test_router_not_mounted_without_flag(self):
|
||||
from backend.main import app
|
||||
|
||||
assert not [r for r in app.routes if getattr(r, "path", "").startswith("/api/test")]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- dispatch seam
|
||||
|
||||
class TestWorkerServiceSeam:
|
||||
KINDS = {
|
||||
"trigger_audio_download_worker": "audio_download",
|
||||
"trigger_audio_worker": "audio",
|
||||
"trigger_lyrics_worker": "lyrics",
|
||||
"trigger_screens_worker": "screens",
|
||||
"trigger_render_video_worker": "render_video",
|
||||
"trigger_video_worker": "video",
|
||||
}
|
||||
|
||||
@pytest.mark.parametrize("method,kind", KINDS.items())
|
||||
def test_every_trigger_goes_to_the_fake_when_enabled(self, method, kind):
|
||||
from backend.services.worker_service import WorkerService
|
||||
|
||||
ws = WorkerService()
|
||||
ws.settings = SimpleNamespace(fake_workers=True)
|
||||
with patch("backend.services.fake_workers.dispatch", return_value=True) as dispatch:
|
||||
assert asyncio.run(getattr(ws, method)("job123")) is True
|
||||
dispatch.assert_called_once_with(kind, "job123")
|
||||
|
||||
def test_trigger_does_not_fake_when_disabled(self):
|
||||
from backend.services.worker_service import WorkerService
|
||||
|
||||
ws = WorkerService()
|
||||
assert not fake_workers.fake_workers_enabled(ws.settings)
|
||||
with patch("backend.services.fake_workers.dispatch") as dispatch, \
|
||||
patch.object(ws, "trigger_worker", AsyncMock(return_value=True)):
|
||||
asyncio.run(ws.trigger_screens_worker("job123"))
|
||||
dispatch.assert_not_called()
|
||||
|
||||
def test_dispatch_rejects_unknown_kind(self):
|
||||
with pytest.raises(ValueError):
|
||||
fake_workers.dispatch("nope", "job123")
|
||||
|
||||
def test_dispatch_runs_the_runner_in_a_thread(self):
|
||||
done = []
|
||||
|
||||
async def fake_run(kind, job_id):
|
||||
done.append((kind, job_id))
|
||||
return True
|
||||
|
||||
with patch("backend.services.fake_workers.pipeline.run", fake_run):
|
||||
assert fake_workers.dispatch("lyrics", "abc") is True
|
||||
for t in list(fake_workers._threads):
|
||||
t.join(timeout=5)
|
||||
assert done == [("lyrics", "abc")]
|
||||
assert fake_workers.active_worker_count() == 0
|
||||
|
||||
|
||||
class TestPipelineFailure:
|
||||
def test_fail_at_fails_the_job_like_a_crash(self):
|
||||
from backend.services.fake_workers import pipeline
|
||||
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = SimpleNamespace(user_email="u@example.com")
|
||||
with patch.object(pipeline, "JobManager", return_value=jm), \
|
||||
patch.object(pipeline, "get_fake_config",
|
||||
return_value=FakeConfig(step_delay_seconds=0, fail_at="lyrics")):
|
||||
assert asyncio.run(pipeline.run("lyrics", "job1")) is False
|
||||
jm.fail_job.assert_called_once()
|
||||
args, kwargs = jm.fail_job.call_args
|
||||
assert args[0] == "job1"
|
||||
assert "lyrics" in args[1]
|
||||
assert kwargs["error_details"]["stage"] == "lyrics"
|
||||
|
||||
def test_render_skips_when_not_review_complete(self):
|
||||
from backend.models.job import JobStatus
|
||||
from backend.services.fake_workers import pipeline
|
||||
|
||||
jm = MagicMock()
|
||||
jm.get_job.return_value = SimpleNamespace(user_email="u@example.com", status=JobStatus.AWAITING_REVIEW)
|
||||
with patch.object(pipeline, "JobManager", return_value=jm), \
|
||||
patch.object(pipeline, "StorageService"), \
|
||||
patch.object(pipeline, "get_fake_config", return_value=FakeConfig(step_delay_seconds=0)):
|
||||
assert asyncio.run(pipeline.run("render_video", "job1")) is True
|
||||
jm.transition_to_state.assert_not_called()
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- config
|
||||
|
||||
class TestFakeConfig:
|
||||
def test_defaults(self):
|
||||
c = FakeConfig()
|
||||
assert c.auto_approve is False and c.fail_at is None and c.audio_duration_seconds == 30
|
||||
|
||||
@pytest.mark.parametrize("bad", [
|
||||
{"fail_at": "nope"},
|
||||
{"step_delay_seconds": -1},
|
||||
{"audio_duration_seconds": 2},
|
||||
{"search_result_count": 9},
|
||||
])
|
||||
def test_validate_rejects(self, bad):
|
||||
with pytest.raises(ValueError):
|
||||
FakeConfig(**bad).validate()
|
||||
|
||||
def test_per_user_overrides_layer_over_global(self):
|
||||
db = FakeDB()
|
||||
set_fake_config({"step_delay_seconds": 2.0, "auto_approve": True}, db=db)
|
||||
set_fake_config({"auto_approve": False}, db=db, email="A@Example.com")
|
||||
assert get_fake_config(db).auto_approve is True
|
||||
user = get_fake_config(db, email="a@example.com")
|
||||
assert user.auto_approve is False and user.step_delay_seconds == 2.0
|
||||
assert get_fake_config(db, email="other@example.com").auto_approve is True
|
||||
|
||||
def test_unknown_keys_rejected(self):
|
||||
with pytest.raises(ValueError, match="Unknown"):
|
||||
set_fake_config({"bogus": 1}, db=FakeDB())
|
||||
|
||||
def test_invalid_values_not_stored(self):
|
||||
db = FakeDB()
|
||||
with pytest.raises(ValueError):
|
||||
set_fake_config({"fail_at": "nope"}, db=db, email="a@example.com")
|
||||
assert get_fake_config(db, email="a@example.com").fail_at is None
|
||||
|
||||
def test_clear_removes_global_and_user_overrides(self):
|
||||
db = FakeDB()
|
||||
set_fake_config({"auto_approve": True}, db=db)
|
||||
set_fake_config({"fail_at": "audio"}, db=db, email="a@example.com")
|
||||
fw_config.clear_fake_config(db)
|
||||
assert get_fake_config(db, email="a@example.com") == FakeConfig()
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- canned data
|
||||
|
||||
class TestFakeLyrics:
|
||||
def test_corrections_round_trip_through_the_real_schema(self):
|
||||
from karaoke_gen.lyrics_transcriber.types import CorrectionResult
|
||||
from backend.services.fake_workers.lyrics import FAKE_LINES, build_corrections
|
||||
|
||||
data = build_corrections("Artist", "Title", 30)
|
||||
result = CorrectionResult.from_dict(data)
|
||||
assert len(result.corrected_segments) == len(FAKE_LINES)
|
||||
words = [w for s in result.corrected_segments for w in s.words]
|
||||
assert all(0 < w.start_time < w.end_time <= 30 for w in words)
|
||||
assert [w.start_time for w in words] == sorted(w.start_time for w in words)
|
||||
ref = result.reference_lyrics["lrclib"]
|
||||
assert ref.metadata.is_synced and ref.metadata.track_name == "Title"
|
||||
assert [s.text for s in ref.segments] == [s.text for s in result.corrected_segments]
|
||||
|
||||
def test_lrc(self):
|
||||
from backend.services.fake_workers.lyrics import build_corrections, build_lrc
|
||||
|
||||
lrc = build_lrc(build_corrections("A", "T", 30))
|
||||
assert lrc.startswith("[00:02.00]This is a fake song for testing")
|
||||
|
||||
|
||||
class TestFakeSearch:
|
||||
def _search(self, count=3):
|
||||
from backend.services.fake_workers.search import FakeAudioSearchService
|
||||
|
||||
with patch("backend.services.fake_workers.config.get_fake_config",
|
||||
return_value=FakeConfig(search_result_count=count, audio_duration_seconds=42)):
|
||||
return FakeAudioSearchService().search("Artist", "Song")
|
||||
|
||||
def test_results_in_the_shapes_the_ui_ranks(self):
|
||||
results = self._search()
|
||||
assert [r.provider for r in results] == ["RED", "Spotify", "YouTube"]
|
||||
assert [r.index for r in results] == [0, 1, 2]
|
||||
best = results[0].to_dict()
|
||||
assert best["is_lossless"] is True and best["seeders"] >= 50 and best["duration"] == 42
|
||||
|
||||
def test_zero_results_is_no_results(self):
|
||||
from karaoke_gen.audio_fetcher import NoResultsError
|
||||
|
||||
with pytest.raises(NoResultsError):
|
||||
self._search(count=0)
|
||||
|
||||
def test_factory_returns_fake_when_enabled(self, monkeypatch):
|
||||
from backend.services import audio_search_service as svc
|
||||
from backend.services.fake_workers.search import FakeAudioSearchService
|
||||
|
||||
monkeypatch.setattr(svc, "_audio_search_service", None)
|
||||
monkeypatch.setattr(svc, "get_settings", lambda: SimpleNamespace(fake_workers=True))
|
||||
assert isinstance(svc.get_audio_search_service(), FakeAudioSearchService)
|
||||
monkeypatch.setattr(svc, "_audio_search_service", None)
|
||||
|
||||
|
||||
class TestOutbox:
|
||||
def test_codes_and_links_from_html_only_email(self):
|
||||
from backend.services.fake_workers import outbox
|
||||
|
||||
db = FakeDB()
|
||||
html = (
|
||||
"<style>.x{color:#123456;width:100000px}</style>"
|
||||
"<p>Your code is <b>482915</b></p>"
|
||||
'<a href="http://127.0.0.1:3010/en/auth/verify?token=abc&ref=x">Sign in</a>'
|
||||
)
|
||||
with patch.object(outbox, "_db", return_value=db):
|
||||
outbox.record_email("User@Example.com", "Your code", html, None)
|
||||
emails = outbox.list_emails("user@example.com")
|
||||
assert emails[0]["codes"] == ["482915"]
|
||||
assert emails[0]["links"] == ["http://127.0.0.1:3010/en/auth/verify?token=abc&ref=x"]
|
||||
|
||||
def test_filter_and_newest_first(self):
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from backend.services.fake_workers import outbox
|
||||
|
||||
db = FakeDB()
|
||||
now = datetime.now(timezone.utc)
|
||||
with patch.object(outbox, "_db", return_value=db):
|
||||
for i, to in enumerate(["a@x.com", "b@x.com", "a@x.com"]):
|
||||
db.collection(outbox.OUTBOX_COLLECTION).document(f"d{i}").set(
|
||||
{"to": to, "subject": f"s{i}", "created_at": now + timedelta(seconds=i)})
|
||||
assert [e["subject"] for e in outbox.list_emails("a@x.com")] == ["s2", "s0"]
|
||||
|
||||
def test_capture_failure_never_breaks_a_send(self):
|
||||
from backend.services.fake_workers import outbox
|
||||
|
||||
with patch.object(outbox, "_db", side_effect=RuntimeError("emulator down")):
|
||||
outbox.record_email("a@x.com", "s", "<p>hi</p>", None) # no raise
|
||||
|
||||
|
||||
class TestFakeCheckout:
|
||||
def test_session_and_redirects(self, monkeypatch):
|
||||
from backend.services.fake_workers import checkout
|
||||
|
||||
db = FakeDB()
|
||||
monkeypatch.setenv("LOCAL_STACK_API_URL", "http://10.0.2.2:8010/")
|
||||
with patch.object(checkout, "_db", return_value=db):
|
||||
session = checkout.create_fake_checkout({
|
||||
"customer_email": "a@x.com",
|
||||
"metadata": {"package_id": "3_credits", "credits": "3"},
|
||||
"success_url": "http://web/payment/success?session_id={CHECKOUT_SESSION_ID}",
|
||||
"line_items": [{"price_data": {"unit_amount": 2400, "product_data": {"name": "3 Karaoke Credits"}},
|
||||
"quantity": 1}],
|
||||
})
|
||||
record = checkout.get_checkout(session.id)
|
||||
assert session.id.startswith("e2e-test-") # accepted by the shared test-webhook handler
|
||||
assert session.url == f"http://10.0.2.2:8010/api/test/fake-checkout/{session.id}"
|
||||
assert record["amount_cents"] == 2400 and record["product_name"] == "3 Karaoke Credits"
|
||||
assert checkout.resolve_url(record["success_url"], session.id).endswith(f"session_id={session.id}")
|
||||
|
||||
def test_stripe_service_uses_fake_only_when_unconfigured_and_enabled(self):
|
||||
from backend.services.stripe_service import StripeService
|
||||
|
||||
svc = StripeService()
|
||||
svc.secret_key = None
|
||||
with patch("backend.services.stripe_service.fake_workers_enabled", return_value=False):
|
||||
assert svc.can_checkout() is False
|
||||
ok, url, _ = svc.create_checkout_session("1_credit", "a@x.com")
|
||||
assert ok is False and url is None
|
||||
with patch("backend.services.stripe_service.fake_workers_enabled", return_value=True), \
|
||||
patch("backend.services.fake_workers.checkout.create_fake_checkout",
|
||||
return_value=SimpleNamespace(id="e2e-test-local-1", url="http://api/x")) as fake:
|
||||
assert svc.can_checkout() is True
|
||||
ok, url, _ = svc.create_checkout_session("1_credit", "a@x.com")
|
||||
assert ok is True and url == "http://api/x"
|
||||
assert fake.call_args[0][0]["metadata"]["credits"] == "1"
|
||||
|
||||
|
||||
@pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="ffmpeg not installed")
|
||||
class TestFakeMedia:
|
||||
def test_builds_real_files_of_the_requested_length(self):
|
||||
from backend.services.fake_workers.media import get_media
|
||||
|
||||
media = get_media(5)
|
||||
assert get_media(5) is media # cached
|
||||
out = subprocess.run(
|
||||
["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", media.input_audio],
|
||||
capture_output=True, text=True, check=True,
|
||||
)
|
||||
assert abs(float(out.stdout) - 5) < 0.5
|
||||
for path in (media.vocals, media.backing_vocals, media.title_png, media.final_mp4, media.cdg_zip):
|
||||
assert open(path, "rb").read(4)
|
||||
@@ -0,0 +1,491 @@
|
||||
"""Tests for gen email-code sign-in (/api/users/auth/send-code + verify-code).
|
||||
|
||||
Composed tests: the real login_code router + real LoginCodeStore + real
|
||||
UserService (create_magic_link, sessions, verify) over the in-memory Firestore
|
||||
fake shared with the kjbox tests. Only true external boundaries are mocked
|
||||
(email provider, disposable-email lookups, the welcome-credit AI evaluation,
|
||||
and the signup-cap query, which the fake can't order_by).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, Mock
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from backend.api.routes import login_code
|
||||
from backend.services.email_service import EmailService, get_email_service
|
||||
from backend.services.login_code_store import code_doc_id
|
||||
from backend.services.user_service import MAGIC_LINKS_COLLECTION, get_user_service
|
||||
from backend.tests.test_kjbox_partner_api import ( # noqa: F401 (fixtures + fakes)
|
||||
FakeDb,
|
||||
FakeTransaction,
|
||||
db,
|
||||
user_service,
|
||||
validation,
|
||||
)
|
||||
|
||||
EMAIL = "newuser@example.com"
|
||||
IP = "203.0.113.7"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def email_service():
|
||||
svc = MagicMock()
|
||||
svc.is_configured.return_value = True
|
||||
svc.send_login_code.return_value = True
|
||||
svc.send_welcome_email.return_value = True
|
||||
svc.last_send_suppressed = False
|
||||
svc.build_magic_link_url.side_effect = (
|
||||
lambda token, locale="en", tenant_frontend_url=None, referral_code=None:
|
||||
f"{tenant_frontend_url or 'https://gen.test'}/{locale}/auth/verify?token={token}"
|
||||
)
|
||||
return svc
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def settings():
|
||||
return SimpleNamespace(
|
||||
login_code_pepper="test-pepper",
|
||||
admin_tokens="",
|
||||
login_codes_per_email_per_hour=5,
|
||||
login_codes_per_ip_per_hour=20,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def precompute(monkeypatch):
|
||||
calls = []
|
||||
monkeypatch.setattr(login_code, "_precompute_credit_eval", lambda *a: calls.append(a))
|
||||
|
||||
class SyncThread:
|
||||
def __init__(self, target, args=(), daemon=None):
|
||||
self.target, self.args = target, args
|
||||
|
||||
def start(self):
|
||||
self.target(*self.args)
|
||||
|
||||
monkeypatch.setattr(login_code, "threading", SimpleNamespace(Thread=SyncThread))
|
||||
return calls
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def signup_limited(user_service):
|
||||
state = {"limited": False, "calls": []}
|
||||
|
||||
def fake(ip_address=None, device_fingerprint=None, max_signups=None):
|
||||
state["calls"].append({"ip": ip_address, "fp": device_fingerprint})
|
||||
return state["limited"]
|
||||
|
||||
user_service.is_signup_rate_limited = fake
|
||||
return state
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(monkeypatch, db, user_service, email_service, validation, settings, precompute, signup_limited):
|
||||
original_init = login_code.LoginCodeStore.__init__
|
||||
|
||||
def init_with_fake_runner(self, *args, **kwargs):
|
||||
def runner(fn):
|
||||
db.txn_count += 1
|
||||
return fn(FakeTransaction(db.txn_writes))
|
||||
kwargs["transaction_runner"] = runner
|
||||
original_init(self, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(login_code.LoginCodeStore, "__init__", init_with_fake_runner)
|
||||
monkeypatch.setattr(login_code, "get_settings", lambda: settings)
|
||||
monkeypatch.setattr(login_code, "get_email_validation_service", lambda: validation)
|
||||
monkeypatch.setattr(login_code, "is_production", lambda: False)
|
||||
app = FastAPI()
|
||||
app.include_router(login_code.router, prefix="/api")
|
||||
app.dependency_overrides[get_user_service] = lambda: user_service
|
||||
app.dependency_overrides[get_email_service] = lambda: email_service
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
H = {"X-Forwarded-For": IP}
|
||||
|
||||
|
||||
def _send(client, email=EMAIL, headers=None, **extra):
|
||||
return client.post("/api/users/auth/send-code", json={"email": email, **extra}, headers={**H, **(headers or {})})
|
||||
|
||||
|
||||
def _sent_code(email_service):
|
||||
return email_service.send_login_code.call_args.args[1]
|
||||
|
||||
|
||||
def _verify(client, code, email=EMAIL, headers=None):
|
||||
return client.post("/api/users/auth/verify-code", json={"email": email, "code": code}, headers={**H, **(headers or {})})
|
||||
|
||||
|
||||
def _code_doc(db, email=EMAIL):
|
||||
return db.collection(login_code.LOGIN_CODES_COLLECTION).docs[code_doc_id(email)]
|
||||
|
||||
|
||||
def _wrong(code):
|
||||
return f"{(int(code) + 1) % 1_000_000:06d}"
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ send-code
|
||||
|
||||
class TestSendCode:
|
||||
def test_success_202_and_email_has_code_and_magic_link(self, client, db, email_service, user_service):
|
||||
resp = _send(client, locale="de-AT", device_fingerprint="fp-1")
|
||||
assert resp.status_code == 202
|
||||
assert resp.json() == {"status": "sent"}
|
||||
|
||||
args, kwargs = email_service.send_login_code.call_args
|
||||
assert args[0] == EMAIL and len(args[1]) == 6 and args[1].isdigit()
|
||||
assert kwargs["expiry_minutes"] == 10 and kwargs["locale"] == "de"
|
||||
# The magic link in the email is a real, verifiable magic-link token
|
||||
token = kwargs["magic_link_url"].rsplit("token=", 1)[1]
|
||||
link = db.collection(MAGIC_LINKS_COLLECTION).docs[token]
|
||||
assert link["email"] == EMAIL and link["device_fingerprint"] == "fp-1"
|
||||
|
||||
# Account created at send time with signup audit fields (magic-link parity)
|
||||
user = user_service.get_user(EMAIL)
|
||||
assert user.signup_ip == IP and user.device_fingerprint == "fp-1"
|
||||
|
||||
doc = _code_doc(db)
|
||||
assert doc["locale"] == "de" and doc["ui_locale"] == "de"
|
||||
assert doc["ip_address"] == IP and doc["device_fingerprint"] == "fp-1"
|
||||
assert args[1] not in str(doc.values())
|
||||
|
||||
def test_router_registered_in_main_app(self):
|
||||
from backend.main import app
|
||||
paths = {getattr(r, "path", "") for r in app.routes}
|
||||
assert {"/api/users/auth/send-code", "/api/users/auth/verify-code"} <= paths
|
||||
|
||||
def test_precomputes_credit_eval_onto_code_and_magic_link(self, client, email_service, precompute):
|
||||
_send(client)
|
||||
token = email_service.send_login_code.call_args.kwargs["magic_link_url"].rsplit("token=", 1)[1]
|
||||
assert precompute == [(
|
||||
code_doc_id(EMAIL), EMAIL, login_code.LOGIN_CODES_COLLECTION,
|
||||
((MAGIC_LINKS_COLLECTION, token),),
|
||||
)]
|
||||
|
||||
def test_no_precompute_for_test_email_or_already_granted(self, client, user_service, precompute):
|
||||
_send(client, email="someone@inbox.testmail.app")
|
||||
user_service.get_or_create_user("old@example.com")
|
||||
user_service.update_user("old@example.com", welcome_credits_granted=True)
|
||||
_send(client, email="old@example.com")
|
||||
assert precompute == []
|
||||
|
||||
def test_precompute_eval_helper_writes_all_targets(self, monkeypatch, db, user_service):
|
||||
from backend.api.routes import users as users_routes
|
||||
import backend.services.credit_evaluation_service as ces
|
||||
import backend.services.user_service as us
|
||||
|
||||
db.collection("a").document("1").set({})
|
||||
db.collection("b").document("2").set({})
|
||||
evaluation = SimpleNamespace(decision="grant", reasoning="ok", error=None)
|
||||
monkeypatch.setattr(ces, "get_credit_evaluation_service",
|
||||
lambda: SimpleNamespace(evaluate=lambda e, k: evaluation))
|
||||
monkeypatch.setattr(us, "get_user_service", lambda: user_service)
|
||||
users_routes._precompute_credit_eval("1", EMAIL, "a", (("b", "2"),))
|
||||
assert db.collection("a").docs["1"]["credit_eval_decision"] == "grant"
|
||||
assert db.collection("b").docs["2"]["credit_eval_decision"] == "grant"
|
||||
|
||||
def test_locale_falls_back_to_accept_language(self, client, db, email_service):
|
||||
_send(client, headers={"Accept-Language": "es-ES,es;q=0.9"})
|
||||
assert email_service.send_login_code.call_args.kwargs["locale"] == "es"
|
||||
assert _code_doc(db)["locale"] == "es"
|
||||
|
||||
def test_no_locale_does_not_persist_default(self, client, db, email_service):
|
||||
_send(client)
|
||||
assert email_service.send_login_code.call_args.kwargs["locale"] == "en"
|
||||
assert _code_doc(db)["locale"] is None # don't clobber a stored locale at verify
|
||||
|
||||
def test_invalid_email_422(self, client):
|
||||
assert _send(client, email="not-an-email").status_code == 422
|
||||
|
||||
def test_disposable_domain_422(self, client, validation, email_service):
|
||||
validation.is_disposable_domain.return_value = True
|
||||
resp = _send(client)
|
||||
assert resp.status_code == 422
|
||||
email_service.send_login_code.assert_not_called()
|
||||
|
||||
def test_blocked_email_pretends_success(self, client, validation, email_service, db, user_service):
|
||||
validation.is_email_blocked.return_value = True
|
||||
resp = _send(client)
|
||||
assert resp.status_code == 202 and resp.json() == {"status": "sent"}
|
||||
email_service.send_login_code.assert_not_called()
|
||||
assert user_service.get_user(EMAIL) is None
|
||||
assert db.collection(login_code.LOGIN_CODES_COLLECTION).docs == {}
|
||||
|
||||
def test_blocked_ip_pretends_success(self, client, validation, email_service):
|
||||
validation.is_ip_blocked.side_effect = lambda ip: ip == IP
|
||||
resp = _send(client)
|
||||
assert resp.status_code == 202 and resp.json() == {"status": "sent"}
|
||||
email_service.send_login_code.assert_not_called()
|
||||
|
||||
def test_new_user_signup_rate_limit_pretends_success(self, client, signup_limited, email_service, user_service):
|
||||
signup_limited["limited"] = True
|
||||
resp = _send(client, device_fingerprint="fp-9")
|
||||
assert resp.status_code == 202
|
||||
assert signup_limited["calls"] == [{"ip": IP, "fp": "fp-9"}]
|
||||
email_service.send_login_code.assert_not_called()
|
||||
assert user_service.get_user(EMAIL) is None
|
||||
|
||||
def test_signup_rate_limit_skipped_for_existing_user(self, client, signup_limited, email_service, user_service):
|
||||
user_service.get_or_create_user(EMAIL)
|
||||
signup_limited["limited"] = True
|
||||
assert _send(client).status_code == 202
|
||||
assert signup_limited["calls"] == []
|
||||
email_service.send_login_code.assert_called_once()
|
||||
|
||||
def test_production_without_email_provider_503(self, client, monkeypatch, email_service):
|
||||
monkeypatch.setattr(login_code, "is_production", lambda: True)
|
||||
email_service.is_configured.return_value = False
|
||||
assert _send(client).status_code == 503
|
||||
|
||||
def test_dev_without_email_provider_still_sends(self, client, email_service):
|
||||
email_service.is_configured.return_value = False
|
||||
assert _send(client).status_code == 202
|
||||
email_service.send_login_code.assert_called_once()
|
||||
|
||||
def test_send_failure_is_not_revealed(self, client, email_service):
|
||||
email_service.send_login_code.return_value = False
|
||||
assert _send(client).status_code == 202
|
||||
|
||||
def test_per_email_throttle_429(self, client, settings, email_service):
|
||||
settings.login_codes_per_email_per_hour = 2
|
||||
assert _send(client).status_code == 202
|
||||
assert _send(client).status_code == 202
|
||||
resp = _send(client)
|
||||
assert resp.status_code == 429 and resp.json() == {"detail": "too_many_codes"}
|
||||
assert email_service.send_login_code.call_count == 2
|
||||
|
||||
def test_per_ip_throttle_429_across_emails(self, client, settings, email_service, user_service):
|
||||
settings.login_codes_per_ip_per_hour = 2
|
||||
assert _send(client, email="a@example.com").status_code == 202
|
||||
assert _send(client, email="b@example.com").status_code == 202
|
||||
resp = _send(client, email="c@example.com")
|
||||
assert resp.status_code == 429 and resp.json() == {"detail": "too_many_codes"}
|
||||
# Throttled before the magic link (and so the account) was minted
|
||||
assert user_service.get_user("c@example.com") is None
|
||||
# Another IP is unaffected
|
||||
other = _send(client, email="c@example.com", headers={"X-Forwarded-For": "198.51.100.1"})
|
||||
assert other.status_code == 202
|
||||
|
||||
def test_tenant_disallowed_email_pretends_success(self, client, monkeypatch, email_service):
|
||||
cfg = MagicMock()
|
||||
cfg.is_explicitly_allowlisted.return_value = False
|
||||
cfg.is_email_allowed.return_value = False
|
||||
import backend.middleware.tenant as tenant_mw
|
||||
monkeypatch.setattr(tenant_mw, "get_tenant_config_from_request", lambda r: cfg)
|
||||
monkeypatch.setattr(tenant_mw, "get_tenant_from_request", lambda r: "vocalstar")
|
||||
assert _send(client).status_code == 202
|
||||
email_service.send_login_code.assert_not_called()
|
||||
|
||||
def test_tenant_sign_in_uses_tenant_branding_and_skips_credit(
|
||||
self, client, monkeypatch, email_service, db, precompute, validation, user_service,
|
||||
):
|
||||
cfg = MagicMock()
|
||||
cfg.is_explicitly_allowlisted.return_value = True
|
||||
cfg.is_email_allowed.return_value = True
|
||||
cfg.get_frontend_url.return_value = "https://vs.example"
|
||||
cfg.get_sender_email.return_value = "noreply@vs.example"
|
||||
cfg.name = "Vocal Star"
|
||||
import backend.middleware.tenant as tenant_mw
|
||||
monkeypatch.setattr(tenant_mw, "get_tenant_config_from_request", lambda r: cfg)
|
||||
monkeypatch.setattr(tenant_mw, "get_tenant_from_request", lambda r: "vocalstar")
|
||||
validation.is_disposable_domain.return_value = True # vouched by tenant allowlist
|
||||
assert _send(client).status_code == 202
|
||||
kwargs = email_service.send_login_code.call_args.kwargs
|
||||
assert kwargs["magic_link_url"].startswith("https://vs.example/")
|
||||
assert kwargs["sender_email"] == "noreply@vs.example" and kwargs["tenant_name"] == "Vocal Star"
|
||||
assert precompute == []
|
||||
assert _code_doc(db)["tenant_id"] == "vocalstar"
|
||||
|
||||
import backend.services.tenant_service as ts
|
||||
monkeypatch.setattr(ts, "get_tenant_service", lambda: SimpleNamespace(
|
||||
get_tenant_config=lambda tid: SimpleNamespace(subdomain="vocalstar")))
|
||||
resp = _verify(client, _sent_code(email_service))
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["tenant_subdomain"] == "vocalstar"
|
||||
assert body["credits_granted"] == 0 and body["credit_status"] == "not_applicable"
|
||||
assert user_service.grant_calls == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- verify-code
|
||||
|
||||
class TestVerifyCode:
|
||||
def test_success_shape_session_and_welcome_credit(self, client, email_service, user_service, db):
|
||||
_send(client, locale="es")
|
||||
resp = _verify(client, _sent_code(email_service))
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert set(body) >= {"status", "session_token", "user", "message", "credits_granted", "credit_status",
|
||||
"tenant_subdomain", "redirect_path"}
|
||||
assert body["status"] == "success"
|
||||
assert body["credits_granted"] == user_service.NEW_USER_FREE_CREDITS
|
||||
assert body["credit_status"] == "granted"
|
||||
assert body["user"]["email"] == EMAIL
|
||||
assert body["user"]["credits"] == user_service.NEW_USER_FREE_CREDITS
|
||||
from backend.i18n import t
|
||||
assert body["message"] == t("es", "users.magicLinkSuccess")
|
||||
|
||||
# Session is real and valid
|
||||
ok, user, _ = user_service.validate_session(body["session_token"])
|
||||
assert ok and user.email == EMAIL
|
||||
user = user_service.get_user(EMAIL)
|
||||
assert user.email_verified is True and user.last_login_at is not None
|
||||
assert user.locale == "es"
|
||||
# First login → welcome email in the user's locale
|
||||
email_service.send_welcome_email.assert_called_once()
|
||||
assert email_service.send_welcome_email.call_args.kwargs["locale"] == "es"
|
||||
|
||||
def test_precomputed_eval_passed_to_grant(self, client, email_service, user_service, db):
|
||||
_send(client)
|
||||
doc = _code_doc(db)
|
||||
doc.update({"credit_eval_decision": "grant", "credit_eval_reasoning": "looks real"})
|
||||
_verify(client, _sent_code(email_service))
|
||||
assert user_service.grant_calls[-1]["precomputed_eval"] == {
|
||||
"credit_eval_decision": "grant", "credit_eval_reasoning": "looks real",
|
||||
}
|
||||
|
||||
def test_returning_user_no_second_credit(self, client, email_service, user_service):
|
||||
_send(client)
|
||||
_verify(client, _sent_code(email_service))
|
||||
_send(client)
|
||||
body = _verify(client, _sent_code(email_service)).json()
|
||||
assert body["credits_granted"] == 0 and body["credit_status"] == "already_granted"
|
||||
assert email_service.send_welcome_email.call_count == 1
|
||||
|
||||
def test_wrong_code_401(self, client, email_service):
|
||||
_send(client)
|
||||
resp = _verify(client, _wrong(_sent_code(email_service)))
|
||||
assert resp.status_code == 401 and resp.json() == {"detail": "invalid_code"}
|
||||
|
||||
def test_no_code_issued_401_expired(self, client):
|
||||
resp = _verify(client, "123456")
|
||||
assert resp.status_code == 401 and resp.json() == {"detail": "expired"}
|
||||
|
||||
def test_expired_code_401(self, client, email_service, db):
|
||||
_send(client)
|
||||
_code_doc(db)["expires_at"] = datetime.now(timezone.utc) - timedelta(minutes=1)
|
||||
resp = _verify(client, _sent_code(email_service))
|
||||
assert resp.status_code == 401 and resp.json() == {"detail": "expired"}
|
||||
|
||||
def test_five_wrong_attempts_then_429(self, client, email_service):
|
||||
_send(client)
|
||||
code = _sent_code(email_service)
|
||||
for _ in range(5):
|
||||
assert _verify(client, _wrong(code)).status_code == 401
|
||||
resp = _verify(client, code)
|
||||
assert resp.status_code == 429 and resp.json() == {"detail": "too_many_attempts"}
|
||||
# Burned: even the right code no longer works
|
||||
assert _verify(client, code).json() == {"detail": "expired"}
|
||||
|
||||
def test_code_is_single_use(self, client, email_service):
|
||||
_send(client)
|
||||
code = _sent_code(email_service)
|
||||
assert _verify(client, code).status_code == 200
|
||||
assert _verify(client, code).json() == {"detail": "expired"}
|
||||
|
||||
def test_recreates_account_if_removed_after_send(self, client, email_service, user_service, db):
|
||||
_send(client, device_fingerprint="fp-2")
|
||||
db.collection("gen_users").docs.clear()
|
||||
assert user_service.get_user(EMAIL) is None
|
||||
resp = _verify(client, _sent_code(email_service))
|
||||
assert resp.status_code == 200
|
||||
user = user_service.get_user(EMAIL)
|
||||
assert user.signup_ip == IP and user.device_fingerprint == "fp-2"
|
||||
|
||||
def test_session_gets_fingerprint_from_code_record(self, client, email_service, user_service, monkeypatch):
|
||||
seen = {}
|
||||
real = user_service.create_session
|
||||
|
||||
def spy(email, **kw):
|
||||
seen.update(kw)
|
||||
return real(email, **kw)
|
||||
|
||||
monkeypatch.setattr(user_service, "create_session", spy)
|
||||
_send(client, device_fingerprint="fp-3")
|
||||
_verify(client, _sent_code(email_service), headers={"User-Agent": "ua-x"})
|
||||
assert seen["device_fingerprint"] == "fp-3" and seen["ip_address"] == IP
|
||||
assert seen["user_agent"] == "ua-x" and seen["tenant_id"] is None
|
||||
|
||||
|
||||
# ------------------------------------------------------------------- pepper
|
||||
|
||||
class TestPepper:
|
||||
def test_explicit_pepper(self, monkeypatch, settings):
|
||||
monkeypatch.setattr(login_code, "get_settings", lambda: settings)
|
||||
assert login_code._resolve_pepper() == "test-pepper"
|
||||
|
||||
def test_derived_from_first_admin_token(self, monkeypatch, settings):
|
||||
settings.login_code_pepper = ""
|
||||
settings.admin_tokens = " tok-a , tok-b"
|
||||
monkeypatch.setattr(login_code, "get_settings", lambda: settings)
|
||||
assert login_code._resolve_pepper() == hashlib.sha256(b"gen-login-code-pepper:tok-a").hexdigest()
|
||||
|
||||
def test_dev_fallback(self, monkeypatch, settings):
|
||||
settings.login_code_pepper, settings.admin_tokens = "", None
|
||||
monkeypatch.setattr(login_code, "get_settings", lambda: settings)
|
||||
monkeypatch.setattr(login_code, "is_production", lambda: False)
|
||||
assert login_code._resolve_pepper() == login_code._DEV_PEPPER
|
||||
|
||||
def test_production_without_pepper_returns_503(self, client, monkeypatch, settings):
|
||||
settings.login_code_pepper, settings.admin_tokens = "", ""
|
||||
monkeypatch.setattr(login_code, "is_production", lambda: True)
|
||||
assert _send(client).status_code == 503
|
||||
assert _verify(client, "123456").status_code == 503
|
||||
|
||||
def test_codes_from_one_pepper_fail_under_another(self, client, settings, email_service):
|
||||
_send(client)
|
||||
code = _sent_code(email_service)
|
||||
settings.login_code_pepper = "rotated"
|
||||
assert _verify(client, code).json() == {"detail": "invalid_code"}
|
||||
|
||||
|
||||
# -------------------------------------------------------------------- email
|
||||
|
||||
class TestLoginCodeEmail:
|
||||
def _send(self, locale="en", **kw):
|
||||
service = EmailService()
|
||||
service.provider = Mock()
|
||||
service.provider.send_email.return_value = True
|
||||
url = service.build_magic_link_url("tok123", locale=locale)
|
||||
result = service.send_login_code(
|
||||
"user@example.com", "042917", magic_link_url=url, expiry_minutes=10, locale=locale, **kw,
|
||||
)
|
||||
return result, url, service.provider.send_email.call_args.kwargs
|
||||
|
||||
def test_contains_code_and_magic_link(self):
|
||||
result, url, kwargs = self._send()
|
||||
assert result is True
|
||||
assert url.endswith("/en/auth/verify?token=tok123")
|
||||
assert kwargs["to_email"] == "user@example.com"
|
||||
assert kwargs["subject"] == "Your Nomad Karaoke sign-in code: 042917"
|
||||
for body in (kwargs["html_content"], kwargs["text_content"]):
|
||||
assert "042917" in body
|
||||
assert "10 minutes" in body
|
||||
assert "/auth/verify?token=tok123" in body
|
||||
|
||||
def test_localised(self):
|
||||
from backend.i18n import t
|
||||
_, url, kwargs = self._send(locale="de")
|
||||
assert "/de/auth/verify" in url
|
||||
assert kwargs["subject"] == t("de", "emails.loginCode.subject", brand_name="Nomad Karaoke", code="042917")
|
||||
assert "Anmelde" in kwargs["subject"]
|
||||
|
||||
def test_tenant_branding(self):
|
||||
_, _, kwargs = self._send(sender_email="noreply@vs.example", tenant_name="Vocal Star")
|
||||
assert "Vocal Star" in kwargs["subject"]
|
||||
assert kwargs["from_email_override"] == "noreply@vs.example"
|
||||
assert "© " in kwargs["text_content"] and "Vocal Star" in kwargs["text_content"]
|
||||
|
||||
def test_build_magic_link_url_matches_magic_link_email(self):
|
||||
service = EmailService()
|
||||
service.provider = Mock()
|
||||
service.provider.send_email.return_value = True
|
||||
service.send_magic_link("user@example.com", "tokX", locale="es", referral_code="ref1")
|
||||
html = service.provider.send_email.call_args.kwargs["html_content"]
|
||||
assert service.build_magic_link_url("tokX", locale="es", referral_code="ref1") in html
|
||||
@@ -0,0 +1,213 @@
|
||||
"""Unit tests for LoginCodeStore (emailed 6-digit sign-in codes).
|
||||
|
||||
Runs the real store over the in-memory Firestore fake shared with the kjbox
|
||||
partner tests (writes applied immediately; the store reads before writing).
|
||||
The Firestore primitives themselves (Increment, create()-only markers, real
|
||||
transactions) are covered against the emulator in
|
||||
backend/tests/emulator/test_login_code_store.py.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.services import login_code_store as lcs
|
||||
from backend.services.login_code_store import (
|
||||
CODE_EXPIRY_MINUTES,
|
||||
MAX_VERIFY_ATTEMPTS,
|
||||
CodeThrottled,
|
||||
LoginCodeStore,
|
||||
TransactionContention,
|
||||
VerifyOutcome,
|
||||
code_doc_id,
|
||||
)
|
||||
from backend.tests.test_kjbox_partner_api import FakeDb, FakeTransaction
|
||||
|
||||
EMAIL = "user@example.com"
|
||||
CODES, USES, IPS = "login_codes", "login_code_uses", "login_code_ip_sends"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db():
|
||||
return FakeDb()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(db):
|
||||
def runner(fn):
|
||||
db.txn_count += 1
|
||||
return fn(FakeTransaction(db.txn_writes))
|
||||
|
||||
return LoginCodeStore(db, "pepper", CODES, USES, transaction_runner=runner, ip_collection=IPS)
|
||||
|
||||
|
||||
def _issue(store, email=EMAIL, **over):
|
||||
kwargs = dict(max_per_hour=5, needs_credit_eval=True)
|
||||
kwargs.update(over)
|
||||
return store.issue_code(email, **kwargs)
|
||||
|
||||
|
||||
def _wrong(code):
|
||||
return f"{(int(code) + 1) % 1_000_000:06d}"
|
||||
|
||||
|
||||
def _doc(db, email=EMAIL):
|
||||
return db.collection(CODES).docs[code_doc_id(email)]
|
||||
|
||||
|
||||
class TestIssue:
|
||||
def test_issues_six_digit_code_stored_only_as_hmac(self, store, db):
|
||||
issued = _issue(store, fields={"locale": "de", "ip_address": "1.2.3.4"})
|
||||
assert len(issued.code) == 6 and issued.code.isdigit()
|
||||
assert issued.doc_id == hashlib.sha256(EMAIL.encode()).hexdigest()
|
||||
doc = _doc(db)
|
||||
assert issued.code not in str(doc.values())
|
||||
assert doc["code_hash"] == store.hash_code(doc["code_id"], issued.code)
|
||||
assert doc["attempts"] == 0 and doc["email"] == EMAIL
|
||||
assert doc["expires_at"] - doc["created_at"] == timedelta(minutes=CODE_EXPIRY_MINUTES)
|
||||
# Caller metadata rides on the record
|
||||
assert doc["locale"] == "de" and doc["ip_address"] == "1.2.3.4"
|
||||
|
||||
def test_email_is_normalised(self, store, db):
|
||||
_issue(store, email=" User@Example.COM ")
|
||||
assert code_doc_id(EMAIL) in db.collection(CODES).docs
|
||||
|
||||
def test_pepper_changes_hash(self, db):
|
||||
a = LoginCodeStore(db, "p1", CODES, USES)
|
||||
b = LoginCodeStore(db, "p2", CODES, USES)
|
||||
assert a.hash_code("id", "123456") != b.hash_code("id", "123456")
|
||||
|
||||
def test_reissue_invalidates_previous_code(self, store):
|
||||
first = _issue(store)
|
||||
second = _issue(store)
|
||||
if first.code != second.code:
|
||||
assert store.verify_code(EMAIL, first.code)[0] == VerifyOutcome.INVALID
|
||||
assert store.verify_code(EMAIL, second.code)[0] == VerifyOutcome.OK
|
||||
|
||||
def test_per_email_throttle(self, store, db):
|
||||
for _ in range(3):
|
||||
_issue(store, max_per_hour=3)
|
||||
with pytest.raises(CodeThrottled):
|
||||
_issue(store, max_per_hour=3)
|
||||
assert len(_doc(db)["send_times"]) == 3
|
||||
|
||||
def test_old_sends_fall_out_of_rolling_window(self, store, db):
|
||||
_issue(store)
|
||||
_doc(db)["send_times"] = [datetime.now(timezone.utc) - timedelta(hours=2)] * 5
|
||||
_issue(store, max_per_hour=5) # old sends don't count
|
||||
assert len(_doc(db)["send_times"]) == 1
|
||||
|
||||
def test_per_ip_throttle_spans_emails(self, store, db):
|
||||
for i in range(2):
|
||||
_issue(store, email=f"u{i}@example.com", throttle_ip="9.9.9.9", max_per_ip_per_hour=2)
|
||||
with pytest.raises(CodeThrottled):
|
||||
_issue(store, email="u2@example.com", throttle_ip="9.9.9.9", max_per_ip_per_hour=2)
|
||||
# Throttled request wrote nothing (no code for u2)
|
||||
assert code_doc_id("u2@example.com") not in db.collection(CODES).docs
|
||||
# A different IP is unaffected
|
||||
_issue(store, email="u2@example.com", throttle_ip="8.8.8.8", max_per_ip_per_hour=2)
|
||||
# IP stored only as a hash (doc id), never raw
|
||||
ip_docs = db.collection(IPS).docs
|
||||
assert hashlib.sha256(b"9.9.9.9").hexdigest() in ip_docs
|
||||
assert "9.9.9.9" not in str(ip_docs)
|
||||
|
||||
def test_ip_and_code_written_in_one_transaction(self, store, db):
|
||||
_issue(store, throttle_ip="9.9.9.9", max_per_ip_per_hour=5)
|
||||
assert db.txn_count == 1
|
||||
assert sorted(w[1] is db.collection(IPS) for w in db.txn_writes) == [False, True]
|
||||
|
||||
def test_no_ip_write_without_throttle_ip(self, store, db):
|
||||
_issue(store)
|
||||
assert db.collection(IPS).docs == {}
|
||||
assert len(db.txn_writes) == 1
|
||||
|
||||
def test_throttle_ip_requires_ip_collection(self, db):
|
||||
s = LoginCodeStore(db, "p", CODES, USES, transaction_runner=lambda fn: fn(FakeTransaction([])))
|
||||
with pytest.raises(ValueError):
|
||||
_issue(s, throttle_ip="1.1.1.1", max_per_ip_per_hour=1)
|
||||
|
||||
def test_needs_credit_eval_false_once_eval_stored(self, store, db):
|
||||
assert _issue(store).needs_credit_eval is True
|
||||
_doc(db)["credit_eval_decision"] = "grant"
|
||||
second = _issue(store)
|
||||
assert second.needs_credit_eval is False
|
||||
assert _doc(db)["credit_eval_decision"] == "grant" # merge keeps it
|
||||
assert _issue(store, needs_credit_eval=False).needs_credit_eval is False
|
||||
|
||||
|
||||
class TestVerify:
|
||||
def test_round_trip_and_single_use(self, store, db):
|
||||
issued = _issue(store, fields={"locale": "es"})
|
||||
outcome, record = store.verify_code(EMAIL, issued.code)
|
||||
assert outcome == VerifyOutcome.OK and record["locale"] == "es"
|
||||
assert _doc(db)["code_hash"] is None
|
||||
assert store.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.EXPIRED
|
||||
|
||||
def test_unknown_email_is_expired(self, store):
|
||||
assert store.verify_code("nobody@example.com", "123456") == (VerifyOutcome.EXPIRED, None)
|
||||
|
||||
def test_wrong_code_invalid(self, store):
|
||||
issued = _issue(store)
|
||||
assert store.verify_code(EMAIL, _wrong(issued.code)) == (VerifyOutcome.INVALID, None)
|
||||
|
||||
@pytest.mark.parametrize("bad", ["", "12345", "1234567", "abcdef", " "])
|
||||
def test_malformed_code_invalid(self, store, bad):
|
||||
_issue(store)
|
||||
assert store.verify_code(EMAIL, bad)[0] == VerifyOutcome.INVALID
|
||||
|
||||
def test_whitespace_around_code_tolerated(self, store):
|
||||
issued = _issue(store)
|
||||
assert store.verify_code(EMAIL, f" {issued.code} ")[0] == VerifyOutcome.OK
|
||||
|
||||
def test_expired_code_burned(self, store, db):
|
||||
issued = _issue(store)
|
||||
_doc(db)["expires_at"] = datetime.now(timezone.utc) - timedelta(seconds=1)
|
||||
assert store.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.EXPIRED
|
||||
assert _doc(db)["code_hash"] is None
|
||||
|
||||
def test_attempt_cap_then_burned(self, store, db):
|
||||
issued = _issue(store)
|
||||
for _ in range(MAX_VERIFY_ATTEMPTS):
|
||||
assert store.verify_code(EMAIL, _wrong(issued.code))[0] == VerifyOutcome.INVALID
|
||||
assert store.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.TOO_MANY_ATTEMPTS
|
||||
assert _doc(db)["code_hash"] is None
|
||||
assert store.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.EXPIRED
|
||||
|
||||
def test_use_marker_blocks_replay(self, store, db):
|
||||
issued = _issue(store)
|
||||
db.collection(USES).document(_doc(db)["code_id"]).create({"email": EMAIL})
|
||||
assert store.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.EXPIRED
|
||||
|
||||
def test_collections_are_isolated(self, db):
|
||||
runner = lambda fn: fn(FakeTransaction([])) # noqa: E731
|
||||
gen = LoginCodeStore(db, "p", CODES, USES, transaction_runner=runner)
|
||||
kj = LoginCodeStore(db, "p", "kjbox_login_codes", "kjbox_login_code_uses", transaction_runner=runner)
|
||||
issued = gen.issue_code(EMAIL, max_per_hour=5, needs_credit_eval=False)
|
||||
assert kj.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.EXPIRED
|
||||
assert gen.verify_code(EMAIL, issued.code)[0] == VerifyOutcome.OK
|
||||
|
||||
|
||||
class TestRealRunner:
|
||||
def test_maps_firestore_giveup_to_contention(self, monkeypatch):
|
||||
def give_up(fn):
|
||||
def wrapped(transaction):
|
||||
raise ValueError("Failed to commit transaction in 10 attempts.")
|
||||
return wrapped
|
||||
|
||||
monkeypatch.setattr(lcs.firestore, "transactional", give_up)
|
||||
store = LoginCodeStore(MagicMock(), "p", CODES, USES)
|
||||
with pytest.raises(TransactionContention):
|
||||
store.issue_code(EMAIL, max_per_hour=5, needs_credit_eval=False)
|
||||
|
||||
def test_other_value_errors_propagate(self, monkeypatch):
|
||||
def boom(fn):
|
||||
def wrapped(transaction):
|
||||
raise ValueError("something else")
|
||||
return wrapped
|
||||
|
||||
monkeypatch.setattr(lcs.firestore, "transactional", boom)
|
||||
with pytest.raises(ValueError, match="something else"):
|
||||
lcs.run_firestore_transaction(MagicMock(), lambda t: None)
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "إليك رمز تسجيل الدخول الخاص بك في صفحة مغني ليلة الكاريوكي. اكتبه في الصفحة التي تستخدمها:",
|
||||
"subject": "رمز Nomad Karaoke الخاص بك: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "تنتهي صلاحية هذا الرمز خلال {minutes} دقائق ولا يمكن استخدامه إلا مرة واحدة.",
|
||||
"greeting": "مرحبًا،",
|
||||
"ignoreNote": "إذا لم تطلب هذا البريد الإلكتروني، يمكنك تجاهله بأمان.",
|
||||
"intro": "أدخل هذا الرمز في صفحة تسجيل الدخول للمتابعة:",
|
||||
"linkExpiry": "يعمل رابط تسجيل الدخول لمدة 24 ساعة ولا يمكن استخدامه إلا مرة واحدة.",
|
||||
"orLink": "أو تخطَّ الرمز وسجّل الدخول بنقرة واحدة:",
|
||||
"signInButton": "تسجيل الدخول",
|
||||
"subject": "رمز تسجيل الدخول إلى {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "تفضّل ألا تراجع الكلمات بنفسك؟ اختر Made For You وسأصنع الفيديو كاملاً من أجلك، ليكون جاهزاً خلال 24 ساعة:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Aquí tens el teu codi per iniciar sessió a la pàgina de cantants de la nit de karaoke. Introdueix-lo a la pàgina que estàs utilitzant:",
|
||||
"subject": "El teu codi de Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Aquest codi caduca d'aquí a {minutes} minuts i només es pot fer servir una vegada.",
|
||||
"greeting": "Hola,",
|
||||
"ignoreNote": "Si no has sol·licitat aquest correu, el pots ignorar sense problemes.",
|
||||
"intro": "Introdueix aquest codi a la pàgina d'inici de sessió per continuar:",
|
||||
"linkExpiry": "L'enllaç d'inici de sessió funciona durant 24 hores i només es pot fer servir una vegada.",
|
||||
"orLink": "O salta't el codi i inicia la sessió amb un clic:",
|
||||
"signInButton": "Inicia la sessió",
|
||||
"subject": "El teu codi d'inici de sessió de {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Prefereixes no haver de revisar la lletra? Tria «Made For You» i et prepararé tot el vídeo jo mateix, lliurat en 24 hores:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Tady je tvůj kód pro přihlášení na stránku zpěváka karaoke večera. Zadej ho na stránce, kterou používáš:",
|
||||
"subject": "Tvůj kód pro Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Platnost tohoto kódu vyprší za {minutes} minut a lze jej použít pouze jednou.",
|
||||
"greeting": "Dobrý den,",
|
||||
"ignoreNote": "Pokud jste o tento e-mail nežádali, můžete jej klidně ignorovat.",
|
||||
"intro": "Pro pokračování zadejte tento kód na přihlašovací stránce:",
|
||||
"linkExpiry": "Přihlašovací odkaz platí 24 hodin a lze jej použít pouze jednou.",
|
||||
"orLink": "Nebo kód přeskočte a přihlaste se jedním kliknutím:",
|
||||
"signInButton": "Přihlásit se",
|
||||
"subject": "Váš přihlašovací kód pro {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Nechce se ti kontrolovat text osobně? Zvol možnost Made For You a já pro tebe vytvořím celé video s doručením do 24 hodin:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Her er din kode til at logge ind på sangersiden til karaokenatten. Indtast den på den side, du bruger:",
|
||||
"subject": "Din Nomad Karaoke-kode: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Denne kode udløber om {minutes} minutter og kan kun bruges én gang.",
|
||||
"greeting": "Hej,",
|
||||
"ignoreNote": "Hvis du ikke har anmodet om denne e-mail, kan du roligt ignorere den.",
|
||||
"intro": "Indtast denne kode på login-siden for at fortsætte:",
|
||||
"linkExpiry": "Login-linket virker i 24 timer og kan kun bruges én gang.",
|
||||
"orLink": "Eller spring koden over og log ind med ét klik:",
|
||||
"signInButton": "Log ind",
|
||||
"subject": "Din login-kode til {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Vil du helst slippe for selv at gennemgå teksten? Vælg Made For You, så laver jeg hele videoen for dig, leveret inden for 24 timer:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Hier ist dein Code zur Anmeldung auf der Sängerseite für den Karaoke-Abend. Gib ihn auf der Seite ein, die du gerade verwendest:",
|
||||
"subject": "Dein Nomad Karaoke Code: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Dieser Code läuft in {minutes} Minuten ab und kann nur einmal verwendet werden.",
|
||||
"greeting": "Hallo,",
|
||||
"ignoreNote": "Wenn du diese E-Mail nicht angefordert hast, kannst du sie einfach ignorieren.",
|
||||
"intro": "Gib diesen Code auf der Anmeldeseite ein, um fortzufahren:",
|
||||
"linkExpiry": "Der Anmeldelink ist 24 Stunden gültig und kann nur einmal verwendet werden.",
|
||||
"orLink": "Oder überspringe den Code und melde dich mit einem Klick an:",
|
||||
"signInButton": "Anmelden",
|
||||
"subject": "Dein {brand_name} Anmeldecode: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Möchtest du die Textüberprüfung lieber nicht selbst machen? Wähle „Made For You“ und ich erstelle das komplette Video für dich – geliefert innerhalb von 24 Stunden:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Ορίστε ο κωδικός σου για να συνδεθείς στη σελίδα τραγουδιστή της βραδιάς καραόκε. Πληκτρολόγησέ τον στη σελίδα που χρησιμοποιείς:",
|
||||
"subject": "Ο κωδικός σου για το Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Αυτός ο κωδικός λήγει σε {minutes} λεπτά και μπορεί να χρησιμοποιηθεί μόνο μία φορά.",
|
||||
"greeting": "Γεια σας,",
|
||||
"ignoreNote": "Αν δεν ζητήσατε αυτό το email, μπορείτε να το αγνοήσετε με ασφάλεια.",
|
||||
"intro": "Εισαγάγετε αυτόν τον κωδικό στη σελίδα σύνδεσης για να συνεχίσετε:",
|
||||
"linkExpiry": "Ο σύνδεσμος σύνδεσης ισχύει για 24 ώρες και μπορεί να χρησιμοποιηθεί μόνο μία φορά.",
|
||||
"orLink": "Ή παραλείψτε τον κωδικό και συνδεθείτε με ένα κλικ:",
|
||||
"signInButton": "Σύνδεση",
|
||||
"subject": "Ο κωδικός σύνδεσής σας στο {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Προτιμάς να μην κάνεις εσύ τον έλεγχο των στίχων; Επίλεξε το Made For You και θα σου ετοιμάσω ολόκληρο το βίντεο, με παράδοση εντός 24 ωρών:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -225,6 +225,16 @@
|
||||
"expiry": "This code expires in {minutes} minutes and can only be used once.",
|
||||
"ignoreNote": "If you didn't request this code, you can safely ignore this email."
|
||||
},
|
||||
"loginCode": {
|
||||
"subject": "Your {brand_name} sign-in code: {code}",
|
||||
"greeting": "Hi there,",
|
||||
"intro": "Enter this code on the sign-in page to continue:",
|
||||
"expiry": "This code expires in {minutes} minutes and can only be used once.",
|
||||
"orLink": "Or skip the code and sign in with one click:",
|
||||
"signInButton": "Sign In",
|
||||
"linkExpiry": "The sign-in link works for 24 hours and can only be used once.",
|
||||
"ignoreNote": "If you didn't request this email, you can safely ignore it."
|
||||
},
|
||||
"welcome": {
|
||||
"subject": "Welcome to Nomad Karaoke!",
|
||||
"welcomeGreeting": "Welcome to Nomad Karaoke!",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Aquí tienes tu código para iniciar sesión en la página de cantantes de la noche de karaoke. Escríbelo en la página que estás usando:",
|
||||
"subject": "Tu código de Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Este código caduca en {minutes} minutos y solo se puede usar una vez.",
|
||||
"greeting": "Hola,",
|
||||
"ignoreNote": "Si no solicitaste este correo electrónico, puedes ignorarlo de forma segura.",
|
||||
"intro": "Introduce este código en la página de inicio de sesión para continuar:",
|
||||
"linkExpiry": "El enlace de inicio de sesión funciona durante 24 horas y solo se puede usar una vez.",
|
||||
"orLink": "O sáltate el código e inicia sesión con un clic:",
|
||||
"signInButton": "Iniciar sesión",
|
||||
"subject": "Tu código de inicio de sesión de {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "¿Prefieres no hacer la revisión de letras por tu cuenta? Elige Made For You y crearé todo el video por ti, con entrega en menos de 24 horas:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Tässä on koodisi karaokeillan laulajasivulle kirjautumista varten. Kirjoita se käyttämällesi sivulle:",
|
||||
"subject": "Nomad Karaoke -koodisi: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Tämä koodi vanhenee {minutes} minuutissa, ja sitä voi käyttää vain kerran.",
|
||||
"greeting": "Hei,",
|
||||
"ignoreNote": "Jos et pyytänyt tätä sähköpostia, voit turvallisesti jättää sen huomiotta.",
|
||||
"intro": "Jatka syöttämällä tämä koodi kirjautumissivulle:",
|
||||
"linkExpiry": "Kirjautumislinkki toimii 24 tuntia, ja sitä voi käyttää vain kerran.",
|
||||
"orLink": "Tai ohita koodi ja kirjaudu sisään yhdellä napsautuksella:",
|
||||
"signInButton": "Kirjaudu sisään",
|
||||
"subject": "{brand_name}-kirjautumiskoodisi: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Etkö halua tarkistaa sanoituksia itse? Valitse Made For You, niin teen koko videon puolestasi 24 tunnin kuluessa:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Voici ton code pour te connecter sur la page des chanteurs de la soirée karaoké. Tape-le sur la page que tu utilises :",
|
||||
"subject": "Ton code Nomad Karaoke : {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Ce code expire dans {minutes} minutes et ne peut être utilisé qu'une seule fois.",
|
||||
"greeting": "Bonjour,",
|
||||
"ignoreNote": "Si vous n'avez pas demandé cet e-mail, vous pouvez l'ignorer en toute sécurité.",
|
||||
"intro": "Saisissez ce code sur la page de connexion pour continuer :",
|
||||
"linkExpiry": "Le lien de connexion est valable 24 heures et ne peut être utilisé qu'une seule fois.",
|
||||
"orLink": "Ou passez le code et connectez-vous en un clic :",
|
||||
"signInButton": "Se connecter",
|
||||
"subject": "Votre code de connexion {brand_name} : {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Tu préfères ne pas vérifier les paroles toi-même ? Choisis Made For You et je m'occupe de créer toute la vidéo pour toi, livrée sous 24 heures :",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "הנה הקוד שלך להתחברות לעמוד הזמרים של ערב הקריוקי. יש להזין אותו בעמוד שבו אתם משתמשים:",
|
||||
"subject": "קוד ה-Nomad Karaoke שלך: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "תוקף הקוד יפוג בעוד {minutes} דקות, וניתן להשתמש בו פעם אחת בלבד.",
|
||||
"greeting": "שלום,",
|
||||
"ignoreNote": "אם לא ביקשת את המייל הזה, אפשר פשוט להתעלם ממנו.",
|
||||
"intro": "הזינו את הקוד הזה בדף הכניסה כדי להמשיך:",
|
||||
"linkExpiry": "קישור הכניסה תקף למשך 24 שעות, וניתן להשתמש בו פעם אחת בלבד.",
|
||||
"orLink": "או דלגו על הקוד והיכנסו בלחיצה אחת:",
|
||||
"signInButton": "כניסה",
|
||||
"subject": "קוד הכניסה שלך ל-{brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "מעדיפים לא לעבור על המילים בעצמכם? בחרו ב-Made For You ואני אכין את כל הסרטון עבורכם, מוכן תוך 24 שעות:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "कराओके नाइट सिंगर पेज पर साइन इन करने के लिए यह तुम्हारा कोड है। तुम जिस पेज का उपयोग कर रहे हो, उसमें इसे टाइप करो:",
|
||||
"subject": "तुम्हारा Nomad Karaoke कोड: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "यह कोड {minutes} मिनट में समाप्त हो जाएगा और इसे केवल एक बार उपयोग किया जा सकता है।",
|
||||
"greeting": "नमस्ते,",
|
||||
"ignoreNote": "अगर आपने यह ईमेल नहीं मांगा था, तो आप इसे सुरक्षित रूप से अनदेखा कर सकते हैं।",
|
||||
"intro": "जारी रखने के लिए साइन-इन पेज पर यह कोड दर्ज करें:",
|
||||
"linkExpiry": "साइन-इन लिंक 24 घंटे तक काम करता है और इसे केवल एक बार उपयोग किया जा सकता है।",
|
||||
"orLink": "या कोड छोड़ें और एक क्लिक में साइन इन करें:",
|
||||
"signInButton": "साइन इन करें",
|
||||
"subject": "आपका {brand_name} साइन-इन कोड: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "क्या तुम खुद लिरिक्स रिव्यू नहीं करना चाहते? Made For You चुनो और मैं तुम्हारे लिए पूरा वीडियो बना दूंगा, जो 24 घंटे के अंदर डिलीवर हो जाएगा:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Evo tvog koda za prijavu na stranicu pjevača za karaoke večer. Upiši ga na stranicu koju koristiš:",
|
||||
"subject": "Tvoj Nomad Karaoke kod: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Ovaj kod istječe za {minutes} minuta i može se upotrijebiti samo jednom.",
|
||||
"greeting": "Pozdrav,",
|
||||
"ignoreNote": "Ako niste zatražili ovu e-poruku, možete je slobodno zanemariti.",
|
||||
"intro": "Unesite ovaj kod na stranici za prijavu da biste nastavili:",
|
||||
"linkExpiry": "Poveznica za prijavu vrijedi 24 sata i može se upotrijebiti samo jednom.",
|
||||
"orLink": "Ili preskočite kod i prijavite se jednim klikom:",
|
||||
"signInButton": "Prijava",
|
||||
"subject": "Vaš kod za prijavu u {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Ne želiš samostalno pregledavati tekst? Odaberi Made For You i napravit ću cijeli video za tebe, uz isporuku unutar 24 sata:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Itt a kódod, amivel bejelentkezhetsz a karaoke est énekesi oldalán. Írd be arra az oldalra, amit éppen használsz:",
|
||||
"subject": "A Nomad Karaoke kódod: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Ez a kód {minutes} perc múlva lejár, és csak egyszer használható.",
|
||||
"greeting": "Szia,",
|
||||
"ignoreNote": "Ha nem te kérted ezt az e-mailt, nyugodtan figyelmen kívül hagyhatod.",
|
||||
"intro": "A folytatáshoz írd be ezt a kódot a bejelentkezési oldalon:",
|
||||
"linkExpiry": "A bejelentkezési link 24 óráig érvényes, és csak egyszer használható.",
|
||||
"orLink": "Vagy hagyd ki a kódot, és jelentkezz be egy kattintással:",
|
||||
"signInButton": "Bejelentkezés",
|
||||
"subject": "{brand_name} bejelentkezési kódod: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Inkább nem bajlódnál a dalszöveg ellenőrzésével? Válaszd a Made For You lehetőséget, és az egész videót elkészítem neked 24 órán belül:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Berikut adalah kode Anda untuk masuk di halaman penyanyi malam karaoke. Masukkan kode ini ke halaman yang sedang Anda gunakan:",
|
||||
"subject": "Kode Nomad Karaoke Anda: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Kode ini kedaluwarsa dalam {minutes} menit dan hanya dapat digunakan satu kali.",
|
||||
"greeting": "Halo,",
|
||||
"ignoreNote": "Jika Anda tidak meminta email ini, Anda dapat mengabaikannya dengan aman.",
|
||||
"intro": "Masukkan kode ini di halaman masuk untuk melanjutkan:",
|
||||
"linkExpiry": "Tautan masuk berlaku selama 24 jam dan hanya dapat digunakan satu kali.",
|
||||
"orLink": "Atau lewati kode dan masuk dengan satu klik:",
|
||||
"signInButton": "Masuk",
|
||||
"subject": "Kode masuk {brand_name} Anda: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Tidak ingin memeriksa liriknya sendiri? Pilih Made For You dan saya akan membuatkan seluruh videonya untuk Anda, selesai dalam 24 jam:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Ecco il tuo codice per accedere alla pagina dei cantanti della serata karaoke. Inseriscilo nella pagina che stai utilizzando:",
|
||||
"subject": "Il tuo codice Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Questo codice scade tra {minutes} minuti e può essere usato una sola volta.",
|
||||
"greeting": "Ciao,",
|
||||
"ignoreNote": "Se non hai richiesto questa email, puoi ignorarla tranquillamente.",
|
||||
"intro": "Inserisci questo codice nella pagina di accesso per continuare:",
|
||||
"linkExpiry": "Il link di accesso è valido per 24 ore e può essere usato una sola volta.",
|
||||
"orLink": "Oppure salta il codice e accedi con un clic:",
|
||||
"signInButton": "Accedi",
|
||||
"subject": "Il tuo codice di accesso a {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Preferisci non occuparti della revisione del testo? Scegli Made For You e creerò l'intero video al posto tuo, con consegna entro 24 ore:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "カラオケナイトのシンガーページにサインインするためのコードです。ご利用のページに入力してください:",
|
||||
"subject": "Nomad Karaokeのコード: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "このコードの有効期限は {minutes} 分で、1 回のみ使用できます。",
|
||||
"greeting": "こんにちは。",
|
||||
"ignoreNote": "このメールに心当たりがない場合は、無視していただいて問題ありません。",
|
||||
"intro": "続行するには、サインインページでこのコードを入力してください:",
|
||||
"linkExpiry": "サインインリンクの有効期間は 24 時間で、1 回のみ使用できます。",
|
||||
"orLink": "または、コードを使わずにワンクリックでサインイン:",
|
||||
"signInButton": "サインイン",
|
||||
"subject": "{brand_name} のサインインコード: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "ご自身での歌詞チェックをお手軽に済ませたいですか?「Made For You」をお選びいただければ、動画をまるごと作成して24時間以内にお届けします:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "가라오케 나이트 가수 페이지에 로그인하기 위한 코드입니다. 사용 중인 페이지에 다음 코드를 입력해 주세요:",
|
||||
"subject": "Nomad Karaoke 코드: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "이 코드는 {minutes}분 후에 만료되며 한 번만 사용할 수 있습니다.",
|
||||
"greeting": "안녕하세요,",
|
||||
"ignoreNote": "이 이메일을 요청하지 않으셨다면 무시하셔도 됩니다.",
|
||||
"intro": "계속하려면 로그인 페이지에 이 코드를 입력하세요:",
|
||||
"linkExpiry": "로그인 링크는 24시간 동안 유효하며 한 번만 사용할 수 있습니다.",
|
||||
"orLink": "또는 코드 없이 클릭 한 번으로 로그인하세요:",
|
||||
"signInButton": "로그인",
|
||||
"subject": "{brand_name} 로그인 코드: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "가사를 직접 검토하기 번거로우신가요? Made For You를 선택하시면 24시간 이내에 전체 비디오를 대신 제작해 전달해 드립니다:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Berikut ialah kod anda untuk log masuk ke halaman penyanyi malam karaoke. Taipkan kod ini ke dalam halaman yang anda sedang gunakan:",
|
||||
"subject": "Kod Nomad Karaoke anda: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Kod ini tamat tempoh dalam {minutes} minit dan hanya boleh digunakan sekali.",
|
||||
"greeting": "Hai,",
|
||||
"ignoreNote": "Jika anda tidak meminta e-mel ini, anda boleh mengabaikannya dengan selamat.",
|
||||
"intro": "Masukkan kod ini di halaman log masuk untuk meneruskan:",
|
||||
"linkExpiry": "Pautan log masuk berfungsi selama 24 jam dan hanya boleh digunakan sekali.",
|
||||
"orLink": "Atau langkau kod dan log masuk dengan satu klik:",
|
||||
"signInButton": "Log Masuk",
|
||||
"subject": "Kod log masuk {brand_name} anda: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Tidak mahu menyemak lirik sendiri? Pilih Made For You dan saya akan siapkan keseluruhan video untuk anda, dihantar dalam masa 24 jam:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Her er koden din for å logge inn på sanger-siden for karaokekvelden. Skriv den inn på siden du bruker:",
|
||||
"subject": "Din Nomad Karaoke-kode: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Denne koden utløper om {minutes} minutter og kan bare brukes én gang.",
|
||||
"greeting": "Hei,",
|
||||
"ignoreNote": "Hvis du ikke ba om denne e-posten, kan du trygt se bort fra den.",
|
||||
"intro": "Skriv inn denne koden på påloggingssiden for å fortsette:",
|
||||
"linkExpiry": "Påloggingslenken virker i 24 timer og kan bare brukes én gang.",
|
||||
"orLink": "Eller hopp over koden og logg på med ett klikk:",
|
||||
"signInButton": "Logg på",
|
||||
"subject": "Din påloggingskode for {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Vil du helst slippe å gå gjennom teksten selv? Velg Made For You, så lager jeg hele videoen for deg, levert innen 24 timer:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Hier is je code om in te loggen op de zangerspagina van de karaoke-avond. Typ deze in op de pagina die je gebruikt:",
|
||||
"subject": "Je Nomad Karaoke code: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Deze code verloopt over {minutes} minuten en kan maar één keer worden gebruikt.",
|
||||
"greeting": "Hallo,",
|
||||
"ignoreNote": "Als je deze e-mail niet hebt aangevraagd, kun je hem gerust negeren.",
|
||||
"intro": "Voer deze code in op de inlogpagina om verder te gaan:",
|
||||
"linkExpiry": "De inloglink werkt 24 uur en kan maar één keer worden gebruikt.",
|
||||
"orLink": "Of sla de code over en log in met één klik:",
|
||||
"signInButton": "Inloggen",
|
||||
"subject": "Je {brand_name}-inlogcode: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Liever niet zelf de songtekst controleren? Kies Made For You en ik maak de hele video voor je, geleverd binnen 24 uur:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Oto Twój kod do zalogowania się na stronie wokalisty wieczoru karaoke. Wpisz go na stronie, z której korzystasz:",
|
||||
"subject": "Twój kod Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Ten kod wygaśnie za {minutes} min i można go użyć tylko raz.",
|
||||
"greeting": "Cześć,",
|
||||
"ignoreNote": "Jeśli nie prosiłeś(-aś) o tę wiadomość, możesz ją bezpiecznie zignorować.",
|
||||
"intro": "Wpisz ten kod na stronie logowania, aby kontynuować:",
|
||||
"linkExpiry": "Link do logowania działa przez 24 godziny i można go użyć tylko raz.",
|
||||
"orLink": "Albo pomiń kod i zaloguj się jednym kliknięciem:",
|
||||
"signInButton": "Zaloguj się",
|
||||
"subject": "Twój kod logowania do {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Wolisz nie sprawdzać tekstu samodzielnie? Wybierz Made For You, a przygotuję całe wideo za Ciebie – gotowe w ciągu 24 godzin:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Aqui está o seu código para entrar na página do cantor da noite de karaokê. Digite-o na página que você está usando:",
|
||||
"subject": "Seu código do Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Este código expira em {minutes} minutos e só pode ser usado uma vez.",
|
||||
"greeting": "Olá,",
|
||||
"ignoreNote": "Se você não solicitou este e-mail, pode ignorá-lo com segurança.",
|
||||
"intro": "Digite este código na página de login para continuar:",
|
||||
"linkExpiry": "O link de acesso funciona por 24 horas e só pode ser usado uma vez.",
|
||||
"orLink": "Ou pule o código e entre com um clique:",
|
||||
"signInButton": "Entrar",
|
||||
"subject": "Seu código de acesso ao {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Prefere não revisar a letra você mesmo? Escolha o Feito Para Você e eu farei o vídeo inteiro para você, entregue em até 24 horas:",
|
||||
"madeForYouLink": "Feito Para Você",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Iată codul tău pentru a te conecta pe pagina cântărețului din seara de karaoke. Introdu-l în pagina pe care o folosești:",
|
||||
"subject": "Codul tău Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Acest cod expiră în {minutes} minute și poate fi folosit o singură dată.",
|
||||
"greeting": "Bună,",
|
||||
"ignoreNote": "Dacă nu ai solicitat acest e-mail, îl poți ignora în siguranță.",
|
||||
"intro": "Introdu acest cod pe pagina de conectare pentru a continua:",
|
||||
"linkExpiry": "Linkul de conectare funcționează timp de 24 de ore și poate fi folosit o singură dată.",
|
||||
"orLink": "Sau sari peste cod și conectează-te cu un singur clic:",
|
||||
"signInButton": "Conectare",
|
||||
"subject": "Codul tău de conectare la {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Preferi să nu verifici tu versurile? Alege Made For You și voi face eu întregul videoclip pentru tine, livrat în 24 de ore:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Вот твой код для входа на страницу певца караоке-вечеринки. Введи его на странице, которую ты используешь:",
|
||||
"subject": "Твой код Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Срок действия кода истекает через {minutes} мин., и его можно использовать только один раз.",
|
||||
"greeting": "Здравствуйте!",
|
||||
"ignoreNote": "Если вы не запрашивали это письмо, просто проигнорируйте его.",
|
||||
"intro": "Введите этот код на странице входа, чтобы продолжить:",
|
||||
"linkExpiry": "Ссылка для входа действует 24 часа, и её можно использовать только один раз.",
|
||||
"orLink": "Или пропустите ввод кода и войдите одним нажатием:",
|
||||
"signInButton": "Войти",
|
||||
"subject": "Ваш код для входа в {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Не хочешь проверять текст самостоятельно? Выбери Made For You, и я сделаю всё видео за тебя и пришлю в течение 24 часов:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Tu je tvoj kód na prihlásenie na stránke speváka pre karaoke večer. Zadaj ho na stránke, ktorú používaš:",
|
||||
"subject": "Tvoj kód pre Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Platnosť tohto kódu vyprší o {minutes} minút a možno ho použiť iba raz.",
|
||||
"greeting": "Dobrý deň,",
|
||||
"ignoreNote": "Ak ste o tento e-mail nežiadali, môžete ho pokojne ignorovať.",
|
||||
"intro": "Ak chcete pokračovať, zadajte tento kód na prihlasovacej stránke:",
|
||||
"linkExpiry": "Prihlasovací odkaz platí 24 hodín a možno ho použiť iba raz.",
|
||||
"orLink": "Alebo kód preskočte a prihláste sa jedným kliknutím:",
|
||||
"signInButton": "Prihlásiť sa",
|
||||
"subject": "Váš prihlasovací kód pre {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Nechce sa ti kontrolovať text piesne? Vyber si možnosť Made For You a celé video ti vytvorím ja, s doručením do 24 hodín:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Här är din kod för att logga in på karaokekvällens sångarsida. Skriv in den på sidan du använder:",
|
||||
"subject": "Din Nomad Karaoke-kod: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Koden går ut om {minutes} minuter och kan bara användas en gång.",
|
||||
"greeting": "Hej,",
|
||||
"ignoreNote": "Om du inte har begärt det här mejlet kan du lugnt ignorera det.",
|
||||
"intro": "Ange den här koden på inloggningssidan för att fortsätta:",
|
||||
"linkExpiry": "Inloggningslänken fungerar i 24 timmar och kan bara användas en gång.",
|
||||
"orLink": "Eller hoppa över koden och logga in med ett klick:",
|
||||
"signInButton": "Logga in",
|
||||
"subject": "Din inloggningskod för {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Föredrar du att slippa granska texten själv? Välj Made For You så skapar jag hela videon åt dig, med leverans inom 24 timmar:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "นี่คือรหัสของคุณสำหรับเข้าสู่ระบบในหน้าสำหรับนักร้องในคืนคาราโอเกะ กรอกรหัสนี้ลงในหน้าที่คุณกำลังใช้งานอยู่:",
|
||||
"subject": "รหัส Nomad Karaoke ของคุณ: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "รหัสนี้จะหมดอายุใน {minutes} นาทีและใช้ได้เพียงครั้งเดียว",
|
||||
"greeting": "สวัสดี",
|
||||
"ignoreNote": "หากคุณไม่ได้ขออีเมลนี้ คุณสามารถเพิกเฉยได้อย่างปลอดภัย",
|
||||
"intro": "ป้อนรหัสนี้ในหน้าลงชื่อเข้าใช้เพื่อดำเนินการต่อ:",
|
||||
"linkExpiry": "ลิงก์ลงชื่อเข้าใช้ใช้งานได้ 24 ชั่วโมงและใช้ได้เพียงครั้งเดียว",
|
||||
"orLink": "หรือข้ามรหัสแล้วลงชื่อเข้าใช้ด้วยการคลิกเดียว:",
|
||||
"signInButton": "ลงชื่อเข้าใช้",
|
||||
"subject": "รหัสลงชื่อเข้าใช้ {brand_name} ของคุณ: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "ไม่อยากตรวจทานเนื้อเพลงด้วยตัวเองใช่ไหม? เลือก Made For You แล้วเราจะสร้างวิดีโอทั้งหมดให้คุณเอง พร้อมส่งมอบภายใน 24 ชั่วโมง:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Narito ang iyong code para mag-sign in sa karaoke night singer page. I-type ito sa page na ginagamit mo:",
|
||||
"subject": "Ang iyong Nomad Karaoke code: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Mag-e-expire ang code na ito sa loob ng {minutes} minuto at isang beses lang magagamit.",
|
||||
"greeting": "Kumusta,",
|
||||
"ignoreNote": "Kung hindi mo hiniling ang email na ito, puwede mo itong balewalain.",
|
||||
"intro": "Ilagay ang code na ito sa sign-in page para magpatuloy:",
|
||||
"linkExpiry": "Gumagana ang sign-in link nang 24 oras at isang beses lang magagamit.",
|
||||
"orLink": "O laktawan ang code at mag-sign in sa isang click:",
|
||||
"signInButton": "Mag-sign In",
|
||||
"subject": "Ang iyong sign-in code sa {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Ayaw mo bang mag-review ng lyrics nang mag-isa? Piliin ang Made For You at ako na ang gagawa ng buong video para sa iyo, ihahatid sa loob ng 24 oras:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "İşte karaoke gecesi şarkıcı sayfasına giriş yapmak için kodun. Bunu kullandığın sayfaya yaz:",
|
||||
"subject": "Nomad Karaoke kodun: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Bu kodun süresi {minutes} dakika içinde dolar ve yalnızca bir kez kullanılabilir.",
|
||||
"greeting": "Merhaba,",
|
||||
"ignoreNote": "Bu e-postayı siz istemediyseniz, güvenle yok sayabilirsiniz.",
|
||||
"intro": "Devam etmek için bu kodu giriş sayfasına girin:",
|
||||
"linkExpiry": "Giriş bağlantısı 24 saat geçerlidir ve yalnızca bir kez kullanılabilir.",
|
||||
"orLink": "Ya da kodu atlayıp tek tıkla giriş yapın:",
|
||||
"signInButton": "Giriş Yap",
|
||||
"subject": "{brand_name} giriş kodunuz: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Şarkı sözü incelemesini kendin yapmak istemiyor musun? Made For You'yu seç, tüm videoyu senin için hazırlayıp 24 saat içinde teslim edeyim:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Ось твій код для входу на сторінку співака караоке-вечірки. Введи його на сторінці, яку ти використовуєш:",
|
||||
"subject": "Твій код Nomad Karaoke: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Термін дії коду спливає через {minutes} хв, і його можна використати лише один раз.",
|
||||
"greeting": "Вітаємо!",
|
||||
"ignoreNote": "Якщо ви не запитували цей лист, просто проігноруйте його.",
|
||||
"intro": "Введіть цей код на сторінці входу, щоб продовжити:",
|
||||
"linkExpiry": "Посилання для входу діє 24 години, і його можна використати лише один раз.",
|
||||
"orLink": "Або пропустіть код і увійдіть одним натисканням:",
|
||||
"signInButton": "Увійти",
|
||||
"subject": "Ваш код для входу в {brand_name}: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Не хочеш перевіряти текст самостійно? Обери Made For You, і я зроблю все відео за тебе з доставкою протягом 24 годин:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "Đây là mã của bạn để đăng nhập vào trang ca sĩ đêm karaoke. Hãy nhập mã này vào trang bạn đang sử dụng:",
|
||||
"subject": "Mã Nomad Karaoke của bạn: {code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "Mã này sẽ hết hạn sau {minutes} phút và chỉ dùng được một lần.",
|
||||
"greeting": "Xin chào,",
|
||||
"ignoreNote": "Nếu bạn không yêu cầu email này, bạn có thể yên tâm bỏ qua.",
|
||||
"intro": "Nhập mã này trên trang đăng nhập để tiếp tục:",
|
||||
"linkExpiry": "Liên kết đăng nhập có hiệu lực trong 24 giờ và chỉ dùng được một lần.",
|
||||
"orLink": "Hoặc bỏ qua mã và đăng nhập chỉ với một lần nhấp:",
|
||||
"signInButton": "Đăng nhập",
|
||||
"subject": "Mã đăng nhập {brand_name} của bạn: {code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "Bạn không muốn tự mình kiểm tra lời bài hát? Hãy chọn Made For You và mình sẽ làm toàn bộ video cho bạn, bàn giao trong vòng 24 giờ:",
|
||||
"madeForYouLink": "Made For You",
|
||||
|
||||
@@ -404,6 +404,16 @@
|
||||
"intro": "这是您登录卡拉OK之夜歌手页面的验证码。请在您正在使用的页面中输入它:",
|
||||
"subject": "您的 Nomad Karaoke 验证码:{code}"
|
||||
},
|
||||
"loginCode": {
|
||||
"expiry": "此验证码将在 {minutes} 分钟后过期,且只能使用一次。",
|
||||
"greeting": "你好,",
|
||||
"ignoreNote": "如果你没有请求此邮件,可以放心忽略。",
|
||||
"intro": "请在登录页面输入此验证码以继续:",
|
||||
"linkExpiry": "登录链接 24 小时内有效,且只能使用一次。",
|
||||
"orLink": "或者跳过验证码,一键登录:",
|
||||
"signInButton": "登录",
|
||||
"subject": "你的 {brand_name} 登录验证码:{code}"
|
||||
},
|
||||
"conversion": {
|
||||
"madeForYouLine": "不想自己校对歌词?选择“为您定制”,我将为您制作整支视频,24小时内交付:",
|
||||
"madeForYouLink": "为您定制",
|
||||
|
||||
@@ -28,6 +28,8 @@ from datetime import datetime, timedelta, timezone
|
||||
from backend.models.job import JobStatus
|
||||
from backend.services.job_manager import JobManager
|
||||
from backend.services.storage_service import StorageService
|
||||
from backend.config import get_settings
|
||||
from backend.services.fake_workers import fake_workers_enabled
|
||||
from backend.services.worker_service import get_worker_service
|
||||
from backend.services.flacfetch_client import (
|
||||
get_flacfetch_client,
|
||||
@@ -452,6 +454,12 @@ async def _download_audio(
|
||||
|
||||
Routes to the appropriate download handler based on source type.
|
||||
"""
|
||||
if fake_workers_enabled(get_settings()):
|
||||
# Local stack: every provider "downloads" the canned fake song.
|
||||
from backend.services.fake_workers.pipeline import fake_download_audio
|
||||
|
||||
return await fake_download_audio(job_id, selected, storage_service)
|
||||
|
||||
if source_name == 'YouTube':
|
||||
return await _download_youtube(job_id, source_id, download_url, selected)
|
||||
|
||||
|
||||
+45
@@ -1652,6 +1652,51 @@ Returns session token, user info, and `credits_granted` (number of welcome credi
|
||||
|
||||
**Admin Login Token**: The same endpoint supports admin login tokens embedded in notification emails. When a made-for-you order is received, the admin notification email includes a link with `?admin_token=TOKEN` that auto-logs the admin into the app. The frontend detects this parameter and calls the verify endpoint to authenticate. Admin tokens expire after 24 hours.
|
||||
|
||||
### Send Sign-In Code
|
||||
|
||||
```http
|
||||
POST /api/users/auth/send-code
|
||||
Content-Type: application/json
|
||||
|
||||
{"email": "user@example.com", "locale": "de", "device_fingerprint": "optional", "referral_code": "optional", "attribution": {}}
|
||||
```
|
||||
|
||||
→ `202 {"status": "sent"}`. Emails a **6-digit code and a regular magic link** (either one signs in). Use it where
|
||||
a link can't bring the user back into the same session, for example the mobile app or another device. Only
|
||||
`email` is required. `locale` picks the email language; if it's missing, `Accept-Language` is used. The other fields
|
||||
work the same as on `POST /auth/magic-link`.
|
||||
|
||||
- The code is stored only as an HMAC (pepper `LOGIN_CODE_PEPPER`; if that's unset, it's derived from the first
|
||||
`ADMIN_TOKENS` entry). It expires after 10 minutes and works once. Collections: `login_codes`, `login_code_uses`.
|
||||
- Throttles per rolling hour: `LOGIN_CODES_PER_EMAIL_PER_HOUR` (default 5) and `LOGIN_CODES_PER_IP_PER_HOUR` (default
|
||||
20; hashed IP, collection `login_code_ip_sends`). Both are checked in the same Firestore transaction as the code
|
||||
write. Over either limit → `429 {"detail": "too_many_codes"}`.
|
||||
- The abuse checks match the magic-link endpoint. A disposable domain → `422`, unless the tenant allowlisted the address. These
|
||||
pretend success (`202`, no email): a blocked email or IP, the new-user per-IP/fingerprint signup cap, or an email outside the
|
||||
tenant's allowed domains. With no email provider in production, or no pepper in production → `503`.
|
||||
- The magic link in the email comes from the same `create_magic_link` machinery as `POST /auth/magic-link`. So, like
|
||||
that endpoint, the **account is created at send time**, with `signup_ip`, fingerprint and attribution recorded.
|
||||
- New users get the welcome-credit AI evaluation precomputed in the background. It's stored on both the code record
|
||||
and the magic-link doc. Test emails, users who already got the credit, and tenant portals skip it.
|
||||
|
||||
### Verify Sign-In Code
|
||||
|
||||
```http
|
||||
POST /api/users/auth/verify-code
|
||||
Content-Type: application/json
|
||||
|
||||
{"email": "user@example.com", "code": "042917"}
|
||||
```
|
||||
|
||||
→ `200` with the same body as `GET /auth/verify` (`VerifyMagicLinkResponse`: `status: "success"`, `session_token`,
|
||||
`user`, `message`, `credits_granted`, `credit_status`, `tenant_subdomain`). The post-verification steps are shared
|
||||
(`complete_verified_login`): welcome credit (not on tenant portals), referral attribution, locale persistence,
|
||||
the session (with the fingerprint recorded at send), and the first-login welcome email.
|
||||
|
||||
Errors: `401 {"detail": "invalid_code"}` (wrong code), `401 {"detail": "expired"}` (no code, expired, superseded
|
||||
or already used), `429 {"detail": "too_many_attempts"}` (the 6th guess burns the code, even if it's right).
|
||||
Requesting a new code invalidates the previous one.
|
||||
|
||||
### Resend Magic Link From Token
|
||||
|
||||
```http
|
||||
|
||||
@@ -62,6 +62,20 @@ upload** (URL jobs require the flacfetch cloud service). Set `MODEL_DIR` to
|
||||
enable local audio separation. See [CONTRIBUTING.md](../CONTRIBUTING.md) for
|
||||
the full walkthrough.
|
||||
|
||||
### Local stack (full app, fake workers)
|
||||
|
||||
```bash
|
||||
scripts/local-stack.sh up --frontend
|
||||
# API http://127.0.0.1:8010 (FAKE_WORKERS=1), web http://127.0.0.1:3010
|
||||
```
|
||||
|
||||
The whole product runs offline: search, job creation, review, credits, fake
|
||||
Stripe checkout, emails (read them at `/api/test/emails`) and downloads. Jobs
|
||||
move through their real states in seconds via in-process fake workers, and
|
||||
`POST /api/test/scenario` seeds users and jobs in any state. This is what the
|
||||
`frontend/e2e/local-stack` Playwright suite and the mobile app's Maestro flows
|
||||
run against. See [TESTING.md § Local stack](TESTING.md#local-stack-real-backend-fake-workers).
|
||||
|
||||
### Frontend
|
||||
|
||||
```bash
|
||||
|
||||
@@ -6,6 +6,13 @@ Key insights for future AI agents working on this codebase.
|
||||
|
||||
---
|
||||
|
||||
## A stateful fake backend finds bugs that static mocks can't (Oct 2026, v0.275.0)
|
||||
|
||||
The first run of the local-stack suite (real API on the emulators, fake workers) found that every job confirmed at "This song is longer than expected" would stall forever. `confirm-duration` moved the job to `separating_stage1`, which can only move on to `separating_stage2`, and the lyrics worker only advances jobs from `PREP_PHASE_STATUSES`. No unit test could see it: each one checked its own half (the route asserted it set `SEPARATING_STAGE1`, and the screens gate tests asserted the prep statuses). Only walking a job across the boundary did. No production job had confirmed a duration yet.
|
||||
|
||||
- **Lesson:** for any new job state or transition, add a scenario or journey to `frontend/e2e/local-stack/`. It costs seconds and runs the real routes and state machine.
|
||||
- **Gotcha for env-flag seams:** check flags with `is True` (see `fake_workers_enabled`). Hundreds of unit tests patch `settings` with a `MagicMock`, and a plain `if settings.fake_workers:` is truthy on a mock, which would silently swap real code for the fakes in those tests.
|
||||
|
||||
## User uploads sat under a 7-day-expiring prefix — inputs are irreplaceable (Oct 2026, v0.268.2)
|
||||
|
||||
Every upload lands in `uploads/{job_id}/`, which had a bucket lifecycle **delete after 7 days**. Only
|
||||
|
||||
@@ -57,6 +57,12 @@
|
||||
|
||||
## Recent Changes
|
||||
|
||||
- **Local E2E stack + email-code sign-in** (2026-10-15, v0.275.0). Plan: [archive/2026-10-15-local-stack-e2e-plan.md](archive/2026-10-15-local-stack-e2e-plan.md).
|
||||
- **Local stack.** `scripts/local-stack.sh up --frontend` runs the real API on the Firestore + GCS emulators with `FAKE_WORKERS=1`. In-process fake workers (`backend/services/fake_workers/`) move jobs through their real states in seconds with small real media; the search, download, Stripe Checkout, email, Secret Manager and encoding-VM seams are faked or skipped. The `/api/test/*` router (only mounted in that mode; startup refuses it in production) seeds scenarios, tunes the fakes per user, exposes captured email and serves a fake checkout page.
|
||||
- **New suite.** `frontend/e2e/local-stack` covers sign-up, make, review, instrumental, download, buying credits, the duration-confirm and failure states, and code sign-in through the real UI and API in about 1 minute. It runs in CI as the required `local-stack-e2e` job. Mobile Maestro flows will use the same stack. See [TESTING.md § Local stack](TESTING.md#local-stack-real-backend-fake-workers).
|
||||
- **Email-code sign-in** for the mobile app (nomad-mobile spec §4.1). `POST /api/users/auth/send-code` emails a 6-digit code plus the usual magic link. `POST /api/users/auth/verify-code` returns the same response as magic-link verify, including the welcome credit. Codes are HMAC-hashed, expire after 10 minutes, allow 5 attempts, are single-use, and sends are limited per email and per IP. The code logic is shared with kjbox (`backend/services/login_code_store.py`). See [API.md](API.md).
|
||||
- **Fix: jobs confirmed at "This song is longer than expected" never reached review.** `confirm-duration` moved the job to `separating_stage1`. That status can only move on to `separating_stage2`, and the lyrics worker only advances jobs from the prep statuses, so every confirmed job would have stalled. It now resumes at `downloading`. No production job had hit this path yet; the local-stack suite found it on its first run.
|
||||
|
||||
- **Encoding worker heals itself after a bad boot** (2026-10-06, v0.266.4). The primary encoding VM booted before its network was up, so `bootstrap.sh` couldn't download `startup.sh` and fell back to the image's `startup-fallback.sh`. That script left out `ENCODING_API_KEY` and `GCE_METADATA_MTLS_MODE=none`, so every render and preview failed with `CERTIFICATE_VERIFY_FAILED` on the metadata server. A Randy Vild render looped in `render_pending_capacity` for an hour until the service was restarted by hand. Fixes:
|
||||
- The worker defaults `GCE_METADATA_MTLS_MODE=none` itself (`gce_encoding/main.py`), so every boot path gets it.
|
||||
- The worker refuses to start when the API key is missing or it can't get GCP credentials, and exits after an infra/auth job failure once idle. systemd then re-runs the bootstrap (`gce_encoding/self_heal.py`; only on worker VMs).
|
||||
|
||||
+69
-2
@@ -100,6 +100,7 @@ test('Delete outputs flow', async ({ page }) => {
|
||||
|----------|-----------|----------|
|
||||
| UI component behavior | Jest unit test | `components/__tests__/` |
|
||||
| UI flow with mocked API | Playwright regression | `e2e/regression/` |
|
||||
| Multi-step journey / job states, real backend, offline | Playwright local stack | `e2e/local-stack/` |
|
||||
| Full flow against real backend | Playwright production | `e2e/production/` |
|
||||
| API endpoint logic | pytest unit test | `backend/tests/` |
|
||||
| Database operations | Emulator test | `backend/tests/emulator/` |
|
||||
@@ -277,6 +278,7 @@ make emulators-stop
|
||||
|
||||
**Locations:**
|
||||
- `frontend/e2e/regression/` - Mocked API tests (CI-safe, fast)
|
||||
- `frontend/e2e/local-stack/` - Real backend on emulators with fake workers (CI-safe, ~1 min) — see [Local stack](#local-stack-real-backend-fake-workers)
|
||||
- `frontend/e2e/production/` - Real backend tests (comprehensive, slower)
|
||||
- `frontend/e2e/manual/` - Tests requiring manual setup (not in CI)
|
||||
|
||||
@@ -297,6 +299,70 @@ make emulators-stop
|
||||
- Require separate dev servers (e.g., LyricsTranscriber frontend on port 5173)
|
||||
- See `frontend/e2e/manual/README.md` for setup instructions
|
||||
|
||||
## Local stack (real backend, fake workers)
|
||||
|
||||
`scripts/local-stack.sh` runs the **real** FastAPI backend on the Firestore + GCS
|
||||
emulators with `FAKE_WORKERS=1`. Every API route, the review UI's data endpoints,
|
||||
credits, emails and downloads run for real. Only the workers are swapped out
|
||||
(`backend/services/fake_workers/`): they walk each job through its real states in
|
||||
seconds, writing small real media (ffmpeg tones, 320x180 video) and a
|
||||
schema-valid `corrections.json` built from the real `CorrectionResult` types.
|
||||
Web Playwright (`frontend/e2e/local-stack/`) and mobile Maestro flows run against
|
||||
it, so both clients hit the same endpoints.
|
||||
|
||||
```bash
|
||||
scripts/local-stack.sh up --frontend # emulators + API :8010 + Next dev :3010
|
||||
make test-local-stack # or: cd frontend && npm run test:e2e:local-stack
|
||||
scripts/local-stack.sh logs backend # tail the API log
|
||||
scripts/local-stack.sh down
|
||||
```
|
||||
|
||||
Requirements: Docker (fake-gcs-server), Java 21 + the gcloud Firestore emulator,
|
||||
ffmpeg, and the poetry env. The Playwright config starts the stack itself if it
|
||||
isn't running (`scripts/local-stack.sh serve` + `next dev`).
|
||||
|
||||
**What is faked, and where.** Each fake lives at one seam, guarded by
|
||||
`fake_workers_enabled()`. That check is strict (`is True`), so a `MagicMock`
|
||||
settings object in a unit test never turns it on.
|
||||
|
||||
| Real thing | Fake | Seam |
|
||||
|------------|------|------|
|
||||
| Cloud Run Jobs / Cloud Tasks workers | in-process threads (`fake_workers/pipeline.py`) | `WorkerService.trigger_*` |
|
||||
| flacfetch / YouTube search | 3 deterministic results (lossless "perfect match", Spotify, YouTube) | `get_audio_search_service()` |
|
||||
| audio download | canned tone song (then the REAL `process_audio_download` continues: duration reconcile etc.) | `audio_download_worker._download_audio` |
|
||||
| Stripe Checkout | `/api/test/fake-checkout/{id}` page; Pay runs the real webhook handler | `StripeService._create_checkout` |
|
||||
| Postmark | console provider + captured outbox | `ConsoleEmailProvider` |
|
||||
| Secret Manager, DeBounce, encoding VM | skipped | `get_secret`, `is_disposable_domain`, warmup |
|
||||
|
||||
The audio worker reuses the real stem upload, OGG transcode and backing-vocals
|
||||
analysis. The review preview video is the real local ffmpeg render. Startup
|
||||
**refuses** `FAKE_WORKERS` in production (`validate_production_config`).
|
||||
|
||||
**Test API** (`/api/test/*`, mounted only with `FAKE_WORKERS`, no auth):
|
||||
|
||||
| Endpoint | Purpose |
|
||||
|----------|---------|
|
||||
| `GET /api/test/status` | readiness, effective config, running fake workers, scenario names |
|
||||
| `POST /api/test/scenario {name, email?, credits?, artist?, title?}` | seed `user`, `job_awaiting_review`, `job_complete`, `job_failed`, `job_awaiting_duration_confirm` → `{email, session_token, credits, job_id?}`. Jobs are made through the real search + create-from-search routes. |
|
||||
| `GET/PUT /api/test/fake-config[?email=]` | knobs: `step_delay_seconds`, `auto_approve`, `fail_at`, `audio_duration_seconds`, `search_result_count`. With `?email=` the knobs apply only to that user's jobs, so tests that each use their own user can run in parallel. |
|
||||
| `GET /api/test/emails?to=` | captured outbound email, newest first, with extracted `links` and 6-digit `codes` |
|
||||
| `POST /api/test/reset` | wipe the emulator Firestore + bucket (keeps seeded themes) |
|
||||
|
||||
**Sign-in in tests:** seed a user and put `session_token` in
|
||||
`localStorage.karaoke_access_token` (`signInAs` in `e2e/local-stack/stack.ts`).
|
||||
Alternatively, drive the real magic link or email code and read it from
|
||||
`/api/test/emails`.
|
||||
|
||||
**Mobile (Maestro / dev builds):** run the stack, then point the app at
|
||||
`http://10.0.2.2:8010` (Android emulator) or `http://127.0.0.1:8010` (iOS
|
||||
simulator). Set `LOCAL_STACK_API_URL` to that address before `up`, so fake-checkout
|
||||
URLs open on the device. Seed state with `curl -X POST .../api/test/scenario`, and read
|
||||
sign-in codes from `/api/test/emails`.
|
||||
|
||||
**Ports:** `LOCAL_STACK_API_PORT` (8010) and `LOCAL_STACK_WEB_PORT` (3010). The
|
||||
emulators use the fixed 8080 / 4443 that `start-emulators.sh` uses, so stop other
|
||||
emulator runs first.
|
||||
|
||||
## Playwright Usage
|
||||
|
||||
### Installation
|
||||
@@ -393,6 +459,7 @@ All PRs must pass these checks (path-filtered for efficiency):
|
||||
| `backend-lint` | Ruff linter | No errors |
|
||||
| `backend-unit-tests` | Backend tests (excludes emulator) | Must pass |
|
||||
| `backend-emulator-tests` | GCP emulator integration | Must pass |
|
||||
| `local-stack-e2e` | Playwright full journeys vs the local stack (backend or frontend changes) | Must pass |
|
||||
| `package-unit-tests` | CLI package tests | 69% coverage |
|
||||
| `package-integration-tests` | CLI integration tests | Must pass |
|
||||
| `frontend-lint` | ESLint (Rules of Hooks) | No errors |
|
||||
@@ -413,8 +480,8 @@ CI intelligently runs only relevant tests based on changed files:
|
||||
|
||||
| Changed Path | Tests Run |
|
||||
|--------------|-----------|
|
||||
| `frontend/` | Frontend lint, unit, E2E |
|
||||
| `backend/` | Backend lint, unit, emulator |
|
||||
| `frontend/` | Frontend lint, unit, E2E, local-stack E2E |
|
||||
| `backend/` | Backend lint, unit, emulator, local-stack E2E |
|
||||
| `karaoke_gen/` | Package unit, integration |
|
||||
| `tests/` | Package tests |
|
||||
| `infrastructure/` | Infrastructure validation |
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
# gen local stack for E2E — plan (2026-10-15)
|
||||
|
||||
Backlog: INBOX "fake gen backend" item → precursor to mobile Phase 1 (Frog #4).
|
||||
Spec ref: nomad-mobile `docs/superpowers/specs/2026-10-14-mobile-phase0-design.md` §4.1.
|
||||
|
||||
## Goal
|
||||
|
||||
One command brings up the **real** gen backend on the Firestore + GCS emulators, with
|
||||
**fake workers** that move jobs through their real states using canned media, a
|
||||
**test-only scenario API**, and **email-code sign-in**. Web Playwright and mobile Maestro
|
||||
full-journey tests both run against it (same endpoints).
|
||||
|
||||
## Pieces
|
||||
|
||||
### 1. `FAKE_WORKERS` mode (backend)
|
||||
- Setting `fake_workers` (`FAKE_WORKERS=1`). Hard-refused when `is_production()` (startup raises).
|
||||
- Seam: every `WorkerService.trigger_*` (audio_download, audio, lyrics, screens,
|
||||
render_video, video) short-circuits to `backend/services/fake_workers/` when on. Fakes run
|
||||
in a daemon thread (own event loop) so they survive sync routes / short-lived loops.
|
||||
- Each fake writes what the real worker writes (status transitions via `JobManager`,
|
||||
`file_urls`, `state_data`, `processing_metadata`) using real tiny media generated once
|
||||
with ffmpeg (tone + "vocals" tone, ~20 s), and a `corrections.json` built from the real
|
||||
`CorrectionResult` dataclasses (so schema drift breaks the fake's unit test, not E2E).
|
||||
- audio_download → `input.audio`, DOWNLOADING, then audio + lyrics.
|
||||
- audio → stems (clean / with_backing / vocals / lead / backing), `backing_vocals_analysis`,
|
||||
`instrumental_options`, `audio_complete`.
|
||||
- lyrics → `lyrics.corrections`, `lyrics_metadata`, `lyrics_complete`, then the real
|
||||
`advance_to_screens_if_ready`.
|
||||
- screens → GENERATING_SCREENS → title/end PNGs → AWAITING_REVIEW, or straight to
|
||||
REVIEW_COMPLETE + render when the scenario says auto-approve.
|
||||
- render_video → RENDERING_VIDEO → `videos.with_vocals` → INSTRUMENTAL_SELECTED → video.
|
||||
- video → GENERATING_VIDEO → ENCODING → PACKAGING → COMPLETE with `finals.*` and
|
||||
`packages.*` (tiny real mp4 / zip) so download-urls works.
|
||||
- Fake audio search: `get_audio_search_service()` returns a `FakeAudioSearchService`
|
||||
(deterministic results for any artist/title).
|
||||
- Review preview video: stub mp4 instead of an ffmpeg render.
|
||||
- Skips: encoding-VM warmup, Secret Manager lookups (no ADC needed), DeBounce / ip-api,
|
||||
credit-eval Gemini (`CREDIT_EVAL_ENABLED=false`).
|
||||
- Per-run behaviour (`/api/test/fake-config`): step delay, review mode
|
||||
(`human` | `auto`), `fail_at` worker, duration for the duration-confirm path.
|
||||
|
||||
### 2. Test-only API `/api/test/*` (router mounted only when `FAKE_WORKERS`)
|
||||
- `POST /api/test/reset`: wipe the emulator Firestore + bucket objects (keeps seed).
|
||||
- `GET/PUT /api/test/fake-config`.
|
||||
- `POST /api/test/scenario {name, email?}`: seeds named states and returns
|
||||
`{session_token, email, job_id?}`. Includes `user` (signed in, N credits), `job_awaiting_review`,
|
||||
`job_complete`, `job_failed`, `job_awaiting_duration_confirm`.
|
||||
- `GET /api/test/emails?to=`: captured outbound mail (the console provider records to
|
||||
Firestore `test_email_outbox`), so tests read the magic link or the 6-digit code.
|
||||
- Fake Stripe: when Stripe isn't configured and fake mode is on, checkout returns
|
||||
`/api/test/fake-checkout/{session_id}`. That page runs the real webhook handler path
|
||||
(`checkout.session.completed`) and redirects to `success_url`.
|
||||
|
||||
### 3. Email-code sign-in (production feature, spec §4.1)
|
||||
- `POST /api/users/auth/send-code {email, locale}` → 202; `POST /api/users/auth/verify-code
|
||||
{email, code}` → `VerifyMagicLinkResponse`-compatible
|
||||
`{status, session_token, user, credits_granted, credit_status, message}`.
|
||||
- The code store is generalized from `KjboxPartnerService` (HMAC-hashed, 10 min TTL,
|
||||
5 attempts → 429, per-email throttle in a transaction, single-use marker) into a reusable
|
||||
`LoginCodeStore(collection prefix)`. kjbox keeps its collections.
|
||||
- Per-IP send limit (rolling hour). The usual magic-link abuse checks apply (disposable, blocked
|
||||
email / IP, signup IP / fingerprint caps for new users). The account is created at verify time.
|
||||
- Email contains the code **and** a magic link (same login, either works).
|
||||
- Pepper: `LOGIN_CODE_PEPPER` env, falling back to a value derived from the admin token.
|
||||
|
||||
### 4. Tooling
|
||||
- `scripts/local-stack.sh up|down|status|logs`: emulators + seed + backend (FAKE_WORKERS,
|
||||
fixed ports) + optional frontend dev server pointed at it (`BACKEND_URL`). It binds
|
||||
0.0.0.0 so the Android emulator (10.0.2.2) and iOS simulator can reach it.
|
||||
- `frontend/playwright.local-stack.config.ts` + `frontend/e2e/local-stack/*.spec.ts`
|
||||
(full journey: sign in → search → create → review lyrics → instrumental → complete →
|
||||
download; buy credits via fake checkout; code sign-in; failure + duration-confirm states).
|
||||
- CI job `local-stack-e2e` (emulator setup from `backend-emulator-tests` + Playwright).
|
||||
- Docs: TESTING.md placement table + section, DEVELOPMENT.md, API.md (new auth endpoints).
|
||||
|
||||
## Testing
|
||||
- Unit: login-code store, send/verify routes, fake media/corrections builders (validate via
|
||||
`CorrectionResult.from_dict`), fake dispatch seam, test router not mounted without the flag,
|
||||
startup refusal in production.
|
||||
- Emulator: fake pipeline drives a job PENDING → COMPLETE with real Firestore/GCS.
|
||||
- E2E: Playwright local-stack suite (above), also in CI.
|
||||
@@ -1,5 +1,6 @@
|
||||
playwright-report-prod/
|
||||
playwright-report-demo/
|
||||
playwright-report-local-stack/
|
||||
|
||||
# Demo video output and raw recordings (large files, not for git)
|
||||
demo-output/
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Email-code sign-in (POST /api/users/auth/send-code + verify-code), the login
|
||||
* the mobile app uses. API-level here: there is no web UI for it yet, and the
|
||||
* mobile Maestro flows drive the same endpoints against the same stack.
|
||||
*/
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { API_URL, uniqueEmail, waitForEmail } from './stack';
|
||||
|
||||
test('a new user signs in with an emailed 6-digit code and gets the welcome credit', async ({ request }) => {
|
||||
const email = uniqueEmail('code');
|
||||
|
||||
const sent = await request.post(`${API_URL}/api/users/auth/send-code`, { data: { email, locale: 'en' } });
|
||||
expect(sent.status()).toBe(202);
|
||||
|
||||
const mail = await waitForEmail(request, email, /.+/);
|
||||
expect(mail.codes, 'one 6-digit code in the email').toHaveLength(1);
|
||||
// The same email also carries a normal magic link.
|
||||
expect(mail.links.some((l) => l.includes('/auth/verify?token='))).toBe(true);
|
||||
|
||||
const wrong = await request.post(`${API_URL}/api/users/auth/verify-code`, {
|
||||
data: { email, code: mail.codes[0] === '000000' ? '111111' : '000000' },
|
||||
});
|
||||
expect(wrong.status()).toBe(401);
|
||||
|
||||
const verified = await request.post(`${API_URL}/api/users/auth/verify-code`, {
|
||||
data: { email, code: mail.codes[0] },
|
||||
});
|
||||
expect(verified.status()).toBe(200);
|
||||
const body = await verified.json();
|
||||
expect(body.session_token).toBeTruthy();
|
||||
expect(body.user.email).toBe(email);
|
||||
expect(body.credits_granted).toBe(1);
|
||||
|
||||
const me = await request.get(`${API_URL}/api/users/me`, {
|
||||
headers: { Authorization: `Bearer ${body.session_token}` },
|
||||
});
|
||||
expect(me.status()).toBe(200);
|
||||
|
||||
// Codes are single-use.
|
||||
const replay = await request.post(`${API_URL}/api/users/auth/verify-code`, {
|
||||
data: { email, code: mail.codes[0] },
|
||||
});
|
||||
expect(replay.status()).toBe(401);
|
||||
});
|
||||
|
||||
test('five wrong guesses lock the code', async ({ request }) => {
|
||||
const email = uniqueEmail('lock');
|
||||
await request.post(`${API_URL}/api/users/auth/send-code`, { data: { email } });
|
||||
const mail = await waitForEmail(request, email, /.+/);
|
||||
const wrongCode = mail.codes[0] === '000000' ? '111111' : '000000';
|
||||
|
||||
const statuses: number[] = [];
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const r = await request.post(`${API_URL}/api/users/auth/verify-code`, { data: { email, code: wrongCode } });
|
||||
statuses.push(r.status());
|
||||
}
|
||||
expect(statuses.slice(0, 5)).toEqual([401, 401, 401, 401, 401]);
|
||||
expect(statuses[5]).toBe(429);
|
||||
|
||||
// Even the right code no longer works once the code is burnt.
|
||||
const right = await request.post(`${API_URL}/api/users/auth/verify-code`, { data: { email, code: mail.codes[0] } });
|
||||
expect([401, 429]).toContain(right.status());
|
||||
});
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* The whole customer journey through the real UI and the real backend, with
|
||||
* fake workers: sign up by magic link → make a video → review lyrics → pick the
|
||||
* instrumental → download the finished video.
|
||||
*/
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { setFakeConfig, uniqueEmail, waitForEmail, waitForJobStatus } from './stack';
|
||||
|
||||
const SONG = { artist: 'Local Stack', title: 'Full Journey' };
|
||||
|
||||
test('new user signs up, makes a karaoke video, reviews it and downloads it', async ({ page, request }) => {
|
||||
const email = uniqueEmail('journey');
|
||||
// Small delay so the job visibly moves through its states; human review.
|
||||
await setFakeConfig(request, email, { step_delay_seconds: 0.3, auto_approve: false });
|
||||
|
||||
await test.step('sign up with a magic link', async () => {
|
||||
await page.goto('/en');
|
||||
await page.getByRole('button', { name: /sign up free/i }).first().click();
|
||||
const dialog = page.getByRole('dialog');
|
||||
await dialog.getByPlaceholder('you@example.com').fill(email);
|
||||
await page.getByRole('button', { name: /send sign-in link/i }).click();
|
||||
await expect(page.getByText(/check your email/i)).toBeVisible();
|
||||
|
||||
const mail = await waitForEmail(request, email, /sign in/i);
|
||||
const link = mail.links.find((l) => l.includes('/auth/verify?token='));
|
||||
expect(link, 'magic link in email').toBeTruthy();
|
||||
await page.goto(link!);
|
||||
await page.getByRole('button', { name: /complete sign-?in/i }).click();
|
||||
|
||||
// New users get the welcome-credit interstitial.
|
||||
await page.getByRole('button', { name: /start creating|go to dashboard|explore the app/i }).click();
|
||||
await page.waitForURL(/\/app/);
|
||||
await expect(page.getByText(/1\s+credit/i).first()).toBeVisible();
|
||||
});
|
||||
|
||||
let jobId = '';
|
||||
await test.step('make a video with the guided flow', async () => {
|
||||
await page.getByTestId('guided-artist-input').fill(SONG.artist);
|
||||
await page.getByTestId('guided-title-input').fill(SONG.title);
|
||||
await page.getByRole('button', { name: /choose audio/i }).click();
|
||||
|
||||
// The fake search's lossless, well-seeded result is the "perfect match".
|
||||
await expect(page.getByTestId('pick-card')).toBeVisible();
|
||||
await page.getByRole('button', { name: /use this audio/i }).click();
|
||||
await page.getByText('Use audio as-is').click();
|
||||
await page.getByRole('button', { name: /keep private/i }).click();
|
||||
await expect(page.getByRole('heading', { name: 'Customize & Create' })).toBeVisible();
|
||||
await page.getByRole('button', { name: /create karaoke video/i }).click();
|
||||
await expect(page.getByText('Job Created')).toBeVisible();
|
||||
|
||||
const idText = (await page.getByTestId('created-job-id').textContent()) || '';
|
||||
jobId = idText.match(/([a-f0-9]{8,})/i)?.[1] || '';
|
||||
expect(jobId, 'job id on the success card').toBeTruthy();
|
||||
});
|
||||
|
||||
const token = await page.evaluate(() => window.localStorage.getItem('karaoke_access_token'));
|
||||
expect(token).toBeTruthy();
|
||||
|
||||
await test.step('job reaches review and the card offers it', async () => {
|
||||
await waitForJobStatus(request, token!, jobId, 'awaiting_review');
|
||||
await page.getByRole('button', { name: /refresh/i }).first().click();
|
||||
await expect(page.getByRole('link', { name: /review lyrics/i }).first()).toBeVisible();
|
||||
});
|
||||
|
||||
await test.step('review lyrics, preview, choose the instrumental', async () => {
|
||||
await page.goto(`/en/app/jobs#/${jobId}/review`);
|
||||
const preview = page.getByRole('button', { name: /preview video/i });
|
||||
await expect(preview).toBeVisible();
|
||||
// The fake lyrics are on the review screen.
|
||||
await expect(page.getByText('fake').first()).toBeVisible();
|
||||
await preview.click();
|
||||
const modal = page.getByRole('dialog');
|
||||
await expect(modal).toBeVisible();
|
||||
// The real local preview render runs against the canned audio.
|
||||
await expect(modal.locator('video')).toBeVisible({ timeout: 60_000 });
|
||||
|
||||
const finish = page.getByRole('button', { name: /proceed to instrumental|complete track/i });
|
||||
await expect(finish).toBeEnabled();
|
||||
const label = (await finish.textContent()) || '';
|
||||
await finish.click();
|
||||
if (!/complete track/i.test(label)) {
|
||||
await expect(page).toHaveURL(new RegExp(`#/${jobId}/instrumental`));
|
||||
await page.locator('.selection-option:has-text("Clean")').first().click();
|
||||
await page.locator('#submit-btn').click();
|
||||
}
|
||||
});
|
||||
|
||||
await test.step('video finishes and downloads work', async () => {
|
||||
await waitForJobStatus(request, token!, jobId, 'complete');
|
||||
await page.goto('/en/app');
|
||||
await page.getByRole('button', { name: /refresh/i }).first().click();
|
||||
const download = page.getByRole('link', { name: /720p/i }).first()
|
||||
.or(page.getByRole('button', { name: /720p/i }).first());
|
||||
await expect(download).toBeVisible();
|
||||
|
||||
const res = await request.get(`/api/jobs/${jobId}/download/finals/lossy_720p_mp4`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
expect(res.status()).toBe(200);
|
||||
expect((await res.body()).length).toBeGreaterThan(10_000);
|
||||
|
||||
const done = await waitForEmail(request, email, /ready/i);
|
||||
expect(done.subject).toContain(SONG.title);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* How the job list presents jobs in each state, using seeded scenarios: a job
|
||||
* made through the real routes and walked by the fake workers to the state.
|
||||
*/
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { scenario, signInAs, waitForJobStatus } from './stack';
|
||||
|
||||
function jobCard(page: import('@playwright/test').Page, jobId: string) {
|
||||
return page.locator('[class*="rounded-lg"][class*="border"]').filter({ hasText: jobId.slice(0, 8) }).first();
|
||||
}
|
||||
|
||||
test('a song longer than estimated asks to confirm the extra credit, then carries on', async ({ page, request }) => {
|
||||
const user = await scenario(request, 'job_awaiting_duration_confirm', { credits: 2 });
|
||||
await signInAs(page, user);
|
||||
await page.goto('/en/app');
|
||||
|
||||
const card = jobCard(page, user.job_id!);
|
||||
await card.getByRole('button', { name: /confirm cost/i }).click();
|
||||
const dialog = page.getByRole('dialog');
|
||||
await expect(dialog.getByText(/longer than expected/i)).toBeVisible();
|
||||
await dialog.getByRole('button', { name: /confirm & start/i }).click();
|
||||
|
||||
await waitForJobStatus(request, user.session_token, user.job_id!, 'awaiting_review', 120_000);
|
||||
const me = await (await request.get('/api/users/me', {
|
||||
headers: { Authorization: `Bearer ${user.session_token}` },
|
||||
})).json();
|
||||
expect((me.user ?? me).credits).toBe(0); // 1 at creation + 1 for the extra 10 minutes
|
||||
});
|
||||
|
||||
test('a failed job says so on its card', async ({ page, request }) => {
|
||||
const user = await scenario(request, 'job_failed');
|
||||
await signInAs(page, user);
|
||||
await page.goto('/en/app');
|
||||
await expect(jobCard(page, user.job_id!).getByText(/failed/i).first()).toBeVisible();
|
||||
});
|
||||
|
||||
test('a finished job offers its downloads', async ({ page, request }) => {
|
||||
const user = await scenario(request, 'job_complete');
|
||||
await signInAs(page, user);
|
||||
await page.goto('/en/app');
|
||||
const card = jobCard(page, user.job_id!);
|
||||
await expect(card.getByText(/720p/i).first()).toBeVisible();
|
||||
});
|
||||
|
||||
test('a job awaiting review opens straight into the lyrics review screen', async ({ page, request }) => {
|
||||
const user = await scenario(request, 'job_awaiting_review');
|
||||
await signInAs(page, user);
|
||||
await page.goto(`/en/app/jobs#/${user.job_id}/review`);
|
||||
await expect(page.getByRole('button', { name: /preview video/i })).toBeVisible();
|
||||
await expect(page.getByText(/This is a fake song for testing/i).first()).toBeVisible();
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Buying credits through the (fake) Stripe Checkout. The checkout page is served
|
||||
* by the local stack, and paying it runs the same completion handler as the real
|
||||
* Stripe webhook before redirecting to /payment/success.
|
||||
*/
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { scenario, signInAs, waitForEmail } from './stack';
|
||||
|
||||
test('user with no credits buys a pack and sees the new balance', async ({ page, request }) => {
|
||||
const user = await scenario(request, 'user', { credits: 0 });
|
||||
await signInAs(page, user);
|
||||
|
||||
await page.goto('/en/app?buy=1');
|
||||
const dialog = page.getByRole('dialog');
|
||||
await expect(dialog).toBeVisible();
|
||||
await dialog.getByRole('button', { name: /^3\s*credits/i }).click();
|
||||
await dialog.getByRole('button', { name: /continue to payment/i }).click();
|
||||
|
||||
await page.waitForURL(/\/api\/test\/fake-checkout\//);
|
||||
await expect(page.getByTestId('fake-checkout-product')).toHaveText(/3 Karaoke Credits/);
|
||||
await page.getByTestId('fake-checkout-pay').click();
|
||||
|
||||
await page.waitForURL(/\/payment\/success/);
|
||||
await expect(page.getByRole('heading', { name: /payment successful/i })).toBeVisible();
|
||||
await expect(page.getByText(/credits available/i)).toBeVisible();
|
||||
await expect(page.getByText('3', { exact: true })).toBeVisible();
|
||||
|
||||
await waitForEmail(request, user.email, /credits added/i);
|
||||
});
|
||||
|
||||
test('cancelling checkout leaves the balance unchanged', async ({ page, request }) => {
|
||||
const user = await scenario(request, 'user', { credits: 0 });
|
||||
await signInAs(page, user);
|
||||
|
||||
await page.goto('/en/app?buy=1');
|
||||
const dialog = page.getByRole('dialog');
|
||||
await dialog.getByRole('button', { name: /^1\s*credit/i }).click();
|
||||
await dialog.getByRole('button', { name: /continue to payment/i }).click();
|
||||
await page.waitForURL(/\/api\/test\/fake-checkout\//);
|
||||
await page.getByTestId('fake-checkout-cancel').click();
|
||||
|
||||
await page.waitForURL(/cancelled=true/);
|
||||
const me = await request.get('/api/users/me', { headers: { Authorization: `Bearer ${user.session_token}` } });
|
||||
const body = await me.json();
|
||||
expect((body.user ?? body).credits).toBe(0);
|
||||
});
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Client for the local stack's test-only API (/api/test/*, backend/api/routes/test_support.py).
|
||||
*/
|
||||
import { APIRequestContext, Page, expect } from '@playwright/test';
|
||||
|
||||
export const API_URL = process.env.LOCAL_STACK_API_URL || 'http://127.0.0.1:8010';
|
||||
|
||||
export interface SeededUser {
|
||||
email: string;
|
||||
session_token: string;
|
||||
credits: number;
|
||||
job_id?: string;
|
||||
}
|
||||
|
||||
export interface FakeConfig {
|
||||
step_delay_seconds?: number;
|
||||
auto_approve?: boolean;
|
||||
fail_at?: 'audio_download' | 'audio' | 'lyrics' | 'screens' | 'render_video' | 'video' | null;
|
||||
audio_duration_seconds?: number;
|
||||
search_result_count?: number;
|
||||
}
|
||||
|
||||
export interface CapturedEmail {
|
||||
to: string;
|
||||
subject: string;
|
||||
links: string[];
|
||||
codes: string[];
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export function uniqueEmail(tag: string): string {
|
||||
return `e2e-${tag}-${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}@example.com`;
|
||||
}
|
||||
|
||||
async function ok<T>(response: Awaited<ReturnType<APIRequestContext['get']>>): Promise<T> {
|
||||
if (!response.ok()) {
|
||||
throw new Error(`${response.url()} -> ${response.status()} ${await response.text()}`);
|
||||
}
|
||||
return (await response.json()) as T;
|
||||
}
|
||||
|
||||
export async function scenario(
|
||||
request: APIRequestContext,
|
||||
name: string,
|
||||
opts: { email?: string; credits?: number; artist?: string; title?: string } = {},
|
||||
): Promise<SeededUser> {
|
||||
return ok(await request.post(`${API_URL}/api/test/scenario`, { data: { name, ...opts }, timeout: 150_000 }));
|
||||
}
|
||||
|
||||
/** Per-user fake-worker overrides (they only affect jobs owned by `email`). */
|
||||
export async function setFakeConfig(request: APIRequestContext, email: string, config: FakeConfig): Promise<void> {
|
||||
await ok(await request.put(`${API_URL}/api/test/fake-config?email=${encodeURIComponent(email)}`, { data: config }));
|
||||
}
|
||||
|
||||
export async function emailsTo(request: APIRequestContext, to: string): Promise<CapturedEmail[]> {
|
||||
const body = await ok<{ emails: CapturedEmail[] }>(
|
||||
await request.get(`${API_URL}/api/test/emails?to=${encodeURIComponent(to)}`),
|
||||
);
|
||||
return body.emails;
|
||||
}
|
||||
|
||||
/** Wait for the newest email to `to` whose subject matches, and return it. */
|
||||
export async function waitForEmail(
|
||||
request: APIRequestContext,
|
||||
to: string,
|
||||
subject: RegExp,
|
||||
timeoutMs = 30_000,
|
||||
): Promise<CapturedEmail> {
|
||||
let found: CapturedEmail | undefined;
|
||||
await expect
|
||||
.poll(async () => {
|
||||
found = (await emailsTo(request, to)).find((e) => subject.test(e.subject));
|
||||
return !!found;
|
||||
}, { timeout: timeoutMs, message: `email to ${to} matching ${subject}` })
|
||||
.toBe(true);
|
||||
return found!;
|
||||
}
|
||||
|
||||
export async function jobStatus(request: APIRequestContext, token: string, jobId: string): Promise<string> {
|
||||
const job = await ok<{ status: string }>(
|
||||
await request.get(`${API_URL}/api/jobs/${jobId}`, { headers: { Authorization: `Bearer ${token}` } }),
|
||||
);
|
||||
return job.status;
|
||||
}
|
||||
|
||||
export async function waitForJobStatus(
|
||||
request: APIRequestContext,
|
||||
token: string,
|
||||
jobId: string,
|
||||
status: string,
|
||||
timeoutMs = 60_000,
|
||||
): Promise<void> {
|
||||
await expect
|
||||
.poll(() => jobStatus(request, token, jobId), { timeout: timeoutMs, message: `job ${jobId} -> ${status}` })
|
||||
.toBe(status);
|
||||
}
|
||||
|
||||
/** Sign the browser in as a seeded user (token in localStorage before any page script runs). */
|
||||
export async function signInAs(page: Page, user: SeededUser): Promise<void> {
|
||||
await page.addInitScript((token) => {
|
||||
window.localStorage.setItem('karaoke_access_token', token);
|
||||
}, user.session_token);
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
"test:e2e:ci": "playwright test --reporter=list",
|
||||
"test:e2e:record": "playwright test --config=playwright.record.config.ts",
|
||||
"test:e2e:prod": "playwright test --config=playwright.production.config.ts",
|
||||
"test:e2e:local-stack": "playwright test --config=playwright.local-stack.config.ts",
|
||||
"test:e2e:prod:headed": "playwright test --config=playwright.production.config.ts --headed",
|
||||
"test:e2e:prod:debug": "playwright test --config=playwright.production.config.ts --debug",
|
||||
"test:e2e:demo": "playwright test --config=playwright.demo.config.ts",
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { defineConfig, devices } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Full-journey E2E against the gen LOCAL STACK: the real backend on the
|
||||
* Firestore + GCS emulators with fake workers (scripts/local-stack.sh). Unlike
|
||||
* e2e/regression (static per-test API mocks), every request here hits real
|
||||
* routes, and jobs move through their real states in seconds.
|
||||
*
|
||||
* npm run test:e2e:local-stack # starts the stack if it isn't up
|
||||
* scripts/local-stack.sh up --frontend # or keep it running between runs
|
||||
*
|
||||
* Ports: LOCAL_STACK_API_PORT (8010), LOCAL_STACK_WEB_PORT (3010).
|
||||
* See docs/TESTING.md § "Local stack".
|
||||
*/
|
||||
const API_PORT = process.env.LOCAL_STACK_API_PORT || '8010';
|
||||
const WEB_PORT = process.env.LOCAL_STACK_WEB_PORT || '3010';
|
||||
const API_URL = `http://127.0.0.1:${API_PORT}`;
|
||||
const WEB_URL = `http://127.0.0.1:${WEB_PORT}`;
|
||||
|
||||
process.env.LOCAL_STACK_API_URL = process.env.LOCAL_STACK_API_URL || API_URL;
|
||||
|
||||
export default defineConfig({
|
||||
testDir: './e2e/local-stack',
|
||||
// Every test creates its own user, and fake-worker knobs are per user, so
|
||||
// tests are independent. One worker keeps the Next dev server's first-compile
|
||||
// cost from timing out parallel tests; raise it once the pages are warm.
|
||||
fullyParallel: true,
|
||||
workers: process.env.LOCAL_STACK_WORKERS ? Number(process.env.LOCAL_STACK_WORKERS) : 1,
|
||||
forbidOnly: !!process.env.CI,
|
||||
retries: process.env.CI ? 1 : 0,
|
||||
reporter: [['html', { open: 'never', outputFolder: 'playwright-report-local-stack' }], ['list']],
|
||||
timeout: 180_000,
|
||||
expect: { timeout: 30_000 },
|
||||
use: {
|
||||
baseURL: WEB_URL,
|
||||
trace: 'retain-on-failure',
|
||||
screenshot: 'only-on-failure',
|
||||
video: 'retain-on-failure',
|
||||
actionTimeout: 30_000,
|
||||
},
|
||||
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
|
||||
webServer: [
|
||||
{
|
||||
command: '../scripts/local-stack.sh serve',
|
||||
url: `${API_URL}/api/test/status`,
|
||||
reuseExistingServer: !process.env.CI,
|
||||
timeout: 180_000,
|
||||
},
|
||||
{
|
||||
command: `npx next dev -p ${WEB_PORT} -H 127.0.0.1`,
|
||||
url: WEB_URL,
|
||||
reuseExistingServer: !process.env.CI,
|
||||
timeout: 180_000,
|
||||
env: { BACKEND_URL: API_URL },
|
||||
},
|
||||
],
|
||||
});
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "karaoke-gen"
|
||||
version = "0.274.0"
|
||||
version = "0.275.0"
|
||||
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
|
||||
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
|
||||
license = "MIT"
|
||||
|
||||
Executable
+175
@@ -0,0 +1,175 @@
|
||||
#!/bin/bash
|
||||
# gen "local stack" for E2E: the REAL backend on the Firestore + GCS emulators, with
|
||||
# fake workers (FAKE_WORKERS=1) that walk jobs through their real states, the
|
||||
# /api/test/* router, captured email and fake Stripe checkout. Web Playwright
|
||||
# (frontend/e2e/local-stack) and mobile Maestro flows run against it.
|
||||
#
|
||||
# Usage:
|
||||
# scripts/local-stack.sh up [--frontend] start emulators + backend (+ Next dev server)
|
||||
# scripts/local-stack.sh down stop everything
|
||||
# scripts/local-stack.sh status show what's running
|
||||
# scripts/local-stack.sh logs [backend|frontend]
|
||||
# scripts/local-stack.sh serve emulators + backend in the FOREGROUND (Playwright webServer, CI)
|
||||
# scripts/local-stack.sh env print the backend env (for running uvicorn yourself)
|
||||
#
|
||||
# Ports (override via env): LOCAL_STACK_API_PORT (8010), LOCAL_STACK_WEB_PORT (3010).
|
||||
# The backend binds 0.0.0.0 so the Android emulator (10.0.2.2) and iOS simulator
|
||||
# can reach it; set LOCAL_STACK_API_URL to the address devices should use.
|
||||
# See docs/TESTING.md § "Local stack".
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
STATE_DIR="$PROJECT_ROOT/.local-stack"
|
||||
mkdir -p "$STATE_DIR"
|
||||
|
||||
API_PORT="${LOCAL_STACK_API_PORT:-8010}"
|
||||
WEB_PORT="${LOCAL_STACK_WEB_PORT:-3010}"
|
||||
API_URL="${LOCAL_STACK_API_URL:-http://127.0.0.1:$API_PORT}"
|
||||
WEB_URL="${LOCAL_STACK_WEB_URL:-http://127.0.0.1:$WEB_PORT}"
|
||||
ADMIN_TOKEN="${LOCAL_STACK_ADMIN_TOKEN:-local-stack-admin}"
|
||||
|
||||
backend_env() {
|
||||
cat <<EOF
|
||||
FAKE_WORKERS=1
|
||||
ENVIRONMENT=development
|
||||
FIRESTORE_EMULATOR_HOST=127.0.0.1:8080
|
||||
STORAGE_EMULATOR_HOST=http://127.0.0.1:4443
|
||||
GOOGLE_CLOUD_PROJECT=test-project
|
||||
GCS_BUCKET_NAME=test-bucket
|
||||
FIRESTORE_COLLECTION=jobs
|
||||
ADMIN_TOKENS=$ADMIN_TOKEN
|
||||
FRONTEND_URL=$WEB_URL
|
||||
LOCAL_STACK_API_URL=$API_URL
|
||||
PORT=$API_PORT
|
||||
ENABLE_CLOUD_TASKS=false
|
||||
CREDIT_EVAL_ENABLED=false
|
||||
REVIEW_AUDIO_PROXY=1
|
||||
MAX_SIGNUPS_PER_IP=100000
|
||||
BOARD_MAX_SIGNUPS_PER_IP=100000
|
||||
LOGIN_CODES_PER_IP_PER_HOUR=100000
|
||||
POSTMARK_SERVER_TOKEN=
|
||||
STRIPE_SECRET_KEY=
|
||||
EOF
|
||||
}
|
||||
|
||||
export_backend_env() {
|
||||
while IFS= read -r line; do
|
||||
export "${line?}"
|
||||
done < <(backend_env)
|
||||
}
|
||||
|
||||
is_running() {
|
||||
local pidfile="$STATE_DIR/$1.pid"
|
||||
[[ -f "$pidfile" ]] && kill -0 "$(cat "$pidfile")" 2>/dev/null
|
||||
}
|
||||
|
||||
wait_for() {
|
||||
local url="$1" name="$2" tries="${3:-90}"
|
||||
for _ in $(seq 1 "$tries"); do
|
||||
if curl -sf "$url" >/dev/null 2>&1; then
|
||||
echo "✅ $name ready ($url)"
|
||||
return 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "❌ $name did not become ready at $url"
|
||||
return 1
|
||||
}
|
||||
|
||||
start_backend() {
|
||||
if is_running backend; then
|
||||
echo "⚠️ backend already running (pid $(cat "$STATE_DIR/backend.pid"))"
|
||||
return
|
||||
fi
|
||||
export_backend_env
|
||||
(cd "$PROJECT_ROOT" && poetry run python "$SCRIPT_DIR/seed-local-data.py")
|
||||
echo "🌐 Starting backend on :$API_PORT (FAKE_WORKERS=1)"
|
||||
(
|
||||
cd "$PROJECT_ROOT"
|
||||
nohup poetry run uvicorn backend.main:app --host 0.0.0.0 --port "$API_PORT" \
|
||||
> "$STATE_DIR/backend.log" 2>&1 &
|
||||
echo $! > "$STATE_DIR/backend.pid"
|
||||
)
|
||||
if ! wait_for "http://127.0.0.1:$API_PORT/api/test/status" backend 120; then
|
||||
tail -40 "$STATE_DIR/backend.log"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
start_frontend() {
|
||||
if is_running frontend; then
|
||||
echo "⚠️ frontend already running (pid $(cat "$STATE_DIR/frontend.pid"))"
|
||||
return
|
||||
fi
|
||||
echo "🖥️ Starting Next dev server on :$WEB_PORT (BACKEND_URL=$API_URL)"
|
||||
(
|
||||
cd "$PROJECT_ROOT/frontend"
|
||||
BACKEND_URL="http://127.0.0.1:$API_PORT" nohup npx next dev -p "$WEB_PORT" -H 127.0.0.1 \
|
||||
> "$STATE_DIR/frontend.log" 2>&1 &
|
||||
echo $! > "$STATE_DIR/frontend.pid"
|
||||
)
|
||||
wait_for "$WEB_URL/" frontend 180
|
||||
}
|
||||
|
||||
stop_pid() {
|
||||
local name="$1" pidfile="$STATE_DIR/$1.pid"
|
||||
if is_running "$name"; then
|
||||
local pid
|
||||
pid="$(cat "$pidfile")"
|
||||
# Kill the process group too (poetry/npx spawn children).
|
||||
pkill -TERM -P "$pid" 2>/dev/null || true
|
||||
kill "$pid" 2>/dev/null || true
|
||||
echo "🛑 stopped $name (pid $pid)"
|
||||
fi
|
||||
rm -f "$pidfile"
|
||||
}
|
||||
|
||||
cmd="${1:-}"
|
||||
shift || true
|
||||
case "$cmd" in
|
||||
up)
|
||||
"$SCRIPT_DIR/start-emulators.sh"
|
||||
start_backend
|
||||
if [[ "${1:-}" == "--frontend" ]]; then
|
||||
start_frontend
|
||||
fi
|
||||
echo ""
|
||||
echo "Local stack up:"
|
||||
echo " API $API_URL (docs: $API_URL/docs, test API: $API_URL/api/test/status)"
|
||||
is_running frontend && echo " Web $WEB_URL"
|
||||
echo " Admin X-Admin-Token: $ADMIN_TOKEN"
|
||||
echo " Logs scripts/local-stack.sh logs backend"
|
||||
;;
|
||||
down)
|
||||
stop_pid frontend
|
||||
stop_pid backend
|
||||
"$SCRIPT_DIR/stop-emulators.sh" || true
|
||||
;;
|
||||
serve)
|
||||
"$SCRIPT_DIR/start-emulators.sh"
|
||||
export_backend_env
|
||||
(cd "$PROJECT_ROOT" && poetry run python "$SCRIPT_DIR/seed-local-data.py")
|
||||
cd "$PROJECT_ROOT"
|
||||
echo "🌐 Backend on :$API_PORT (FAKE_WORKERS=1), logging to .local-stack/backend.log"
|
||||
exec poetry run uvicorn backend.main:app --host 0.0.0.0 --port "$API_PORT" \
|
||||
> "$STATE_DIR/backend.log" 2>&1
|
||||
;;
|
||||
status)
|
||||
for name in backend frontend; do
|
||||
if is_running "$name"; then echo "$name: running (pid $(cat "$STATE_DIR/$name.pid"))"; else echo "$name: stopped"; fi
|
||||
done
|
||||
curl -sf "http://127.0.0.1:$API_PORT/api/test/status" && echo || true
|
||||
;;
|
||||
logs)
|
||||
tail -f "$STATE_DIR/${1:-backend}.log"
|
||||
;;
|
||||
env)
|
||||
backend_env
|
||||
;;
|
||||
*)
|
||||
sed -n '2,20p' "$0"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Reference in new issue
Block a user