mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
## Summary
Two admin-only features so support/intervention work can (a) know what
language/country a user is using the product in, and (b) see the exact
emails ever sent to a user, rendered as they appeared in the inbox with
full delivery metadata.
### A — Language / country visibility (admin-only)
- New `get_full_locale_from_request()` captures the real UI language
(any of 33, not the narrowed en/es/de email locale) on **every**
job-creation path (URL, upload, audio-search, bulk, made-for-you via
Stripe metadata). Stored as `Job.locale` (+ added to the dashboard
summary projection) and `User.ui_locale` (kept separate from
`User.locale`).
- `LocaleBadge` (flag + language) shown in the admin jobs table, admin
job detail (+ country via the existing `IpInfo` on the creation IP), the
user-detail header, and the customer `JobCard` **gated to admins only**
— regular users never see it.
- Country is derived from IP (existing `ip_geolocation_service`); no new
capture.
### B — "Emails sent to this user"
- Every send routes through one `EmailService._log_and_send` chokepoint
that persists each email to a new Firestore `email_log` collection
(rendered HTML + Postmark MessageID), best-effort so it can never break
or delay a send. Single-use auth/capability tokens (magic-link, review,
referral) are **redacted** before persistence.
- `PostmarkAdminService` merges the Postmark Messages API (last ~45
days: exact HTML + opens/clicks/bounce/delivery status, paginated) with
the Firestore log (permanent), de-duplicated by message id.
- Endpoints `GET /api/admin/users/{email}/emails` and `GET
/api/admin/emails/{id}`; the user-detail "Emails Sent" card opens a
modal rendering the email in a sandboxed `<iframe srcDoc>` with a
metadata side panel.
- New Firestore composite index (`email_log` recipient + created_at)
added to Pulumi.
## Testing
- **41 new backend tests** (locale capture incl. q=0, email logging +
token redaction, PostmarkAdminService incl. pagination + 45-day
fallback, job-locale persistence, admin endpoints) — all pass.
- **10 new frontend tests** (LocaleBadge, UserEmailsCard list +
sandboxed-iframe modal) — all pass.
- Regression sweep on related existing suites: green. Preview-email flow
smoke-tested through the new chokepoint.
## Notes
- `pulumi up` for the new `email_log` index should be applied before
merge (done locally).
- `Job.locale` populates for new jobs going forward; historical jobs
fall back to the submitting user's locale where shown.
Local CodeRabbit review completed and addressed (token redaction,
pagination, row a11y, locale-overwrite guard, q=0 handling).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
@coderabbitai ignore
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""Tests for the admin email-history endpoints in backend/api/routes/admin.py.
|
|
|
|
Verifies wiring + auth: GET /admin/users/{email}/emails and
|
|
GET /admin/emails/{message_id} (with 404 on missing detail).
|
|
"""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_postmark_service():
|
|
svc = MagicMock()
|
|
svc.get_user_email_history.return_value = {
|
|
"email": "u@x.com", "count": 1, "postmark_available": True,
|
|
"emails": [{"message_id": "m1", "source": "postmark", "subject": "Hi"}],
|
|
}
|
|
svc.get_email_detail.return_value = {
|
|
"message_id": "m1", "source": "postmark", "subject": "Hi",
|
|
"html_body": "<h1>Hi</h1>", "status": "Delivered",
|
|
}
|
|
return svc
|
|
|
|
|
|
@pytest.fixture
|
|
def client(mock_postmark_service):
|
|
mock_creds = MagicMock()
|
|
mock_creds.universe_domain = "googleapis.com"
|
|
with patch("backend.services.firestore_service.firestore"), \
|
|
patch("backend.services.storage_service.storage"), \
|
|
patch("google.auth.default", return_value=(mock_creds, "test-project")), \
|
|
patch("backend.services.postmark_admin_service.get_postmark_admin_service",
|
|
return_value=mock_postmark_service):
|
|
from fastapi import FastAPI
|
|
from backend.api.routes.admin import router
|
|
from backend.api.dependencies import require_admin
|
|
|
|
app = FastAPI()
|
|
app.include_router(router, prefix="/api")
|
|
app.dependency_overrides[require_admin] = lambda: ("admin@nomadkaraoke.com", None, -1)
|
|
yield TestClient(app)
|
|
|
|
|
|
def test_get_user_emails(client, mock_postmark_service):
|
|
resp = client.get("/api/admin/users/u@x.com/emails")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["count"] == 1
|
|
assert data["emails"][0]["message_id"] == "m1"
|
|
mock_postmark_service.get_user_email_history.assert_called_once_with("u@x.com")
|
|
|
|
|
|
def test_get_email_detail_default_source(client, mock_postmark_service):
|
|
resp = client.get("/api/admin/emails/m1")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["html_body"] == "<h1>Hi</h1>"
|
|
mock_postmark_service.get_email_detail.assert_called_once_with("m1", source="postmark")
|
|
|
|
|
|
def test_get_email_detail_log_source(client, mock_postmark_service):
|
|
client.get("/api/admin/emails/old1?source=log")
|
|
mock_postmark_service.get_email_detail.assert_called_once_with("old1", source="log")
|
|
|
|
|
|
def test_get_email_detail_404(client, mock_postmark_service):
|
|
mock_postmark_service.get_email_detail.return_value = None
|
|
resp = client.get("/api/admin/emails/missing")
|
|
assert resp.status_code == 404
|