2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 17:29:07 +08:00
|
|
|
require('../support/test-helper');
|
2015-04-28 00:00:47 +08:00
|
|
|
|
2015-01-23 00:55: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');
|
2015-01-23 00:55:47 +08:00
|
|
|
|
2015-04-28 00:00:47 +08:00
|
|
|
describe('template_maps_auth', function() {
|
2015-01-23 00:55:47 +08:00
|
|
|
|
|
|
|
// 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) {
|
2015-04-28 00:00:47 +08:00
|
|
|
it(testScenario.desc, function(done) {
|
2015-01-23 00:55:47 +08:00
|
|
|
var debugMessage = testScenario.expected ? 'should be authorized' : 'unexpectedly authorized';
|
|
|
|
var result = templateMaps.isAuthorized(testScenario.template, testScenario.token);
|
|
|
|
assert.equal(result, testScenario.expected, debugMessage);
|
|
|
|
done();
|
2015-04-28 00:00:47 +08:00
|
|
|
});
|
2015-01-23 00:55:47 +08:00
|
|
|
});
|
|
|
|
|
2015-04-28 00:00:47 +08:00
|
|
|
it("auth as 'open' string is authorized", function(done) {
|
2015-01-29 00:29:50 +08:00
|
|
|
var template = {
|
|
|
|
name: 'wadus_template',
|
|
|
|
auth: 'open'
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.ok(templateMaps.isAuthorized(template));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2015-01-23 00:55:47 +08:00
|
|
|
});
|