From 4f4d8efacca775fb0add61ce61925708848c57df Mon Sep 17 00:00:00 2001
From: Tania Fecheta <tania.fecheta@thinslices.com>
Date: Wed, 12 Dec 2018 16:02:46 +0200
Subject: [PATCH] test(collections/patch): add tests for soft delete

---
 .../src/routes/collections/patch.js           |   4 +-
 .../src/tests/collections/patch.test.js       | 126 ++++++++++++++++++
 2 files changed, 128 insertions(+), 2 deletions(-)
 create mode 100644 packages/component-manuscript-manager/src/tests/collections/patch.test.js

diff --git a/packages/component-manuscript-manager/src/routes/collections/patch.js b/packages/component-manuscript-manager/src/routes/collections/patch.js
index f5fa4cd7b..ceee01bb2 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 000000000..f09c8b294
--- /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')
+  })
+})
-- 
GitLab