Skip to content

Commit

Permalink
1706-file-size-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 10, 2020
1 parent fafcd34 commit 54c4223
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
45 changes: 41 additions & 4 deletions __tests__/FileUpload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const path = require('path');
const FileAttachment = require('../src/file/FileAttachment');
const sequelize = require('../src/config/database');
const fs = require('fs');
const en = require('../locales/en/translation.json');
const tr = require('../locales/tr/translation.json');
const config = require('config');

const { uploadDir, attachmentDir } = config;
Expand All @@ -18,10 +20,12 @@ beforeEach(async () => {
await FileAttachment.destroy({ truncate: true });
});

const uploadFile = (file = 'test-png.png') => {
return request(app)
.post('/api/1.0/hoaxes/attachments')
.attach('file', path.join('.', '__tests__', 'resources', file));
const uploadFile = (file = 'test-png.png', options = {}) => {
const agent = request(app).post('/api/1.0/hoaxes/attachments');
if (options.language) {
agent.set('Accept-Language', options.language);
}
return agent.attach('file', path.join('.', '__tests__', 'resources', file));
};

describe('Upload File for Hoax', () => {
Expand Down Expand Up @@ -82,4 +86,37 @@ describe('Upload File for Hoax', () => {
expect(fs.existsSync(filePath)).toBe(true);
}
);

it('returns 400 when uploaded file size is bigger than 5mb', async () => {
const fiveMB = 5 * 1024 * 1024;
const filePath = path.join('.', '__tests__', 'resources', 'random-file');
fs.writeFileSync(filePath, 'a'.repeat(fiveMB) + 'a');
const response = await uploadFile('random-file');
expect(response.status).toBe(400);
fs.unlinkSync(filePath);
});
it('returns 200 when uploaded file size is 5mb', async () => {
const fiveMB = 5 * 1024 * 1024;
const filePath = path.join('.', '__tests__', 'resources', 'random-file');
fs.writeFileSync(filePath, 'a'.repeat(fiveMB));
const response = await uploadFile('random-file');
expect(response.status).toBe(200);
fs.unlinkSync(filePath);
});
it.each`
language | message
${'tr'} | ${tr.attachment_size_limit}
${'en'} | ${en.attachment_size_limit}
`('returns $message when attachment size is bigger than 5mb', async ({ language, message }) => {
const fiveMB = 5 * 1024 * 1024;
const filePath = path.join('.', '__tests__', 'resources', 'random-file');
fs.writeFileSync(filePath, 'a'.repeat(fiveMB) + 'a');
const nowInMillis = Date.now();
const response = await uploadFile('random-file', { language });
const error = response.body;
expect(error.path).toBe('/api/1.0/hoaxes/attachments');
expect(error.message).toBe(message);
expect(error.timestamp).toBeGreaterThan(nowInMillis);
fs.unlinkSync(filePath);
});
});
3 changes: 2 additions & 1 deletion locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"unsupported_image_file": "Only JPEG or PNG files are allowed",
"unauthorized_hoax_submit": "You are not authorized to post hoax",
"hoax_submit_success": "Hoax is saved",
"hoax_content_size": "Hoax must be min 10 and max 5000 characters"
"hoax_content_size": "Hoax must be min 10 and max 5000 characters",
"attachment_size_limit": "Uploaded file cannot be bigger than 5MB"
}
3 changes: 2 additions & 1 deletion locales/tr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"unsupported_image_file": "Sadece JPEG ya da PNG dosyalarını kullanabilirsiniz",
"unauthorized_hoax_submit": "Efsane göndermeye yetkiniz bulunmamaktadır",
"hoax_submit_success": "Efsaneniz kaydedildi",
"hoax_content_size": "Gönderiniz en az 10 en fazla 5000 karakter olmalı"
"hoax_content_size": "Gönderiniz en az 10 en fazla 5000 karakter olmalı",
"attachment_size_limit": "Yüklediğiniz dosya 5MB'dan büyük olamaz"
}
17 changes: 13 additions & 4 deletions src/file/FileRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ const express = require('express');
const router = express.Router();
const FileService = require('./FileService');
const multer = require('multer');
const FileSizeException = require('./FileSizeException');

const upload = multer();
const FIVE_MB = 5 * 1024 * 1024;

router.post('/api/1.0/hoaxes/attachments', upload.single('file'), async (req, res) => {
await FileService.saveAttachment(req.file);
res.send();
const upload = multer({ limits: { fileSize: FIVE_MB } }).single('file');

router.post('/api/1.0/hoaxes/attachments', (req, res, next) => {
upload(req, res, async (err) => {
if (err) {
return next(new FileSizeException());
}

await FileService.saveAttachment(req.file);
res.send();
});
});

module.exports = router;
4 changes: 4 additions & 0 deletions src/file/FileSizeException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function FileSizeException() {
this.status = 400;
this.message = 'attachment_size_limit';
};

0 comments on commit 54c4223

Please sign in to comment.