Files
punimtag/e2e/tests/api.fastapi-login.spec.ts
T
ilia c0c997f796
CI / skip-ci-check (pull_request) Successful in 5s
CI / docker-ci (pull_request) Successful in 7s
CI / secret-scan (pull_request) Successful in 12s
CI / viewer-unit (pull_request) Successful in 1m29s
CI / e2e (pull_request) Successful in 4m8s
CI / admin-unit (pull_request) Successful in 4m24s
test: timing budgets + CI hardening (artifact v3 pin, npm cache retry)
Closes three items from the outstanding e2e/CI gap list:

- Timing budgets: timings.measure() only ever recorded durations for the
  (still-unwired) Pushgateway export — nothing failed CI when a step got
  slow. Add e2e/timing-budgets.ts (expectWithinBudget + shared BUDGET_MS
  buckets) and wire it into every measure() call site across the suite.
  Mail-wait steps are deliberately left unbudgeted (external mail-trap
  delivery latency, not a code performance signal).

- actions/upload-artifact@v4 doesn't work against this Gitea/act runner's
  artifact backend — pin to v3 for the e2e failure-report upload.

- Shared act_runner npm cache has corrupted platform-native tarballs before
  (@next/swc-linux-x64-musl) and reds viewer-unit/admin-unit/e2e with no
  product bug involved. All three npm ci steps now retry once after
  `npm cache clean --force` on first failure.

Verified: full local suite green against DEV (37 passed, 6 skipped, no
budget assertion failures) before wiring into CI.
2026-07-15 08:56:31 -04:00

51 lines
1.8 KiB
TypeScript

import { z } from 'zod';
import { test, expect } from '../fixtures';
import { BUDGET_MS, expectWithinBudget } from '../timing-budgets';
/**
* FastAPI `/api/v1/auth/login` uses a *separate* user DB from NextAuth.
* Set E2E_API_USERNAME + E2E_API_PASSWORD (or reuse admin FastAPI creds) to enable.
*
* Schemas mirror `backend/schemas/auth.py` (`TokenResponse` / `UserResponse`).
*/
const TokenResponse = z.object({
access_token: z.string().min(1),
refresh_token: z.string().min(1),
password_change_required: z.boolean(),
});
const UserResponse = z.object({
username: z.string().min(1),
is_admin: z.boolean(),
role: z.string().min(1),
permissions: z.record(z.boolean()),
});
type TokenResponse = z.infer<typeof TokenResponse>;
type UserResponse = z.infer<typeof UserResponse>;
test.describe('fastapi login @smoke', () => {
test('login returns bearer token when API creds set', async ({ api, timings }) => {
const username = process.env.E2E_API_USERNAME || '';
const password = process.env.E2E_API_PASSWORD || '';
test.skip(!username || !password, 'E2E_API_USERNAME/PASSWORD required for FastAPI auth');
const res = await timings.measure('api_login', () =>
api.post<TokenResponse>('/api/v1/auth/login', {
body: { username, password },
expectedStatus: 200,
schema: TokenResponse,
}),
);
expectWithinBudget(timings, 'api_login', BUDGET_MS.api);
expect(res.data.access_token).toBeTruthy();
const authed = api.withAuthBearer(res.data.access_token);
const me = await timings.measure('api_me', () =>
authed.get<UserResponse>('/api/v1/auth/me', { expectedStatus: 200, schema: UserResponse }),
);
expectWithinBudget(timings, 'api_me', BUDGET_MS.api);
expect(me.status).toBe(200);
expect(me.data.username).toBe(username);
});
});