forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-policy.spec.ts
More file actions
195 lines (163 loc) · 7.97 KB
/
Copy pathupload-policy.spec.ts
File metadata and controls
195 lines (163 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { BadRequestException } from '@nestjs/common';
import { UploadPolicyValidator, DEFAULT_UPLOAD_POLICY } from './upload-policy';
describe('UploadPolicyValidator', () => {
let validator: UploadPolicyValidator;
beforeEach(() => {
validator = new UploadPolicyValidator();
});
describe('validateMimeType', () => {
it('should allow valid MIME types', () => {
expect(() => validator.validateMimeType('image/jpeg')).not.toThrow();
expect(() => validator.validateMimeType('image/png')).not.toThrow();
expect(() => validator.validateMimeType('application/pdf')).not.toThrow();
});
it('should throw error for invalid MIME types', () => {
expect(() => validator.validateMimeType('text/plain')).toThrow(BadRequestException);
expect(() => validator.validateMimeType('application/octet-stream')).toThrow(BadRequestException);
expect(() => validator.validateMimeType('video/mp4')).toThrow(BadRequestException);
});
it('should include allowed types in error message', () => {
try {
validator.validateMimeType('text/plain');
} catch (error: unknown) {
expect(error).toBeInstanceOf(BadRequestException);
expect((error as BadRequestException).message).toContain('image/jpeg');
expect((error as BadRequestException).message).toContain('image/png');
expect((error as BadRequestException).message).toContain('application/pdf');
}
});
});
describe('validateFileSize', () => {
it('should allow files within size limit', () => {
expect(() => validator.validateFileSize(1000)).not.toThrow();
expect(() => validator.validateFileSize(DEFAULT_UPLOAD_POLICY.maxFileSize - 1)).not.toThrow();
});
it('should throw error for oversized files', () => {
expect(() => validator.validateFileSize(DEFAULT_UPLOAD_POLICY.maxFileSize + 1)).toThrow(BadRequestException);
});
it('should include size limit in error message', () => {
try {
validator.validateFileSize(DEFAULT_UPLOAD_POLICY.maxFileSize + 1);
} catch (error: unknown) {
expect(error).toBeInstanceOf(BadRequestException);
expect((error as BadRequestException).message).toContain(DEFAULT_UPLOAD_POLICY.maxFileSize.toString());
}
});
});
describe('validateFileExtension', () => {
it('should allow valid extensions', () => {
expect(() => validator.validateFileExtension('test.jpg')).not.toThrow();
expect(() => validator.validateFileExtension('test.jpeg')).not.toThrow();
expect(() => validator.validateFileExtension('test.png')).not.toThrow();
expect(() => validator.validateFileExtension('test.pdf')).not.toThrow();
});
it('should throw error for invalid extensions', () => {
expect(() => validator.validateFileExtension('test.exe')).toThrow(BadRequestException);
expect(() => validator.validateFileExtension('test.bat')).toThrow(BadRequestException);
expect(() => validator.validateFileExtension('test.txt')).toThrow(BadRequestException);
});
it('should handle uppercase extensions', () => {
expect(() => validator.validateFileExtension('test.JPG')).not.toThrow();
expect(() => validator.validateFileExtension('test.PDF')).not.toThrow();
});
it('should include allowed extensions in error message', () => {
try {
validator.validateFileExtension('test.exe');
} catch (error: unknown) {
expect(error).toBeInstanceOf(BadRequestException);
expect((error as BadRequestException).message).toContain('.jpg');
expect((error as BadRequestException).message).toContain('.png');
expect((error as BadRequestException).message).toContain('.pdf');
}
});
});
describe('sanitizeFilename', () => {
it('should preserve valid filenames', () => {
expect(validator.sanitizeFilename('test.jpg')).toBe('test.jpg');
expect(validator.sanitizeFilename('my-file_123.png')).toBe('my-file_123.png');
});
it('should replace invalid characters', () => {
expect(validator.sanitizeFilename('test<script>.jpg')).toBe('test_script_.jpg');
expect(validator.sanitizeFilename('test@file.jpg')).toBe('test_file.jpg');
expect(validator.sanitizeFilename('test file.jpg')).toBe('test_file.jpg');
});
it('should prevent path traversal', () => {
expect(validator.sanitizeFilename('../../../etc/passwd')).toBe('___etc_passwd');
expect(validator.sanitizeFilename('..\\..\\windows\\system32')).toBe('____windows_system32');
expect(validator.sanitizeFilename('/etc/passwd')).toBe('_etc_passwd');
});
it('should truncate long filenames', () => {
const longFilename = 'a'.repeat(300) + '.jpg';
const result = validator.sanitizeFilename(longFilename);
expect(result.length).toBeLessThanOrEqual(DEFAULT_UPLOAD_POLICY.filenameSanitization.maxLength);
});
it('should handle empty or invalid filenames', () => {
expect(validator.sanitizeFilename('')).toBe('file');
expect(validator.sanitizeFilename('!!!')).toBe('file');
});
it('should preserve file extension', () => {
expect(validator.sanitizeFilename('test.jpg')).toBe('test.jpg');
expect(validator.sanitizeFilename('test@file.jpeg')).toBe('test_file.jpeg');
});
});
describe('generateObjectKey', () => {
it('should generate proper object key format', () => {
const uuid = '123e4567-e89b-12d3-a456-426614174000';
const filename = 'test.jpg';
const key = validator.generateObjectKey(filename, uuid);
expect(key).toBe(`${DEFAULT_UPLOAD_POLICY.keyPrefix}/${uuid}-${filename}`);
});
it('should work with sanitized filenames', () => {
const uuid = '123e4567-e89b-12d3-a456-426614174000';
const filename = 'test<script>.jpg';
const sanitizedFilename = validator.sanitizeFilename(filename);
const key = validator.generateObjectKey(sanitizedFilename, uuid);
expect(key).toBe(`${DEFAULT_UPLOAD_POLICY.keyPrefix}/${uuid}-${sanitizedFilename}`);
});
});
describe('getDownloadHeaders', () => {
it('should return proper download headers', () => {
const headers = validator.getDownloadHeaders();
expect(headers).toHaveProperty('Cache-Control', DEFAULT_UPLOAD_POLICY.downloadHeaders.cacheControl);
expect(headers).toHaveProperty('Content-Disposition', DEFAULT_UPLOAD_POLICY.downloadHeaders.contentDisposition);
});
});
describe('getUploadExpiration', () => {
it('should return upload expiration time', () => {
const expiration = validator.getUploadExpiration();
expect(expiration).toBe(DEFAULT_UPLOAD_POLICY.urlExpiration.upload);
});
});
describe('getDownloadExpiration', () => {
it('should return download expiration time', () => {
const expiration = validator.getDownloadExpiration();
expect(expiration).toBe(DEFAULT_UPLOAD_POLICY.urlExpiration.download);
});
});
describe('with custom policy', () => {
let customValidator: UploadPolicyValidator;
const customPolicy = {
...DEFAULT_UPLOAD_POLICY,
maxFileSize: 5 * 1024 * 1024, // 5MB
allowedMimeTypes: ['image/jpeg', 'image/png'],
keyPrefix: 'custom-uploads'
};
beforeEach(() => {
customValidator = new UploadPolicyValidator(customPolicy);
});
it('should use custom file size limit', () => {
expect(() => customValidator.validateFileSize(4 * 1024 * 1024)).not.toThrow();
expect(() => customValidator.validateFileSize(6 * 1024 * 1024)).toThrow(BadRequestException);
});
it('should use custom MIME types', () => {
expect(() => customValidator.validateMimeType('image/jpeg')).not.toThrow();
expect(() => customValidator.validateMimeType('application/pdf')).toThrow(BadRequestException);
});
it('should use custom key prefix', () => {
const uuid = '123e4567-e89b-12d3-a456-426614174000';
const filename = 'test.jpg';
const key = customValidator.generateObjectKey(filename, uuid);
expect(key).toBe(`${customPolicy.keyPrefix}/${uuid}-${filename}`);
});
});
});