gongxiangp2/lib/__tests__/product-package-complete-route-security.test.ts

39 lines
1.2 KiB
TypeScript

/** @jest-environment node */
import { getServerSession } from 'next-auth'
import { prisma } from '@/lib/prisma'
import { POST } from '@/app/api/materials/upload/product-package/complete/route'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/prisma', () => ({
prisma: {
material: {
findUnique: jest.fn(),
},
},
}))
describe('product-package complete public route security', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'user-1' } })
})
test.each([
['fileUrl', { fileUrl: 'http://127.0.0.1:3000/internal' }],
['localFilePath', { localFilePath: 'C:\\Windows\\win.ini' }],
])('公开 complete 路由拒绝 %s 来源', async (_source, sourceInput) => {
const response = await POST({
json: jest.fn().mockResolvedValue({
materialId: 'material-1',
assets: {
main: [{ ...sourceInput, fileName: 'main.png', fileSize: 10 }],
},
}),
} as any)
expect(response.status).toBe(400)
expect(prisma.material.findUnique).not.toHaveBeenCalled()
})
})