/** * Postgres / SQLite without the migration that adds the 2FA column, against the REAL adapter: the start-up step * must log the loud line (the database's own words are in the error's `cause`, not in its message), and every * query on the collection fails — which is why the docs call the migration a required step of the same deploy. * SQLite in memory by default; `IPAL_TEST_POSTGRES_URI` runs it on Postgres. Not for MongoDB (no columns). */ import type { Payload } from 'payload' import { payloadTotp } from '@clocklimited/payload-2fa' import { lexicalEditor } from '@payloadcms/richtext-lexical' import os from 'os' import path from 'path' import { getPayload } from 'payload' import { afterAll, beforeAll, expect, test, vi } from 'vitest' import { backfillSetupFrom } from '../src/modules/security/totp/setupRequired.js' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' import { testDatabase } from './http/db.js' vi.mock('server-only', () => ({})) vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock) const sql = !process.env.IPAL_TEST_MONGO_URI let payload: Payload beforeAll(async () => { if (!sql) {return} const config = buildTestConfig({ admin: { user: 'users' }, collections: [{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } }], db: testDatabase('setupcol'), editor: lexicalEditor(), plugins: [iapl({ access: { authCollection: 'users' }, i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, twoFactor: { issuer: 'Example', plugin: payloadTotp as never } })], secret: 'integration-test-secret-0123456789', telemetry: false, }) payload = await getPayload({ config }) }, 120_000) afterAll(async () => { await payload?.destroy?.() }) test.skipIf(!sql)('without the column: the loud log line, and every query on the collection fails; with it: the window starts', async () => { await payload.create({ collection: 'users' as never, data: { email: 'before@example.com', password: 'a-long-test-password-1', roles: ['admin'] } as never }) const db = payload.db as unknown as { client?: { execute: (q: string) => Promise }; name: string; pool?: { query: (q: string) => Promise }; schemaName?: string } const run = (statement: string) => (db.pool ? db.pool.query(statement) : db.client!.execute(statement)) const table = db.pool && db.schemaName ? `"${db.schemaName}"."users"` : '"users"' // the schema as the previous version left it await run(`ALTER TABLE ${table} DROP COLUMN "totp_setup_from"`) const error = vi.spyOn(payload.logger, 'error').mockImplementation(() => undefined as never) const warn = vi.spyOn(payload.logger, 'warn').mockImplementation(() => undefined as never) expect(await backfillSetupFrom(payload, 'users')).toBe(0) // does not throw: the public site must keep running expect(error).toHaveBeenCalledTimes(1) expect(String(error.mock.calls[0]?.[0])).toMatch(/the column for "totpSetupFrom" is missing.*NOBODY CAN LOG IN.*prodMigrations/s) expect(warn).not.toHaveBeenCalled() // what "nobody can log in" means: the account cannot even be read await expect(payload.find({ collection: 'users' as never })).rejects.toThrow() await expect(payload.login({ collection: 'users' as never, data: { email: 'before@example.com', password: 'a-long-test-password-1' } as never })).rejects.toThrow() // the migration: the column is there → the start-up step gives the existing account its window, once await run(`ALTER TABLE ${table} ADD COLUMN "totp_setup_from" ${db.pool ? 'timestamp(3) with time zone' : 'text'}`) error.mockClear() expect(await backfillSetupFrom(payload, 'users')).toBe(1) expect(await backfillSetupFrom(payload, 'users')).toBe(0) expect(error).not.toHaveBeenCalled() expect((await payload.login({ collection: 'users' as never, data: { email: 'before@example.com', password: 'a-long-test-password-1' } as never })).token).toBeTruthy() error.mockRestore() warn.mockRestore() })