Now turbo-cartocss is also parsed in template modification.

This commit is contained in:
Daniel García Aubert 2016-03-11 11:06:51 +01:00
parent d937d8970d
commit cc5443152b
2 changed files with 101 additions and 10 deletions

View File

@ -85,18 +85,39 @@ NamedMapsAdminController.prototype.update = function(req, res) {
var cdbuser = req.context.user;
var template;
var tpl_id;
step(
function checkPerms(){
self.authApi.authorizedByAPIKey(cdbuser, req, this);
},
function updateTemplate(err, authenticated) {
function parseTurboCartoCss(err, authenticated) {
assert.ifError(err);
ifUnauthenticated(authenticated, 'Only authenticated user can update templated maps');
ifInvalidContentType(req, 'template PUT data must be of type application/json');
ifUnauthenticated(authenticated, 'Only authenticated users can get template maps');
ifInvalidContentType(req, 'template POST data must be of type application/json');
var next = this;
template = req.body;
self.turboCartoCssAdapter.getLayers(cdbuser, template.layergroup.layers, function (err, layers) {
if (err) {
return next(err);
}
if (layers) {
template.layergroup.layers = layers;
}
return next();
});
},
function updateTemplate(err) {
assert.ifError(err);
var next = this;
tpl_id = templateName(req.params.template_id);
self.templateMaps.updTemplate(cdbuser, tpl_id, template, this);
self.templateMaps.updTemplate(cdbuser, tpl_id, template, next);
},
function prepareResponse(err){
assert.ifError(err);

View File

@ -18,6 +18,13 @@ describe('turbo-cartocss for named maps', function() {
testHelper.deleteRedisKeys(keysToDelete, done);
});
var turboCartocss = [
'#layer {' +
' marker-fill: ramp([price], colorbrewer(Reds));' +
' marker-allow-overlap:true;' +
'}'
].join('');
var expectedCartocss = [
'#layer {',
' marker-allow-overlap:true;',
@ -29,6 +36,24 @@ describe('turbo-cartocss for named maps', function() {
'}'
].join('');
var turboCartocssModified = [
'#layer {' +
' marker-fill: ramp([price], colorbrewer(Blues));' +
' marker-allow-overlap:true;' +
'}'
].join('');
var expectedUpdatedCartocss = [
'#layer {',
' marker-allow-overlap:true;',
' marker-fill:#eff3ff;',
' [ price > 10.25 ] { marker-fill:#bdd7e7}',
' [ price > 10.75 ] { marker-fill:#6baed6}',
' [ price > 11.5 ] { marker-fill:#3182bd}',
' [ price > 16.5 ] { marker-fill:#08519c}',
'}'
].join('');
var templateId = 'turbo-cartocss-template-1';
var template = {
@ -52,12 +77,7 @@ describe('turbo-cartocss for named maps', function() {
' SELECT 5, 21.00',
') _prices ON _prices.cartodb_id = test_table.cartodb_id'
].join('\n'),
cartocss: [
'#layer {' +
' marker-fill: ramp([price], colorbrewer(Reds));' +
' marker-allow-overlap:true;' +
'}'
].join(''),
cartocss: turboCartocss,
cartocss_version: '2.0.2'
}
}
@ -175,6 +195,56 @@ describe('turbo-cartocss for named maps', function() {
return null;
},
function updateTemplate() {
var next = this;
// clone the previous one and rename it
var changedTemplate = JSON.parse(JSON.stringify(template));
changedTemplate.layergroup.layers[0].options.cartocss = turboCartocssModified;
assert.response(server, {
url: '/api/v1/map/named/' + templateId + '/?api_key=1234',
method: 'PUT',
headers: { host: 'localhost', 'Content-Type': 'application/json' },
data: JSON.stringify(changedTemplate)
}, {},
function (res, err) {
next(err, res);
});
},
function checkUpdatedTemplate(err, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.deepEqual(JSON.parse(res.body), {
template_id: templateId
});
return null;
},
function getUpdatedTemplate() {
var next = this;
assert.response(server, {
url: '/api/v1/map/named/' + templateId + '?api_key=1234',
method: 'GET',
headers: { host: 'localhost' }
}, {},
function(res, err) {
next(err, res);
});
},
function checkGetUpdatedTemplate(err, res) {
assert.ifError(err);
var bodyParsed = JSON.parse(res.body);
assert.equal(res.statusCode, 200);
assert.equal(bodyParsed.template.layergroup.layers[0].options.cartocss, expectedUpdatedCartocss);
return null;
},
function deleteTemplate(err) {
assert.ifError(err);