From 981be0edd545f7eb0ef2234ed762283bed223b15 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 22 Jan 2015 17:55:47 +0100 Subject: [PATCH] Replace signed maps auth tests with template maps tests --- test/unit/cartodb/signed_maps.test.js | 109 ------------------- test/unit/cartodb/template_maps_auth.test.js | 95 ++++++++++++++++ 2 files changed, 95 insertions(+), 109 deletions(-) delete mode 100644 test/unit/cartodb/signed_maps.test.js create mode 100644 test/unit/cartodb/template_maps_auth.test.js diff --git a/test/unit/cartodb/signed_maps.test.js b/test/unit/cartodb/signed_maps.test.js deleted file mode 100644 index 8d1ed604..00000000 --- a/test/unit/cartodb/signed_maps.test.js +++ /dev/null @@ -1,109 +0,0 @@ -var assert = require('assert') - //, _ = require('underscore') - , RedisPool = require('redis-mpool') - , SignedMaps = require('../../../lib/cartodb/signed_maps.js') - , test_helper = require('../../support/test_helper') - , Step = require('step') - , tests = module.exports = {}; - -suite('signed_maps', function() { - - // configure redis pool instance to use in tests - var redis_pool = RedisPool(global.environment.redis); - - test('can sign map with open and token-based auth', function(done) { - var smap = new SignedMaps(redis_pool); - assert.ok(smap); - var sig = 'sig1'; - var map = 'map1'; - var tok = 'tok1'; - var crt = { - version:'0.0.1', - layergroup_id:map, - auth: {} - }; - var crt1_id; // by token - var crt2_id; // open - Step( - function() { - smap.isAuthorized(sig,map,tok,this); - }, - function checkAuthFailure1(err, authorized) { - if ( err ) throw err; - assert.ok(!authorized, "unexpectedly authorized"); - crt.auth.method = 'token'; - crt.auth.valid_tokens = [tok]; - smap.addSignature(sig, map, crt, this) - }, - function getCert1(err, id) { - if ( err ) throw err; - assert.ok(id, "undefined signature id"); - crt1_id = id; // keep note of it -//console.log("Certificate 1 is " + crt1_id); - smap.isAuthorized(sig,map,'',this); - }, - function checkAuthFailure2(err, authorized) { - if ( err ) throw err; - assert.ok(!authorized, "unexpectedly authorized"); - smap.isAuthorized(sig,map,tok,this); - }, - function checkAuthSuccess1(err, authorized) { - if ( err ) throw err; - assert.ok(authorized, "unauthorized :("); - crt.auth.method = 'open'; - delete crt.auth.valid_tokens; - smap.addSignature(sig, map, crt, this) - }, - function getCert2(err, id) { - if ( err ) throw err; - assert.ok(id, "undefined signature id"); - crt2_id = id; // keep note of it -//console.log("Certificate 2 is " + crt2_id); - smap.isAuthorized(sig,map,'arbitrary',this); - }, - function checkAuthSuccess2_delCert2(err, authorized) { - if ( err ) throw err; - assert.ok(authorized, "unauthorized :("); - var next = this; - smap.delCertificate(sig, crt2_id, function(e) { - if (e) next(e); - else smap.isAuthorized(sig,map,'arbitrary',next); - }); - }, - function checkAuthFailure3_delCert2(err, authorized) { - if ( err ) throw err; - assert.ok(!authorized, "unexpectedly authorized"); - smap.delCertificate(sig, crt1_id, this); - }, - function finish(err) { - done(err); - } - ); - }); - - test('can validate certificates', function(done) { - var smap = new SignedMaps(redis_pool); - assert.ok(smap); - Step( - function invalidVersion() { - var cert = { version: '-1' }; - var err = smap.checkInvalidCertificate(cert); - assert.ok(err); - assert.equal(err.message, "Unsupported certificate version -1"); - return null; - }, - function invalidTokenAuth() { - var cert = { version: '0.0.1', auth: { method:'token', valid_token:[] } }; - var err = smap.checkInvalidCertificate(cert); - assert.ok(err); - assert.equal(err.message, "Invalid 'token' authentication: missing valid_tokens"); - return null; - }, - function finish(err) { - done(err); - } - ); - }); - - -}); diff --git a/test/unit/cartodb/template_maps_auth.test.js b/test/unit/cartodb/template_maps_auth.test.js new file mode 100644 index 00000000..c01e841e --- /dev/null +++ b/test/unit/cartodb/template_maps_auth.test.js @@ -0,0 +1,95 @@ +var assert = require('assert'); +var RedisPool = require('redis-mpool'); +var TemplateMaps = require('../../../lib/cartodb/template_maps'); +var test_helper = require('../../support/test_helper'); +var Step = require('step'); +var tests = module.exports = {}; + +suite('template_maps_auth', function() { + + // configure redis pool instance to use in tests + var redisPool = new RedisPool(global.environment.redis), + templateMaps = new TemplateMaps(redisPool, {max_user_templates: 1000}); + + function makeTemplate(method, validTokens) { + var template = { + name: 'wadus_template', + auth: { + method: method + } + }; + + if (method === 'token') { + template.auth.valid_tokens = validTokens || []; + } + + return template; + } + + var methodToken = 'token', + methodOpen = 'open'; + + var tokenFoo = 'foo', + tokenBar = 'bar'; + + var authorizationTestScenarios = [ + { + desc: 'open method is always authorized', + template: makeTemplate(methodOpen), + token: undefined, + expected: true + }, + { + desc: 'token method is authorized for valid token', + template: makeTemplate(methodToken, [tokenFoo]), + token: tokenFoo, + expected: true + }, + { + desc: 'token method not authorized for invalid token', + template: makeTemplate(methodToken, [tokenFoo]), + token: tokenBar, + expected: false + }, + { + desc: 'token method is authorized for valid token array', + template: makeTemplate(methodToken, [tokenFoo]), + token: [tokenFoo], + expected: true + }, + { + desc: 'token method not authorized for invalid token array', + template: makeTemplate(methodToken, [tokenFoo]), + token: [tokenBar], + expected: false + }, + { + desc: 'wadus method not authorized', + template: makeTemplate('wadus', [tokenFoo]), + token: tokenFoo, + expected: false + }, + { + desc: 'undefined template result in not authorized', + template: undefined, + token: tokenFoo, + expected: false + }, + { + desc: 'undefined template auth result in not authorized', + template: {}, + token: tokenFoo, + expected: false + } + ]; + + authorizationTestScenarios.forEach(function(testScenario) { + test(testScenario.desc, function(done) { + var debugMessage = testScenario.expected ? 'should be authorized' : 'unexpectedly authorized'; + var result = templateMaps.isAuthorized(testScenario.template, testScenario.token); + assert.equal(result, testScenario.expected, debugMessage); + done(); + }) + }); + +});