Skip to content

Commit

Permalink
Merge pull request #510 from prabalsingh24/fix-edition-group-bug
Browse files Browse the repository at this point in the history
fix: convert collectionType to appropiate format before param-validation
  • Loading branch information
MonkeyDo committed Sep 10, 2020
2 parents ea70d68 + 8f22779 commit 939b54f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server/helpers/middleware.js
Expand Up @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions test/src/server/routes/collection.js
Expand Up @@ -58,6 +58,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('EditionGroup');
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',
Expand Down Expand Up @@ -218,6 +237,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('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'));
});

it('should correctly add a new collaborator and return 200 status code', async () => {
const newCollaborator = await createEditor();
const newData = {
Expand Down

0 comments on commit 939b54f

Please sign in to comment.