Skip to content

Commit

Permalink
fix: change 406 error to 400 error for invalid bbid
Browse files Browse the repository at this point in the history
reduce the number of entities created while testing(5->2)
  • Loading branch information
prabalsingh24 committed May 6, 2020
1 parent 4093dbe commit b7e3522
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/api/helpers/entityLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import * as commonUtils from '../../common/helpers/utils';
* If BBID is valid then extract the entity data from database by using BBID and relations of
* that entity. If entity is found succesfully then set that entity data to the res.locals.entity
* otherwise return an object {message: errMessage} as response with status code 404.
* If the BBID is not valid then return a status code 406 and an object {message: 'BBID is not valid uuid'}.
* If the BBID is not valid then return a status code 400 and an object {message: 'BBID is not valid uuid'}.
*/


Expand All @@ -58,6 +58,6 @@ export function makeEntityLoader(modelName, relations, errMessage, isBrowse) {
return res.status(404).send({message: errMessage});
}
}
return res.status(406).send({message: 'BBID is not valid uuid'});
return res.status(400).send({message: 'BBID is not valid uuid'});
};
}
35 changes: 13 additions & 22 deletions src/api/helpers/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@

import * as commonUtils from '../../common/helpers/utils';
import _ from 'lodash';
import log from 'log';


/* eslint-disable*/
export function validateBrowseRequestQueryParameters(validLinkedEntities) {
// eslint-disable-next-line consistent-return
return (req, res, next) => {
const queriedEntities = validLinkedEntities.filter(keyName => _.has(req.query, keyName));

export function validateBrowseRequestQueryParameters (validLinkedEntities) {
return async (req, res, next) => {
const queriedEntities = validLinkedEntities.filter(keyName=>{
return _.has(req.query, keyName);
});
if(queriedEntities.length < 1 || queriedEntities.length > 1){
const errorMessage = `Browse requests require exactly 1 linked entity in query parameters; you passed ${queriedEntities.length}: ${queriedEntities}`;
if (queriedEntities.length < 1 || queriedEntities.length > 1) {
const errorMessage =
`Browse requests require exactly 1 linked entity in query parameters; you passed ${queriedEntities.length}: ${queriedEntities}`;
return res.status(400).send({message: errorMessage});
}
const queriedEntityType = queriedEntities[0];
Expand All @@ -42,17 +40,15 @@ export function validateBrowseRequestQueryParameters (validLinkedEntities) {

export async function loadEntity(orm, relEntity, fetchRelated) {
const model = commonUtils.getEntityModelByType(orm, relEntity.type);
const entity = await model.forge({bbid: relEntity.bbid})
.fetch({withRelated: ['defaultAlias','disambiguation'].concat(fetchRelated || [])});
const entity = await model.forge({bbid: relEntity.bbid})
.fetch({withRelated: ['defaultAlias', 'disambiguation'].concat(fetchRelated || [])});
return entity.toJSON();
}

export function loadEntityRelationshipsForBrowse() {

return async (req, res, next) => {
const {orm} = req.app.locals;
const {RelationshipSet} = orm;
const bbid = req.query.bbid;
const entityData = res.locals.entity;
try {
const relationshipSet = await RelationshipSet.forge({id: entityData.relationshipSetId})
Expand All @@ -72,17 +68,12 @@ export function loadEntityRelationshipsForBrowse() {
// What do we do with the error here?
return next(error);
}
}
};
}

export function formatQueryParameters() {

return async (req, res, next) => {
req.query = _.transform(req.query, (result, value, key) => {
result[key.toLowerCase()] = value;
});
return (req, res, next) => {
req.query = _.mapKeys(req.query, (value, key) => _.toLower(key));
return next();
}
};
}


2 changes: 1 addition & 1 deletion src/api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import workRouter from './routes/work';
* example: '<Name of entity>'
* sortName:
* type: string
* example: '<Sort name of enity>'
* example: '<Sort name of entity>'
* aliasLanguage:
* type: string
* example: '<Alias language>'
Expand Down
10 changes: 5 additions & 5 deletions src/api/routes/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const authorError = 'Author not found';
* $ref: '#/definitions/AuthorDetail'
* 404:
* description: Author not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -164,7 +164,7 @@ router.get('/:bbid',
* $ref: '#/definitions/Aliases'
* 404:
* description: Author not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -199,7 +199,7 @@ router.get('/:bbid/aliases',
* $ref: '#/definitions/Identifiers'
* 404:
* description: Author not found
* 406:
* 400:
* description: Invalid BBID
*/
router.get('/:bbid/identifiers',
Expand Down Expand Up @@ -233,7 +233,7 @@ router.get('/:bbid/identifiers',
* $ref: '#/definitions/Relationships'
* 404:
* description: Author not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -273,7 +273,7 @@ router.get('/:bbid/relationships',
* $ref: '#/definitions/BrowsedAuthors'
* 404:
* description: Work not found or Edition not found
* 406:
* 400:
* description: Invalid BBID paased in query params
*/

Expand Down
8 changes: 4 additions & 4 deletions src/api/routes/edition-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const editionGroupError = 'Edition Group not found';
* $ref: '#/definitions/EditionGroupDetail'
* 404:
* description: Edition Group not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -123,7 +123,7 @@ router.get('/:bbid',
* $ref: '#/definitions/Aliases'
* 404:
* description: Edition Group not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -158,7 +158,7 @@ router.get('/:bbid/aliases',
* $ref: '#/definitions/Identifiers'
* 404:
* description: Edition Group not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -194,7 +194,7 @@ router.get('/:bbid/identifiers',
* $ref: '#/definitions/Relationships'
* 404:
* description: Edition Group not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down
10 changes: 5 additions & 5 deletions src/api/routes/edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const editionError = 'Edition not found';
* $ref: '#/definitions/EditionDetail'
* 404:
* description: Edition not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -184,7 +184,7 @@ router.get('/:bbid',
* $ref: '#/definitions/Aliases'
* 404:
* description: Edition not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -219,7 +219,7 @@ router.get('/:bbid/aliases',
* $ref: '#/definitions/Identifiers'
* 404:
* description: Edition not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -254,7 +254,7 @@ router.get('/:bbid/identifiers',
* $ref: '#/definitions/Relationships'
* 404:
* description: Edition not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -304,7 +304,7 @@ router.get('/:bbid/relationships',
* $ref: '#/definitions/BrowsedEditions'
* 404:
* description: Related entity not found
* 406:
* 400:
* description: Invalid BBID paased in query params
*/

Expand Down
10 changes: 5 additions & 5 deletions src/api/routes/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const publisherError = 'Publisher not found';
* $ref: '#/definitions/PublisherDetail'
* 404:
* description: Publisher not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -162,7 +162,7 @@ router.get('/:bbid',
* $ref: '#/definitions/Aliases'
* 404:
* description: Publisher not found
* 406:
* 400:
* description: Invalid BBID
*/
router.get('/:bbid/aliases',
Expand Down Expand Up @@ -196,7 +196,7 @@ router.get('/:bbid/aliases',
* $ref: '#/definitions/Identifiers'
* 404:
* description: Publisher not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -231,7 +231,7 @@ router.get('/:bbid/identifiers',
* $ref: '#/definitions/Relationships'
* 404:
* description: Publisher not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -271,7 +271,7 @@ router.get('/:bbid/relationships',
* $ref: '#/definitions/BrowsedPublishers'
* 404:
* description: Work not found or Edition not found
* 406:
* 400:
* description: Invalid BBID paased in query params
*/

Expand Down
10 changes: 5 additions & 5 deletions src/api/routes/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const workError = 'Work not found';
* $ref: '#/definitions/WorkDetail'
* 404:
* description: Work not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -158,7 +158,7 @@ router.get('/:bbid',
* $ref: '#/definitions/Aliases'
* 404:
* description: Work not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -194,7 +194,7 @@ router.get('/:bbid/aliases',
* $ref: '#/definitions/Identifiers'
* 404:
* description: Work not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -230,7 +230,7 @@ router.get('/:bbid/identifiers',
* $ref: '#/definitions/Relationships'
* 404:
* description: Work not found
* 406:
* 400:
* description: Invalid BBID
*/

Expand Down Expand Up @@ -270,7 +270,7 @@ router.get('/:bbid/relationships',
* $ref: '#/definitions/BrowsedAuthors'
* 404:
* description: Author not found or Edition not found
* 406:
* 400:
* description: Invalid BBID paased in query params
*/

Expand Down
8 changes: 4 additions & 4 deletions test/src/api/routes/test-author.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ describe('GET /Author', () => {
});
});

it('should throw a 406 error if trying to access an author with invalid BBID', function (done) {
it('should throw a 400 error if trying to access an author with invalid BBID', function (done) {
chai.request(app)
.get(`/author/${inValidBBID}`)
.end(function (err, res) {
if (err) { return done(err); }
expect(res).to.have.status(406);
expect(res).to.have.status(400);
expect(res.ok).to.be.false;
expect(res.body).to.be.an('object');
expect(res.body.message).to.equal('BBID is not valid uuid');
Expand Down Expand Up @@ -252,12 +252,12 @@ describe('Browse Author', () => {
expect(_.toLower(res.body.authors[0].entity.authorType)).to.equal('author type 1');
});

it('should throw 406 error for invalid bbid', (done) => {
it('should throw 400 error for invalid bbid', (done) => {
chai.request(app)
.get('/author?work=1212121')
.end(function (err, res) {
if (err) { return done(err); }
expect(res).to.have.status(406);
expect(res).to.have.status(400);
return done();
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/src/api/routes/test-edition-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ describe('GET /EditionGroup', () => {
});
});

it('should throw a 406 error if trying to access an edition with invalid BBID', function (done) {
it('should throw a 400 error if trying to access an edition with invalid BBID', function (done) {
chai.request(app)
.get(`/edition-group/${inValidBBID}`)
.end(function (err, res) {
if (err) { return done(err); }
expect(res).to.have.status(406);
expect(res).to.have.status(400);
expect(res.ok).to.be.false;
expect(res.body).to.be.an('object');
expect(res.body.message).to.equal('BBID is not valid uuid');
Expand Down Expand Up @@ -211,12 +211,12 @@ describe('Browse EditionGroup', () => {
expect(_.toLower(res.body.editionGroups[0].entity.editionGroupType)).to.equal('edition group type 1');
});

it('should throw 406 error for invalid bbid', (done) => {
it('should throw 400 error for invalid bbid', (done) => {
chai.request(app)
.get('/edition-group?edition=1212121')
.end(function (err, res) {
if (err) { return done(err); }
expect(res).to.have.status(406);
expect(res).to.have.status(400);
return done();
});
});
Expand Down
Loading

0 comments on commit b7e3522

Please sign in to comment.