API is starting to run

This commit is contained in:
Gaël Métais
2014-12-05 16:14:08 +01:00
parent e18cd87a07
commit 906f0876a0
18 changed files with 423 additions and 108 deletions
-85
View File
@@ -1,85 +0,0 @@
var should = require('chai').should();
var phantomasWrapper = require('../../lib/tools/phantomasWrapper');
describe('phantomasWrapper', function() {
it('should have a method execute', function() {
phantomasWrapper.should.have.property('execute').that.is.a('function');
});
it('should execute', function(done) {
var url = 'http://localhost:8388/simple-page.html';
this.timeout(15000);
phantomasWrapper.execute({
params: {
url: url,
options: {}
}
}).then(function(data) {
data.should.be.an('object');
data.should.have.a.property('generator');
data.generator.should.contain('phantomas');
data.should.have.a.property('url').that.equals(url);
data.should.have.a.property('metrics').that.is.an('object').not.empty();
data.should.have.a.property('offenders').that.is.an('object').not.empty();
data.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('array').not.empty();
done();
}).fail(function(err) {
done(err);
});
});
it('should fail with error 254', function(done) {
var url = 'http://localhost:8389/not-existing.html';
this.timeout(15000);
phantomasWrapper.execute({
params: {
url: url,
options: {}
}
}).then(function(data) {
done('Error: unwanted success');
}).fail(function(err) {
should.exist(err);
err.should.equal(254);
done();
});
});
it('should timeout but return some results', function(done) {
var url = 'http://localhost:8388/simple-page.html';
this.timeout(5000);
phantomasWrapper.execute({
params: {
url: url,
options: {
timeout: 1
}
}
}).then(function(data) {
data.should.be.an('object');
data.should.have.a.property('generator');
data.generator.should.contain('phantomas');
data.should.have.a.property('url').that.equals(url);
data.should.have.a.property('metrics').that.is.an('object').not.empty();
data.should.have.a.property('offenders').that.is.an('object').not.empty();
data.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('array').not.empty();
done();
}).fail(function(err) {
done(err);
});
});
});
+74
View File
@@ -0,0 +1,74 @@
var should = require('chai').should();
var resultsDatastore = require('../../lib/server/datastores/resultsDatastore');
describe('resultsDatastore', function() {
var datastore = new resultsDatastore();
var testId1 = '123456789';
var testData1 = {
runId: testId1,
other: {
foo: 'foo',
bar: 1
}
};
it('should store a result', function(done) {
datastore.should.have.a.property('saveResult').that.is.a('function');
datastore.saveResult(testData1).then(function() {
done();
}).fail(function(err) {
done(err);
});
});
it('should store another result', function(done) {
var testData2 = {
runId: '987654321',
other: {
foo: 'foo',
bar: 2
}
};
datastore.saveResult(testData2).then(function() {
done();
}).fail(function(err) {
done(err);
});
});
it('should retrieve a result', function(done) {
datastore.getResult(testId1)
.then(function(results) {
// Compare results with testData
results.should.deep.equal(testData1);
done();
}).fail(function(err) {
done(err);
});
});
it('should delete a result', function(done) {
datastore.deleteResult(testId1)
.then(function() {
done();
}).fail(function(err) {
done(err);
});
});
it('should not find the result anymore', function(done) {
datastore.getResult(testId1)
.then(function(results) {
done('Error, the result is still in the datastore');
}).fail(function(err) {
done();
});
});
});
-18
View File
@@ -1,18 +0,0 @@
var should = require('chai').should();
var rulesChecker = require('../../lib/rulesChecker');
describe('rulesChecker', function() {
it('should have a method check', function() {
rulesChecker.should.have.property('check').that.is.a('function');
});
it('should produce a nice rules object', function() {
var data = require('../fixtures/rulesCheckerInput.json');
var policies = require('../fixtures/rulesCheckerPolicies.json');
var expected = require('../fixtures/rulesCheckerOutput.json');
var results = rulesChecker.check(data, policies);
results.should.deep.equals(expected);
});
});
+81
View File
@@ -0,0 +1,81 @@
var should = require('chai').should();
var runsDatastore = require('../../lib/server/datastores/runsDatastore');
describe('runsDatastore', function() {
var datastore = new runsDatastore();
var firstRunId = 333;
var secondRunId = 999;
it('should accept new runs', function() {
datastore.should.have.a.property('add').that.is.a('function');
datastore.add({
runId: firstRunId,
otherData: 123456789
}, 0);
datastore.add({
runId: secondRunId,
otherData: 'whatever'
}, 1);
});
it('should have stored the runs with a status "runnung"', function() {
datastore.should.have.a.property('get').that.is.a('function');
var firstRun = datastore.get(firstRunId);
firstRun.should.have.a.property('runId').that.equals(firstRunId);
firstRun.should.have.a.property('status').that.deep.equals({
statusCode: 'running'
});
var secondRun = datastore.get(secondRunId);
secondRun.should.have.a.property('runId').that.equals(secondRunId);
secondRun.should.have.a.property('status').that.deep.equals({
statusCode: 'awaiting',
position: 1
});
});
it('should have exactly 2 runs in the store', function() {
var runs = datastore.list();
runs.should.be.a('array');
runs.should.have.length(2);
runs[0].should.have.a.property('runId').that.equals(firstRunId);
});
it('shoud update statuses correctly', function() {
datastore.markAsComplete(firstRunId);
var firstRun = datastore.get(firstRunId);
firstRun.should.have.a.property('status').that.deep.equals({
statusCode: 'complete'
});
datastore.updatePosition(secondRunId, 0);
var secondRun = datastore.get(secondRunId);
secondRun.should.have.a.property('status').that.deep.equals({
statusCode: 'running'
});
datastore.markAsFailed(secondRunId);
secondRun = datastore.get(secondRunId);
secondRun.should.have.a.property('status').that.deep.equals({
statusCode: 'failed'
});
});
it('should delete a run', function() {
datastore.delete(firstRunId);
var runs = datastore.list();
runs.should.be.a('array');
runs.should.have.length(1);
runs[0].should.have.a.property('runId').that.equals(secondRunId);
});
});
+68
View File
@@ -0,0 +1,68 @@
var should = require('chai').should();
var runsQueue = require('../../lib/server/datastores/runsQueue.js');
describe('runsQueue', function() {
var queue = new runsQueue();
var aaaRun = null;
var bbbRun = null;
var cccRun = null;
it('should accept a new runId', function(done) {
queue.should.have.a.property('push').that.is.a('function');
aaaRun = queue.push('aaa');
bbbRun = queue.push('bbb');
aaaRun.then(function() {
done();
});
});
it('should return the right positions', function() {
var aaaPosition = queue.getPosition('aaa');
aaaPosition.should.equal(0);
aaaRun.startingPosition.should.equal(0);
var bbbPosition = queue.getPosition('bbb');
bbbPosition.should.equal(1);
bbbRun.startingPosition.should.equal(1);
var cccPosition = queue.getPosition('ccc');
cccPosition.should.equal(-1);
});
it('should refresh runs\' positions', function(done) {
cccRun = queue.push('ccc');
cccRun.progress(function(position) {
position.should.equal(1);
var positionDoubleCheck = queue.getPosition('ccc');
positionDoubleCheck.should.equal(1);
done();
});
queue.remove('aaa');
});
it('should fulfill the promise when first in the line', function(done) {
cccRun.then(function() {
done();
});
queue.remove('bbb');
});
it('should not keep removed runs', function() {
var aaaPosition = queue.getPosition('aaa');
aaaPosition.should.equal(-1);
var bbbPosition = queue.getPosition('bbb');
bbbPosition.should.equal(-1);
var cccPosition = queue.getPosition('ccc');
cccPosition.should.equal(0);
});
});
-87
View File
@@ -1,87 +0,0 @@
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var should = chai.should();
var YellowLabTools = require('../../lib/yellowlabtools.js');
chai.use(sinonChai);
describe('yellowlabtools', function() {
it('returns a promise', function() {
var ylt = new YellowLabTools();
ylt.should.have.property('then').that.is.a('function');
ylt.should.have.property('fail').that.is.a('function');
});
it('fails an undefined url', function(done) {
var ylt = new YellowLabTools().fail(function(err) {
err.should.be.a('string').that.equals('URL missing');
done();
});
});
it('fails with an empty url string', function(done) {
var ylt = new YellowLabTools('').fail(function(err) {
err.should.be.a('string').that.equals('URL missing');
done();
});
});
it('succeeds on simple-page.html', function(done) {
this.timeout(15000);
// Check if console.log is called
sinon.spy(console, 'log');
var url = 'http://localhost:8388/simple-page.html';
var ylt = new YellowLabTools(url)
.then(function(data) {
data.should.be.an('object');
data.toolsResults.should.be.an('object');
// Test Phantomas
data.toolsResults.phantomas.should.be.an('object');
data.toolsResults.phantomas.should.have.a.property('url').that.equals(url);
data.toolsResults.phantomas.should.have.a.property('metrics').that.is.an('object');
data.toolsResults.phantomas.metrics.should.have.a.property('requests').that.equals(1);
data.toolsResults.phantomas.should.have.a.property('offenders').that.is.an('object');
data.toolsResults.phantomas.offenders.should.have.a.property('DOMelementMaxDepth');
data.toolsResults.phantomas.offenders.DOMelementMaxDepth.should.have.length(1);
data.toolsResults.phantomas.offenders.DOMelementMaxDepth[0].should.equal('body > h1[1]');
// Test rules
data.should.have.a.property('rules').that.is.an('object');
data.rules.should.have.a.property('DOMelementMaxDepth').that.is.an('object');
data.rules.DOMelementMaxDepth.should.deep.equal({
policy: {
"tool": "phantomas",
"label": "DOM max depth",
"message": "<p>A deep DOM makes the CSS matching with DOM elements difficult.</p><p>It also slows down Javascript modifications to the DOM because changing the dimensions of an element makes the browser re-calculate the dimensions of it's parents. Same thing for Javascript events, that bubble up to the document root.</p>",
"isOkThreshold": 10,
"isBadThreshold": 20,
"isAbnormalThreshold": 30
},
"value": 1,
"bad": false,
"abnormal": false,
"score": 100,
"abnormalityScore": 0,
"offenders": ["body > h1[1]"]
});
/*jshint expr: true*/
console.log.should.not.have.been.called;
done();
}).fail(function(err) {
done(err);
});
});
});