Skip to content

Commit

Permalink
Added few more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SujithThirumalaisamy committed Feb 24, 2025
1 parent 7eb56e0 commit c33b248
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BadRequestException } from '@nestjs/common';

import { FileFolder } from 'src/engine/core-modules/file/interfaces/file-folder.interface';

import {
Expand Down Expand Up @@ -61,19 +63,30 @@ describe('FileUtils', () => {
});

describe('checkFileFolder', () => {
it('should return root folder', () => {
const filePath = `${FileFolder.Attachment}/file.png`;
const rootFolder = checkFileFolder(filePath);

expect(rootFolder).toBe(`${FileFolder.Attachment}`);
it('should return the root folder when it is allowed', () => {
expect(checkFileFolder(`${FileFolder.Attachment}/file.txt`)).toBe(
FileFolder.Attachment,
);
});

it('should throw an error for invalid root folder', () => {
const folder = `invalid-folder`;
it('should throw BadRequestException for disallowed folders', () => {
expect(() => checkFileFolder('invalid-folder/file.txt')).toThrow(
BadRequestException,
);
});

expect(() => checkFileFolder(folder)).toThrow(
`Folder ${folder} is not allowed`,
it('should sanitize null characters in file path', () => {
expect(() => checkFileFolder('\0invalid-folder/file.txt')).toThrow(
BadRequestException,
);
});

it('should handle edge cases like empty file path', () => {
expect(() => checkFileFolder('')).toThrow(BadRequestException);
});

it('should handle cases where filePath has no folder', () => {
expect(() => checkFileFolder('file.txt')).toThrow(BadRequestException);
});
});
});

0 comments on commit c33b248

Please sign in to comment.