61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { GET } from '@/app/api/materials/share/route'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
material: {
|
|
findFirst: jest.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
describe('materials share security', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue(null)
|
|
;(prisma.material.findFirst as jest.Mock).mockResolvedValue(null)
|
|
})
|
|
|
|
test('公开分享只查询未删除素材', async () => {
|
|
const response = await GET(
|
|
new NextRequest('http://localhost/api/materials/share?id=material-deleted'),
|
|
)
|
|
const body = await response.json()
|
|
|
|
expect(response.status).toBe(404)
|
|
expect(body.error).toBe('素材不存在')
|
|
expect(prisma.material.findFirst).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: {
|
|
id: 'material-deleted',
|
|
deletedAt: null,
|
|
},
|
|
}),
|
|
)
|
|
})
|
|
|
|
test('鉴权下载响应禁止共享缓存', () => {
|
|
const routeSource = fs.readFileSync(
|
|
path.join(process.cwd(), 'app/api/materials/download/route.ts'),
|
|
'utf8',
|
|
)
|
|
|
|
expect(routeSource).toContain("'Cache-Control': 'private, no-store, max-age=0'")
|
|
expect(routeSource).not.toContain("'Cache-Control': 'public, max-age=86400, must-revalidate'")
|
|
})
|
|
})
|