d7fb7b7a7b
Build & Deploy / Build & Push Docker Image (push) Failing after 47s
Build & Deploy / Deploy to VPS (push) Has been skipped
Test / API Unit Tests (push) Failing after 5m4s
Test / Marketplace Unit Tests (push) Failing after 4m55s
Test / Admin Unit Tests (push) Successful in 9m37s
Test / Dashboard Unit Tests (push) Successful in 9m37s
Test / API Integration Tests (push) Successful in 9m54s
42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { ValidationError } from '../errors'
|
|
import { assertImageFile, assertImageFiles } from './index'
|
|
|
|
const file = (overrides: Partial<Express.Multer.File> = {}) => ({
|
|
fieldname: 'file',
|
|
originalname: 'photo.jpg',
|
|
encoding: '7bit',
|
|
mimetype: 'image/jpeg',
|
|
size: 123,
|
|
buffer: Buffer.from([0xff, 0xd8, 0xff, 0xdb]),
|
|
stream: undefined as any,
|
|
destination: '',
|
|
filename: '',
|
|
path: '',
|
|
...overrides,
|
|
})
|
|
|
|
describe('upload assertions', () => {
|
|
it('accepts a single image file and narrows the route input', () => {
|
|
expect(() => assertImageFile(file())).not.toThrow()
|
|
})
|
|
|
|
it('rejects missing single file uploads with a field-specific error', () => {
|
|
expect(() => assertImageFile(undefined, 'license image')).toThrow(ValidationError)
|
|
expect(() => assertImageFile(undefined, 'license image')).toThrow('A license image is required')
|
|
})
|
|
|
|
it('rejects non-image single file uploads', () => {
|
|
expect(() => assertImageFile(file({ mimetype: 'application/pdf', originalname: 'license.pdf' }))).toThrow('MIME type does not match file content')
|
|
})
|
|
|
|
it('accepts multiple image files and rejects empty or mixed batches', () => {
|
|
expect(() => assertImageFiles([file({ originalname: 'front.png', mimetype: 'image/png', buffer: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) })])).not.toThrow()
|
|
expect(() => assertImageFiles([], 'inspection photos')).toThrow('At least one inspection photos file is required')
|
|
expect(() => assertImageFiles([
|
|
file({ originalname: 'front.png', mimetype: 'image/png', buffer: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) }),
|
|
file({ originalname: 'report.pdf', mimetype: 'application/pdf', buffer: Buffer.from('%PDF') }),
|
|
])).toThrow('Unsupported or spoofed image file: report.pdf')
|
|
})
|
|
})
|