Add acceptance test for analyses controller

This commit is contained in:
Daniel García Aubert 2017-11-14 13:46:47 +01:00
parent 27521964c7
commit 520e84e46b
2 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,114 @@
require('../../support/test_helper');
var assert = require('../../support/assert');
var TestClient = require('../../support/test-client');
describe('analyses controller', function () {
const mapConfig = {
version: '1.5.0',
layers:
[{
type: 'cartodb',
options:
{
source: { id: 'a1' },
cartocss: TestClient.CARTOCSS.POLYGONS,
cartocss_version: '2.3.0'
}
}],
dataviews: {},
analyses:
[{
id: 'a1',
type: 'buffer',
params: {
source: {
type: 'source',
params: {
query: 'select * from analysis_banks limit 1'
}
},
radius: 250
}
}]
};
beforeEach(function () {
this.testClient = new TestClient(mapConfig, 1234);
});
it('should get an array of analyses from catalog', function (done) {
this.testClient.getAnalysesCatalog({}, (err, result) => {
if (err) {
return done(err);
}
assert.ok(Array.isArray(result.catalog));
done();
});
});
it('should support jsonp responses', function (done) {
this.testClient.getAnalysesCatalog({ jsonp: 'jsonp_test' }, (err, result) => {
if (err) {
return done(err);
}
assert.ok(result);
let didRunJsonCallback = false;
// jshint ignore:start
function jsonp_test(body) {
assert.ok(Array.isArray(body.catalog));
didRunJsonCallback = true;
}
eval(result);
// jshint ignore:end
assert.ok(didRunJsonCallback);
done();
});
});
it('should respond "unauthorized" when missing api_key', function (done) {
const apiKey = this.testClient.apiKey;
this.testClient.apiKey = null;
this.testClient.getAnalysesCatalog({ status: 401 }, (err, result) => {
if (err) {
return done(err);
}
assert.deepEqual(result.errors[0], 'Unauthorized');
this.testClient.apiKey = apiKey;
done();
});
});
it('should get an array of analyses from catalog', function (done) {
this.testClient.getTile(0, 0, 0, (err) => {
if (err) {
return done(err);
}
this.testClient.getAnalysesCatalog({}, (err, result) => {
if (err) {
return done(err);
}
assert.ok(Array.isArray(result.catalog));
assert.ok(result.catalog.length >= 2); // buffer & source at least
result.catalog
.filter(analysis => analysis.node_id === '0a215e1f3405381cf0ea6b3b0deb6fdcfdc2fcaa')
.forEach(analysis => assert.equal(analysis.type, 'buffer'));
this.testClient.drain(done);
});
});
});
});

View File

@ -1174,4 +1174,42 @@ TestClient.prototype.setUserDatabaseTimeoutLimit = function (timeoutLimit, callb
);
};
TestClient.prototype.getAnalysesCatalog = function (params, callback) {
var url = '/api/v1/map/analyses/catalog';
if (this.apiKey) {
url += '?' + qs.stringify({api_key: this.apiKey});
}
if (params.jsonp) {
url += '&' + qs.stringify({callback: params.jsonp});
}
assert.response(this.server,
{
url: url,
method: 'GET',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
}
},
{
status: params.status || 200,
headers: {
'Content-Type': params.jsonp ?
'text/javascript; charset=utf-8' :
'application/json; charset=utf-8'
}
},
function(res, err) {
if (err) {
return callback(err);
}
var parsedBody = params.jsonp ? res.body : JSON.parse(res.body);
return callback(null, parsedBody);
}
);
};