From 3f0d34431397a4097d42514812dd901fe52a1ebe Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 4 Jun 2015 10:41:40 -0400 Subject: [PATCH] Changes rules for names in templates Now valid names can start with numbers and can contain dashes (-). Closes #306 --- docs/Map-API.md | 2 +- lib/cartodb/template_maps.js | 2 +- test/unit/cartodb/template_maps.test.js | 66 ++++++++++++++++--------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/docs/Map-API.md b/docs/Map-API.md index 58410c72..134c775b 100644 --- a/docs/Map-API.md +++ b/docs/Map-API.md @@ -404,7 +404,7 @@ POST /api/v1/map/named ##### Arguments -- **name**: There can be at most _one_ template with the same name for any user. Valid names start with a letter, and only contain letters, numbers, or underscores (_). +- **name**: There can be at most _one_ template with the same name for any user. Valid names start with a letter or a number, and only contain letters, numbers, dashes (-) or underscores (_). - **auth**: - **method** `"token"` or `"open"` (the default if no `"method"` is given). - **valid_tokens** when `"method"` is set to `"token"`, the values listed here allow you to instantiate the named map. diff --git a/lib/cartodb/template_maps.js b/lib/cartodb/template_maps.js index 12c87a06..c809b63f 100644 --- a/lib/cartodb/template_maps.js +++ b/lib/cartodb/template_maps.js @@ -88,7 +88,7 @@ o._redisCmd = function(redisFunc, redisArgs, callback) { ); }; -var _reValidIdentifier = /^[a-zA-Z][0-9a-zA-Z_]*$/; +var _reValidIdentifier = /^[a-z0-9][0-9a-z_\-]*$/i; // jshint maxcomplexity:15 o._checkInvalidTemplate = function(template) { if ( template.version != '0.0.1' ) { diff --git a/test/unit/cartodb/template_maps.test.js b/test/unit/cartodb/template_maps.test.js index e7201e41..da378767 100644 --- a/test/unit/cartodb/template_maps.test.js +++ b/test/unit/cartodb/template_maps.test.js @@ -59,30 +59,50 @@ describe('template_maps', function() { ); }); - it('does not accept template with invalid name', function(done) { - var tmap = new TemplateMaps(redis_pool); - assert.ok(tmap); - var tpl = { version:'0.0.1', - auth: {}, layergroup: {layers:[wadusLayer]} }; - var invalidnames = [ "ab|", "a b", "a@b", "1ab", "_x", "", " x", "x " ]; - var testNext = function() { - if ( ! invalidnames.length ) { done(); return; } - var n = invalidnames.pop(); - tpl.name = n; - tmap.addTemplate('me', tpl, function(err) { - if ( ! err ) { - done(new Error("Unexpected success with invalid name '" + n + "'")); + describe('naming', function() { + + function createTemplate(name) { + return { + version:'0.0.1', + name: name, + auth: {}, + layergroup: { + layers: [ + wadusLayer + ] + } + }; } - else if ( ! err.message.match(/template.*name/i) ) { - done(new Error("Unexpected error message with invalid name '" + n + "': " + err)); - } - else { - testNext(); - } - }); - }; - testNext(); - }); + 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); + done(); + }); + }); + }); + + + }); it('does not accept template with invalid placeholder name', function(done) { var tmap = new TemplateMaps(redis_pool);