2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var redis = require('redis');
|
2015-04-22 00:28:31 +08:00
|
|
|
var step = require('step');
|
2013-12-18 00:35:12 +08:00
|
|
|
var strftime = require('strftime');
|
2016-02-23 02:11:54 +08:00
|
|
|
var QueryTables = require('cartodb-query-tables');
|
2015-07-13 22:36:41 +08:00
|
|
|
var NamedMapsCacheEntry = require('../../lib/cartodb/cache/model/named_maps_entry');
|
2013-12-18 00:35:12 +08:00
|
|
|
var redis_stats_db = 5;
|
|
|
|
|
2014-02-28 22:55:29 +08:00
|
|
|
// Pollute the PG environment to make sure
|
|
|
|
// configuration settings are always enforced
|
|
|
|
// See https://github.com/CartoDB/Windshaft-cartodb/issues/174
|
2015-04-22 00:28:31 +08:00
|
|
|
process.env.PGPORT = '666';
|
|
|
|
process.env.PGHOST = 'fake';
|
2014-02-28 22:55:29 +08:00
|
|
|
|
2015-09-17 01:53:14 +08:00
|
|
|
var fs = require('fs');
|
|
|
|
var http = require('http');
|
|
|
|
|
2014-03-04 17:46:15 +08:00
|
|
|
var helper = require(__dirname + '/../support/test_helper');
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
var CartodbWindshaft = require(__dirname + '/../../lib/cartodb/server');
|
|
|
|
var serverOptions = require(__dirname + '/../../lib/cartodb/server_options');
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2017-08-04 23:51:10 +08:00
|
|
|
var LayergroupToken = require('../../lib/cartodb/models/layergroup-token');
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
describe('template_api', function() {
|
2018-04-16 22:16:23 +08:00
|
|
|
var server;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
server = new CartodbWindshaft(serverOptions);
|
|
|
|
server.setMaxListeners(0);
|
2018-04-18 23:06:53 +08:00
|
|
|
// FIXME: we need a better way to reset cache while running tests
|
2018-04-16 22:16:23 +08:00
|
|
|
server.layergroupAffectedTablesCache.cache.reset();
|
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-09-17 01:53:14 +08:00
|
|
|
var httpRendererResourcesServer;
|
|
|
|
before(function(done) {
|
|
|
|
// Start a server to test external resources
|
|
|
|
httpRendererResourcesServer = http.createServer( function(request, response) {
|
|
|
|
var filename = __dirname + '/../fixtures/http/light_nolabels-1-0-0.png';
|
|
|
|
fs.readFile(filename, {encoding: 'binary'}, function(err, file) {
|
|
|
|
response.writeHead(200);
|
|
|
|
response.write(file, "binary");
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
httpRendererResourcesServer.listen(8033, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
httpRendererResourcesServer.close(done);
|
|
|
|
});
|
|
|
|
|
2015-09-25 23:04:08 +08:00
|
|
|
var keysToDelete;
|
|
|
|
beforeEach(function() {
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete = {};
|
2015-09-25 23:04:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function(done) {
|
|
|
|
helper.deleteRedisKeys(keysToDelete, done);
|
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
var template_acceptance1 = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance1',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
2015-04-22 00:28:31 +08:00
|
|
|
sql: 'select cartodb_id, ST_Translate(the_geom_webmercator, -5e6, 0) as the_geom_webmercator' +
|
|
|
|
' from test_table limit 2 offset 2',
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
2013-12-18 00:35:12 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-22 22:40:40 +08:00
|
|
|
function makeTemplate(templateName) {
|
|
|
|
return {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: templateName || 'acceptance1',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
2015-04-22 00:28:31 +08:00
|
|
|
sql: 'select cartodb_id, ST_Translate(the_geom_webmercator, -5e6, 0) as the_geom_webmercator' +
|
|
|
|
' from test_table limit 2 offset 2',
|
2015-01-22 22:40:40 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function extendDefaultsTemplate(template) {
|
|
|
|
return _.extend({}, template, {auth: {method: 'open'}, placeholders: {}});
|
|
|
|
}
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can add template, returning id", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-04-23 17:29:55 +08:00
|
|
|
var expected_tpl_id = "acceptance1";
|
2013-12-18 00:35:12 +08:00
|
|
|
var post_request_1 = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance1)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function postUnauthenticated()
|
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function postTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403);
|
2013-12-18 00:35:12 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'), res.body);
|
|
|
|
err = parsed.errors[0];
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(err.match(/only.*authenticated.*user/i),
|
|
|
|
'Unexpected error response: ' + err);
|
|
|
|
post_request_1.url += '?api_key=1234';
|
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function rePostTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
var expectedBody = { template_id: expected_tpl_id };
|
|
|
|
assert.deepEqual(parsedBody, expectedBody);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkFailure(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.hasOwnProperty('errors'), res.body);
|
|
|
|
assert.ok(parsedBody.errors[0].match(/already exists/i),
|
|
|
|
'Unexpected error for pre-existing template name: ' + parsedBody.errors);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2014-02-06 19:24:14 +08:00
|
|
|
// See https://github.com/CartoDB/Windshaft-cartodb/issues/128
|
2015-04-22 00:28:31 +08:00
|
|
|
it("cannot create template with auth='token' and no valid tokens", function(done) {
|
2014-02-06 19:24:14 +08:00
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
2014-02-06 19:24:14 +08:00
|
|
|
function postTemplate1()
|
|
|
|
{
|
|
|
|
// clone the valid one, and give it another name
|
|
|
|
var broken_template = JSON.parse(JSON.stringify(template_acceptance1));
|
|
|
|
broken_template.name = 'broken1';
|
|
|
|
// Set auth='token' and specify no tokens
|
|
|
|
broken_template.auth.method = 'token';
|
|
|
|
delete broken_template.auth.tokens;
|
|
|
|
var post_request_1 = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-02-06 19:24:14 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(broken_template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-06 19:24:14 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkFailure1(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.hasOwnProperty('errors'), res.body);
|
2015-04-22 00:28:31 +08:00
|
|
|
var re = /invalid.*authentication.*missing/i;
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.errors[0].match(re),
|
|
|
|
'Error for invalid authentication does not match ' + re + ': ' + parsedBody.errors);
|
2014-02-06 19:24:14 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function postTemplate2(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
// clone the valid one and rename it
|
|
|
|
var broken_template = JSON.parse(JSON.stringify(template_acceptance1));
|
|
|
|
broken_template.name = 'broken1';
|
|
|
|
// Set auth='token' and specify no tokens
|
|
|
|
broken_template.auth.method = 'token';
|
|
|
|
broken_template.auth.tokens = [];
|
|
|
|
var post_request_1 = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-02-06 19:24:14 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(broken_template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-06 19:24:14 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkFailure2(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.hasOwnProperty('errors'), res.body);
|
2015-04-22 00:28:31 +08:00
|
|
|
var re = new RegExp(/invalid.*authentication.*missing/i);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.errors[0].match(re),
|
|
|
|
'Error for invalid authentication does not match ' + re + ': ' + parsedBody.errors);
|
2014-02-06 19:24:14 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function postTemplateValid(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
// clone the valid one and rename it
|
|
|
|
var broken_template = JSON.parse(JSON.stringify(template_acceptance1));
|
|
|
|
broken_template.name = 'broken1';
|
|
|
|
var post_request_1 = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-02-06 19:24:14 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(broken_template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-06 19:24:14 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function putTemplateInvalid(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
// clone the valid one and rename it
|
|
|
|
var broken_template = JSON.parse(JSON.stringify(template_acceptance1));
|
|
|
|
broken_template.name = 'broken1';
|
|
|
|
// Set auth='token' and specify no tokens
|
|
|
|
broken_template.auth.method = 'token';
|
|
|
|
broken_template.auth.tokens = [];
|
|
|
|
var put_request_1 = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '/?api_key=1234',
|
2014-02-06 19:24:14 +08:00
|
|
|
method: 'PUT',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(broken_template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-06 19:24:14 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, put_request_1, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function deleteTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ": " + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
2015-04-22 00:28:31 +08:00
|
|
|
var re = /invalid.*authentication.*missing/i;
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.errors[0].match(re),
|
2014-02-06 19:24:14 +08:00
|
|
|
'Error for invalid authentication on PUT does not match ' +
|
2015-06-06 01:39:25 +08:00
|
|
|
re + ': ' + parsed.errors);
|
2014-02-06 19:24:14 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2014-02-06 19:24:14 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-06 19:24:14 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res, err) { next(err, res); });
|
|
|
|
},
|
|
|
|
function checkDelete(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-06 19:24:14 +08:00
|
|
|
assert.equal(res.statusCode, 204, res.statusCode + ': ' + res.body);
|
|
|
|
assert.ok(!res.body, 'Unexpected body in DELETE /template response');
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2014-02-06 19:24:14 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("instance endpoint should return CORS headers", function(done){
|
|
|
|
step(function postTemplate1() {
|
2014-01-23 02:10:09 +08:00
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-01-23 02:10:09 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost.localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance1)
|
|
|
|
};
|
|
|
|
assert.response(server, post_request, {}, function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function testCORS() {
|
2019-01-10 23:56:07 +08:00
|
|
|
const allowHeaders = 'X-Requested-With, X-Prototype-Version, X-CSRF-Token, Authorization, Content-Type';
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named/acceptance1',
|
|
|
|
method: 'OPTIONS'
|
|
|
|
},{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Access-Control-Allow-Headers': allowHeaders,
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
}
|
|
|
|
}, function() { done(); });
|
2014-01-23 02:10:09 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-22 22:31:01 +08:00
|
|
|
describe('server-metadata', function() {
|
|
|
|
var serverMetadata;
|
|
|
|
beforeEach(function() {
|
|
|
|
serverMetadata = global.environment.serverMetadata;
|
|
|
|
global.environment.serverMetadata = { cdn_url : { http:'test', https: 'tests' } };
|
|
|
|
});
|
2014-03-06 22:19:12 +08:00
|
|
|
|
2016-02-22 22:31:01 +08:00
|
|
|
afterEach(function() {
|
|
|
|
global.environment.serverMetadata = serverMetadata;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("instance endpoint should return server metadata", function(done){
|
|
|
|
var tmpl = _.clone(template_acceptance1);
|
|
|
|
tmpl.name = "rambotemplate2";
|
|
|
|
|
|
|
|
step(function postTemplate1() {
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(tmpl)
|
|
|
|
};
|
|
|
|
assert.response(server, post_request, {}, function(res) {
|
|
|
|
next(null, res);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function testCORS() {
|
|
|
|
var next = this;
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named/' + tmpl.name,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' }
|
|
|
|
},{
|
|
|
|
status: 200
|
|
|
|
}, function(res) {
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
assert.ok(_.isEqual(parsed.cdn_url, global.environment.serverMetadata.cdn_url));
|
|
|
|
next(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function deleteTemplate(err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
var del_request = {
|
|
|
|
url: '/api/v1/map/named/' + tmpl.name + '?api_key=1234',
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' }
|
|
|
|
};
|
|
|
|
assert.response(server, del_request, {}, function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2014-03-06 22:19:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-01-23 02:10:09 +08:00
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can list templates", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
var tplid1, tplid2;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate1()
|
2013-12-18 00:35:12 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance1)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function postTemplate2(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tplid1 = parsed.template_id;
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
var backup_name = template_acceptance1.name;
|
|
|
|
template_acceptance1.name += '_new';
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance1)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
template_acceptance1.name = backup_name;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function litsTemplatesUnauthenticated(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tplid2 = parsed.template_id;
|
|
|
|
var next = this;
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function litsTemplates(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403, res.statusCode + ': ' + res.body);
|
2013-12-18 00:35:12 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
2013-12-18 00:35:12 +08:00
|
|
|
'Missing error from response: ' + res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
err = parsed.errors[0];
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(err.match(/authenticated user/), err);
|
|
|
|
var next = this;
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkList(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_ids'),
|
|
|
|
"Missing 'template_ids' from response body: " + res.body);
|
|
|
|
var ids = parsed.template_ids;
|
|
|
|
assert.equal(ids.length, 2);
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ok(ids.indexOf(tplid1) !== -1,
|
2013-12-18 00:35:12 +08:00
|
|
|
'Missing "' + tplid1 + "' from list response: " + ids.join(','));
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ok(ids.indexOf(tplid2) !== -1,
|
2013-12-18 00:35:12 +08:00
|
|
|
'Missing "' + tplid2 + "' from list response: " + ids.join(','));
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can update template", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2013-12-18 00:35:12 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
2015-01-22 22:40:40 +08:00
|
|
|
data: JSON.stringify(makeTemplate())
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function putMisnamedTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var backup_name = template_acceptance1.name;
|
|
|
|
template_acceptance1.name = 'changed_name';
|
|
|
|
var put_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '/?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'PUT',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance1)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
template_acceptance1.name = backup_name;
|
|
|
|
var next = this;
|
|
|
|
assert.response(server, put_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function putUnexistentTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ": " + res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.hasOwnProperty('errors'), res.body);
|
|
|
|
assert.ok(parsedBody.errors[0].match(/cannot update name/i),
|
|
|
|
'Unexpected error for invalid update: ' + parsedBody.errors);
|
2013-12-18 00:35:12 +08:00
|
|
|
var put_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/unexistent/?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'PUT',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
2015-01-22 22:40:40 +08:00
|
|
|
data: JSON.stringify(makeTemplate())
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, put_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function putValidTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ": " + res.body);
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.hasOwnProperty('errors'), res.body);
|
|
|
|
assert.ok(parsedBody.errors[0].match(/cannot update name/i),
|
|
|
|
'Unexpected error for invalid update: ' + parsedBody.errors);
|
2013-12-18 00:35:12 +08:00
|
|
|
var put_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '/?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'PUT',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
2015-01-22 22:40:40 +08:00
|
|
|
data: JSON.stringify(makeTemplate())
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, put_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkValidUpate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ": " + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
assert.equal(tpl_id, parsed.template_id);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can get a template by id", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2013-12-18 00:35:12 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
2015-01-22 22:40:40 +08:00
|
|
|
data: JSON.stringify(makeTemplate())
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function getTemplateUnauthorized(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id,
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function getTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403, res.statusCode + ": " + res.body);
|
2013-12-18 00:35:12 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsedBody.hasOwnProperty('errors'), res.body);
|
|
|
|
assert.ok(parsedBody.errors[0].match(/only.*authenticated.*user/i),
|
|
|
|
'Unexpected error for unauthenticated template get: ' + parsedBody.errors);
|
2013-12-18 00:35:12 +08:00
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkReturnTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ": " + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template'),
|
|
|
|
"Missing 'template' from response body: " + res.body);
|
2015-01-22 22:40:40 +08:00
|
|
|
assert.deepEqual(extendDefaultsTemplate(makeTemplate()), parsed.template);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can delete a template by id", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2013-12-18 00:35:12 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
2015-01-22 22:40:40 +08:00
|
|
|
data: JSON.stringify(makeTemplate())
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function getTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function deleteTemplateUnauthorized(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ": " + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template'),
|
|
|
|
"Missing 'template' from response body: " + res.body);
|
2015-01-22 22:40:40 +08:00
|
|
|
assert.deepEqual(extendDefaultsTemplate(makeTemplate()), parsed.template);
|
2013-12-18 00:35:12 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id,
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function deleteTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403, res.statusCode + ": " + res.body);
|
2013-12-18 00:35:12 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/only.*authenticated.*user/i),
|
|
|
|
'Unexpected error for unauthenticated template get: ' + parsed.errors);
|
2013-12-18 00:35:12 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function getMissingTemplate(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 204, res.statusCode + ': ' + res.body);
|
|
|
|
assert.ok(!res.body, 'Unexpected body in DELETE /template response');
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkGetFailure(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 404, res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/cannot find/i),
|
|
|
|
'Unexpected error for missing template: ' + parsed.errors);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-23 17:29:55 +08:00
|
|
|
it("can instanciate a template by id wadus", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template_acceptance2 = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance1',
|
|
|
|
auth: { method: 'token', valid_tokens: ['valid1','valid2'] },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
|
|
|
sql: "select * from test_table_private_1 LIMIT 0",
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
2013-12-18 00:35:12 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var template_params = {};
|
|
|
|
|
|
|
|
var tpl_id;
|
|
|
|
var layergroupid;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2013-12-18 00:35:12 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance2)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateNoAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id,
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2014-02-28 23:05:46 +08:00
|
|
|
// See https://github.com/CartoDB/Windshaft-cartodb/issues/173
|
|
|
|
function instanciateForeignDB(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected success instanciating template with no auth: ' + res.statusCode + ': ' + res.body);
|
2013-12-18 00:35:12 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/unauthorized/i),
|
|
|
|
'Unexpected error for unauthorized instance : ' + parsed.errors);
|
2013-12-18 00:35:12 +08:00
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?auth_token=valid2',
|
2014-02-28 23:05:46 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'foreign', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-28 23:05:46 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-04-23 17:29:55 +08:00
|
|
|
assert.equal(res.statusCode, 404, res.statusCode + ': ' + res.body);
|
2014-02-28 23:05:46 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'), "Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/not found/i), 'Unexpected error for forbidden instance : ' + parsed.errors);
|
2014-02-28 23:05:46 +08:00
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?auth_token=valid2',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchTileNoAuth(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
|
|
|
'Instantiating template: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('layergroupid'),
|
|
|
|
"Missing 'layergroupid' from response body: " + res.body);
|
|
|
|
layergroupid = parsed.layergroupid;
|
|
|
|
assert.ok(layergroupid.match(/^localhost@/),
|
2015-04-22 00:28:31 +08:00
|
|
|
"Returned layergroupid does not start with signer name: " + layergroupid);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('last_updated'),
|
|
|
|
"Missing 'last_updated' from response body: " + res.body);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
// TODO: check value of last_updated ?
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb0/0/0/0.png',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchTileAuth(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2013-12-18 00:35:12 +08:00
|
|
|
'Fetching tile with no auth: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/permission denied/i),
|
|
|
|
'Unexpected error for unauthorized instance (expected /permission denied/): ' + parsed.errors);
|
2013-12-18 00:35:12 +08:00
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + '/0/0/0.png?auth_token=valid1',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkTile(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2016-01-29 02:44:25 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected error for authorized instance: ' + res.statusCode + ' -- ' + res.body);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.headers['content-type'], "image/png");
|
|
|
|
return null;
|
|
|
|
},
|
2014-02-28 20:24:38 +08:00
|
|
|
// See https://github.com/CartoDB/Windshaft-cartodb/issues/172
|
2015-04-22 00:28:31 +08:00
|
|
|
function fetchTileForeignSignature(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-28 20:24:38 +08:00
|
|
|
var foreignsigned = layergroupid.replace(/[^@]*@/, 'foreign@');
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + foreignsigned + '/0/0/0.png?auth_token=valid1',
|
2014-02-28 20:24:38 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-28 20:24:38 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkForeignSignerError(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2016-01-29 02:44:25 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected error for authorized instance: ' + res.statusCode + ' -- ' + res.body);
|
2014-02-28 20:24:38 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/cannot use/i),
|
|
|
|
'Unexpected error for unauthorized instance (expected /cannot use/): ' + parsed.errors);
|
2014-02-28 20:24:38 +08:00
|
|
|
return null;
|
|
|
|
},
|
2013-12-18 00:35:12 +08:00
|
|
|
function deleteTemplate(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchTileDeleted(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 204,
|
|
|
|
'Deleting template: ' + res.statusCode + ':' + res.body);
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + '/0/0/0.png?auth_token=valid1',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2015-01-22 22:40:40 +08:00
|
|
|
function checkTileAvailable(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-01-22 22:40:40 +08:00
|
|
|
assert.equal(res.statusCode, 200, 'Tile should be accessible');
|
|
|
|
assert.equal(res.headers['content-type'], "image/png");
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2014-02-08 01:00:13 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can instanciate a template with torque layer by id", function(done) {
|
2014-02-08 01:00:13 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance1',
|
|
|
|
auth: { method: 'token', valid_tokens: ['valid1','valid2'] },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.1.0',
|
|
|
|
layers: [
|
|
|
|
{ type: 'torque', options: {
|
|
|
|
sql: "select * from test_table_private_1 LIMIT 0",
|
2015-04-22 00:28:31 +08:00
|
|
|
cartocss: "Map { -torque-frame-count:1; -torque-resolution:1; " +
|
|
|
|
"-torque-aggregation-function:'count(*)'; -torque-time-attribute:'updated_at'; }"
|
2014-02-08 01:00:13 +08:00
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var template_params = {};
|
|
|
|
|
|
|
|
var tpl_id;
|
|
|
|
var layergroupid;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2014-02-08 01:00:13 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateNoAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-08 01:00:13 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id,
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected success instanciating template with no auth: ' + res.statusCode + ': ' + res.body);
|
2014-02-08 01:00:13 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/unauthorized/i),
|
|
|
|
'Unexpected error for unauthorized instance : ' + parsed.errors);
|
2014-02-08 01:00:13 +08:00
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?auth_token=valid2',
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchTileNoAuth(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-08 01:00:13 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
|
|
|
'Instantiating template: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('layergroupid'),
|
|
|
|
"Missing 'layergroupid' from response body: " + res.body);
|
|
|
|
layergroupid = parsed.layergroupid;
|
|
|
|
assert.ok(layergroupid.match(/^localhost@/),
|
2015-04-22 00:28:31 +08:00
|
|
|
"Returned layergroupid does not start with signer name: " + layergroupid);
|
2014-02-08 01:00:13 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('last_updated'),
|
|
|
|
"Missing 'last_updated' from response body: " + res.body);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2014-02-08 01:00:13 +08:00
|
|
|
// TODO: check value of last_updated ?
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb0/0/0/0/0.json.torque',
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchTileAuth(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2014-02-08 01:00:13 +08:00
|
|
|
'Fetching tile with no auth: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/permission denied/i),
|
|
|
|
'Unexpected error for unauthorized instance (expected /permission denied): ' + parsed.errors);
|
2014-02-08 01:00:13 +08:00
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb1/0/0/0/0.json.torque?auth_token=valid1',
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2014-04-08 15:16:16 +08:00
|
|
|
function checkTile_fetchOnRestart(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2016-01-29 02:44:25 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected error for authorized instance: ' + res.statusCode + ' -- ' + res.body);
|
2014-04-08 15:16:16 +08:00
|
|
|
assert.equal(res.headers['content-type'], "application/json; charset=utf-8");
|
|
|
|
var cc = res.headers['x-cache-channel'];
|
2017-03-14 00:28:29 +08:00
|
|
|
var expectedCC = 'test_windshaft_cartodb_user_1_db:public.test_table_private_1';
|
2014-04-08 15:16:16 +08:00
|
|
|
assert.ok(cc);
|
2017-03-14 00:28:29 +08:00
|
|
|
assert.equal(cc, expectedCC);
|
2014-04-08 15:16:16 +08:00
|
|
|
// hack simulating restart...
|
2018-04-04 01:08:56 +08:00
|
|
|
// FIXME: we need a better way to reset cache while running tests
|
2015-07-15 22:51:26 +08:00
|
|
|
server.layergroupAffectedTablesCache.cache.reset(); // need to clean channel cache
|
2014-04-08 15:16:16 +08:00
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb1/0/0/0/1.json.torque?auth_token=valid1',
|
2014-04-08 15:16:16 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-04-08 15:16:16 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkCacheChannel(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2016-01-29 02:44:25 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected error for authorized instance: ' + res.statusCode + ' -- ' + res.body);
|
2014-02-08 01:00:13 +08:00
|
|
|
assert.equal(res.headers['content-type'], "application/json; charset=utf-8");
|
2014-04-08 15:16:16 +08:00
|
|
|
var cc = res.headers['x-cache-channel'];
|
2017-03-14 00:28:29 +08:00
|
|
|
var expectedCC = 'test_windshaft_cartodb_user_1_db:public.test_table_private_1';
|
2014-04-08 15:16:16 +08:00
|
|
|
assert.ok(cc, "Missing X-Cache-Channel on fetch-after-restart");
|
2017-03-14 00:28:29 +08:00
|
|
|
assert.equal(cc, expectedCC);
|
2014-02-08 01:00:13 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function deleteTemplate(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-08 01:00:13 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchTileDeleted(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-08 01:00:13 +08:00
|
|
|
assert.equal(res.statusCode, 204,
|
|
|
|
'Deleting template: ' + res.statusCode + ':' + res.body);
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb2/0/0/0/0.json.torque?auth_token=valid1',
|
2014-02-08 01:00:13 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-08 01:00:13 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2015-01-22 22:40:40 +08:00
|
|
|
function checkTorqueTileAvailable(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-01-22 22:40:40 +08:00
|
|
|
assert.equal(res.statusCode, 200, 'Torque tile should be accessible');
|
|
|
|
assert.equal(res.headers['content-type'], "application/json; charset=utf-8");
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2014-02-10 19:31:36 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can instanciate a template with attribute service by id", function(done) {
|
2014-02-10 19:31:36 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance1',
|
|
|
|
auth: { method: 'token', valid_tokens: ['valid1','valid2'] },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.1.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
|
|
|
sql: "select * from test_table_private_1 where cartodb_id in ( 5,6 )",
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
2014-02-10 19:31:36 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
attributes: { id:'cartodb_id', columns: ['name', 'address'] }
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var template_params = {};
|
|
|
|
|
|
|
|
var tpl_id;
|
|
|
|
var layergroupid;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2014-02-10 19:31:36 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateNoAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 19:31:36 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id,
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected success instanciating template with no auth: ' + res.statusCode + ': ' + res.body);
|
2014-02-10 19:31:36 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/unauthorized/i),
|
|
|
|
'Unexpected error for unauthorized instance : ' + parsed.errors);
|
2014-02-10 19:31:36 +08:00
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?auth_token=valid2',
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
2015-04-22 00:28:31 +08:00
|
|
|
function(res) { next(null, res); });
|
2014-02-10 19:31:36 +08:00
|
|
|
},
|
|
|
|
function fetchAttributeNoAuth(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 19:31:36 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
|
|
|
'Instantiating template: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('layergroupid'),
|
|
|
|
"Missing 'layergroupid' from response body: " + res.body);
|
|
|
|
layergroupid = parsed.layergroupid;
|
|
|
|
assert.ok(layergroupid.match(/^localhost@/),
|
2015-04-22 00:28:31 +08:00
|
|
|
"Returned layergroupid does not start with signer name: " + layergroupid);
|
2015-06-18 07:13:33 +08:00
|
|
|
assert.equal(res.headers['x-layergroup-id'], parsed.layergroupid);
|
2014-02-10 19:31:36 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('last_updated'),
|
|
|
|
"Missing 'last_updated' from response body: " + res.body);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2014-02-10 19:31:36 +08:00
|
|
|
// TODO: check value of last_updated ?
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb0/0/attributes/5',
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchAttributeAuth(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 00:18:06 +08:00
|
|
|
assert.equal(res.statusCode, 403,
|
2014-02-10 19:31:36 +08:00
|
|
|
'Fetching tile with no auth: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.ok(parsed.hasOwnProperty('errors'),
|
|
|
|
"Missing 'errors' from response body: " + res.body);
|
|
|
|
assert.ok(parsed.errors[0].match(/permission denied/i),
|
|
|
|
'Unexpected error for unauthorized getAttributes (expected /permission denied/): ' + parsed.errors);
|
2014-02-10 19:31:36 +08:00
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb1/0/attributes/5?auth_token=valid2',
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function checkAttribute(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2016-01-29 02:44:25 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected error for authorized getAttributes: ' + res.statusCode + ' -- ' + res.body);
|
2014-02-10 19:31:36 +08:00
|
|
|
assert.equal(res.headers['content-type'], "application/json; charset=utf-8");
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function deleteTemplate(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 19:31:36 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?api_key=1234',
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function fetchAttrDeleted(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 19:31:36 +08:00
|
|
|
assert.equal(res.statusCode, 204,
|
|
|
|
'Deleting template: ' + res.statusCode + ':' + res.body);
|
|
|
|
var get_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + ':cb2/0/attributes/5?auth_token=valid2',
|
2014-02-10 19:31:36 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 19:31:36 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, get_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2015-01-22 22:40:40 +08:00
|
|
|
function checkLayerAttributesAvailable(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-01-22 22:40:40 +08:00
|
|
|
assert.equal(res.statusCode, 200, 'Layer attributes should be accessible');
|
|
|
|
assert.equal(res.headers['content-type'], "application/json; charset=utf-8");
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can instanciate a template by id with open auth", function(done) {
|
2014-01-28 19:12:33 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template_acceptance_open = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance_open',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
|
|
|
sql: "select * from test_table_private_1 LIMIT 0",
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
2014-01-28 19:12:33 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var template_params = {};
|
|
|
|
|
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2014-01-28 19:12:33 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-01-28 19:12:33 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance_open)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-01-28 19:12:33 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateNoAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-01-28 19:12:33 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id,
|
2014-01-28 19:12:33 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-03-04 17:46:15 +08:00
|
|
|
helper.checkNoCache(res);
|
2014-01-28 19:12:33 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-01-28 19:12:33 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
2015-04-22 00:28:31 +08:00
|
|
|
'Unexpected success instanciating template with no auth: ' + res.statusCode + ': ' + res.body);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(JSON.parse(res.body).layergroupid).token] = 0;
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2014-01-28 19:12:33 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can instanciate a template using jsonp", function(done) {
|
2014-01-29 20:11:37 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template_acceptance_open = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance_open_jsonp',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
|
|
|
sql: "select * from test_table_private_1 LIMIT 0",
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
2014-01-29 20:11:37 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2014-01-29 20:11:37 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-01-29 20:11:37 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance_open)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-01-29 20:11:37 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateNoAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-01-29 20:11:37 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-09-25 23:04:08 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + "/jsonp?callback=json_test",
|
2014-01-29 20:11:37 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' }
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-01-29 20:11:37 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2014-03-04 17:46:15 +08:00
|
|
|
function checkInstanciation(err, res)
|
2014-01-29 20:11:37 +08:00
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 17:46:15 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// See https://github.com/CartoDB/Windshaft-cartodb/issues/176
|
2014-06-24 22:05:54 +08:00
|
|
|
helper.checkCache(res);
|
2015-07-13 22:36:41 +08:00
|
|
|
var expectedSurrogateKey = [
|
2016-02-22 18:40:25 +08:00
|
|
|
new QueryTables.DatabaseTablesEntry([{dbname: 'test_windshaft_cartodb_user_1_db', schema_name: 'public',
|
2016-02-15 23:21:13 +08:00
|
|
|
table_name: 'test_table_private_1'}]).key(),
|
2015-07-13 22:36:41 +08:00
|
|
|
new NamedMapsCacheEntry('localhost', template_acceptance_open.name).key()
|
|
|
|
].join(' ');
|
|
|
|
helper.checkSurrogateKey(res, expectedSurrogateKey);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
// jshint ignore:start
|
|
|
|
function json_test(body) {
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(body.layergroupid).token] = 0;
|
|
|
|
}
|
|
|
|
eval(res.body);
|
|
|
|
// jshint ignore:end
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2014-03-04 17:46:15 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
2014-01-29 20:11:37 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can instanciate a template using jsonp with params", function(done) {
|
2014-01-29 20:11:37 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template_acceptance_open = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance_open_jsonp_params',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
placeholders: {
|
|
|
|
color: { type: "css_color", default: "red" }
|
2015-01-24 00:02:13 +08:00
|
|
|
},
|
2014-01-29 20:11:37 +08:00
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
|
|
|
sql: "select * from test_table_private_1 LIMIT 0",
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill: <%= color %>; marker-allow-overlap:true; }',
|
2014-01-29 20:11:37 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var tpl_id;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2014-01-29 20:11:37 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-01-29 20:11:37 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance_open)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-01-29 20:11:37 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instanciateNoAuth(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-01-29 20:11:37 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-09-25 23:04:08 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + "/jsonp?callback=json_test&config=" + JSON.stringify({color:'blue'}),
|
2014-01-29 20:11:37 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'localhost' }
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-01-29 20:11:37 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
2014-03-04 17:46:15 +08:00
|
|
|
function checkInstanciation(err, res)
|
2014-01-29 20:11:37 +08:00
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-03-04 17:46:15 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
|
|
|
// See https://github.com/CartoDB/Windshaft-cartodb/issues/176
|
2015-01-24 00:02:13 +08:00
|
|
|
helper.checkCache(res);
|
2015-07-13 22:36:41 +08:00
|
|
|
var expectedSurrogateKey = [
|
2016-02-22 22:26:06 +08:00
|
|
|
new QueryTables.DatabaseTablesEntry([{dbname: 'test_windshaft_cartodb_user_1_db', schema_name: 'public',
|
2016-02-15 23:21:13 +08:00
|
|
|
table_name: 'test_table_private_1'}]).key(),
|
2015-07-13 22:36:41 +08:00
|
|
|
new NamedMapsCacheEntry('localhost', template_acceptance_open.name).key()
|
|
|
|
].join(' ');
|
|
|
|
helper.checkSurrogateKey(res, expectedSurrogateKey);
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
// jshint ignore:start
|
|
|
|
function json_test(body) {
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(body.layergroupid).token] = 0;
|
|
|
|
}
|
|
|
|
eval(res.body);
|
|
|
|
// jshint ignore:end
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2014-01-29 20:11:37 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("template instantiation raises mapviews counter", function(done) {
|
2013-12-18 00:35:12 +08:00
|
|
|
var layergroup = {
|
|
|
|
stat_tag: 'random_tag',
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
2015-04-22 00:28:31 +08:00
|
|
|
sql: 'select 1 as cartodb_id, !pixel_height! as h,' +
|
|
|
|
' ST_Buffer(!bbox!, -32*greatest(!pixel_width!,!pixel_height!)) as the_geom_webmercator',
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { polygon-fill:red; }',
|
|
|
|
cartocss_version: '2.0.1'
|
2013-12-18 00:35:12 +08:00
|
|
|
} }
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'stat_gathering',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
layergroup: layergroup
|
|
|
|
};
|
|
|
|
var statskey = "user:localhost:mapviews";
|
|
|
|
var redis_stats_client = redis.createClient(global.environment.redis.port);
|
2016-01-29 02:44:25 +08:00
|
|
|
var template_id; // will be set on template post
|
2013-12-18 00:35:12 +08:00
|
|
|
var now = strftime("%Y%m%d", new Date());
|
|
|
|
var errors = [];
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function clean_stats()
|
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
redis_stats_client.select(redis_stats_db, function(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err ) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
redis_stats_client.del(statskey+':global', next);
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function do_post_tempate(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instantiateTemplate(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
template_id = JSON.parse(res.body).template_id;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + template_id,
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify({})
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function check_global_stats(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
|
|
|
'Instantiating template: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('layergroupid'),
|
|
|
|
"Missing 'layergroupid' from response body: " + res.body);
|
2015-09-25 23:04:08 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-04-22 00:28:31 +08:00
|
|
|
redis_stats_client.ZSCORE(statskey + ":global", now, this);
|
2013-12-18 00:35:12 +08:00
|
|
|
},
|
|
|
|
function check_tag_stats(err, val) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-04-22 00:28:31 +08:00
|
|
|
assert.equal(val, 1, "Expected score of " + now + " in " + statskey + ":global to be 1, got " + val);
|
|
|
|
redis_stats_client.ZSCORE(statskey+':stat_tag:random_tag', now, this);
|
2013-12-18 00:35:12 +08:00
|
|
|
},
|
|
|
|
function check_tag_stats_value(err, val) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-04-22 00:28:31 +08:00
|
|
|
assert.equal(val, 1, "Expected score of " + now + " in " + statskey + ":stat_tag:" + layergroup.stat_tag +
|
|
|
|
" to be 1, got " + val);
|
2013-12-18 00:35:12 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function deleteTemplate(err)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
var del_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + template_id + '?api_key=1234',
|
2013-12-18 00:35:12 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {host: 'localhost'}
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, del_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function cleanup_stats(err, res) {
|
2015-09-25 23:04:08 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2015-07-15 21:03:28 +08:00
|
|
|
}
|
2015-09-25 23:04:08 +08:00
|
|
|
assert.equal(res.statusCode, 204, res.statusCode + ': ' + res.body);
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err ) {
|
|
|
|
errors.push('' + err);
|
|
|
|
}
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
keysToDelete[statskey+':stat_tag:'+layergroup.stat_tag] = 5;
|
|
|
|
|
|
|
|
done();
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("instance map token changes with templates certificate changes", function(done) {
|
2014-02-10 22:48:35 +08:00
|
|
|
|
|
|
|
// This map fetches data from a private table
|
|
|
|
var template_acceptance2 = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance2',
|
|
|
|
auth: { method: 'token', valid_tokens: ['valid1','valid2'] },
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
|
|
|
sql: "select * from test_table_private_1 LIMIT 0",
|
2016-01-29 02:44:25 +08:00
|
|
|
cartocss: '#layer { marker-fill:blue; marker-allow-overlap:true; }',
|
2014-02-10 22:48:35 +08:00
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var template_params = {};
|
|
|
|
|
|
|
|
var tpl_id;
|
|
|
|
var layergroupid;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
|
|
|
function postTemplate()
|
2014-02-10 22:48:35 +08:00
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2014-02-10 22:48:35 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_acceptance2)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 22:48:35 +08:00
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instance1(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 22:48:35 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
tpl_id = parsed.template_id;
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?auth_token=valid2',
|
2014-02-10 22:48:35 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 22:48:35 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res, err) { next(err, res); });
|
|
|
|
},
|
|
|
|
function checkInstance1(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 22:48:35 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
|
|
|
'Instantiating template: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('layergroupid'),
|
|
|
|
"Missing 'layergroupid' from response body: " + res.body);
|
|
|
|
layergroupid = parsed.layergroupid;
|
2015-01-24 00:02:13 +08:00
|
|
|
helper.checkSurrogateKey(res, new NamedMapsCacheEntry('localhost', template_acceptance2.name).key());
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2014-02-10 22:48:35 +08:00
|
|
|
return null;
|
|
|
|
},
|
2015-04-22 00:28:31 +08:00
|
|
|
function updateTemplate(err)
|
2014-02-10 22:48:35 +08:00
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 22:48:35 +08:00
|
|
|
// clone the valid one and rename it
|
|
|
|
var changedTemplate = JSON.parse(JSON.stringify(template_acceptance2));
|
|
|
|
changedTemplate.auth.method = 'open';
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '/?api_key=1234',
|
2014-02-10 22:48:35 +08:00
|
|
|
method: 'PUT',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(changedTemplate)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 22:48:35 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res) { next(null, res); });
|
|
|
|
},
|
|
|
|
function instance2(err, res)
|
|
|
|
{
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 22:48:35 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'),
|
|
|
|
"Missing 'template_id' from response body: " + res.body);
|
|
|
|
assert.equal(tpl_id, parsed.template_id);
|
|
|
|
var post_request = {
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + tpl_id + '?auth_token=valid2',
|
2014-02-10 22:48:35 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template_params)
|
2015-04-22 00:28:31 +08:00
|
|
|
};
|
2014-02-10 22:48:35 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(server, post_request, {},
|
|
|
|
function(res, err) { next(err, res); });
|
|
|
|
},
|
|
|
|
function checkInstance2(err, res) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-10 22:48:35 +08:00
|
|
|
assert.equal(res.statusCode, 200,
|
|
|
|
'Instantiating template: ' + res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('layergroupid'),
|
|
|
|
"Missing 'layergroupid' from response body: " + res.body);
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ok(layergroupid !== parsed.layergroupid);
|
2015-01-24 00:02:13 +08:00
|
|
|
helper.checkSurrogateKey(res, new NamedMapsCacheEntry('localhost', template_acceptance2.name).key());
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
|
|
|
done();
|
2014-02-10 22:48:35 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-02-05 01:57:12 +08:00
|
|
|
|
2015-04-22 00:28:31 +08:00
|
|
|
it("can use an http layer", function(done) {
|
2015-02-05 01:57:12 +08:00
|
|
|
|
|
|
|
var username = 'localhost';
|
|
|
|
|
|
|
|
var httpTemplateName = 'acceptance_http';
|
|
|
|
var httpTemplate = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: httpTemplateName,
|
|
|
|
layergroup: {
|
|
|
|
version: '1.3.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: "http",
|
|
|
|
options: {
|
2015-09-17 01:53:14 +08:00
|
|
|
urlTemplate: "http://127.0.0.1:8033/{s}/{z}/{x}/{y}.png",
|
2015-02-05 01:57:12 +08:00
|
|
|
subdomains: [
|
|
|
|
"a",
|
|
|
|
"b",
|
|
|
|
"c"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
sql: "select * from test_table_private_1",
|
|
|
|
cartocss: '#layer { marker-fill:blue; }',
|
|
|
|
cartocss_version: '2.0.2',
|
|
|
|
interactivity: 'cartodb_id'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var template_params = {};
|
|
|
|
|
2015-04-23 17:29:55 +08:00
|
|
|
var expectedTemplateId = httpTemplateName;
|
2015-02-05 01:57:12 +08:00
|
|
|
var layergroupid;
|
2015-04-22 00:28:31 +08:00
|
|
|
step(
|
2015-02-05 01:57:12 +08:00
|
|
|
function createTemplate()
|
|
|
|
{
|
|
|
|
var next = this;
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
2015-02-05 01:57:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: username,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(httpTemplate)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
next(err, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function instantiateTemplate(err, res) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
assert.deepEqual(JSON.parse(res.body), { template_id: expectedTemplateId });
|
|
|
|
var next = this;
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + expectedTemplateId,
|
2015-02-05 01:57:12 +08:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: username,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(template_params)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
next(null, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function fetchTile(err, res) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-04-22 00:28:31 +08:00
|
|
|
assert.ok(
|
|
|
|
parsed.hasOwnProperty('layergroupid'), "Missing 'layergroupid' from response body: " + res.body);
|
2015-02-05 01:57:12 +08:00
|
|
|
layergroupid = parsed.layergroupid;
|
|
|
|
|
2015-09-25 23:04:08 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-09-25 23:51:19 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-09-25 23:04:08 +08:00
|
|
|
|
2015-02-05 01:57:12 +08:00
|
|
|
var next = this;
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/' + layergroupid + '/all/0/0/0.png',
|
2015-02-05 01:57:12 +08:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
},
|
|
|
|
encoding: 'binary'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
next(null, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function checkTile(err, res) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
assert.equal(res.headers['content-type'], "image/png");
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function deleteTemplate(err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
var next = this;
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
2015-03-23 22:54:45 +08:00
|
|
|
url: '/api/v1/map/named/' + expectedTemplateId + '?api_key=1234',
|
2015-02-05 01:57:12 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 204
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
next(err, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2015-09-25 23:04:08 +08:00
|
|
|
done(err);
|
2015-02-05 01:57:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-22 00:59:52 +08:00
|
|
|
describe('named map nonexistent tokens', function() {
|
|
|
|
var username = 'localhost';
|
|
|
|
var templateHash = 'deadbeef';
|
|
|
|
var nonexistentToken = 'wadus';
|
|
|
|
|
|
|
|
function request(token) {
|
|
|
|
return {
|
|
|
|
url: '/api/v1/map/' + token + '/all/0/0/0.png',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
},
|
|
|
|
encoding: 'binary'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var expectedResponse = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
},
|
|
|
|
status: 400
|
|
|
|
};
|
|
|
|
|
|
|
|
function checkTileFn(done) {
|
|
|
|
return function checkTile(res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2016-06-13 22:14:01 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body).errors,
|
|
|
|
["Invalid or nonexistent map configuration token '" + nonexistentToken + "'"]);
|
2015-04-22 00:59:52 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it("returns an error for named map nonexistent tokens", function(done) {
|
|
|
|
|
|
|
|
var nonexistentNamedMapToken = username + '@' + templateHash + '@' + nonexistentToken;
|
|
|
|
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
request(nonexistentNamedMapToken),
|
|
|
|
expectedResponse,
|
|
|
|
checkTileFn(done)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns an error for named map nonexistent tokens without template hash", function(done) {
|
|
|
|
|
|
|
|
var nonexistentNamedMapToken = username + '@' + nonexistentToken;
|
|
|
|
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
request(nonexistentNamedMapToken),
|
|
|
|
expectedResponse,
|
|
|
|
checkTileFn(done)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-12-22 03:01:44 +08:00
|
|
|
var torqueParamsScenarios = [
|
|
|
|
{
|
|
|
|
templateParams: {},
|
|
|
|
expectedTile: [{"x__uint8":125,"y__uint8":159,"vals__uint8":[2],"dates__uint16":[0]}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
templateParams: { namesFilter: "'Hawai'" },
|
|
|
|
expectedTile: [{"x__uint8":125,"y__uint8":159,"vals__uint8":[1],"dates__uint16":[0]}]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
torqueParamsScenarios.forEach(function(scenario) {
|
|
|
|
it("can instantiate with torque layer and params=" + JSON.stringify(scenario.templateParams), function(done) {
|
|
|
|
|
|
|
|
var torqueParamsTemplate = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance_torque_params',
|
|
|
|
auth: {
|
|
|
|
method: 'open'
|
|
|
|
},
|
|
|
|
placeholders: {
|
|
|
|
namesFilter: {
|
|
|
|
"type": "sql_ident",
|
|
|
|
"default": "'Hawai', 'El Estocolmo'"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
version: '1.4.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: 'torque',
|
|
|
|
options: {
|
|
|
|
sql: "select * from test_table_private_1 where name in (<%= namesFilter %>)",
|
|
|
|
cartocss: "Map { -torque-frame-count:1; -torque-resolution:1; " +
|
|
|
|
"-torque-aggregation-function:'count(*)'; -torque-time-attribute:'cartodb_id'; }",
|
|
|
|
cartocss_version: '2.0.2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var layergroupIdToDelete;
|
|
|
|
step(
|
|
|
|
function createTemplate() {
|
|
|
|
var next = this;
|
|
|
|
var createTemplateRequest = {
|
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(torqueParamsTemplate)
|
|
|
|
};
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
createTemplateRequest,
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
next(err, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function instantiateTemplate(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.hasOwnProperty('template_id'), "Missing 'template_id' from response: " + res.body);
|
|
|
|
var templateId = parsed.template_id;
|
|
|
|
var instantiatePostRequest = {
|
|
|
|
url: '/api/v1/map/named/' + templateId,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(scenario.templateParams)
|
|
|
|
};
|
|
|
|
var next = this;
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
instantiatePostRequest,
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
return next(err, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function requestTile(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var layergroupId = JSON.parse(res.body).layergroupid;
|
|
|
|
layergroupIdToDelete = LayergroupToken.parse(layergroupId).token;
|
|
|
|
|
|
|
|
var torqueTileRequest = {
|
|
|
|
url: '/api/v1/map/' + layergroupId + '/0/0/0/0.torque.json',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var next = this;
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
torqueTileRequest,
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
return next(err, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function validateTileAndFinish(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + layergroupIdToDelete] = 0;
|
|
|
|
keysToDelete['map_tpl|localhost'] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
JSON.parse(res.body),
|
|
|
|
scenario.expectedTile
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
});
|