-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into 1702-debug-logging
- Loading branch information
Showing
33 changed files
with
1,198 additions
and
112 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`upload routes GET/customisation/upload should return all uploads 1`] = ` | ||
Array [ | ||
"upload1", | ||
"upload2", | ||
] | ||
`; | ||
|
||
exports[`upload routes POST/customisation/upload should save the upload and return it 1`] = ` | ||
Object { | ||
"__v": 0, | ||
"filename": "f2082bf51b6ef839690485d7153e847a.pdf", | ||
"mimetype": "application/octet-stream", | ||
"originalname": "gadgets-01.pdf", | ||
"size": 171411271, | ||
} | ||
`; |
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,14 +1,20 @@ | ||
import db from 'api/utils/testing_db'; | ||
|
||
const entityId = db.id(); | ||
const uploadId = db.id(); | ||
|
||
export default { | ||
entities: [ | ||
{_id: entityId, sharedId: 'id', language: 'es', title: 'Gadgets 01 ES', toc: [{_id: db.id(), label: 'existingToc'}]}, | ||
{_id: db.id(), sharedId: 'id', language: 'en', title: 'Gadgets 01 EN'} | ||
{ _id: entityId, sharedId: 'id', language: 'es', title: 'Gadgets 01 ES', toc: [{ _id: db.id(), label: 'existingToc' }] }, | ||
{ _id: db.id(), sharedId: 'id', language: 'en', title: 'Gadgets 01 EN' } | ||
], | ||
uploads: [ | ||
{ _id: uploadId, originalname: 'upload1' }, | ||
{ _id: db.id(), originalname: 'upload2' }, | ||
] | ||
}; | ||
|
||
export { | ||
entityId | ||
entityId, | ||
uploadId | ||
}; |
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,62 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
import { catchErrors } from 'api/utils/jasmineHelpers'; | ||
import db from 'api/utils/testing_db'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import uploads from '../uploads'; | ||
import uploadsModel from '../uploadsModel'; | ||
import { uploadDocumentsPath } from '../../config/paths'; | ||
|
||
describe('uploads', () => { | ||
let file; | ||
const uploadId = db.id(); | ||
|
||
beforeEach((done) => { | ||
file = { | ||
fieldname: 'file', | ||
originalname: 'gadgets-01.pdf', | ||
encoding: '7bit', | ||
mimetype: 'application/octet-stream', | ||
destination: `${__dirname}/uploads/`, | ||
filename: 'f2082bf51b6ef839690485d7153e847a.pdf', | ||
path: `${__dirname}/uploads/f2082bf51b6ef839690485d7153e847a.pdf`, | ||
size: 171411271 | ||
}; | ||
|
||
db.clearAllAndLoad({ uploads: [{ _id: uploadId, filename: 'upload.filename' }] }).then(done).catch(catchErrors(done)); | ||
}); | ||
|
||
afterAll((done) => { | ||
db.disconnect().then(done); | ||
}); | ||
|
||
describe('save', () => { | ||
it('should save file passed', async () => { | ||
let saved = await uploads.save(file); | ||
saved = await uploadsModel.getById(saved._id); | ||
|
||
expect(saved.creationDate).toBeDefined(); | ||
|
||
expect(saved).toMatchObject({ | ||
originalname: 'gadgets-01.pdf', | ||
mimetype: 'application/octet-stream', | ||
filename: 'f2082bf51b6ef839690485d7153e847a.pdf', | ||
size: 171411271 | ||
}); | ||
}); | ||
}); | ||
|
||
describe('delete', () => { | ||
fit('should delete the file', async () => { | ||
fs.writeFileSync(path.join(uploadDocumentsPath, 'upload.filename')); | ||
|
||
expect(fs.existsSync(path.join(uploadDocumentsPath, 'upload.filename'))).toBe(true); | ||
|
||
await uploads.delete(uploadId); | ||
|
||
expect(fs.existsSync(path.join(uploadDocumentsPath, 'upload.filename'))).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
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,29 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { uploadDocumentsPath } from '../config/paths'; | ||
import model from './uploadsModel'; | ||
|
||
const deleteFile = filename => new Promise((resolve, reject) => { | ||
fs.unlink(path.join(uploadDocumentsPath, filename), (err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
|
||
export default { | ||
save: model.save.bind(model), | ||
|
||
get: model.get.bind(model), | ||
|
||
async delete(_id) { | ||
const upload = await model.getById(_id); | ||
|
||
await model.delete(_id); | ||
await deleteFile(upload.filename); | ||
|
||
return upload; | ||
} | ||
}; |
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,17 @@ | ||
import mongoose from 'mongoose'; | ||
import date from 'api/utils/date.js'; | ||
|
||
import instanceModel from 'api/odm'; | ||
|
||
const uploadSchema = new mongoose.Schema({ | ||
originalname: String, | ||
filename: String, | ||
mimetype: String, | ||
size: Number, | ||
creationDate: { type: Number, default: date.currentUTC }, | ||
}, { emitIndexErrors: true }); | ||
|
||
const schema = mongoose.model('uploads', uploadSchema); | ||
const Model = instanceModel(schema); | ||
|
||
export default Model; |
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
Oops, something went wrong.