2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 17:29:07 +08:00
|
|
|
var testHelper = require('../support/test-helper');
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var RedisPool = require('redis-mpool');
|
2019-10-07 17:29:07 +08:00
|
|
|
var TemplateMaps = require('../../lib/backends/template-maps');
|
2015-04-27 23:58:56 +08:00
|
|
|
var step = require('step');
|
|
|
|
var _ = require('underscore');
|
|
|
|
|
|
|
|
describe('template_maps', function() {
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
// configure redis pool instance to use in tests
|
2015-04-27 23:58:56 +08:00
|
|
|
var redis_pool = new RedisPool(global.environment.redis);
|
2014-09-26 01:00:35 +08:00
|
|
|
|
2015-09-25 23:59:00 +08:00
|
|
|
var keysToDelete;
|
|
|
|
beforeEach(function() {
|
|
|
|
keysToDelete = {};
|
|
|
|
});
|
2015-09-25 20:08:59 +08:00
|
|
|
afterEach(function(done) {
|
2015-09-25 23:59:00 +08:00
|
|
|
testHelper.deleteRedisKeys(keysToDelete, done);
|
2015-09-17 21:07:54 +08:00
|
|
|
});
|
|
|
|
|
2015-01-26 22:51:10 +08:00
|
|
|
var wadusLayer = {
|
|
|
|
options: {
|
|
|
|
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
|
|
|
|
cartocss: '#layer { marker-fill:blue; }',
|
|
|
|
cartocss_version: '2.3.0'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-24 01:59:39 +08:00
|
|
|
var LAYER_PLAIN = {
|
|
|
|
type: 'plain',
|
|
|
|
options: {
|
|
|
|
color: 'red'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('does not accept template with unsupported version', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var tpl = { version:'6.6.6',
|
2015-01-26 22:51:10 +08:00
|
|
|
name:'k', auth: {}, layergroup: {layers:[wadusLayer]} };
|
2015-04-27 23:58:56 +08:00
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function() {
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function checkFailed(err) {
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/unsupported.*version/i), err);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('does not accept template with missing name', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var tpl = { version:'0.0.1',
|
2015-01-26 22:51:10 +08:00
|
|
|
auth: {}, layergroup: {layers:[wadusLayer]} };
|
2015-04-27 23:58:56 +08:00
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function() {
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function checkFailed(err) {
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/missing.*name/i), err);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-06-04 22:41:40 +08:00
|
|
|
describe('naming', function() {
|
|
|
|
|
|
|
|
function createTemplate(name) {
|
|
|
|
return {
|
|
|
|
version:'0.0.1',
|
|
|
|
name: name,
|
|
|
|
auth: {},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
wadusLayer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
2015-06-04 22:41:40 +08:00
|
|
|
var templateMaps = new TemplateMaps(redis_pool);
|
|
|
|
|
|
|
|
var invalidNames = [ "ab|", "a b", "a@b", "-1ab", "_x", "", " x", "x " ];
|
|
|
|
invalidNames.forEach(function(invalidName) {
|
|
|
|
it('should NOT accept template with invalid name: ' + invalidName, function(done) {
|
|
|
|
templateMaps.addTemplate('me', createTemplate(invalidName), function(err) {
|
|
|
|
assert.ok(err, "Unexpected success with invalid name '" + invalidName + "'");
|
|
|
|
assert.ok(
|
|
|
|
err.message.match(/template.*name/i),
|
|
|
|
"Unexpected error message with invalid name '" + invalidName + "': " + err.message
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var validNames = [
|
|
|
|
"AB", "1ab", "DFD19A1A-0AC6-11E5-B0CA-6476BA93D4F6", "25ad8300-0ac7-11e5-b93f-6476ba93d4f6"
|
|
|
|
];
|
|
|
|
validNames.forEach(function(validName) {
|
|
|
|
it('should accept template with valid name: ' + validName, function(done) {
|
|
|
|
templateMaps.addTemplate('me', createTemplate(validName), function(err) {
|
|
|
|
assert.ok(!err, "Unexpected error with valid name '" + validName + "': " + err);
|
2015-09-25 23:59:00 +08:00
|
|
|
keysToDelete['map_tpl|me'] = 0;
|
2015-06-04 22:41:40 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2013-12-18 00:35:12 +08:00
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('does not accept template with invalid placeholder name', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var tpl = { version:'0.0.1',
|
|
|
|
name: "valid", placeholders: {},
|
2015-01-26 22:51:10 +08:00
|
|
|
auth: {}, layergroup: {layers:[wadusLayer]} };
|
2013-12-18 00:35:12 +08:00
|
|
|
var invalidnames = [ "ab|", "a b", "a@b", "1ab", "_x", "", " x", "x " ];
|
|
|
|
var testNext = function() {
|
|
|
|
if ( ! invalidnames.length ) { done(); return; }
|
|
|
|
var n = invalidnames.pop();
|
|
|
|
tpl.placeholders = {};
|
|
|
|
tpl.placeholders[n] = { type:'number', default:1 };
|
|
|
|
tmap.addTemplate('me', tpl, function(err) {
|
|
|
|
if ( ! err ) {
|
|
|
|
done(new Error("Unexpected success with invalid name '" + n + "'"));
|
|
|
|
}
|
|
|
|
else if ( ! err.message.match(/invalid.*name/i) ) {
|
2015-04-27 23:58:56 +08:00
|
|
|
done(new Error("Unexpected error message with invalid name '" + n + "': " + err));
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
testNext();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
testNext();
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('does not accept template with missing placeholder default', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var tpl = { version:'0.0.1',
|
|
|
|
name: "valid", placeholders: { v: {} },
|
2015-01-26 22:51:10 +08:00
|
|
|
auth: {}, layergroup: {layers:[wadusLayer]} };
|
2013-12-18 00:35:12 +08:00
|
|
|
tmap.addTemplate('me', tpl, function(err) {
|
|
|
|
if ( ! err ) {
|
|
|
|
done(new Error("Unexpected success with missing placeholder default"));
|
|
|
|
}
|
|
|
|
else if ( ! err.message.match(/missing default/i) ) {
|
2015-04-27 23:58:56 +08:00
|
|
|
done(new Error("Unexpected error message with missing placeholder default: " + err));
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('does not accept template with missing placeholder type', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var tpl = { version:'0.0.1',
|
|
|
|
name: "valid", placeholders: { v: { default:1 } },
|
2015-01-26 22:51:10 +08:00
|
|
|
auth: {}, layergroup: {layers:[wadusLayer]} };
|
2013-12-18 00:35:12 +08:00
|
|
|
tmap.addTemplate('me', tpl, function(err) {
|
|
|
|
if ( ! err ) {
|
|
|
|
done(new Error("Unexpected success with missing placeholder type"));
|
|
|
|
}
|
|
|
|
else if ( ! err.message.match(/missing type/i) ) {
|
2015-04-27 23:58:56 +08:00
|
|
|
done(new Error("Unexpected error message with missing placeholder default: " + err));
|
2013-12-18 00:35:12 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-06 19:24:14 +08:00
|
|
|
// See http://github.com/CartoDB/Windshaft-cartodb/issues/128
|
2015-04-27 23:58:56 +08:00
|
|
|
it('does not accept template with invalid token auth (undefined tokens)',
|
2014-02-06 19:24:14 +08:00
|
|
|
function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2014-02-06 19:24:14 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var tpl = { version:'0.0.1',
|
|
|
|
name: "invalid_auth1", placeholders: { },
|
2015-01-26 22:51:10 +08:00
|
|
|
auth: { method: 'token' }, layergroup: {layers:[wadusLayer]} };
|
2014-02-06 19:24:14 +08:00
|
|
|
tmap.addTemplate('me', tpl, function(err) {
|
|
|
|
if ( ! err ) {
|
|
|
|
done(new Error("Unexpected success with invalid token auth (undefined tokens)"));
|
|
|
|
}
|
|
|
|
else if ( ! err.message.match(/invalid 'token' authentication/i) ) {
|
2015-04-27 23:58:56 +08:00
|
|
|
done(new Error("Unexpected error message with invalid token auth (undefined tokens): " + err));
|
2014-02-06 19:24:14 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('add, get and delete a valid template', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var expected_failure = false;
|
|
|
|
var tpl_id;
|
|
|
|
var tpl = { version:'0.0.1',
|
2015-01-26 22:51:10 +08:00
|
|
|
name: 'first', auth: {}, layergroup: {layers:[wadusLayer]} };
|
2015-04-27 23:58:56 +08:00
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function() {
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function addOmonimousTemplate(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
tpl_id = id;
|
|
|
|
assert.equal(tpl_id, 'first');
|
|
|
|
expected_failure = true;
|
|
|
|
// should fail, as it already exists
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function getTemplate(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( ! expected_failure && err ) {
|
|
|
|
throw err;
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/already exists/i), err);
|
|
|
|
tmap.getTemplate('me', tpl_id, this);
|
|
|
|
},
|
|
|
|
function delTemplate(err, got_tpl) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2015-01-22 22:40:40 +08:00
|
|
|
assert.deepEqual(got_tpl, _.extend({}, tpl, {auth: {method: 'open'}, placeholders: {}}));
|
2013-12-18 00:35:12 +08:00
|
|
|
tmap.delTemplate('me', tpl_id, this);
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('add multiple templates, list them', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
2015-01-26 22:51:10 +08:00
|
|
|
var tpl1 = { version:'0.0.1', name: 'first', auth: {}, layergroup: {layers:[wadusLayer]} };
|
2013-12-18 00:35:12 +08:00
|
|
|
var tpl1_id;
|
2015-01-26 22:51:10 +08:00
|
|
|
var tpl2 = { version:'0.0.1', name: 'second', auth: {}, layergroup: {layers:[wadusLayer]} };
|
2013-12-18 00:35:12 +08:00
|
|
|
var tpl2_id;
|
2015-04-27 23:58:56 +08:00
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function addTemplate1() {
|
|
|
|
tmap.addTemplate('me', tpl1, this);
|
|
|
|
},
|
|
|
|
function addTemplate2(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
tpl1_id = id;
|
|
|
|
tmap.addTemplate('me', tpl2, this);
|
|
|
|
},
|
|
|
|
function listTemplates(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
tpl2_id = id;
|
|
|
|
tmap.listTemplates('me', this);
|
|
|
|
},
|
|
|
|
function checkTemplates(err, ids) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.equal(ids.length, 2);
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ok(ids.indexOf(tpl1_id) !== -1, ids.join(','));
|
|
|
|
assert.ok(ids.indexOf(tpl2_id) !== -1, ids.join(','));
|
2013-12-18 00:35:12 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function delTemplate1(err) {
|
|
|
|
if ( tpl1_id ) {
|
|
|
|
var next = this;
|
|
|
|
tmap.delTemplate('me', tpl1_id, function(e) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err || e ) {
|
|
|
|
next(new Error(err + '; ' + e));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
});
|
|
|
|
} else {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function delTemplate2(err) {
|
|
|
|
if ( tpl2_id ) {
|
|
|
|
var next = this;
|
|
|
|
tmap.delTemplate('me', tpl2_id, function(e) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err || e ) {
|
|
|
|
next(new Error(err + '; ' + e));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
});
|
|
|
|
} else {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('update templates', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
var expected_failure = false;
|
|
|
|
var owner = 'me';
|
|
|
|
var tpl = { version:'0.0.1',
|
|
|
|
name: 'first',
|
|
|
|
auth: { method: 'open' },
|
2015-01-26 22:51:10 +08:00
|
|
|
layergroup: {layers:[wadusLayer]}
|
2013-12-18 00:35:12 +08:00
|
|
|
};
|
|
|
|
var tpl_id;
|
2015-04-27 23:58:56 +08:00
|
|
|
step(
|
2013-12-18 00:35:12 +08:00
|
|
|
function addTemplate() {
|
|
|
|
tmap.addTemplate(owner, tpl, this);
|
|
|
|
},
|
|
|
|
// Updating template name should fail
|
|
|
|
function updateTemplateName(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
tpl_id = id;
|
|
|
|
expected_failure = true;
|
|
|
|
tpl.name = 'second';
|
|
|
|
tmap.updTemplate(owner, tpl_id, tpl, this);
|
|
|
|
},
|
|
|
|
function updateTemplateAuth(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err && ! expected_failure) {
|
|
|
|
throw err;
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
expected_failure = false;
|
|
|
|
assert.ok(err);
|
|
|
|
tpl.name = 'first';
|
|
|
|
tpl.auth.method = 'token';
|
2014-02-06 19:24:14 +08:00
|
|
|
tpl.auth.valid_tokens = [ 'tok1' ];
|
2013-12-18 00:35:12 +08:00
|
|
|
tmap.updTemplate(owner, tpl_id, tpl, this);
|
|
|
|
},
|
|
|
|
function updateTemplateWithInvalid(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2013-12-18 00:35:12 +08:00
|
|
|
tpl.version = '999.999.999';
|
|
|
|
expected_failure = true;
|
|
|
|
tmap.updTemplate(owner, tpl_id, tpl, this);
|
|
|
|
},
|
|
|
|
function updateUnexistentTemplate(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err && ! expected_failure) {
|
|
|
|
throw err;
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/unsupported.*version/i), err);
|
|
|
|
tpl.version = '0.0.1';
|
|
|
|
expected_failure = true;
|
|
|
|
tmap.updTemplate(owner, 'unexistent', tpl, this);
|
|
|
|
},
|
|
|
|
function delTemplate(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err && ! expected_failure) {
|
|
|
|
throw err;
|
|
|
|
}
|
2013-12-18 00:35:12 +08:00
|
|
|
expected_failure = false;
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/cannot update name/i), err);
|
|
|
|
tmap.delTemplate(owner, tpl_id, this);
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-04-27 23:58:56 +08:00
|
|
|
it('instanciate templates', function() {
|
2015-07-15 21:10:59 +08:00
|
|
|
// jshint maxcomplexity:7
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool);
|
2013-12-18 00:35:12 +08:00
|
|
|
assert.ok(tmap);
|
|
|
|
|
|
|
|
var tpl1 = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: 'acceptance1',
|
|
|
|
auth: { method: 'open' },
|
|
|
|
placeholders: {
|
|
|
|
fill: { type: "css_color", default: "red" },
|
|
|
|
color: { type: "css_color", default: "#a0fF9A" },
|
|
|
|
name: { type: "sql_literal", default: "test" },
|
|
|
|
zoom: { type: "number", default: "0" },
|
2015-04-27 23:58:56 +08:00
|
|
|
test_number: { type: "number", default: 23 }
|
2013-12-18 00:35:12 +08:00
|
|
|
},
|
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
global_cartocss_version: '2.0.2',
|
|
|
|
layers: [
|
|
|
|
{ options: {
|
2014-02-07 00:33:26 +08:00
|
|
|
sql: "select '<%=name %>' || id, g from t",
|
2014-02-07 00:45:48 +08:00
|
|
|
cartocss: '#layer { marker-fill:<%= fill %>; marker-width: <%=test_number %>; }'
|
2013-12-18 00:35:12 +08:00
|
|
|
} },
|
|
|
|
{ options: {
|
2014-02-07 00:33:26 +08:00
|
|
|
sql: "select fun('<%= name%>') g from x",
|
2013-12-18 00:35:12 +08:00
|
|
|
cartocss: '#layer { line-color:<%= color %>; marker-fill:<%= color %>; }'
|
|
|
|
} },
|
|
|
|
{ options: {
|
|
|
|
sql: "select g from x",
|
|
|
|
cartocss: '#layer[zoom=<%=zoom%>] { }'
|
|
|
|
} }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var inst = tmap.instance(tpl1, {});
|
|
|
|
|
|
|
|
var lyr = inst.layers[0].options;
|
|
|
|
assert.equal(lyr.sql, "select 'test' || id, g from t");
|
2014-02-07 00:45:48 +08:00
|
|
|
assert.equal(lyr.cartocss, '#layer { marker-fill:red; marker-width: 23; }');
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
lyr = inst.layers[1].options;
|
|
|
|
assert.equal(lyr.sql, "select fun('test') g from x");
|
|
|
|
assert.equal(lyr.cartocss, '#layer { line-color:#a0fF9A; marker-fill:#a0fF9A; }');
|
|
|
|
|
|
|
|
inst = tmap.instance(tpl1, {color:'yellow', name:"it's dangerous"});
|
|
|
|
|
|
|
|
lyr = inst.layers[0].options;
|
|
|
|
assert.equal(lyr.sql, "select 'it''s dangerous' || id, g from t");
|
2014-02-07 00:45:48 +08:00
|
|
|
assert.equal(lyr.cartocss, '#layer { marker-fill:red; marker-width: 23; }');
|
2013-12-18 00:35:12 +08:00
|
|
|
|
|
|
|
lyr = inst.layers[1].options;
|
|
|
|
assert.equal(lyr.sql, "select fun('it''s dangerous') g from x");
|
|
|
|
assert.equal(lyr.cartocss, '#layer { line-color:yellow; marker-fill:yellow; }');
|
|
|
|
|
|
|
|
// Invalid css_color
|
|
|
|
var err = null;
|
2015-04-27 23:58:56 +08:00
|
|
|
try { tmap.instance(tpl1, {color:'##ff00ff'}); }
|
2013-12-18 00:35:12 +08:00
|
|
|
catch (e) { err = e; }
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/invalid css_color/i), err);
|
|
|
|
|
|
|
|
// Invalid css_color 2 (too few digits)
|
2015-04-27 23:58:56 +08:00
|
|
|
err = null;
|
|
|
|
try { tmap.instance(tpl1, {color:'#ff'}); }
|
2013-12-18 00:35:12 +08:00
|
|
|
catch (e) { err = e; }
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/invalid css_color/i), err);
|
|
|
|
|
|
|
|
// Invalid css_color 3 (too many digits)
|
2015-04-27 23:58:56 +08:00
|
|
|
err = null;
|
|
|
|
try { tmap.instance(tpl1, {color:'#1234567'}); }
|
2013-12-18 00:35:12 +08:00
|
|
|
catch (e) { err = e; }
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/invalid css_color/i), err);
|
|
|
|
|
|
|
|
// Invalid number
|
2015-04-27 23:58:56 +08:00
|
|
|
err = null;
|
|
|
|
try { tmap.instance(tpl1, {zoom:'#'}); }
|
2013-12-18 00:35:12 +08:00
|
|
|
catch (e) { err = e; }
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/invalid number/i), err);
|
|
|
|
|
|
|
|
// Invalid number 2
|
2015-04-27 23:58:56 +08:00
|
|
|
err = null;
|
|
|
|
try { tmap.instance(tpl1, {zoom:'23e'}); }
|
2013-12-18 00:35:12 +08:00
|
|
|
catch (e) { err = e; }
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/invalid number/i), err);
|
|
|
|
|
|
|
|
// Valid number
|
2015-04-27 23:58:56 +08:00
|
|
|
err = null;
|
|
|
|
try { tmap.instance(tpl1, {zoom:'-.23e10'}); }
|
2013-12-18 00:35:12 +08:00
|
|
|
catch (e) { err = e; }
|
|
|
|
assert.ok(!err);
|
|
|
|
});
|
2014-02-13 21:55:31 +08:00
|
|
|
|
|
|
|
// Can set a limit on the number of user templates
|
2015-04-27 23:58:56 +08:00
|
|
|
it('can limit number of user templates', function(done) {
|
2015-01-22 22:40:40 +08:00
|
|
|
var tmap = new TemplateMaps(redis_pool, {
|
2014-02-13 21:55:31 +08:00
|
|
|
max_user_templates: 2
|
|
|
|
});
|
|
|
|
assert.ok(tmap);
|
2015-01-26 22:51:10 +08:00
|
|
|
var tpl = { version:'0.0.1', auth: {}, layergroup: {layers:[wadusLayer]} };
|
2014-02-13 21:55:31 +08:00
|
|
|
var expectErr = false;
|
|
|
|
var idMe = [];
|
|
|
|
var idYou = [];
|
2015-04-27 23:58:56 +08:00
|
|
|
step(
|
2014-02-13 21:55:31 +08:00
|
|
|
function oneForMe() {
|
|
|
|
tpl.name = 'oneForMe';
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function twoForMe(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
assert.ok(id);
|
|
|
|
idMe.push(id);
|
|
|
|
tpl.name = 'twoForMe';
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function threeForMe(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
assert.ok(id);
|
|
|
|
idMe.push(id);
|
|
|
|
tpl.name = 'threeForMe';
|
|
|
|
expectErr = true;
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
2015-04-27 23:58:56 +08:00
|
|
|
function errForMe(err/*, id*/) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err && ! expectErr ) {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-02-13 21:55:31 +08:00
|
|
|
expectErr = false;
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/limit.*template/), err);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function delOneMe(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
tmap.delTemplate('me', idMe.shift(), this);
|
|
|
|
},
|
|
|
|
function threeForMeRetry(err) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
tpl.name = 'threeForMe';
|
|
|
|
tmap.addTemplate('me', tpl, this);
|
|
|
|
},
|
|
|
|
function oneForYou(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
assert.ok(id);
|
|
|
|
idMe.push(id);
|
|
|
|
tpl.name = 'oneForYou';
|
|
|
|
tmap.addTemplate('you', tpl, this);
|
|
|
|
},
|
|
|
|
function twoForYou(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
assert.ok(id);
|
|
|
|
idYou.push(id);
|
|
|
|
tpl.name = 'twoForYou';
|
|
|
|
tmap.addTemplate('you', tpl, this);
|
|
|
|
},
|
|
|
|
function threeForYou(err, id) {
|
2015-07-15 21:03:28 +08:00
|
|
|
assert.ifError(err);
|
2014-02-13 21:55:31 +08:00
|
|
|
assert.ok(id);
|
|
|
|
idYou.push(id);
|
|
|
|
tpl.name = 'threeForYou';
|
|
|
|
expectErr = true;
|
|
|
|
tmap.addTemplate('you', tpl, this);
|
|
|
|
},
|
2015-04-27 23:58:56 +08:00
|
|
|
function errForYou(err/*, id*/) {
|
2015-07-15 21:03:28 +08:00
|
|
|
if ( err && ! expectErr ) {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-02-13 21:55:31 +08:00
|
|
|
expectErr = false;
|
|
|
|
assert.ok(err);
|
|
|
|
assert.ok(err.message.match(/limit.*template/), err);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2015-09-25 23:59:00 +08:00
|
|
|
keysToDelete['map_tpl|you'] = 0;
|
|
|
|
keysToDelete['map_tpl|me'] = 0;
|
2014-02-13 21:55:31 +08:00
|
|
|
// TODO: delete all templates
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2014-09-26 01:00:35 +08:00
|
|
|
|
2015-09-24 01:59:39 +08:00
|
|
|
describe('emit', function() {
|
|
|
|
var owner = 'me';
|
|
|
|
var templateName = 'emit';
|
|
|
|
var template = {
|
|
|
|
version:'0.0.1',
|
|
|
|
name: templateName,
|
|
|
|
auth: {
|
|
|
|
method: 'open'
|
|
|
|
},
|
|
|
|
placeholders: {},
|
|
|
|
layergroup: {
|
|
|
|
layers: [
|
|
|
|
wadusLayer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var templateUpdated = _.extend({}, template, { layergroup: { layers: [LAYER_PLAIN] } });
|
|
|
|
var templateMaps;
|
|
|
|
beforeEach(function() {
|
|
|
|
templateMaps = new TemplateMaps(redis_pool);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit on template update', function(done) {
|
|
|
|
templateMaps.on('update', function(_owner, _templateName, _template) {
|
|
|
|
assert.equal(_owner, owner);
|
|
|
|
assert.equal(_templateName, templateName);
|
|
|
|
assert.deepEqual(_template, templateUpdated);
|
|
|
|
|
|
|
|
templateMaps.delTemplate(owner, templateName, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
templateMaps.addTemplate(owner, template, function(err, templateName, _template) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
assert.deepEqual(_template, template);
|
|
|
|
templateMaps.updTemplate(owner, templateName, templateUpdated, function() {});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit on template deletion', function(done) {
|
|
|
|
templateMaps.on('delete', function(_owner, _templateName) {
|
|
|
|
assert.equal(_owner, owner);
|
|
|
|
assert.equal(_templateName, templateName);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
templateMaps.addTemplate(owner, template, function(err, templateName, _template) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
assert.deepEqual(_template, template);
|
|
|
|
templateMaps.delTemplate(owner, templateName, function() {});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT emit on template update when template is the same', function(done) {
|
|
|
|
templateMaps.on('update', function(_owner, _templateName, _template) {
|
|
|
|
assert.equal(_owner, owner);
|
|
|
|
assert.equal(_templateName, templateName);
|
|
|
|
assert.deepEqual(_template, templateUpdated);
|
|
|
|
|
|
|
|
templateMaps.delTemplate(owner, templateName, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
templateMaps.addTemplate(owner, template, function(err, templateName, _template) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.deepEqual(_template, template);
|
|
|
|
|
|
|
|
templateMaps.updTemplate(owner, templateName, template, function() {
|
|
|
|
templateMaps.updTemplate(owner, templateName, templateUpdated, function() {});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
});
|