From cee0a2f5f46e47aca7049a45348707de32d794db Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebi.mihalache@gmail.com>
Date: Tue, 4 Dec 2018 12:46:15 +0200
Subject: [PATCH] test(recommendations): add collections for revision tests

---
 .../src/fixtures/collectionIDs.js             |   6 +-
 .../src/fixtures/collections.js               | 161 +++++++++++++++++-
 .../src/fixtures/teamIDs.js                   |   3 +-
 .../src/fixtures/teams.js                     |  30 +++-
 .../src/fixtures/users.js                     |   5 +-
 .../strategies/hePublish.js                   |   2 +-
 .../src/tests/collections/get.test.js         |   2 +-
 .../fragmentsRecommendations/post.test.js     |  78 ++++-----
 .../tests/config/authsome-helpers.test.js     |   4 +-
 9 files changed, 232 insertions(+), 59 deletions(-)

diff --git a/packages/component-fixture-manager/src/fixtures/collectionIDs.js b/packages/component-fixture-manager/src/fixtures/collectionIDs.js
index a964c2c49..a6082362b 100644
--- a/packages/component-fixture-manager/src/fixtures/collectionIDs.js
+++ b/packages/component-fixture-manager/src/fixtures/collectionIDs.js
@@ -4,9 +4,13 @@ const chance = new Chance()
 
 module.exports = {
   standardCollID: chance.guid(),
-  collectionReviewCompletedID: chance.guid(),
   collectionNoInvitesID: chance.guid(),
   twoVersionsCollectionId: chance.guid(),
+  minorRevisionCollectionID: chance.guid(),
+  majorRevisionCollectionID: chance.guid(),
+  collectionReviewCompletedID: chance.guid(),
   oneReviewedFragmentCollectionID: chance.guid(),
   noEditorRecomedationCollectionID: chance.guid(),
+  minorRevisionWithoutReviewCollectionID: chance.guid(),
+  majorRevisionWithoutReviewCollectionID: chance.guid(),
 }
diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js
index 0fbff658c..4d9299912 100644
--- a/packages/component-fixture-manager/src/fixtures/collections.js
+++ b/packages/component-fixture-manager/src/fixtures/collections.js
@@ -1,24 +1,35 @@
 const Chance = require('chance')
+
 const {
   user,
   handlingEditor,
   answerHE,
   noRecommendationHE,
 } = require('./userData')
+
 const {
   fragment,
   fragment1,
-  reviewCompletedFragment,
   noInvitesFragment,
+  noInvitesFragment1,
+  minorRevisionWithReview,
+  majorRevisionWithReview,
+  reviewCompletedFragment,
+  minorRevisionWithoutReview,
   noEditorRecomedationFragment,
 } = require('./fragments')
+
 const {
   standardCollID,
-  collectionReviewCompletedID,
   collectionNoInvitesID,
   twoVersionsCollectionId,
+  minorRevisionCollectionID,
+  majorRevisionCollectionID,
+  collectionReviewCompletedID,
   oneReviewedFragmentCollectionID,
   noEditorRecomedationCollectionID,
+  minorRevisionWithoutReviewCollectionID,
+  majorRevisionWithoutReviewCollectionID,
 } = require('./collectionIDs')
 
 const chance = new Chance()
@@ -302,6 +313,152 @@ const collections = {
     status: 'revisionRequested',
     customId: chance.natural({ min: 999999, max: 9999999 }),
   },
+  minorRevisionCollection: {
+    id: minorRevisionCollectionID,
+    delete: jest.fn(),
+    title: chance.sentence(),
+    type: 'collection',
+    fragments: [minorRevisionWithReview.id, noInvitesFragment1.id],
+    owners: [user.id],
+    save: jest.fn(() => collections.minorRevisionCollection),
+    getFragments: jest.fn(() => [minorRevisionWithReview, noInvitesFragment1]),
+    invitations: [
+      {
+        id: chance.guid(),
+        role: 'handlingEditor',
+        hasAnswer: true,
+        isAccepted: true,
+        userId: handlingEditor.id,
+        invitedOn: chance.timestamp(),
+        respondedOn: null,
+      },
+    ],
+    handlingEditor: {
+      id: handlingEditor.id,
+      hasAnswer: true,
+      isAccepted: true,
+      email: handlingEditor.email,
+      invitedOn: chance.timestamp(),
+      respondedOn: chance.timestamp(),
+      name: `${handlingEditor.firstName} ${handlingEditor.lastName}`,
+    },
+    technicalChecks: {
+      token: chance.guid(),
+    },
+    status: 'reviewCompleted',
+    customId: chance.natural({ min: 999999, max: 9999999 }),
+  },
+  minorRevisionWithoutReviewCollection: {
+    id: minorRevisionWithoutReviewCollectionID,
+    delete: jest.fn(),
+    title: chance.sentence(),
+    type: 'collection',
+    fragments: [minorRevisionWithoutReview.id, noInvitesFragment1.id],
+    owners: [user.id],
+    save: jest.fn(() => collections.minorRevisionWithoutReviewCollection),
+    getFragments: jest.fn(() => [
+      minorRevisionWithoutReview,
+      noInvitesFragment1,
+    ]),
+    invitations: [
+      {
+        id: chance.guid(),
+        role: 'handlingEditor',
+        hasAnswer: true,
+        isAccepted: true,
+        userId: handlingEditor.id,
+        invitedOn: chance.timestamp(),
+        respondedOn: null,
+      },
+    ],
+    handlingEditor: {
+      id: handlingEditor.id,
+      hasAnswer: true,
+      isAccepted: true,
+      email: handlingEditor.email,
+      invitedOn: chance.timestamp(),
+      respondedOn: chance.timestamp(),
+      name: `${handlingEditor.firstName} ${handlingEditor.lastName}`,
+    },
+    technicalChecks: {
+      token: chance.guid(),
+    },
+    status: 'reviewCompleted',
+    customId: chance.natural({ min: 999999, max: 9999999 }),
+  },
+  majorRevisionCollection: {
+    id: majorRevisionCollectionID,
+    delete: jest.fn(),
+    title: chance.sentence(),
+    type: 'collection',
+    fragments: [majorRevisionWithReview.id, reviewCompletedFragment.id],
+    owners: [user.id],
+    save: jest.fn(() => collections.minorRevisionCollection),
+    getFragments: jest.fn(() => [
+      majorRevisionWithReview,
+      reviewCompletedFragment,
+    ]),
+    invitations: [
+      {
+        id: chance.guid(),
+        role: 'handlingEditor',
+        hasAnswer: true,
+        isAccepted: true,
+        userId: handlingEditor.id,
+        invitedOn: chance.timestamp(),
+        respondedOn: null,
+      },
+    ],
+    handlingEditor: {
+      id: handlingEditor.id,
+      hasAnswer: true,
+      isAccepted: true,
+      email: handlingEditor.email,
+      invitedOn: chance.timestamp(),
+      respondedOn: chance.timestamp(),
+      name: `${handlingEditor.firstName} ${handlingEditor.lastName}`,
+    },
+    technicalChecks: {
+      token: chance.guid(),
+    },
+    status: 'reviewCompleted',
+    customId: chance.natural({ min: 999999, max: 9999999 }),
+  },
+  majorRevisionWithoutReviewCollection: {
+    id: majorRevisionWithoutReviewCollectionID,
+    delete: jest.fn(),
+    title: chance.sentence(),
+    type: 'collection',
+    fragments: [majorRevisionWithReview.id, noInvitesFragment1.id],
+    owners: [user.id],
+    save: jest.fn(() => collections.majorRevisionWithoutReviewCollection),
+    getFragments: jest.fn(() => [majorRevisionWithReview, noInvitesFragment1]),
+    invitations: [
+      {
+        id: chance.guid(),
+        role: 'handlingEditor',
+        hasAnswer: true,
+        isAccepted: true,
+        userId: handlingEditor.id,
+        invitedOn: chance.timestamp(),
+        respondedOn: null,
+      },
+    ],
+    handlingEditor: {
+      id: handlingEditor.id,
+      hasAnswer: true,
+      isAccepted: true,
+      email: handlingEditor.email,
+      invitedOn: chance.timestamp(),
+      respondedOn: chance.timestamp(),
+      name: `${handlingEditor.firstName} ${handlingEditor.lastName}`,
+    },
+    technicalChecks: {
+      token: chance.guid(),
+    },
+    status: 'reviewCompleted',
+    customId: chance.natural({ min: 999999, max: 9999999 }),
+  },
 }
 
 module.exports = collections
diff --git a/packages/component-fixture-manager/src/fixtures/teamIDs.js b/packages/component-fixture-manager/src/fixtures/teamIDs.js
index 162cdb391..1de042935 100644
--- a/packages/component-fixture-manager/src/fixtures/teamIDs.js
+++ b/packages/component-fixture-manager/src/fixtures/teamIDs.js
@@ -5,9 +5,10 @@ const chance = new Chance()
 module.exports = {
   heTeamID: chance.guid(),
   revTeamID: chance.guid(),
+  rev1TeamID: chance.guid(),
   authorTeamID: chance.guid(),
+  majorRevisionHeTeamID: chance.guid(),
   revRecommendationTeamID: chance.guid(),
-  rev1TeamID: chance.guid(),
   heNoRecommendationTeamID: chance.guid(),
   revNoEditorRecommendationTeamID: chance.guid(),
 }
diff --git a/packages/component-fixture-manager/src/fixtures/teams.js b/packages/component-fixture-manager/src/fixtures/teams.js
index dbd51a4ac..79b650069 100644
--- a/packages/component-fixture-manager/src/fixtures/teams.js
+++ b/packages/component-fixture-manager/src/fixtures/teams.js
@@ -5,21 +5,28 @@ const fragments = require('./fragments')
 const {
   heTeamID,
   revTeamID,
+  rev1TeamID,
   authorTeamID,
+  majorRevisionHeTeamID,
   revRecommendationTeamID,
-  rev1TeamID,
   heNoRecommendationTeamID,
   revNoEditorRecommendationTeamID,
 } = require('./teamIDs')
 const { submittingAuthor } = require('./userData')
 
-const { collection, noEditorRecomedationCollection } = collections
+const {
+  collection,
+  majorRevisionCollection,
+  noEditorRecomedationCollection,
+} = collections
+
 const {
   fragment,
-  reviewCompletedFragment,
   fragment1,
+  reviewCompletedFragment,
   noEditorRecomedationFragment,
 } = fragments
+
 const {
   handlingEditor,
   reviewer,
@@ -148,5 +155,22 @@ const teams = {
     updateProperties: jest.fn(() => teams.revNoEditorRecommendationTeam),
     id: revNoEditorRecommendationTeamID,
   },
+  majorRevisionHeTeam: {
+    teamType: {
+      name: 'handlingEditor',
+      permissions: 'handlingEditor',
+    },
+    group: 'handlingEditor',
+    name: 'HandlingEditor',
+    object: {
+      type: 'collection',
+      id: majorRevisionCollection.id,
+    },
+    members: [handlingEditor.id],
+    save: jest.fn(() => teams.majorRevisionHeTeam),
+    delete: jest.fn(),
+    updateProperties: jest.fn(() => teams.majorRevisionHeTeam),
+    id: majorRevisionHeTeamID,
+  },
 }
 module.exports = teams
diff --git a/packages/component-fixture-manager/src/fixtures/users.js b/packages/component-fixture-manager/src/fixtures/users.js
index 0ec653aab..2b03c5977 100644
--- a/packages/component-fixture-manager/src/fixtures/users.js
+++ b/packages/component-fixture-manager/src/fixtures/users.js
@@ -5,9 +5,10 @@ const chance = new Chance()
 const {
   heTeamID,
   revTeamID,
+  rev1TeamID,
   authorTeamID,
+  majorRevisionHeTeamID,
   revRecommendationTeamID,
-  rev1TeamID,
   heNoRecommendationTeamID,
   revNoEditorRecommendationTeamID,
 } = require('./teamIDs')
@@ -24,7 +25,7 @@ users = keys.reduce((obj, item) => {
       teams = [heTeamID]
       break
     case 'handlingEditor':
-      teams = [heTeamID]
+      teams = [heTeamID, majorRevisionHeTeamID]
       break
     case 'noRecommendationHE':
       teams = [heNoRecommendationTeamID]
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/strategies/hePublish.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/strategies/hePublish.js
index 238efbc14..bbbe74cae 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/strategies/hePublish.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/strategies/hePublish.js
@@ -7,7 +7,7 @@ module.exports = {
     newRecommendation,
   }) => {
     const fragments = await collectionHelper.collection.getFragments()
-
+    // console.log('FRAGs', fragments)
     if (!collectionHelper.canHEMakeRecommendation(fragments, fragmentHelper)) {
       throw new Error('Cannot publish without at least one reviewer report.')
     }
diff --git a/packages/component-manuscript-manager/src/tests/collections/get.test.js b/packages/component-manuscript-manager/src/tests/collections/get.test.js
index 28c027fd4..7ad0c1284 100644
--- a/packages/component-manuscript-manager/src/tests/collections/get.test.js
+++ b/packages/component-manuscript-manager/src/tests/collections/get.test.js
@@ -36,7 +36,7 @@ describe('Get collections route handler', () => {
     expect(res.statusCode).toBe(200)
     const data = JSON.parse(res._getData())
 
-    expect(data).toHaveLength(1)
+    expect(data).toHaveLength(handlingEditor.teams.length)
     expect(data[0].type).toEqual('collection')
     expect(data[0]).toHaveProperty('currentVersion')
     expect(data[0]).toHaveProperty('visibleStatus')
diff --git a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
index 48138e455..409e5238e 100644
--- a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
+++ b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
@@ -130,52 +130,45 @@ describe('Post fragments recommendations route handler', () => {
   })
 
   it('should return success when creating a recommendation as a HE after minor revision and we have at least one review on collection.', async () => {
-    const { handlingEditor } = testFixtures.users
-    const { collection } = testFixtures.collections
+    const { handlingEditor: { id: userId } } = testFixtures.users
     const {
-      minorRevisionWithReview,
-      noInvitesFragment1,
-    } = testFixtures.fragments
+      minorRevisionCollection: { id: collectionId },
+    } = testFixtures.collections
+    const { noInvitesFragment1: { id: fragmentId } } = testFixtures.fragments
 
-    collection.fragments = [minorRevisionWithReview.id, noInvitesFragment1.id]
     const res = await requests.sendRequest({
       body,
-      userId: handlingEditor.id,
+      userId,
       models,
       route,
       path,
       params: {
-        collectionId: collection.id,
-        fragmentId: noInvitesFragment1.id,
+        collectionId,
+        fragmentId,
       },
     })
 
     expect(res.statusCode).toBe(200)
     const data = JSON.parse(res._getData())
-    expect(data.userId).toEqual(handlingEditor.id)
+    expect(data.userId).toEqual(userId)
   })
 
   it('should return error when creating a recommendation as a HE after minor revision and there are no reviews.', async () => {
-    const { handlingEditor } = testFixtures.users
-    const { collection } = testFixtures.collections
+    const { handlingEditor: { id: userId } } = testFixtures.users
     const {
-      minorRevisionWithoutReview,
-      noInvitesFragment1,
-    } = testFixtures.fragments
-
-    collection.fragments = [
-      minorRevisionWithoutReview.id,
-      noInvitesFragment1.id,
-    ]
+      minorRevisionWithoutReviewCollection: { id: collectionId },
+    } = testFixtures.collections
+    const { noInvitesFragment1: { id: fragmentId } } = testFixtures.fragments
+
     const res = await requests.sendRequest({
       body,
-      userId: handlingEditor.id,
+      userId,
       models,
       route,
       path,
       params: {
-        collectionId: collection.id,
-        fragmentId: noInvitesFragment1.id,
+        collectionId,
+        fragmentId,
       },
     })
 
@@ -187,53 +180,46 @@ describe('Post fragments recommendations route handler', () => {
   })
 
   it('should return success when creating a recommendation as a HE after major revision and there are least one review on fragment.', async () => {
-    const { handlingEditor } = testFixtures.users
-    const { collection } = testFixtures.collections
+    const { handlingEditor: { id: userId } } = testFixtures.users
     const {
-      majorRevisionWithReview,
-      reviewCompletedFragment,
-    } = testFixtures.fragments
-
-    reviewCompletedFragment.collectionId = collection.id
-    collection.fragments = [
-      majorRevisionWithReview.id,
-      reviewCompletedFragment.id,
-    ]
+      majorRevisionCollection: { id: collectionId },
+    } = testFixtures.collections
+    const { reviewCompletedFragment } = testFixtures.fragments
+
+    reviewCompletedFragment.collectionId = collectionId
     const res = await requests.sendRequest({
       body,
-      userId: handlingEditor.id,
+      userId,
       models,
       route,
       path,
       params: {
-        collectionId: collection.id,
+        collectionId,
         fragmentId: reviewCompletedFragment.id,
       },
     })
 
     expect(res.statusCode).toBe(200)
     const data = JSON.parse(res._getData())
-    expect(data.userId).toEqual(handlingEditor.id)
+    expect(data.userId).toEqual(userId)
   })
 
   it('should return error when creating a recommendation as a HE after major revision there are no reviews on fragment.', async () => {
-    const { handlingEditor } = testFixtures.users
-    const { collection } = testFixtures.collections
+    const { handlingEditor: { id: userId } } = testFixtures.users
     const {
-      majorRevisionWithReview,
-      noInvitesFragment1,
-    } = testFixtures.fragments
+      majorRevisionWithoutReviewCollection: { id: collectionId },
+    } = testFixtures.collections
+    const { noInvitesFragment1: { id: fragmentId } } = testFixtures.fragments
 
-    collection.fragments = [majorRevisionWithReview.id, noInvitesFragment1.id]
     const res = await requests.sendRequest({
       body,
-      userId: handlingEditor.id,
+      userId,
       models,
       route,
       path,
       params: {
-        collectionId: collection.id,
-        fragmentId: noInvitesFragment1.id,
+        collectionId,
+        fragmentId,
       },
     })
 
diff --git a/packages/xpub-faraday/tests/config/authsome-helpers.test.js b/packages/xpub-faraday/tests/config/authsome-helpers.test.js
index 41fa760af..10571579a 100644
--- a/packages/xpub-faraday/tests/config/authsome-helpers.test.js
+++ b/packages/xpub-faraday/tests/config/authsome-helpers.test.js
@@ -167,7 +167,7 @@ describe('Authsome Helpers', () => {
         user: answerReviewer,
       })
       const { recommendations } = result
-      expect(recommendations).toHaveLength(7)
+      expect(recommendations).toHaveLength(8)
     })
 
     it('reviewer should not see other reviewers recommendations on latest fragment', () => {
@@ -181,7 +181,7 @@ describe('Authsome Helpers', () => {
         user: answerReviewer,
       })
       const { recommendations } = result
-      expect(recommendations).toHaveLength(6)
+      expect(recommendations).toHaveLength(7)
     })
 
     it('reviewer should not see any reviewer recommendation on previous version if he did not submit a review on that fragment', () => {
-- 
GitLab