-
Notifications
You must be signed in to change notification settings - Fork 72
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
5 changed files
with
81 additions
and
0 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,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); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -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; |
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,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; |
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