"""API tests: Gmail accounts manager, Send Test Email, public unsubscribe flow, and the full campaign lifecycle (wizard options, create/patch/delete/clear-all, validate-step, preview, start/pause/resume/cancel, progress, recipients, export). Real behavior is asserted everywhere; nothing is faked. Where real Google OAuth cannot be exercised (no credentials in CI), the app's honest error responses are asserted instead of mocked successes. """ import csv import io import json import secrets import uuid from datetime import datetime, timedelta, timezone import pytest # --------------------------------------------------------------------------- helpers def csrf(client) -> str: for c in client.cookies.jar: if c.name == "mailer_csrf": return c.value raise AssertionError("CSRF cookie missing") def H(client): return {"X-CSRF-Token": csrf(client)} def user_id_of(client) -> int: r = client.get("/auth/me") assert r.status_code == 200 return r.json()["id"] def celery_available() -> bool: """True when a real Redis broker is reachable (campaign start enqueues jobs).""" try: import redis as _r from app.core.config import settings conn = _r.from_url(settings.REDIS_URL, socket_timeout=1) conn.ping() conn.close() return True except Exception: return False requires_broker = pytest.mark.skipif(not celery_available(), reason="Redis broker not available") def _seed_account(db, user_id, email=None, status="authorized", method="api", with_token=True, refresh="refresh-abc"): """Insert a GmailAccount (+ OAuthToken) directly. Tokens are fake strings — the app never uses them in these endpoints except honest-failure paths.""" from app.models import GmailAccount, OAuthToken