Skip to content

Commit 33da279

Browse files
committed
Merge pull request #437 from CodeNow/SAN-694/ignore-whitespace-only-ENV-values-on-PATCH-to-instances-id
filtering whitespace-only env keys in instance PATCH/POST routes + tests
2 parents cb806d8 + 11cc7fb commit 33da279

5 files changed

Lines changed: 50 additions & 10 deletions

File tree

lib/routes/instances/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ var bodyValidations = flow.series(
7575
),
7676
mw.body('env').require()
7777
.then(
78-
mw.body('env').array()
79-
.validate(validations.isArrayOf('string')),
78+
mw.body('env').array(),
79+
mw.body('env').mapValues(function (envArr) {
80+
return envArr.filter(function (val) {
81+
return !(/^\s*$/.test(val));
82+
});
83+
}),
8084
mw.body('env').each(
8185
function (env, req, eachReq, res, next) {
8286
eachReq.env = env;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"connect-datadog": "0.0.5",
2323
"connect-redis": "^2.0.0",
2424
"cors": "^2.4.1",
25-
"dat-middleware": "^1.9.2",
25+
"dat-middleware": "^1.9.3",
2626
"debug": "^1.0.2",
2727
"defaults": "^1.0.0",
2828
"docker-stream-cleanser": "0.1.2",

test/fixtures/types-test-util.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function errorMessageSuffix (paramType, type) {
3535
'string': 'must be a string',
3636
'repo-string' : 'must be a string',
3737
'number': 'must be a number',
38-
'array': 'should be an array',
38+
'array': 'must be instance of Array',
3939
'object': 'must be an object',
4040
'ObjectId': 'is not an ObjectId',
4141
};
@@ -140,9 +140,7 @@ function setupArrayParamsTests (ctx, handler, def, types, param, buildBodyFuncti
140140
body[param.name].push(typeValue(ctx, arrayItemType));
141141
body[param.name].push(typeValue(ctx, arrayItemType));
142142
body[param.name].push(typeValue(ctx, arrayItemType));
143-
// e.g. body parameter "env" should be an array of strings
144-
var regexp = 'body parameter "' + param.name + '" ' + errorMessageSuffix(param.type, arrayItemType) +
145-
' of ' + param.itemType + 's';
143+
regexp = '"env" should match';
146144
var message = new RegExp(regexp);
147145
var cb = expects.error(400, message, done);
148146
handler(body, cb);
@@ -190,4 +188,4 @@ exports.makeTestFromDef = function (def, ctx, handler) {
190188
setupArrayParamsTests(ctx, handler, def, types, param, buildBodyWithRequiredParams, index);
191189
});
192190
}
193-
};
191+
};

test/instances-id/patch/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,20 @@ describe('Instance - /instances/:id', {timeout:1000}, function () {
477477
ctx.instance.fetch(expects.success(200, expected, done));
478478
}));
479479
});
480+
it('should filter empty/whitespace-only strings from env array', function (done) {
481+
var body = {
482+
env: ['', ' ', 'ONE=1']
483+
};
484+
var expected = {
485+
env: ['ONE=1']
486+
};
487+
require('../../fixtures/mocks/github/user')(ctx.user);
488+
ctx.instance.update(body, expects.success(200, expected, function (err) {
489+
if (err) { return done(err); }
490+
//sanity check
491+
ctx.instance.fetch(expects.success(200, expected, done));
492+
}));
493+
});
480494
});
481495

482496
var updates = [{

test/instances/post/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ describe('POST /instances', function () {
309309
'containers[0]': exists
310310
};
311311
require('../../fixtures/mocks/github/user')(ctx.user);
312-
require('../../fixtures/mocks/github/user')(ctx.user);
313312
ctx.user.createInstance(json,
314313
expects.success(201, expected, done));
315314
});
@@ -322,7 +321,32 @@ describe('POST /instances', function () {
322321
}]
323322
};
324323
ctx.user.createInstance(json,
325-
expects.errorStatus(400, /should be an array of strings/, done));
324+
expects.errorStatus(400, /"env" should match/, done));
325+
});
326+
it('should filter empty/whitespace-only strings from env array', function (done) {
327+
var json = {
328+
name: uuid(),
329+
build: ctx.build.id(),
330+
env: [
331+
'', ' ', 'ONE=1'
332+
]
333+
};
334+
var expected = {
335+
_id: exists,
336+
name: json.name,
337+
env: ['ONE=1'],
338+
owner: {
339+
github: ctx.user.json().accounts.github.id,
340+
username: ctx.user.json().accounts.github.login
341+
},
342+
public: false,
343+
'build._id': ctx.build.id(),
344+
containers: exists,
345+
'containers[0]': exists
346+
};
347+
require('../../fixtures/mocks/github/user')(ctx.user);
348+
ctx.user.createInstance(json,
349+
expects.success(201, expected, done));
326350
});
327351
it('should error if body.env contains an invalid variable', function (done) {
328352
var json = {

0 commit comments

Comments
 (0)