diff --git a/packages/component-invite/src/routes/postAssignation.js b/packages/component-invite/src/routes/postAssignation.js index 5829a21ed5f3c2da7e7edb7c56f21a577e839bea..54fa931fef694a06e51021a92e90188c4757a6d3 100644 --- a/packages/component-invite/src/routes/postAssignation.js +++ b/packages/component-invite/src/routes/postAssignation.js @@ -17,34 +17,22 @@ module.exports = models => async (req, res) => { return } const { collectionId } = req.params - const assignations = user.assignations.filter( - assignation => assignation.collectionId === collectionId, + const filteredAssignations = user.assignations.filter( + assignation => + assignation.collectionId === collectionId && assignation.type === type, ) - if (assignations.length === 0) { + if (filteredAssignations.length === 0) { res.status(400).json({ - error: `Collection ${collectionId} does not match any user assignation`, + error: `Request data does not match any user assignation`, }) logger.error( - `Collection ${collectionId} does not match any user assignation`, - ) - return - } - - const matchingAssignation = assignations[0] - - if (type !== matchingAssignation.type) { - res.status(400).json({ - error: 'User assignation type and provided type do not match', - }) - logger.error( - `Param ${type} does not match user assignation type: ${ - matchingAssignation.type - }`, + `Collection ${collectionId} and type '${type}' do not match any user assignation`, ) return } + const matchingAssignation = filteredAssignations[0] try { await models.Collection.find(collectionId) // TODO: create a team and add the team id to the user's teams array diff --git a/packages/component-invite/src/tests/fixtures/users.js b/packages/component-invite/src/tests/fixtures/users.js index b918c91c60b44252d28d7750280dc3ffc4a5c616..b29fdee4fd9a82142361029799cddc591007efbe 100644 --- a/packages/component-invite/src/tests/fixtures/users.js +++ b/packages/component-invite/src/tests/fixtures/users.js @@ -1,3 +1,6 @@ +const collections = require('./collections') +const { standardCollection } = collections + const users = { admin: { type: 'user', @@ -36,6 +39,7 @@ const users = { type: 'handlingEditor', hasAnswer: false, isAccepted: false, + collectionId: standardCollection.id, }, ], save: jest.fn(() => users.handlingEditor), diff --git a/packages/component-invite/src/tests/postAssignation.test.js b/packages/component-invite/src/tests/postAssignation.test.js index a2e4dbd9be1de81560050c6fcd7004b24ffd9a6d..e148f3f34e2b5dbdcfad96083c187148a6b48285 100644 --- a/packages/component-invite/src/tests/postAssignation.test.js +++ b/packages/component-invite/src/tests/postAssignation.test.js @@ -48,6 +48,7 @@ describe('Post assignation route handler', () => { body, }) req.user = acceptingHE + req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() const models = buildModels(standardCollection, acceptingHE) await require(postAssignationPath)(models)(req, res) @@ -66,6 +67,7 @@ describe('Post assignation route handler', () => { body, }) req.user = refusingHE + req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() const models = buildModels(standardCollection, refusingHE) await require(postAssignationPath)(models)(req, res) @@ -82,6 +84,7 @@ describe('Post assignation route handler', () => { body, }) req.user = handlingEditor + req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() const models = buildModels(standardCollection, handlingEditor) await require(postAssignationPath)(models)(req, res) @@ -99,6 +102,7 @@ describe('Post assignation route handler', () => { body, }) req.user = handlingEditor + req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() const models = buildModels(notFoundError, handlingEditor) await require(postAssignationPath)(models)(req, res) @@ -118,6 +122,7 @@ describe('Post assignation route handler', () => { }) delete noAssignationEditor.assignations req.user = noAssignationEditor + req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() const models = buildModels(standardCollection, noAssignationEditor) await require(postAssignationPath)(models)(req, res) @@ -126,4 +131,44 @@ describe('Post assignation route handler', () => { const data = JSON.parse(res._getData()) expect(data.error).toEqual('The user has no assignation') }) + it('should return an error when the request type the user assignation type do not match', async () => { + const body = { + type: 'aWrongType', + accept: false, + } + const req = httpMocks.createRequest({ + body, + }) + req.user = handlingEditor + req.params.collectionId = standardCollection.id + const res = httpMocks.createResponse() + const models = buildModels(standardCollection, handlingEditor) + await require(postAssignationPath)(models)(req, res) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual( + 'Request data does not match any user assignation', + ) + }) + it('should return an error when the request collection and the user assignation collection do not match', async () => { + const body = { + type: 'handlingEditor', + accept: false, + } + const req = httpMocks.createRequest({ + body, + }) + req.user = handlingEditor + req.params.collectionId = '123' + const res = httpMocks.createResponse() + const models = buildModels(standardCollection, handlingEditor) + await require(postAssignationPath)(models)(req, res) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual( + 'Request data does not match any user assignation', + ) + }) })