-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
163 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import type { Response, Request } from 'express'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { uploadFiles } from '../controllers/filesController'; | ||
import type { User } from '../entities/user'; | ||
import { AwsS3 } from '../fileUpload/s3'; | ||
import { AppError } from '../middlewares/handleErrors'; | ||
import { appDataSource, fakeUser } from './mock'; | ||
|
||
beforeAll(() => { | ||
return appDataSource.initialize(); | ||
}); | ||
beforeEach(() => {}); | ||
afterAll(() => { | ||
return appDataSource.destroy(); | ||
}); | ||
|
||
const dummyPdf = fs.readFileSync(path.join(__dirname, 'files/dummy.pdf')); | ||
describe('Upload files', () => { | ||
test('Should return an url array', async () => { | ||
jest.spyOn(AwsS3.prototype, 'uploadFile').mockImplementationOnce(() => { | ||
return Promise.resolve('url/test'); | ||
}); | ||
jest.spyOn(AwsS3.prototype, 'uploadS3File').mockImplementationOnce(() => { | ||
return Promise.resolve('url/test'); | ||
}); | ||
|
||
const mockRequest: Partial<Request> = { | ||
user: fakeUser as User, | ||
files: [ | ||
{ | ||
buffer: dummyPdf, | ||
fieldname: 'files', | ||
filename: 'dummy.pdf', | ||
mimetype: 'application/pdf', | ||
originalname: 'dummy.pdf', | ||
path: 'server/__tests__/files', | ||
size: 1000, | ||
destination: 'server/__tests__/files', | ||
} as Express.Multer.File, | ||
], | ||
}; | ||
|
||
const res = {} as unknown as Response; | ||
res.json = jest.fn(); | ||
res.status = jest.fn(() => res); // chained | ||
|
||
await uploadFiles(mockRequest as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(res.json).toHaveBeenCalledWith(['url/test']); | ||
}); | ||
test('Should throw - files are missing -', async () => { | ||
const mockRequest: Partial<Request> = { | ||
user: fakeUser as User, | ||
files: [], | ||
}; | ||
|
||
const res = {} as unknown as Response; | ||
res.json = jest.fn(); | ||
res.status = jest.fn(() => res); // chained | ||
|
||
await uploadFiles(mockRequest as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.json).toHaveBeenCalledWith('Files are missing'); | ||
}); | ||
test('User forbiden', async () => { | ||
const mockRequest: Partial<Request> = { | ||
files: [], | ||
}; | ||
|
||
const res = {} as unknown as Response; | ||
res.json = jest.fn(); | ||
res.status = jest.fn(() => res); // chained | ||
|
||
await uploadFiles(mockRequest as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.json).toHaveBeenCalledWith('Forbidden'); | ||
}); | ||
test('Should throw an app error', async () => { | ||
jest.spyOn(AwsS3.prototype, 'uploadFile').mockImplementationOnce(() => { | ||
throw new AppError('Yolo', 0); | ||
}); | ||
|
||
const mockRequest: Partial<Request> = { | ||
user: fakeUser as User, | ||
files: [ | ||
{ | ||
buffer: dummyPdf, | ||
fieldname: 'files', | ||
filename: 'dummy.pdf', | ||
mimetype: 'application/pdf', | ||
originalname: 'dummy.pdf', | ||
path: 'server/__tests__/files', | ||
size: 1000, | ||
destination: 'server/__tests__/files', | ||
} as Express.Multer.File, | ||
], | ||
}; | ||
|
||
const res = {} as unknown as Response; | ||
res.json = jest.fn(); | ||
res.status = jest.fn(() => res); // chained | ||
|
||
await uploadFiles(mockRequest as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.json).toHaveBeenCalledWith('Yolo'); | ||
}); | ||
test('Should throw an unknown error', async () => { | ||
jest.spyOn(AwsS3.prototype, 'uploadFile').mockImplementationOnce(() => { | ||
throw new Error('Yolo'); | ||
}); | ||
|
||
const mockRequest: Partial<Request> = { | ||
user: fakeUser as User, | ||
files: [ | ||
{ | ||
buffer: dummyPdf, | ||
fieldname: 'files', | ||
filename: 'dummy.pdf', | ||
mimetype: 'application/pdf', | ||
originalname: 'dummy.pdf', | ||
path: 'server/__tests__/files', | ||
size: 1000, | ||
destination: 'server/__tests__/files', | ||
} as Express.Multer.File, | ||
], | ||
}; | ||
|
||
const res = {} as unknown as Response; | ||
res.json = jest.fn(); | ||
res.status = jest.fn(() => res); // chained | ||
|
||
await uploadFiles(mockRequest as Request, res as Response); | ||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalledWith('Internal server error'); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Router } from 'express'; | ||
import multer from 'multer'; | ||
|
||
import { UserType } from '../../types/user.type'; | ||
import { uploadFiles } from '../controllers/filesController'; | ||
import { diskStorageToImages } from '../controllers/multer'; | ||
import { fileUplad } from '../controllers/multer'; | ||
import { authenticate } from '../middlewares/authenticate'; | ||
import { handleErrors } from '../middlewares/handleErrors'; | ||
|
||
export const filesRouter = Router(); | ||
|
||
filesRouter.post('/', multer({ storage: diskStorageToImages }).array('files'), handleErrors(authenticate(UserType.ADMIN)), uploadFiles); | ||
filesRouter.post('/', fileUplad.array('files'), handleErrors(authenticate(UserType.ADMIN)), uploadFiles); |