diff --git a/test/acceptance/analysis/analyses-controller.js b/test/acceptance/analysis/analyses-controller.js new file mode 100644 index 00000000..2591f0e1 --- /dev/null +++ b/test/acceptance/analysis/analyses-controller.js @@ -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); + }); + + }); + }); +}); \ No newline at end of file diff --git a/test/support/test-client.js b/test/support/test-client.js index f31f9db4..3188d0fc 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -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); + } + ); +};