From 2ab843efdbc55ebb67b92d987fed224e6fada82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Sun, 14 Dec 2014 22:36:25 +0100 Subject: [PATCH] Added some sub-requests to the API --- Gruntfile.js | 2 +- lib/server/controllers/apiController.js | 53 ++++++++-- test/api/apiTest.js | 128 ++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 9 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 81702ea..ac5dc6a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -88,7 +88,7 @@ module.exports = function(grunt) { options: { reporter: 'spec', }, - src: ['coverage/test/core/scoreCalculatorTest.js'] + src: ['coverage/test/api/apiTest.js'] }, coverage: { options: { diff --git a/lib/server/controllers/apiController.js b/lib/server/controllers/apiController.js index a1c0952..0f976d7 100644 --- a/lib/server/controllers/apiController.js +++ b/lib/server/controllers/apiController.js @@ -60,7 +60,6 @@ var ApiController = function(app) { // Send result if the user was waiting if (run.params.waitForResponse) { - res.redirect(302, '/api/results/' + run.runId); } @@ -133,21 +132,59 @@ var ApiController = function(app) { // Retrive one result by id app.get('/api/results/:id', function(req, res) { - var runId = req.params.id; + getPartialResults(req.params.id, res, function(data) { + return data; + }); + }); + // Retrieve one result and return only the generalScores part of the response + app.get('/api/results/:id/generalScores', function(req, res) { + getPartialResults(req.params.id, res, function(data) { + return data.scoreProfiles.generic; + }); + }); + + app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) { + getPartialResults(req.params.id, res, function(data) { + return data.scoreProfiles[req.params.scoreProfile]; + }); + }); + + app.get('/api/results/:id/rules', function(req, res) { + getPartialResults(req.params.id, res, function(data) { + return data.rules; + }); + }); + + app.get('/api/results/:id/javascriptExecutionTree', function(req, res) { + getPartialResults(req.params.id, res, function(data) { + return data.javascriptExecutionTree; + }); + }); + + app.get('/api/results/:id/toolsResults/phantomas', function(req, res) { + getPartialResults(req.params.id, res, function(data) { + return data.toolsResults.phantomas; + }); + }); + + function getPartialResults(runId, res, partialGetterFn) { resultsDatastore.getResult(runId) .then(function(data) { - // This is the pivot format, we might need to clean it first? - - // Hide phantomas results - data.toolsResults.phantomas = {}; + var results = partialGetterFn(data); + if (typeof results === 'undefined') { + res.status(404).send('Not found'); + return; + } + res.setHeader('Content-Type', 'application/json'); - res.send(JSON.stringify(data, null, 2)); + res.send(JSON.stringify(results, null, 2)); + }).fail(function() { res.status(404).send('Not found'); }); - }); + } }; diff --git a/test/api/apiTest.js b/test/api/apiTest.js index 44c599f..f7ff020 100644 --- a/test/api/apiTest.js +++ b/test/api/apiTest.js @@ -324,6 +324,134 @@ describe('api', function() { }); + it('should return the generic score object', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '/generalScores', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + body.should.have.a.property('globalScore').that.is.a('number'); + body.should.have.a.property('categories').that.is.an('object'); + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + + it('should return the generic score object also', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '/generalScores/generic', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + body.should.have.a.property('globalScore').that.is.a('number'); + body.should.have.a.property('categories').that.is.an('object'); + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + + it('should not find an unknown score object', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '/generalScores/unknown', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 404) { + done(); + } else { + done(error || response.statusCode); + } + }); + }); + + + it('should return the rules', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '/rules', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + var firstRule = body[Object.keys(body)[0]]; + firstRule.should.have.a.property('policy').that.is.an('object'); + firstRule.should.have.a.property('value').that.is.a('number'); + firstRule.should.have.a.property('bad').that.is.a('boolean'); + firstRule.should.have.a.property('abnormal').that.is.a('boolean'); + firstRule.should.have.a.property('score').that.is.a('number'); + firstRule.should.have.a.property('abnormalityScore').that.is.a('number'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + + it('should return the javascript execution tree', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '/javascriptExecutionTree', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + body.should.have.a.property('data').that.is.an('object'); + body.data.should.have.a.property('type').that.equals('main'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + + it('should return the phantomas results', function(done) { + this.timeout(5000); + + request({ + method: 'GET', + url: serverUrl + '/api/results/' + asyncRunId + '/toolsResults/phantomas', + json: true, + }, function(error, response, body) { + if (!error && response.statusCode === 200) { + + body.should.have.a.property('metrics').that.is.an('object'); + body.should.have.a.property('offenders').that.is.an('object'); + + done(); + + } else { + done(error || response.statusCode); + } + }); + }); + + // Stop the server after(function() { console.log('Closing the server');