Skip to content

Commit 982004a

Browse files
authored
Merge pull request Expensify#67697 from samranahm/66829/remove-tooSmallError-from-validateAttachmentFile
Remove minimum file size restriction for attachments
2 parents 8dce6b3 + aff4246 commit 982004a

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/libs/fileDownload/FileUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ const validateAttachment = (file: FileObject, isCheckingMultipleFiles?: boolean,
436436
return isCheckingMultipleFiles ? CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE_MULTIPLE : CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE;
437437
}
438438

439-
if ((file?.size ?? 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) {
439+
if (isValidatingReceipt && (file?.size ?? 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) {
440440
return CONST.FILE_VALIDATION_ERRORS.FILE_TOO_SMALL;
441441
}
442442

tests/unit/FileUtilsTest.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import * as FileUtils from '../../src/libs/fileDownload/FileUtils';
44

55
jest.useFakeTimers();
66

7+
const createMockFile = (name: string, size: number) => ({
8+
name,
9+
size,
10+
});
11+
712
describe('FileUtils', () => {
813
describe('splitExtensionFromFileName', () => {
914
it('should return correct file name and extension', () => {
@@ -38,4 +43,42 @@ describe('FileUtils', () => {
3843
expect(actualFileName).toEqual(expectedFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_'));
3944
});
4045
});
46+
47+
describe('validateAttachment', () => {
48+
it('should not return FILE_TOO_SMALL when validating small attachment', () => {
49+
const file = createMockFile('file.csv', CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE - 1);
50+
const error = FileUtils.validateAttachment(file, false, false);
51+
expect(error).not.toBe(CONST.FILE_VALIDATION_ERRORS.FILE_TOO_SMALL);
52+
});
53+
54+
it('should return FILE_TOO_SMALL when validating small receipt', () => {
55+
const file = createMockFile('receipt.jpg', CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE - 1);
56+
const error = FileUtils.validateAttachment(file, false, true);
57+
expect(error).toBe(CONST.FILE_VALIDATION_ERRORS.FILE_TOO_SMALL);
58+
});
59+
60+
it('should return FILE_TOO_LARGE for large non-image file', () => {
61+
const file = createMockFile('file.pdf', CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE + 1);
62+
const error = FileUtils.validateAttachment(file);
63+
expect(error).toBe(CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE);
64+
});
65+
66+
it('should return FILE_TOO_LARGE_MULTIPLE when checking multiple files', () => {
67+
const file = createMockFile('file.pdf', CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE + 1);
68+
const error = FileUtils.validateAttachment(file, true);
69+
expect(error).toBe(CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE_MULTIPLE);
70+
});
71+
72+
it('should return WRONG_FILE_TYPE for invalid receipt extension', () => {
73+
const file = createMockFile('receipt.exe', CONST.API_ATTACHMENT_VALIDATIONS.RECEIPT_MAX_SIZE - 1);
74+
const error = FileUtils.validateAttachment(file, false, true);
75+
expect(error).toBe(CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE);
76+
});
77+
78+
it('should return empty string for valid image receipt', () => {
79+
const file = createMockFile('receipt.jpg', CONST.API_ATTACHMENT_VALIDATIONS.RECEIPT_MAX_SIZE - 1);
80+
const error = FileUtils.validateAttachment(file, false, true);
81+
expect(error).toBe('');
82+
});
83+
});
4184
});

0 commit comments

Comments
 (0)