tests: Clear hooks before running webaccess tests

Also factor out common test setup code.
pull/4451/head
Richard Hansen 2020-10-29 15:28:27 -04:00
parent 6961e0e05b
commit dbe9151d89
1 changed files with 29 additions and 47 deletions

View File

@ -1,3 +1,5 @@
/* global __dirname, __filename, afterEach, before, beforeEach, describe, it, require */
function m(mod) { return __dirname + '/../../../src/' + mod; } function m(mod) { return __dirname + '/../../../src/' + mod; }
const assert = require('assert').strict; const assert = require('assert').strict;
@ -7,23 +9,33 @@ const settings = require(m('node/utils/Settings'));
describe(__filename, function() { describe(__filename, function() {
let agent; let agent;
const backups = {};
const authHookNames = ['preAuthorize', 'authenticate', 'authorize'];
const failHookNames = ['preAuthzFailure', 'authnFailure', 'authzFailure', 'authFailure'];
before(async function() { agent = await common.init(); }); before(async function() { agent = await common.init(); });
beforeEach(async function() {
describe('webaccess: without plugins', function() { backups.hooks = {};
const backup = {}; for (const hookName of authHookNames.concat(failHookNames)) {
backups.hooks[hookName] = plugins.hooks[hookName];
before(async function() { plugins.hooks[hookName] = [];
Object.assign(backup, settings); }
backups.settings = {};
for (const setting of ['requireAuthentication', 'requireAuthorization', 'users']) {
backups.settings[setting] = settings[setting];
}
settings.requireAuthentication = false;
settings.requireAuthorization = false;
settings.users = { settings.users = {
admin: {password: 'admin-password', is_admin: true}, admin: {password: 'admin-password', is_admin: true},
user: {password: 'user-password'}, user: {password: 'user-password'},
}; };
}); });
afterEach(async function() {
after(async function() { Object.assign(plugins.hooks, backups.hooks);
Object.assign(settings, backup); Object.assign(settings, backups.settings);
}); });
describe('webaccess: without plugins', function() {
it('!authn !authz anonymous / -> 200', async function() { it('!authn !authz anonymous / -> 200', async function() {
settings.requireAuthentication = false; settings.requireAuthentication = false;
settings.requireAuthorization = false; settings.requireAuthorization = false;
@ -105,30 +117,16 @@ describe(__filename, function() {
} }
}; };
const handlers = {}; const handlers = {};
const hookNames = ['preAuthorize', 'authenticate', 'authorize'];
const hooksBackup = {};
const settingsBackup = {};
beforeEach(async function() { beforeEach(async function() {
callOrder = []; callOrder = [];
hookNames.forEach((hookName) => { for (const hookName of authHookNames) {
// Create two handlers for each hook to test deferral to the next function. // Create two handlers for each hook to test deferral to the next function.
const h0 = new Handler(hookName, '_0'); const h0 = new Handler(hookName, '_0');
const h1 = new Handler(hookName, '_1'); const h1 = new Handler(hookName, '_1');
handlers[hookName] = [h0, h1]; handlers[hookName] = [h0, h1];
hooksBackup[hookName] = plugins.hooks[hookName] || [];
plugins.hooks[hookName] = [{hook_fn: h0.handle.bind(h0)}, {hook_fn: h1.handle.bind(h1)}]; plugins.hooks[hookName] = [{hook_fn: h0.handle.bind(h0)}, {hook_fn: h1.handle.bind(h1)}];
}); }
hooksBackup.preAuthzFailure = plugins.hooks.preAuthzFailure || [];
Object.assign(settingsBackup, settings);
settings.users = {
admin: {password: 'admin-password', is_admin: true},
user: {password: 'user-password'},
};
});
afterEach(async function() {
Object.assign(plugins.hooks, hooksBackup);
Object.assign(settings, settingsBackup);
}); });
describe('preAuthorize', function() { describe('preAuthorize', function() {
@ -357,31 +355,15 @@ describe(__filename, function() {
} }
}; };
const handlers = {}; const handlers = {};
const hookNames = ['authnFailure', 'authzFailure', 'authFailure'];
const settingsBackup = {};
const hooksBackup = {};
beforeEach(function() { beforeEach(function() {
Object.assign(settingsBackup, settings); failHookNames.forEach((hookName) => {
hookNames.forEach((hookName) => {
if (plugins.hooks[hookName] == null) plugins.hooks[hookName] = [];
});
Object.assign(hooksBackup, plugins.hooks);
hookNames.forEach((hookName) => {
const handler = new Handler(hookName); const handler = new Handler(hookName);
handlers[hookName] = handler; handlers[hookName] = handler;
plugins.hooks[hookName] = [{hook_fn: handler.handle.bind(handler)}]; plugins.hooks[hookName] = [{hook_fn: handler.handle.bind(handler)}];
}); });
settings.requireAuthentication = true; settings.requireAuthentication = true;
settings.requireAuthorization = true; settings.requireAuthorization = true;
settings.users = {
admin: {password: 'admin-password', is_admin: true},
user: {password: 'user-password'},
};
});
afterEach(function() {
Object.assign(settings, settingsBackup);
Object.assign(plugins.hooks, hooksBackup);
}); });
// authn failure tests // authn failure tests