diff --git a/__tests__/FileUpload.spec.js b/__tests__/FileUpload.spec.js new file mode 100644 index 0000000..13c58c4 --- /dev/null +++ b/__tests__/FileUpload.spec.js @@ -0,0 +1,36 @@ +const request = require('supertest'); +const app = require('../src/app'); +const path = require('path'); +const FileAttachment = require('../src/file/FileAttachment'); +const sequelize = require('../src/config/database'); + +beforeAll(async () => { + if (process.env.NODE_ENV === 'test') { + await sequelize.sync(); + } +}); + +beforeEach(async () => { + await FileAttachment.destroy({ truncate: true }); +}); + +const uploadFile = () => { + return request(app) + .post('/api/1.0/hoaxes/attachments') + .attach('file', path.join('.', '__tests__', 'resources', 'test-png.png')); +}; + +describe('Upload File for Hoax', () => { + it('returns 200 ok after successful upload', async () => { + const response = await uploadFile(); + expect(response.status).toBe(200); + }); + it('saves dynamicFilename, uploadDate as attachment object in database', async () => { + const beforeSubmit = Date.now(); + await uploadFile(); + const attachments = await FileAttachment.findAll(); + const attachment = attachments[0]; + expect(attachment.filename).not.toBe('test-png.png'); + expect(attachment.uploadDate.getTime()).toBeGreaterThan(beforeSubmit); + }); +}); diff --git a/src/app.js b/src/app.js index 644436f..31f8116 100644 --- a/src/app.js +++ b/src/app.js @@ -10,6 +10,7 @@ const FileService = require('./file/FileService'); const config = require('config'); const path = require('path'); const HoaxRouter = require('./hoax/HoaxRouter'); +const FileRouter = require('./file/FileRouter'); const { uploadDir, profileDir } = config; const profileFolder = path.join('.', uploadDir, profileDir); @@ -47,6 +48,7 @@ app.use(tokenAuthentication); app.use(UserRouter); app.use(AuthenticationRouter); app.use(HoaxRouter); +app.use(FileRouter); app.use(errorHandler); diff --git a/src/file/FileAttachment.js b/src/file/FileAttachment.js new file mode 100644 index 0000000..27f7139 --- /dev/null +++ b/src/file/FileAttachment.js @@ -0,0 +1,24 @@ +const Sequelize = require('sequelize'); +const sequelize = require('../config/database'); + +const Model = Sequelize.Model; + +class FileAttachment extends Model {} + +FileAttachment.init( + { + filename: { + type: Sequelize.STRING, + }, + uploadDate: { + type: Sequelize.DATE, + }, + }, + { + sequelize, + modelName: 'fileAttachment', + timestamps: false, + } +); + +module.exports = FileAttachment; diff --git a/src/file/FileRouter.js b/src/file/FileRouter.js new file mode 100644 index 0000000..bf8359e --- /dev/null +++ b/src/file/FileRouter.js @@ -0,0 +1,10 @@ +const express = require('express'); +const router = express.Router(); +const FileService = require('./FileService'); + +router.post('/api/1.0/hoaxes/attachments', async (req, res) => { + await FileService.saveAttachment(); + res.send(); +}); + +module.exports = router; diff --git a/src/file/FileService.js b/src/file/FileService.js index dcff563..925eec9 100644 --- a/src/file/FileService.js +++ b/src/file/FileService.js @@ -3,6 +3,7 @@ const path = require('path'); const config = require('config'); const { randomString } = require('../shared/generator'); const FileType = require('file-type'); +const FileAttachment = require('./FileAttachment'); const { uploadDir, profileDir, attachmentDir } = config; const profileFolder = path.join('.', uploadDir, profileDir); @@ -41,10 +42,18 @@ const isSupportedFileType = async (buffer) => { return !type ? false : type.mime === 'image/png' || type.mime === 'image/jpeg'; }; +const saveAttachment = async () => { + await FileAttachment.create({ + filename: randomString(32), + uploadDate: new Date(), + }); +}; + module.exports = { createFolders, saveProfileImage, deleteProfileImage, isLessThan2MB, isSupportedFileType, + saveAttachment, };