Move config adaptation into a module and make it testable

pull/21/head
Clement Michaud 2017-03-21 20:57:03 +01:00
parent 1fc27083c9
commit 4b93338bae
8 changed files with 116 additions and 29 deletions

View File

@ -1,4 +1,7 @@
# The port to listen on
port: 8080
# Level of verbosity for logs
logs_level: info

View File

@ -22,21 +22,6 @@ console.log('Parse configuration file: %s', config_path);
var yaml_config = YAML.load(config_path);
var config = {
port: process.env.PORT || 8080,
ldap_url: yaml_config.ldap.url || 'ldap://127.0.0.1:389',
ldap_user_search_base: yaml_config.ldap.user_search_base,
ldap_user_search_filter: yaml_config.ldap.user_search_filter,
ldap_user: yaml_config.ldap.user,
ldap_password: yaml_config.ldap.password,
session_domain: yaml_config.session.domain,
session_secret: yaml_config.session.secret,
session_max_age: yaml_config.session.expiration || 3600000, // in ms
store_directory: yaml_config.store_directory,
logs_level: yaml_config.logs_level,
notifier: yaml_config.notifier,
}
var ldap_client = ldap.createClient({
url: config.ldap_url,
reconnect: true
@ -53,4 +38,4 @@ deps.nodemailer = nodemailer;
deps.ldap = ldap;
deps.session = session;
server.run(config, ldap_client, deps);
server.run(yaml_config, ldap_client, deps);

View File

@ -0,0 +1,20 @@
var objectPath = require('object-path');
module.exports = function(yaml_config) {
return {
port: objectPath.get(yaml_config, 'port', 8080),
ldap_url: objectPath.get(yaml_config, 'ldap.url', 'ldap://127.0.0.1:389'),
ldap_user_search_base: objectPath.get(yaml_config, 'ldap.user_search_base'),
ldap_user_search_filter: objectPath.get(yaml_config, 'ldap.user_search_filter'),
ldap_user: objectPath.get(yaml_config, 'ldap.user'),
ldap_password: objectPath.get(yaml_config, 'ldap.password'),
session_domain: objectPath.get(yaml_config, 'session.domain'),
session_secret: objectPath.get(yaml_config, 'session.secret'),
session_max_age: objectPath.get(yaml_config, 'session.expiration', 3600000), // in ms
store_directory: objectPath.get(yaml_config, 'store_directory'),
logs_level: objectPath.get(yaml_config, 'logs_level'),
notifier: objectPath.get(yaml_config, 'notifier'),
}
};

View File

@ -12,8 +12,11 @@ var UserDataStore = require('./user_data_store');
var Notifier = require('./notifier');
var AuthenticationRegulator = require('./authentication_regulator');
var setup_endpoints = require('./setup_endpoints');
var config_adapter = require('./config_adapter');
function run(yaml_config, ldap_client, deps, fn) {
var config = config_adapter(yaml_config);
function run(config, ldap_client, deps, fn) {
var view_directory = path.resolve(__dirname, '../views');
var public_html_directory = path.resolve(__dirname, '../public_html');
var datastore_options = {};

View File

@ -0,0 +1,67 @@
var assert = require('assert');
var config_adapter = require('../../src/lib/config_adapter');
describe('test config adapter', function() {
it('should read the port from the yaml file', function() {
yaml_config = {};
yaml_config.port = 7070;
var config = config_adapter(yaml_config);
assert.equal(config.port, 7070);
});
it('should default the port to 8080 if not provided', function() {
yaml_config = {};
var config = config_adapter(yaml_config);
assert.equal(config.port, 8080);
});
it('should get the ldap attributes', function() {
yaml_config = {};
yaml_config.ldap = {};
yaml_config.ldap.url = 'http://ldap';
yaml_config.ldap.user_search_base = 'ou=groups,dc=example,dc=com';
yaml_config.ldap.user_search_filter = 'uid';
yaml_config.ldap.user = 'admin';
yaml_config.ldap.password = 'pass';
var config = config_adapter(yaml_config);
assert.equal(config.ldap_url, 'http://ldap');
assert.equal(config.ldap_user_search_base, 'ou=groups,dc=example,dc=com');
assert.equal(config.ldap_user_search_filter, 'uid');
assert.equal(config.ldap_user, 'admin');
assert.equal(config.ldap_password, 'pass');
});
it('should get the session attributes', function() {
yaml_config = {};
yaml_config.session = {};
yaml_config.session.domain = 'example.com';
yaml_config.session.secret = 'secret';
yaml_config.session.expiration = 3600;
var config = config_adapter(yaml_config);
assert.equal(config.session_domain, 'example.com');
assert.equal(config.session_secret, 'secret');
assert.equal(config.session_max_age, 3600);
});
it('should get the log level', function() {
yaml_config = {};
yaml_config.logs_level = 'debug';
var config = config_adapter(yaml_config);
assert.equal(config.logs_level, 'debug');
});
it('should get the notifier config', function() {
yaml_config = {};
yaml_config.notifier = 'notifier';
var config = config_adapter(yaml_config);
assert.equal(config.notifier, 'notifier');
});
});

View File

@ -53,10 +53,14 @@ describe('test data persistence', function() {
config = {
port: PORT,
totp_secret: 'totp_secret',
ldap_url: 'ldap://127.0.0.1:389',
ldap_user_search_base: 'ou=users,dc=example,dc=com',
session_secret: 'session_secret',
session_max_age: 50000,
ldap: {
url: 'ldap://127.0.0.1:389',
user_search_base: 'ou=users,dc=example,dc=com',
},
session: {
secret: 'session_secret',
expiration: 50000,
},
store_directory: tmpDir.name,
notifier: { gmail: { user: 'user@example.com', pass: 'password' } }
};

View File

@ -32,13 +32,17 @@ describe('test the server', function() {
var config = {
port: PORT,
totp_secret: 'totp_secret',
ldap_url: 'ldap://127.0.0.1:389',
ldap_user_search_base: 'ou=users,dc=example,dc=com',
ldap_user_search_filter: 'cn',
ldap_user: 'cn=admin,dc=example,dc=com',
ldap_password: 'password',
session_secret: 'session_secret',
session_max_age: 50000,
ldap: {
url: 'ldap://127.0.0.1:389',
user_search_base: 'ou=users,dc=example,dc=com',
user_search_filter: 'cn',
user: 'cn=admin,dc=example,dc=com',
password: 'password',
},
session: {
secret: 'session_secret',
expiration: 50000,
},
store_in_memory: true,
notifier: {
gmail: {

View File

@ -34,7 +34,8 @@ describe('test server configuration', function() {
it('should set cookie scope to domain set in the config', function() {
config.session_domain = 'example.com';
config.session = {};
config.session.domain = 'example.com';
server.run(config, undefined, deps);
assert(deps.session.calledOnce);