flnxi-Wu/lib/__tests__/material-share-token.test.ts
2026-05-25 15:36:51 +08:00

56 lines
1.5 KiB
TypeScript

import {
createMaterialShareToken,
findValidMaterialShareToken,
} from '../material-share-token'
import { prisma } from '../prisma'
jest.mock('../prisma', () => ({
prisma: {
materialShareToken: {
create: jest.fn(),
findFirst: jest.fn(),
},
},
}))
describe('material-share-token', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('创建分享票据时只保存哈希,不保存明文 token', async () => {
const token = await createMaterialShareToken('material-1', 'user-1')
expect(token.token).toEqual(expect.any(String))
expect(token.token.length).toBeGreaterThan(30)
expect(prisma.materialShareToken.create).toHaveBeenCalledWith({
data: expect.objectContaining({
tokenHash: expect.any(String),
materialId: 'material-1',
createdByUserId: 'user-1',
expiresAt: expect.any(Date),
}),
})
expect((prisma.materialShareToken.create as jest.Mock).mock.calls[0][0].data.tokenHash).not.toBe(token.token)
})
it('查找有效分享票据时要求未过期、未撤销且素材未删除', async () => {
await findValidMaterialShareToken('plain-token', 'material-1')
expect(prisma.materialShareToken.findFirst).toHaveBeenCalledWith({
where: {
tokenHash: expect.any(String),
materialId: 'material-1',
revokedAt: null,
expiresAt: { gt: expect.any(Date) },
material: {
deletedAt: null,
},
},
include: {
material: true,
},
})
})
})