Windshaft-cartodb/test/unit/template-maps-auth-test.js

106 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict';
2019-10-07 17:29:07 +08:00
require('../support/test-helper');
2015-04-28 00:00:47 +08:00
var assert = require('assert');
var RedisPool = require('redis-mpool');
2015-04-28 00:00:47 +08:00
2019-10-07 17:29:07 +08:00
var TemplateMaps = require('../../lib/backends/template-maps');
2019-10-22 01:07:24 +08:00
describe('template_maps_auth', function () {
// configure redis pool instance to use in tests
2019-10-22 01:07:24 +08:00
var redisPool = new RedisPool(global.environment.redis);
var templateMaps = new TemplateMaps(redisPool, { max_user_templates: 1000 });
2019-10-22 01:07:24 +08:00
function makeTemplate (method, validTokens) {
var template = {
name: 'wadus_template',
auth: {
method: method
}
};
if (method === 'token') {
template.auth.valid_tokens = validTokens || [];
}
return template;
}
2019-10-22 01:07:24 +08:00
var methodToken = 'token';
var methodOpen = 'open';
2019-10-22 01:07:24 +08:00
var tokenFoo = 'foo';
var 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
}
];
2019-10-22 01:07:24 +08:00
authorizationTestScenarios.forEach(function (testScenario) {
it(testScenario.desc, function (done) {
var debugMessage = testScenario.expected ? 'should be authorized' : 'unexpectedly authorized';
var result = templateMaps.isAuthorized(testScenario.template, testScenario.token);
assert.strictEqual(result, testScenario.expected, debugMessage);
done();
2015-04-28 00:00:47 +08:00
});
});
2019-10-22 01:07:24 +08:00
it("auth as 'open' string is authorized", function (done) {
var template = {
name: 'wadus_template',
auth: 'open'
};
assert.ok(templateMaps.isAuthorized(template));
done();
});
});