diff --git a/packages/component-manuscript-manager/src/routes/collections/patch.js b/packages/component-manuscript-manager/src/routes/collections/patch.js index f5fa4cd7b31d0015a6679c47c5900be4c6370213..ceee01bb26ec3fec72c5684458cef1cb2c7dcf61 100644 --- a/packages/component-manuscript-manager/src/routes/collections/patch.js +++ b/packages/component-manuscript-manager/src/routes/collections/patch.js @@ -17,12 +17,12 @@ module.exports = models => async (req, res) => { if (!canArchive) return res.status(403).json({ - error: 'Unauthorized.', + error: 'Unauthorized', }) if (!has(collection, 'status')) { return res.status(400).json({ - error: 'You cannot archive manuscripts while in draft.', + error: 'You cannot delete manuscripts while in draft.', }) } diff --git a/packages/component-manuscript-manager/src/tests/collections/patch.test.js b/packages/component-manuscript-manager/src/tests/collections/patch.test.js new file mode 100644 index 0000000000000000000000000000000000000000..f09c8b29410befd65448d3f10afb278563a1421f --- /dev/null +++ b/packages/component-manuscript-manager/src/tests/collections/patch.test.js @@ -0,0 +1,126 @@ +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' +process.env.SUPPRESS_NO_CONFIG_WARNING = true + +const { cloneDeep } = require('lodash') +const fixturesService = require('pubsweet-component-fixture-service') +const requests = require('../requests') + +const { Model, fixtures } = fixturesService + +const reqBody = { + comments: {}, +} + +const path = '../routes/collections/patch' +const route = { + path: '/api/collections/:collectionId/archive', +} + +describe('Patch colection route handler', () => { + let testFixtures = {} + let body = {} + let models + beforeEach(() => { + testFixtures = cloneDeep(fixtures) + body = cloneDeep(reqBody) + models = Model.build(testFixtures) + }) + + it('should return success when deleting a manuscript as admin', async () => { + const { admin } = testFixtures.users + const { collection } = testFixtures.collections + collection.status = 'underReview' + const res = await requests.sendRequest({ + userId: admin.id, + body, + models, + route, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(200) + expect(JSON.parse(res._getData()).status).toEqual('deleted') + }) + + it('should return an error when deleting a manuscript as EiC', async () => { + const { editorInChief } = testFixtures.users + const { collection } = testFixtures.collections + collection.status = 'underReview' + const res = await requests.sendRequest({ + userId: editorInChief.id, + body, + models, + route, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(403) + const data = JSON.parse(res._getData()) + expect(data.error).toBe('Unauthorized') + }) + it('should return an error when deleting a manuscript as author', async () => { + const { author } = testFixtures.users + const { collection } = testFixtures.collections + collection.status = 'underReview' + const res = await requests.sendRequest({ + userId: author.id, + body, + models, + route, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(403) + const data = JSON.parse(res._getData()) + expect(data.error).toBe('Unauthorized') + }) + + it('should return an error when deleting a manuscript while in draft', async () => { + const { admin } = testFixtures.users + const { collection } = testFixtures.collections + delete collection.status + const res = await requests.sendRequest({ + userId: admin.id, + body, + models, + route, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toBe('You cannot delete manuscripts while in draft.') + }) + + it('should return an error when deleting a manuscript already deleted', async () => { + const { admin } = testFixtures.users + const { collection } = testFixtures.collections + collection.status = 'deleted' + const res = await requests.sendRequest({ + userId: admin.id, + body, + models, + route, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toBe('Manuscript already deleted') + }) +})