Added some sub-requests to the API

This commit is contained in:
Gaël Métais
2014-12-14 22:36:25 +01:00
parent 90dac7dd19
commit 2ab843efdb
3 changed files with 174 additions and 9 deletions
+1 -1
View File
@@ -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: {
+45 -8
View File
@@ -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');
});
});
}
};
+128
View File
@@ -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');