From 94538c920894db8c767f35229437ddfc1ad6eb22 Mon Sep 17 00:00:00 2001
From: Iosif Boanca <iosif.boanca@thinslices.com>
Date: Wed, 31 Oct 2018 13:42:52 +0200
Subject: [PATCH] delete change

---
 .../src/fixtures/collections.js               |  3 +
 .../src/fixtures/fragments.js                 | 15 ++++-
 .../src/fixtures/teams.js                     |  4 ++
 .../src/routes/collections/delete.js          | 15 +++--
 .../src/tests/collections/delete.test.js      | 65 +++++++++++++++++++
 5 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 packages/component-manuscript-manager/src/tests/collections/delete.test.js

diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js
index 4984c9f07..4267f01c1 100644
--- a/packages/component-fixture-manager/src/fixtures/collections.js
+++ b/packages/component-fixture-manager/src/fixtures/collections.js
@@ -10,6 +10,7 @@ const chance = new Chance()
 const collections = {
   collection: {
     id: standardCollID,
+    delete: jest.fn(),
     title: chance.sentence(),
     type: 'collection',
     fragments: [fragment.id],
@@ -52,6 +53,7 @@ const collections = {
   },
   collection1: {
     id: standardCollID,
+    delete: jest.fn(),
     title: chance.sentence(),
     type: 'collection',
     fragments: [fragment.id],
@@ -94,6 +96,7 @@ const collections = {
   },
   collectionReviewCompleted: {
     id: collectionReviewCompletedID,
+    delete: jest.fn(),
     type: 'collection',
     owners: [user.id],
     status: 'reviewCompleted',
diff --git a/packages/component-fixture-manager/src/fixtures/fragments.js b/packages/component-fixture-manager/src/fixtures/fragments.js
index c8931dd9b..5696e1701 100644
--- a/packages/component-fixture-manager/src/fixtures/fragments.js
+++ b/packages/component-fixture-manager/src/fixtures/fragments.js
@@ -18,6 +18,12 @@ const chance = new Chance()
 const fragments = {
   fragment: {
     id: chance.guid(),
+    delete: jest.fn(),
+    files: {
+      manuscripts: [{ id: chance.guid() }],
+      coverLetter: [],
+      supplementary: [],
+    },
     collectionId: standardCollID,
     metadata: {
       title: chance.sentence(),
@@ -180,6 +186,12 @@ const fragments = {
   },
   noParentFragment: {
     id: chance.guid(),
+    delete: jest.fn(),
+    files: {
+      manuscripts: [{ id: chance.guid() }],
+      coverLetter: [],
+      supplementary: [],
+    },
     collectionId: '',
     metadata: {
       title: chance.sentence(),
@@ -199,9 +211,9 @@ const fragments = {
   },
   reviewCompletedFragment: {
     id: chance.guid(),
+    delete: jest.fn(),
     type: 'fragment',
     files: {
-      coverLetter: [],
       manuscripts: [
         {
           id:
@@ -211,6 +223,7 @@ const fragments = {
           originalName: 'LinkedInProfileDemystified.pdf',
         },
       ],
+      coverLetter: [],
       supplementary: [],
     },
     owners: [user.id],
diff --git a/packages/component-fixture-manager/src/fixtures/teams.js b/packages/component-fixture-manager/src/fixtures/teams.js
index 5191421c2..c100c2a37 100644
--- a/packages/component-fixture-manager/src/fixtures/teams.js
+++ b/packages/component-fixture-manager/src/fixtures/teams.js
@@ -33,6 +33,7 @@ const teams = {
     },
     members: [handlingEditor.id],
     save: jest.fn(() => teams.heTeam),
+    delete: jest.fn(),
     updateProperties: jest.fn(() => teams.heTeam),
     id: heTeamID,
   },
@@ -49,6 +50,7 @@ const teams = {
     },
     members: [reviewer.id, inactiveReviewer.id, answerReviewer.id],
     save: jest.fn(() => teams.revTeam),
+    delete: jest.fn(),
     updateProperties: jest.fn(() => teams.revTeam),
     id: revTeamID,
   },
@@ -65,6 +67,7 @@ const teams = {
     },
     members: [reviewer.id, answerReviewer.id, recReviewer.id],
     save: jest.fn(() => teams.revRecommendationTeam),
+    delete: jest.fn(),
     updateProperties: jest.fn(() => teams.revRecommendationTeam),
     id: revRecommendationTeamID,
   },
@@ -81,6 +84,7 @@ const teams = {
     },
     members: [submittingAuthor.id],
     save: jest.fn(() => teams.authorTeam),
+    delete: jest.fn(),
     updateProperties: jest.fn(() => teams.authorTeam),
     id: authorTeamID,
   },
diff --git a/packages/component-manuscript-manager/src/routes/collections/delete.js b/packages/component-manuscript-manager/src/routes/collections/delete.js
index 05b9e042f..62ed1fc8c 100644
--- a/packages/component-manuscript-manager/src/routes/collections/delete.js
+++ b/packages/component-manuscript-manager/src/routes/collections/delete.js
@@ -1,4 +1,4 @@
-const { get, remove, concat } = require('lodash')
+const { get, remove, concat, has } = require('lodash')
 const config = require('config')
 
 const {
@@ -19,6 +19,12 @@ module.exports = models => async (req, res) => {
   try {
     collection = await models.Collection.find(collectionId)
 
+    if (has(collection, 'status')) {
+      return res.status(400).json({
+        error: 'You can only delete manuscripts while in draft.',
+      })
+    }
+
     const fragmentId = collection.fragments[0]
     fragment = await models.Fragment.find(fragmentId)
 
@@ -48,17 +54,18 @@ module.exports = models => async (req, res) => {
             return user.save()
           }),
         )
-
         return team.delete()
       }),
     )
 
-    const fileKeys = concat(
+    let fileKeys = concat(
       fragment.files.manuscripts,
       fragment.files.coverLetter,
       fragment.files.supplementary,
       fragmentId,
-    ).map(file => file.id)
+    )
+
+    fileKeys = fileKeys.map(file => file.id)
 
     if (fileKeys.length > 1) {
       await deleteFilesS3({ fileKeys, s3Config })
diff --git a/packages/component-manuscript-manager/src/tests/collections/delete.test.js b/packages/component-manuscript-manager/src/tests/collections/delete.test.js
new file mode 100644
index 000000000..cee28293a
--- /dev/null
+++ b/packages/component-manuscript-manager/src/tests/collections/delete.test.js
@@ -0,0 +1,65 @@
+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
+
+jest.mock('pubsweet-component-mts-package/src/PackageManager', () => ({
+  deleteFilesS3: jest.fn(),
+}))
+jest.mock('@pubsweet/component-send-email', () => ({
+  send: jest.fn(),
+}))
+
+const path = '../routes/collections/delete'
+const route = {
+  path: '/api/collections/:collectionId/',
+}
+describe('Delete collections route handler', () => {
+  let testFixtures = {}
+  let models
+  beforeEach(() => {
+    testFixtures = cloneDeep(fixtures)
+    models = Model.build(testFixtures)
+  })
+
+  it('should return an error when deleting a collection which is not in draft', async () => {
+    const { admin } = testFixtures.users
+    const { collection } = testFixtures.collections
+
+    const res = await requests.sendRequest({
+      userId: admin.id,
+      route,
+      models,
+      path,
+      params: {
+        collectionId: collection.id,
+      },
+    })
+
+    expect(res.statusCode).toBe(400)
+    const data = JSON.parse(res._getData())
+    expect(data.error).toBe('You can only delete manuscripts while in draft.')
+  })
+
+  it('should return success when deleting a draft collection', async () => {
+    const { admin } = testFixtures.users
+    const { collection } = testFixtures.collections
+    delete collection.status
+
+    const res = await requests.sendRequest({
+      userId: admin.id,
+      route,
+      models,
+      path,
+      params: {
+        collectionId: collection.id,
+      },
+    })
+
+    expect(res.statusCode).toBe(204)
+  })
+})
-- 
GitLab