immich/server/apps/immich/src/config/profile-image-upload.config.spec.ts
2022-11-03 18:55:13 -05:00

114 lines
3.2 KiB
TypeScript

import { Request } from 'express';
import * as fs from 'fs';
import { multerUtils } from './profile-image-upload.config';
const { fileFilter, destination, filename } = multerUtils;
const mock = {
req: {} as Request,
userRequest: {
user: {
id: 'test-user',
},
} as Request,
file: { originalname: 'test.jpg' } as Express.Multer.File,
};
jest.mock('fs');
describe('profileImageUploadOption', () => {
let callback: jest.Mock;
let existsSync: jest.Mock;
let mkdirSync: jest.Mock;
beforeEach(() => {
jest.mock('fs');
mkdirSync = fs.mkdirSync as jest.Mock;
existsSync = fs.existsSync as jest.Mock;
callback = jest.fn();
existsSync.mockImplementation(() => true);
});
afterEach(() => {
jest.resetModules();
});
describe('fileFilter', () => {
it('should require a user', () => {
fileFilter(mock.req, mock.file, callback);
expect(callback).toHaveBeenCalled();
const [error, name] = callback.mock.calls[0];
expect(error).toBeDefined();
expect(name).toBeUndefined();
});
it('should allow images', async () => {
const file = { mimetype: 'image/jpeg', originalname: 'test.jpg' } as any;
fileFilter(mock.userRequest, file, callback);
expect(callback).toHaveBeenCalledWith(null, true);
});
it('should not allow gifs', async () => {
const file = { mimetype: 'image/gif', originalname: 'test.gif' } as any;
const callback = jest.fn();
fileFilter(mock.userRequest, file, callback);
expect(callback).toHaveBeenCalled();
const [error, accepted] = callback.mock.calls[0];
expect(error).toBeDefined();
expect(accepted).toBe(false);
});
});
describe('destination', () => {
it('should require a user', () => {
destination(mock.req, mock.file, callback);
expect(callback).toHaveBeenCalled();
const [error, name] = callback.mock.calls[0];
expect(error).toBeDefined();
expect(name).toBeUndefined();
});
it('should create non-existing directories', () => {
existsSync.mockImplementation(() => false);
destination(mock.userRequest, mock.file, callback);
expect(existsSync).toHaveBeenCalled();
expect(mkdirSync).toHaveBeenCalled();
});
it('should return the destination', () => {
destination(mock.userRequest, mock.file, callback);
expect(mkdirSync).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledWith(null, './upload/test-user/profile');
});
});
describe('filename', () => {
it('should require a user', () => {
filename(mock.req, mock.file, callback);
expect(callback).toHaveBeenCalled();
const [error, name] = callback.mock.calls[0];
expect(error).toBeDefined();
expect(name).toBeUndefined();
});
it('should return the filename', () => {
filename(mock.userRequest, mock.file, callback);
expect(mkdirSync).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledWith(null, 'test-user.jpg');
});
it('should sanitize the filename', () => {
filename(mock.userRequest, { ...mock.file, originalname: 'test.j\u0000pg' }, callback);
expect(callback).toHaveBeenCalledWith(null, 'test-user.jpg');
});
});
});