Skip to content

Commit

Permalink
Merge branch 'master' into addSubjectIdAspcectId
Browse files Browse the repository at this point in the history
  • Loading branch information
pallavi2209 committed Apr 11, 2017
2 parents 16d7549 + f48b749 commit d2e4e16
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 105 deletions.
2 changes: 1 addition & 1 deletion api/v1/controllers/aspects.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function validateTags(requestBody, params) {
* @param {Object} req - The request object
*/
function validateRequest(req) {
utils.noReadOnlyFieldsInReq(req, helper);
utils.noReadOnlyFieldsInReq(req, helper.readOnlyFields);
validateTags(req.body);
} // validateRequest

Expand Down
18 changes: 11 additions & 7 deletions api/v1/controllers/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'use strict'; // eslint-disable-line strict

const featureToggles = require('feature-toggles');

const apiErrors = require('../apiErrors');
const helper = require('../helpers/nouns/samples');
const subHelper = require('../helpers/nouns/subjects');
const doDelete = require('../helpers/verbs/doDelete');
Expand All @@ -28,6 +28,7 @@ const sampleStoreConstants = sampleStore.constants;
const redisModelSample = require('../../../cache/models/samples');
const utils = require('./utils');
const publisher = u.publisher;

module.exports = {

/**
Expand Down Expand Up @@ -98,7 +99,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
patchSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
utils.noReadOnlyFieldsInReq(req, helper.readOnlyFields);
doPatch(req, res, next, helper);
},

Expand All @@ -112,7 +113,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
postSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
utils.noReadOnlyFieldsInReq(req, helper.readOnlyFields);
doPost(req, res, next, helper);
},

Expand All @@ -127,7 +128,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
putSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);
utils.noReadOnlyFieldsInReq(req, helper.readOnlyFields);
doPut(req, res, next, helper);
},

Expand All @@ -143,11 +144,14 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
upsertSample(req, res, next) {
utils.noReadOnlyFieldsInReq(req, helper);

// make the name post-able
const readOnlyFields = helper.readOnlyFields.filter((field) => field !== 'name');
utils.noReadOnlyFieldsInReq(req, readOnlyFields);
const resultObj = { reqStartTime: new Date() };
const sampleQueryBody = req.swagger.params.queryBody.value;

u.getUserNameFromToken(req,
return u.getUserNameFromToken(req,
featureToggles.isFeatureEnabled('enforceWritePermission'))
.then((userName) => {
if (sampleQueryBody.relatedLinks) {
Expand Down Expand Up @@ -197,7 +201,7 @@ module.exports = {
* bulk upsert request has been received.
*/
bulkUpsertSample(req, res/* , next */) {
utils.noReadOnlyFieldsInReq(req, helper);

const resultObj = { reqStartTime: new Date() };
const reqStartTime = Date.now();
const value = req.swagger.params.queryBody.value;
Expand Down
2 changes: 1 addition & 1 deletion api/v1/controllers/subjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function validateTags(requestBody, params) {
* @param {Object} req - The request object
*/
function validateRequest(req) {
utils.noReadOnlyFieldsInReq(req, helper);
utils.noReadOnlyFieldsInReq(req, helper.readOnlyFields);
validateTags(req.body);
} // validateRequest

Expand Down
9 changes: 4 additions & 5 deletions api/v1/controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ function checkReadOnlyFieldInObj(field, obj) {
/**
* Throws a validation error if the read-only fields are found in the request.
* @param {Object} req - The request object
* @param {Object} props - The module containing the properties of the
* resource type.
* @param {Array} readOnlyFields - Contains fields to exclude
*/
function noReadOnlyFieldsInReq(req, props) {
function noReadOnlyFieldsInReq(req, readOnlyFields) {
const requestBody = req.body;
if (props.readOnlyFields) {
props.readOnlyFields.forEach((field) => {
if (readOnlyFields) {
readOnlyFields.forEach((field) => {
// if request body is an array, check each object in array.
if (Array.isArray(requestBody)) {
requestBody.forEach((reqObj) => {
Expand Down
2 changes: 1 addition & 1 deletion api/v1/helpers/nouns/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ module.exports = {
},
readOnlyFields: [
'id', 'isDeleted', 'status', 'previousStatus',
'statusChangedAt', 'createdAt', 'updatedAt',
'statusChangedAt', 'createdAt', 'updatedAt', 'name',
],
}; // exports
8 changes: 4 additions & 4 deletions db/helpers/sampleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
'use strict'; // eslint-disable-line strict
const constants = require('../constants');
const ResourceNotFoundError = require('../dbErrors').ResourceNotFoundError;
const dbErrors = require('../dbErrors');
const fourByteBase = 2;
const fourByteExponent = 31;
const fourByteLimit = Math.pow(fourByteBase, fourByteExponent);
Expand Down Expand Up @@ -160,7 +160,7 @@ function parseName(name) {
return retval;
}

const err = new ResourceNotFoundError();
const err = new dbErrors.ResourceNotFoundError();
err.resourceType = 'Sample';
err.resourceKey = name;
throw err;
Expand Down Expand Up @@ -212,7 +212,7 @@ function getSubjectAndAspectBySampleName(seq, sampleName, idsOnly) {
if (s) {
retval.subject = s;
} else {
const err = new ResourceNotFoundError();
const err = new dbErrors.ResourceNotFoundError();
err.resourceType = 'Subject';
err.resourceKey = parsedName.subject.absolutePath;
throw err;
Expand All @@ -223,7 +223,7 @@ function getSubjectAndAspectBySampleName(seq, sampleName, idsOnly) {
if (a) {
retval.aspect = a;
} else {
const err = new ResourceNotFoundError();
const err = new dbErrors.ResourceNotFoundError();
err.resourceType = 'Aspect';
err.resourceKey = parsedName.aspect.name;
throw err;
Expand Down
32 changes: 15 additions & 17 deletions tests/api/v1/samples/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ describe(`api: PATCH ${path}`, () => {
afterEach(u.forceDelete);
after(tu.forceDeleteUser);

it('reject if name field in request', (done) => {
api.patch(`${path}/${sampleName}`)
.set('Authorization', token)
.send({ name: '2' })
.expect(constants.httpStatus.BAD_REQUEST)
.end((err, res ) => {
if (err) {
done(err);
}

expect(res.body.errors[0].type).to.contain('ValidationError');
done();
});
});

it('apiLinks"s href ends with sample name' +
'updatedAt', (done) => {
api.patch(`${path}/${sampleName}`)
Expand Down Expand Up @@ -151,23 +166,6 @@ describe(`api: PATCH ${path}`, () => {
done();
});
});

it('updates case sensitive name successfully', (done) => {
const name = u.sampleName;
const updatedName = name.toUpperCase();
api.patch(`${path}/${name}`)
.set('Authorization', token)
.send({ name: updatedName })
.expect(constants.httpStatus.OK)
.end((err, res) => {
if (err) {
done(err);
}

expect(res.body.name).to.equal(updatedName);
done();
});
});
});

//
Expand Down
16 changes: 16 additions & 0 deletions tests/api/v1/samples/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ describe(`api: POST ${path}`, () => {
});
});

it('reject if name field in request', (done) => {
sampleToPost.name = '!@#$%^&*(';
api.post(path)
.set('Authorization', token)
.send(sampleToPost)
.expect(constants.httpStatus.BAD_REQUEST)
.end((err, res ) => {
if (err) {
done(err);
}

expect(res.body.errors[0].type).to.contain('ValidationError');
done();
});
});

it('basic post /samples', (done) => {
api.post(path)
.set('Authorization', token)
Expand Down
17 changes: 16 additions & 1 deletion tests/api/v1/samples/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/**
* tests/api/v1/samples/get.js
* tests/api/v1/samples/put.js
*/
'use strict';

Expand Down Expand Up @@ -81,6 +81,21 @@ describe(`api: PUT ${path}`, () => {
});
});

it('reject if name field in request', (done) => {
api.put(`${path}/${sampleName}`)
.set('Authorization', token)
.send({ subjectId, aspectId, value: '2', name: '2' })
.expect(constants.httpStatus.BAD_REQUEST)
.end((err, res ) => {
if (err) {
done(err);
}

expect(res.body.errors[0].type).to.contain('ValidationError');
done();
});
});

it('basic succeeds', (done) => {
api.put(`${path}/${sampleName}`)
.set('Authorization', token)
Expand Down

0 comments on commit d2e4e16

Please sign in to comment.