From ecb92f8204e78d768e509a23d828e17e8c89fa6f Mon Sep 17 00:00:00 2001
From: Tania Fecheta <tania.fecheta@thinslices.com>
Date: Wed, 12 Dec 2018 16:45:02 +0200
Subject: [PATCH] test(collections/patch.test): add test to handle collection
 not found case

---
 .../src/routes/collections/patch.js           |  5 +++++
 .../src/tests/collections/patch.test.js       | 21 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/packages/component-manuscript-manager/src/routes/collections/patch.js b/packages/component-manuscript-manager/src/routes/collections/patch.js
index ceee01bb2..fd8dba25c 100644
--- a/packages/component-manuscript-manager/src/routes/collections/patch.js
+++ b/packages/component-manuscript-manager/src/routes/collections/patch.js
@@ -15,6 +15,11 @@ module.exports = models => async (req, res) => {
     }
     const canArchive = await authsome.can(req.user, 'PATCH', target)
 
+    if (!collection)
+      return res.status(404).json({
+        error: `Collection not found`,
+      })
+
     if (!canArchive)
       return res.status(403).json({
         error: 'Unauthorized',
diff --git a/packages/component-manuscript-manager/src/tests/collections/patch.test.js b/packages/component-manuscript-manager/src/tests/collections/patch.test.js
index f09c8b294..7a0ed8ad3 100644
--- a/packages/component-manuscript-manager/src/tests/collections/patch.test.js
+++ b/packages/component-manuscript-manager/src/tests/collections/patch.test.js
@@ -26,6 +26,26 @@ describe('Patch colection route handler', () => {
     models = Model.build(testFixtures)
   })
 
+  it('should return an error if the manuscript does not exist', 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: '123',
+      },
+    })
+
+    expect(res.statusCode).toBe(404)
+    const data = JSON.parse(res._getData())
+    expect(data.error).toBe('Collection not found')
+  })
+
   it('should return success when deleting a manuscript as admin', async () => {
     const { admin } = testFixtures.users
     const { collection } = testFixtures.collections
@@ -64,6 +84,7 @@ describe('Patch colection route handler', () => {
     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
-- 
GitLab