Skip to content

Commit 6308389

Browse files
committed
filtering whitespace-only env keys in instance PATCH/POST routes + tests
1 parent c86ad0a commit 6308389

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

lib/routes/instances/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ var removeInstanceContainer = // TODO: this has a lot of overlap with instance s
182182
}));
183183
};
184184

185+
/**
186+
* Remove any empty/whitespace-only env values
187+
* @param {array} req.body.env
188+
*/
189+
var instanceTransformEnvs = flow.series(
190+
mw.body('env').require()
191+
.then(
192+
mw.body('env').array(),
193+
mw.body('env').mapValues(function (envArr) {
194+
return envArr.filter(function (val) {
195+
return !(/^\s*$/.test(val));
196+
});
197+
})
198+
)
199+
);
200+
185201
/** Get's the list of instances to be displayed to the user. This should contain all of the
186202
* instances owned by the owner, as well as those owned by groups (s)he is part of
187203
* @event GET rest/instances
@@ -211,6 +227,7 @@ app.get('/instances/',
211227
app.post('/instances/',
212228
mw.body('build').require().validate(validations.isObjectId),
213229
mw.body('name', 'owner', 'env', 'parent', 'build').pick(),
230+
instanceTransformEnvs,
214231
// validate body types
215232
mw.body('owner.github').require()
216233
.then(
@@ -421,6 +438,7 @@ app.patch('/instances/:id',
421438
.then(
422439
mw.body().unset('build')),
423440
mw.body({ or: ['public', 'name', 'build', 'env']}).require().pick(),
441+
instanceTransformEnvs,
424442
bodyValidations,
425443
mw.body({ or: ['build', 'name'] }).require()
426444
.then(

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: 25 additions & 1 deletion
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
});
@@ -324,6 +323,31 @@ describe('POST /instances', function () {
324323
ctx.user.createInstance(json,
325324
expects.errorStatus(400, /should be an array of strings/, done));
326325
});
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));
350+
});
327351
it('should error if body.env contains an invalid variable', function (done) {
328352
var json = {
329353
name: uuid(),

0 commit comments

Comments
 (0)