/** A locked staff account answers like a wrong password, REST and GraphQL (lockedAnswerSuite.ts). 2FA on. */ import { describe, expect, test, vi } from 'vitest' import { lockedAnswerSite, PASSWORD } from './lockedAnswerSuite.js' vi.mock('server-only', () => ({})) vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock) const { gqlLogin, lock, restLogin, state } = lockedAnswerSite() describe('accounts.lockedAnswer (default): a locked staff account looks like a wrong password', () => { test('REST: the locked owner, with any password, gets exactly what an invented address gets', async () => { await lock('users', 'owner@example.com') const invented = await restLogin('users', 'nobody@example.com', 'a-wrong-password') expect(invented.status).toBe(401) expect(await restLogin('users', 'owner@example.com', 'a-wrong-password')).toEqual(invented) expect(await restLogin('users', 'owner@example.com', PASSWORD)).toEqual(invented) expect(invented.body).not.toMatch(/locked/i) // the lock itself is still there const stored = (await state.payload.find({ collection: 'users' as never, overrideAccess: true, showHiddenFields: true, where: { email: { equals: 'owner@example.com' } } })).docs[0] as { lockUntil?: string } expect(new Date(String(stored.lockUntil)).getTime()).toBeGreaterThan(Date.now()) }) test('GraphQL: the same message, status and error name', async () => { const invented = await gqlLogin('loginUser', 'nobody@example.com', 'a-wrong-password') expect(invented.name).toBe('AuthenticationError') expect(await gqlLogin('loginUser', 'owner@example.com', PASSWORD)).toEqual(invented) }) test('a visitor collection keeps Payload\'s answer (not staff)', async () => { await lock('customers', 'client@example.com', 3) expect((await restLogin('customers', 'client@example.com', PASSWORD)).body).toMatch(/locked/i) expect((await gqlLogin('loginCustomer', 'client@example.com', PASSWORD)).name).toBe('LockedAuth') }) })