/** @jest-environment node */ import { authOptions } from '@/lib/auth' import { prisma } from '@/lib/prisma' import bcrypt from 'bcryptjs' import { autoActivateUserIfEligible, autoSyncRoleFromPersonnel } from '@/lib/auto-personnel-rules' import { resolveBrandCenterAccessForUser } from '@/lib/brand-center-access' jest.mock('bcryptjs', () => ({ compare: jest.fn() })) jest.mock('@/lib/brand-center-access', () => ({ resolveBrandCenterAccessForUser: jest.fn() })) jest.mock('@/lib/user-permission-inherit', () => ({ resolveUserVideoEarlyAccess: jest.fn().mockResolvedValue(false) })) jest.mock('@/lib/auto-personnel-rules', () => ({ autoActivateUserIfEligible: jest.fn(), autoSyncRoleFromPersonnel: jest.fn(), })) jest.mock('@/lib/prisma', () => ({ prisma: { user: { findUnique: jest.fn(), update: jest.fn(), }, }, })) jest.mock('@/lib/dingtalk-login', () => ({ authorizeDingtalkH5UserForNextAuth: jest.fn(), })) jest.mock('@/lib/auth-cookie', () => ({ resolveSecureAuthCookieForCurrentRequest: jest.fn().mockResolvedValue(false), })) describe('credentials provider legacy DingTalk password', () => { test('固定钉钉自动登录口令不能再用于邮箱密码登录', async () => { const provider = authOptions.providers[0] as any await expect(provider.options.authorize({ email: 'user@example.com', password: 'dingtalk_auto_login', })).rejects.toThrow('邮箱或密码错误') expect(prisma.user.findUnique).not.toHaveBeenCalled() }) }) describe('Web 账号状态', () => { const active = { id: 'user-1', email: 'user@example.com', status: 'active', isActive: true, archivedAt: null } const authorize = (authOptions.providers[0] as any).options.authorize const input = { email: active.email, password: 'correct' } beforeEach(() => { jest.clearAllMocks() ;(bcrypt.compare as jest.Mock).mockResolvedValue(true) jest.mocked(prisma.user.update).mockResolvedValue(active as any) }) test.each([ { status: 'suspended', isActive: false }, { status: 'archived', isActive: false }, { archivedAt: new Date() }, { status: 'pending', isActive: true, role: 'SYSTEM_ADMIN' }, { status: 'active', isActive: false }, ])('拒绝不合格的账号,且不尝试自动激活 (%j)', async (account) => { jest.mocked(prisma.user.findUnique).mockResolvedValue({ ...active, ...account } as any) await expect(authorize(input)).rejects.toThrow() expect(autoActivateUserIfEligible).not.toHaveBeenCalled() expect(prisma.user.update).not.toHaveBeenCalled() }) test('合格的新账号可以按花名册激活,登录审计只写登录记录', async () => { jest.mocked(prisma.user.findUnique) .mockResolvedValueOnce({ ...active, status: 'pending', isActive: false } as any) .mockResolvedValueOnce(active as any) jest.mocked(autoActivateUserIfEligible).mockResolvedValue(true) await expect(authorize(input)).resolves.toMatchObject({ id: active.id }) expect(autoSyncRoleFromPersonnel).toHaveBeenCalledWith(active.id) expect(prisma.user.update).toHaveBeenCalledWith({ where: { id: active.id }, data: { lastLoginAt: expect.any(Date), lastLoginIp: null, loginCount: { increment: 1 } }, }) }) test.each([ { status: 'pending', isActive: true }, { status: 'pending', isActive: false }, { status: 'suspended', isActive: true }, { status: 'archived', isActive: true }, { archivedAt: new Date() }, { status: 'active', isActive: false }, ])('旧 JWT 在刷新时撤销异常状态账号 (%j)', async (account) => { jest.mocked(prisma.user.findUnique).mockResolvedValue({ ...active, ...account } as any) const callbacks = authOptions.callbacks! const token = await callbacks.jwt!({ token: { id: active.id, accountStatus: 'pending', authCheckedAt: Date.now(), canAccessBrandCenter: true }, } as any) expect(token).toMatchObject({ accountStatus: 'disabled', canAccessBrandCenter: false }) const session = await callbacks.session!({ session: { user: { id: active.id }, expires: '' }, token } as any) expect(session.user).toBeNull() expect(resolveBrandCenterAccessForUser).not.toHaveBeenCalled() }) test('状态、启用标志和归档时间均有效时刷新旧 JWT', async () => { jest.mocked(prisma.user.findUnique).mockResolvedValue({ ...active, role: 'USER', isSuperAdmin: false } as any) jest.mocked(resolveBrandCenterAccessForUser).mockResolvedValue(true) const callbacks = authOptions.callbacks! const token = await callbacks.jwt!({ token: { id: active.id, authCheckedAt: 0 } } as any) expect(token).toMatchObject({ accountStatus: 'active', canAccessBrandCenter: true }) const session = await callbacks.session!({ session: { user: {}, expires: '' }, token } as any) expect(session.user).toMatchObject({ id: active.id, canAccessBrandCenter: true }) }) })