From aba91de4801ce491833c5ff849a22a05e2355e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 11 May 2015 16:59:32 +0200 Subject: [PATCH] Add an exclude parameter to get result in API --- lib/server/controllers/apiController.js | 13 +++ test/api/apiTest.js | 104 ++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/lib/server/controllers/apiController.js b/lib/server/controllers/apiController.js index 501c786..cd211b0 100644 --- a/lib/server/controllers/apiController.js +++ b/lib/server/controllers/apiController.js @@ -249,6 +249,19 @@ var ApiController = function(app) { // Retrive one result by id app.get('/api/results/:id', function(req, res) { getPartialResults(req.params.id, res, function(data) { + + // Some fields can be excluded from the response, this way: + // /api/results/:id?exclude=field1,field2 + if (req.query.exclude && typeof req.query.exclude === 'string') { + var excludedFields = req.query.exclude.split(','); + excludedFields.forEach(function(fieldName) { + if (data[fieldName]) { + console.log('excluding ' + fieldName); + delete data[fieldName]; + } + }); + } + return data; }); }); diff --git a/test/api/apiTest.js b/test/api/apiTest.js index 79886ec..c13701f 100644 --- a/test/api/apiTest.js +++ b/test/api/apiTest.js @@ -544,6 +544,110 @@ describe('api', function() { }); + it('should return the entire object and exclude toolsResults', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '?exclude=toolsResults', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + body.should.have.a.property('runId').that.equals(asyncRunId); + body.should.have.a.property('params').that.is.an('object'); + body.should.have.a.property('scoreProfiles').that.is.an('object'); + body.should.have.a.property('rules').that.is.an('object'); + body.should.have.a.property('javascriptExecutionTree').that.is.an('object'); + + body.should.not.have.a.property('toolsResults').that.is.an('object'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + + it('should return the entire object and exclude params and toolsResults', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '?exclude=toolsResults,params', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + body.should.have.a.property('runId').that.equals(asyncRunId); + body.should.have.a.property('scoreProfiles').that.is.an('object'); + body.should.have.a.property('rules').that.is.an('object'); + body.should.have.a.property('javascriptExecutionTree').that.is.an('object'); + + body.should.not.have.a.property('params').that.is.an('object'); + body.should.not.have.a.property('toolsResults').that.is.an('object'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + it('should return the entire object and don\'t exclude anything', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '?exclude=', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + body.should.have.a.property('runId').that.equals(asyncRunId); + body.should.have.a.property('scoreProfiles').that.is.an('object'); + body.should.have.a.property('rules').that.is.an('object'); + body.should.have.a.property('javascriptExecutionTree').that.is.an('object'); + body.should.have.a.property('params').that.is.an('object'); + body.should.have.a.property('toolsResults').that.is.an('object'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + it('should return the entire object and don\'t exclude anything', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '?exclude=null', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + body.should.have.a.property('runId').that.equals(asyncRunId); + body.should.have.a.property('scoreProfiles').that.is.an('object'); + body.should.have.a.property('rules').that.is.an('object'); + body.should.have.a.property('javascriptExecutionTree').that.is.an('object'); + body.should.have.a.property('params').that.is.an('object'); + body.should.have.a.property('toolsResults').that.is.an('object'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + it('should retrieve the screenshot', function(done) { this.timeout(5000);