|
| 1 | +import { describe, it, mock, before, after } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import fs from 'fs/promises' |
| 4 | +import path from 'path' |
| 5 | +import os from 'os' |
| 6 | +import App from 'adapt-authoring-core/lib/App.js' |
| 7 | + |
| 8 | +mock.getter(App, 'instance', () => ({ |
| 9 | + errors: { |
| 10 | + UNEXPECTED_FILE_TYPES: { |
| 11 | + setData (data) { |
| 12 | + const e = new Error('UNEXPECTED_FILE_TYPES') |
| 13 | + e.code = 'UNEXPECTED_FILE_TYPES' |
| 14 | + e.data = data |
| 15 | + return e |
| 16 | + } |
| 17 | + }, |
| 18 | + FILE_EXCEEDS_MAX_SIZE: { |
| 19 | + setData (data) { |
| 20 | + const e = new Error('FILE_EXCEEDS_MAX_SIZE') |
| 21 | + e.code = 'FILE_EXCEEDS_MAX_SIZE' |
| 22 | + e.data = data |
| 23 | + return e |
| 24 | + } |
| 25 | + }, |
| 26 | + VALIDATION_FAILED: { |
| 27 | + setData (data) { |
| 28 | + const e = new Error('VALIDATION_FAILED') |
| 29 | + e.code = 'VALIDATION_FAILED' |
| 30 | + e.data = data |
| 31 | + return e |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +})) |
| 36 | + |
| 37 | +const { validateUploadedFiles } = await import('../lib/utils/validateUploadedFiles.js') |
| 38 | + |
| 39 | +describe('validateUploadedFiles()', () => { |
| 40 | + const makeReq = () => ({ translate: (e) => e.message || String(e) }) |
| 41 | + let tmpDir |
| 42 | + |
| 43 | + before(async () => { |
| 44 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'middleware-test-')) |
| 45 | + // Create test files |
| 46 | + await fs.writeFile(path.join(tmpDir, 'test.txt'), 'hello world') |
| 47 | + await fs.writeFile(path.join(tmpDir, 'subtitle.srt'), '1\n00:00:00,000 --> 00:00:01,000\nHello') |
| 48 | + }) |
| 49 | + |
| 50 | + after(async () => { |
| 51 | + await fs.rm(tmpDir, { recursive: true }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should pass when file matches expected type', async () => { |
| 55 | + const req = makeReq() |
| 56 | + const files = { file: [{ mimetype: 'image/png', originalFilename: 'test.png', size: 100, filepath: path.join(tmpDir, 'test.txt') }] } |
| 57 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 58 | + await assert.doesNotReject(() => validateUploadedFiles(req, files, options)) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should pass with multiple files all matching expected types', async () => { |
| 62 | + const req = makeReq() |
| 63 | + const files = { |
| 64 | + file1: [{ mimetype: 'image/png', originalFilename: 'a.png', size: 100, filepath: path.join(tmpDir, 'test.txt') }], |
| 65 | + file2: [{ mimetype: 'image/jpeg', originalFilename: 'b.jpg', size: 200, filepath: path.join(tmpDir, 'test.txt') }] |
| 66 | + } |
| 67 | + const options = { expectedFileTypes: ['image/png', 'image/jpeg'], maxFileSize: 1000 } |
| 68 | + await assert.doesNotReject(() => validateUploadedFiles(req, files, options)) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should throw VALIDATION_FAILED when file exceeds max size', async () => { |
| 72 | + const req = makeReq() |
| 73 | + const files = { file: [{ mimetype: 'image/png', originalFilename: 'big.png', size: 5000, filepath: path.join(tmpDir, 'test.txt') }] } |
| 74 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 75 | + await assert.rejects( |
| 76 | + () => validateUploadedFiles(req, files, options), |
| 77 | + (err) => err.code === 'VALIDATION_FAILED' |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should throw VALIDATION_FAILED for unexpected file type', async () => { |
| 82 | + // fileTypeFromFile returns null for plain text files, so mimetype stays unmatched |
| 83 | + const req = makeReq() |
| 84 | + const files = { file: [{ mimetype: 'text/plain', originalFilename: 'test.txt', size: 100, filepath: path.join(tmpDir, 'test.txt') }] } |
| 85 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 86 | + await assert.rejects( |
| 87 | + () => validateUploadedFiles(req, files, options), |
| 88 | + (err) => err.code === 'VALIDATION_FAILED' |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should treat .srt files as application/x-subrip when file inspection returns null', async () => { |
| 93 | + const req = makeReq() |
| 94 | + const files = { file: [{ mimetype: 'text/plain', originalFilename: 'subtitle.srt', size: 100, filepath: path.join(tmpDir, 'subtitle.srt') }] } |
| 95 | + const options = { expectedFileTypes: ['application/x-subrip'], maxFileSize: 1000 } |
| 96 | + await assert.doesNotReject(() => validateUploadedFiles(req, files, options)) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should handle empty files object', async () => { |
| 100 | + const req = makeReq() |
| 101 | + const files = {} |
| 102 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 103 | + await assert.doesNotReject(() => validateUploadedFiles(req, files, options)) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should flatten nested file arrays', async () => { |
| 107 | + const req = makeReq() |
| 108 | + const files = { |
| 109 | + images: [ |
| 110 | + { mimetype: 'image/png', originalFilename: 'a.png', size: 100, filepath: path.join(tmpDir, 'test.txt') }, |
| 111 | + { mimetype: 'image/png', originalFilename: 'b.png', size: 200, filepath: path.join(tmpDir, 'test.txt') } |
| 112 | + ] |
| 113 | + } |
| 114 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 115 | + await assert.doesNotReject(() => validateUploadedFiles(req, files, options)) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should include both type and size errors in VALIDATION_FAILED', async () => { |
| 119 | + const req = makeReq() |
| 120 | + const files = { file: [{ mimetype: 'text/plain', originalFilename: 'test.txt', size: 5000, filepath: path.join(tmpDir, 'test.txt') }] } |
| 121 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 122 | + await assert.rejects( |
| 123 | + () => validateUploadedFiles(req, files, options), |
| 124 | + (err) => { |
| 125 | + return err.code === 'VALIDATION_FAILED' && err.data.schemaName === 'fileupload' |
| 126 | + } |
| 127 | + ) |
| 128 | + }) |
| 129 | + |
| 130 | + it('should pass when file size equals max size', async () => { |
| 131 | + const req = makeReq() |
| 132 | + const files = { file: [{ mimetype: 'image/png', originalFilename: 'exact.png', size: 1000, filepath: path.join(tmpDir, 'test.txt') }] } |
| 133 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 134 | + await assert.doesNotReject(() => validateUploadedFiles(req, files, options)) |
| 135 | + }) |
| 136 | + |
| 137 | + it('should fail when file size is one byte over max', async () => { |
| 138 | + const req = makeReq() |
| 139 | + const files = { file: [{ mimetype: 'image/png', originalFilename: 'over.png', size: 1001, filepath: path.join(tmpDir, 'test.txt') }] } |
| 140 | + const options = { expectedFileTypes: ['image/png'], maxFileSize: 1000 } |
| 141 | + await assert.rejects( |
| 142 | + () => validateUploadedFiles(req, files, options), |
| 143 | + (err) => err.code === 'VALIDATION_FAILED' |
| 144 | + ) |
| 145 | + }) |
| 146 | +}) |
0 commit comments