From 43037920a4e48ae42cab96be3a1ad97696d6d741 Mon Sep 17 00:00:00 2001 From: PrabalSingh Date: Tue, 8 Sep 2020 21:30:19 +0530 Subject: [PATCH 1/2] fix: convert collectionType to appropiate format before param-validation --- src/server/helpers/middleware.js | 2 +- test/src/server/routes/collection.js | 42 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/server/helpers/middleware.js b/src/server/helpers/middleware.js index 345c5fc786..a2f8596943 100644 --- a/src/server/helpers/middleware.js +++ b/src/server/helpers/middleware.js @@ -215,7 +215,7 @@ export async function validateCollectionParams(req, res, next) { return next(new error.BadRequestError('Invalid collection name: Empty string not allowed', req)); } const entityTypes = _.keys(commonUtils.getEntityModels(orm)); - if (!entityTypes.includes(entityType)) { + if (!entityTypes.includes(_.upperFirst(_.camelCase(entityType)))) { return next(new error.BadRequestError(`Invalid entity type: ${entityType} does not exist`, req)); } const collaboratorIds = collaborators.map(collaborator => collaborator.id); diff --git a/test/src/server/routes/collection.js b/test/src/server/routes/collection.js index 66ce0f85ae..11cf8634ef 100644 --- a/test/src/server/routes/collection.js +++ b/test/src/server/routes/collection.js @@ -1,6 +1,7 @@ import {createAuthor, createEditor, truncateEntities} from '../../../test-helpers/create-entities'; import {generateIndex, refreshIndex, searchByName} from '../../../../src/common/helpers/search'; +import _ from 'lodash'; import app from '../../../../src/server/app'; import assertArrays from 'chai-arrays'; import chai from 'chai'; @@ -58,6 +59,25 @@ describe('POST /collection/create', () => { expect(res.status).to.equal(200); }); + it('should correctly create Edition-Group collection and return with status code 200 for correct data', async () => { + const data = { + description: 'some description', + entityType: 'Edition-Group', + name: 'collectionName', + privacy: 'public' + }; + const res = await agent.post('/collection/create/handler').send(data); + const collection = await new UserCollection({id: res.body.id}).fetch(); + + expect(collection.get('id')).to.equal(res.body.id); + expect(collection.get('ownerId')).to.equal(collectionOwner.get('id')); + expect(collection.get('name')).to.equal(data.name); + expect(collection.get('entityType')).to.equal(_.upperFirst(_.camelCase(data.entityType))); + expect(collection.get('description')).to.equal(data.description); + expect(collection.get('public')).to.equal(true); + expect(res.status).to.equal(200); + }); + it('should add the collection in the ES index', async () => { const data = { description: 'some description1234', @@ -218,6 +238,28 @@ describe('POST collection/edit', () => { expect(updatedCollectionJSON.collaborators[0].collaboratorId).to.equal(newCollaborator.get('id')); }); + it('should correctly update the collection to Edition-Group type and return 200 status code', async () => { + const newCollaborator = await createEditor(); + const newData = { + collaborators: [newCollaborator.toJSON()], + description: 'new description', + entityType: 'Edition-Group', + name: 'new collection name', + privacy: 'private' + }; + + const res = await agent.post(`/collection/${collectionJSON.id}/edit/handler`).send(newData); + const updatedCollection = await new UserCollection({id: collectionJSON.id}).fetch({withRelated: ['collaborators']}); + const updatedCollectionJSON = updatedCollection.toJSON(); + + expect(res.status).to.equal(200); + expect(updatedCollectionJSON.name).to.equal(newData.name); + expect(updatedCollectionJSON.entityType).to.equal(_.upperFirst(_.camelCase(newData.entityType))); + expect(updatedCollectionJSON.description).to.equal(newData.description); + expect(updatedCollectionJSON.collaborators.length).to.equal(1); + expect(updatedCollectionJSON.collaborators[0].collaboratorId).to.equal(newCollaborator.get('id')); + }); + it('should correctly add a new collaborator and return 200 status code', async () => { const newCollaborator = await createEditor(); const newData = { From 8f2277984e63aecc600293f78de049d8b8a5f5a5 Mon Sep 17 00:00:00 2001 From: PrabalSingh Date: Wed, 9 Sep 2020 22:22:36 +0530 Subject: [PATCH 2/2] test: modify the test to expect 'EditionGroup' specifically --- test/src/server/routes/collection.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/src/server/routes/collection.js b/test/src/server/routes/collection.js index 11cf8634ef..de5a5615ea 100644 --- a/test/src/server/routes/collection.js +++ b/test/src/server/routes/collection.js @@ -1,7 +1,6 @@ import {createAuthor, createEditor, truncateEntities} from '../../../test-helpers/create-entities'; import {generateIndex, refreshIndex, searchByName} from '../../../../src/common/helpers/search'; -import _ from 'lodash'; import app from '../../../../src/server/app'; import assertArrays from 'chai-arrays'; import chai from 'chai'; @@ -72,7 +71,7 @@ describe('POST /collection/create', () => { expect(collection.get('id')).to.equal(res.body.id); expect(collection.get('ownerId')).to.equal(collectionOwner.get('id')); expect(collection.get('name')).to.equal(data.name); - expect(collection.get('entityType')).to.equal(_.upperFirst(_.camelCase(data.entityType))); + expect(collection.get('entityType')).to.equal('EditionGroup'); expect(collection.get('description')).to.equal(data.description); expect(collection.get('public')).to.equal(true); expect(res.status).to.equal(200); @@ -254,7 +253,7 @@ describe('POST collection/edit', () => { expect(res.status).to.equal(200); expect(updatedCollectionJSON.name).to.equal(newData.name); - expect(updatedCollectionJSON.entityType).to.equal(_.upperFirst(_.camelCase(newData.entityType))); + expect(updatedCollectionJSON.entityType).to.equal('EditionGroup'); expect(updatedCollectionJSON.description).to.equal(newData.description); expect(updatedCollectionJSON.collaborators.length).to.equal(1); expect(updatedCollectionJSON.collaborators[0].collaboratorId).to.equal(newCollaborator.get('id'));