Files
civitai__civitai/tests/preview-smoke.spec.ts
Zachary Lowden ef8c158d7c test(preview): browser smoke tests for deployed PR previews (#2466)
* test(preview): browser smoke tests for deployed PR previews

Adds a Playwright suite that runs against a live PR preview URL and asserts
the IS_PREVIEW login gate (preview-auth.middleware) behaves per role:

- mod / tester / gold clear the gate and core pages render
- a logged-in non-tester is redirected to /preview-restricted
- an anonymous request is redirected to /login

The local /testing/testing-login flow is dead against a preview (NODE_ENV=
production disables the provider + route), so preview-auth.setup.ts mints the
NextAuth session cookie directly with the preview's shared NEXTAUTH_SECRET via
next-auth/jwt encode() — the same function the app signs with. The middleware
reads token.user straight from the cookie and the session callback fail-opens
on an untracked token id, yielding a fully authenticated session. Backing User
rows (+ a gold subscription) are seeded into cnpg-cluster-dev out-of-band.

Runs only under the new playwright.preview.config.ts (needs PREVIEW_URL); the
default config ignores preview-* files so local `npm test` is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(preview): clarify auth-mint model + fix parallelism

Addresses audit feedback:
- Correct the setup-file comment: the minted cookie clears the gate, but SSR
  refreshes token.user from the seeded DB row (refreshToken on an untracked
  token id), so the DB row — not the minted fields — drives the session past
  the gate. The minted object only needs id + isModerator (+ tier for the
  gate's Flipt context).
- playwright.preview.config.ts: workers 1 -> 4 (workers:1 defeated fullyParallel).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(preview): harden against cold-preview SSR latency

The first SSR render of a freshly-deployed preview (the homepage especially)
can take 30-40s while Next warms up — the default 30s per-test timeout flaked
on `mod loads /` (passed on retry). Hardening:

- timeout 30s -> 60s (per-test), navigationTimeout 45s.
- setup fires a warm-up GET / before the suite so the first real test doesn't
  pay the full cold-start cost.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* ci: retrigger preview to exercise pr-smoke-bot comment

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 14:32:57 -05:00

53 lines
1.9 KiB
TypeScript

import { expect, test } from '@playwright/test';
import { storageStatePath } from './preview-fixtures';
/**
* Smoke tests for a deployed PR preview environment.
*
* Asserts the preview login gate (preview-auth.middleware) behaves per role and
* that core pages render for users who pass it. Runs only under
* playwright.preview.config.ts (needs PREVIEW_URL + the minted storage states).
*/
// Core pages every gate-passing user should be able to load.
const CORE_PAGES = ['/', '/models', '/images'];
// Roles that MUST clear the gate (mod via code, tester/gold via Flipt allowlist).
const ALLOWED_ROLES = ['mod', 'tester', 'gold'] as const;
for (const role of ALLOWED_ROLES) {
test.describe(`gate passes for ${role}`, () => {
test.use({ storageState: storageStatePath(role) });
for (const path of CORE_PAGES) {
test(`${role} loads ${path}`, async ({ page }) => {
const resp = await page.goto(path, { waitUntil: 'domcontentloaded' });
expect(resp?.status(), `HTTP status for ${path}`).toBeLessThan(400);
// Not bounced to either gate destination.
expect(page.url(), 'should not redirect to /login').not.toContain('/login');
expect(page.url(), 'should not redirect to /preview-restricted').not.toContain(
'/preview-restricted'
);
});
}
});
}
test.describe('gate denies a logged-in non-tester', () => {
test.use({ storageState: storageStatePath('restricted') });
test('restricted user is redirected to /preview-restricted', async ({ page }) => {
await page.goto('/models', { waitUntil: 'domcontentloaded' });
await expect(page).toHaveURL(/\/preview-restricted/);
});
});
test.describe('gate denies anonymous', () => {
test.use({ storageState: { cookies: [], origins: [] } });
test('anonymous user is redirected to /login', async ({ page }) => {
await page.goto('/models', { waitUntil: 'domcontentloaded' });
await expect(page).toHaveURL(/\/login/);
});
});