teqo/tests/helpers/adminApi.ts
Francisco Solla 504f6c371d
All checks were successful
CI (PR) / checks (pull_request) Successful in 15m47s
PR Ready + auto-merge / ready-automerge (pull_request) Successful in 16m1s
Promote plan-issue Related on main merge / plan-issue-ready (pull_request) Has been skipped
Main merge flips claimed issues to done + in-prod / issue-done (pull_request) Successful in 21s
refactor(S10): simplify pass — Lead wiring unit-tested e raças de estado compartilhado do e2e resolvidas
e2e: stub hermético de connect.facebook.net no guard compartilhado (specs paralelos renderizam a home com o pixel); advisory lock no Postgres serializando o login do usuário de teste (Payload perde sessão em login concorrente read-modify-write — classe documentada no webauthn); seedTestUser vira create-if-missing (sem janela de usuário ausente nem reset de senha); Lead do form protegido do catch de erro (tracking nunca falha uma captura commitada); descrição do campo compartilhada; unidade nova campaignNewsletterLead cobre exatamente um Lead no sucesso.
2026-08-19 20:50:55 -03:00

41 lines
1.6 KiB
TypeScript

import type { APIRequestContext } from '@playwright/test'
import { Client } from 'pg'
import { testUser } from './seedUser'
// Arbitrary fixed key — every spec logs the same test user in through this
// helper, so one serialization point covers the whole suite.
const ADMIN_LOGIN_LOCK_KEY = 727_001
/**
* Logs the shared test user in and returns the `payload-token` cookie.
*
* Serialized by a Postgres advisory lock: Payload keeps auth sessions as a
* read-modify-write array on the user document (the campaign WebAuthn note
* documents the exact class), so two parallel workers logging the same
* `dev@payloadcms.com` in concurrently can lose one session row — the loser's
* token then resolves to nobody and every admin request answers 403. The
* lock makes the read-modify-write exclusive; a waiting worker logs in after
* the winner, keeping every session.
*/
export const adminHeaders = async (
request: APIRequestContext,
baseURL: string,
): Promise<Record<string, string>> => {
const client = new Client({ connectionString: process.env.DATABASE_URL })
await client.connect()
try {
await client.query('SELECT pg_advisory_lock($1)', [ADMIN_LOGIN_LOCK_KEY])
const login = await request.post(`${baseURL}/api/users/login`, {
data: { email: testUser.email, password: testUser.password },
})
if (!login.ok()) {
throw new Error(`admin login failed with ${login.status()}`)
}
const { token } = await login.json()
return { cookie: `payload-token=${token}` }
} finally {
await client.query('SELECT pg_advisory_unlock($1)', [ADMIN_LOGIN_LOCK_KEY]).catch(() => {})
await client.end()
}
}