From 4b93338baed7f0968009f206eeaba019c3304fb9 Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Tue, 21 Mar 2017 20:57:03 +0100 Subject: [PATCH] Move config adaptation into a module and make it testable --- config.template.yml | 3 ++ src/index.js | 17 +------ src/lib/config_adapter.js | 20 ++++++++ src/lib/server.js | 5 +- test/unitary/test_config_adapter.js | 67 +++++++++++++++++++++++++++ test/unitary/test_data_persistence.js | 12 +++-- test/unitary/test_server.js | 18 ++++--- test/unitary/test_server_config.js | 3 +- 8 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 src/lib/config_adapter.js create mode 100644 test/unitary/test_config_adapter.js diff --git a/config.template.yml b/config.template.yml index e1ab52449..1f6a20e59 100644 --- a/config.template.yml +++ b/config.template.yml @@ -1,4 +1,7 @@ +# The port to listen on +port: 8080 + # Level of verbosity for logs logs_level: info diff --git a/src/index.js b/src/index.js index 0719b0ab1..d50549a7b 100755 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/src/lib/config_adapter.js b/src/lib/config_adapter.js new file mode 100644 index 000000000..0fa486740 --- /dev/null +++ b/src/lib/config_adapter.js @@ -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'), + } +}; + diff --git a/src/lib/server.js b/src/lib/server.js index a828d6701..745ca93f8 100644 --- a/src/lib/server.js +++ b/src/lib/server.js @@ -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 = {}; diff --git a/test/unitary/test_config_adapter.js b/test/unitary/test_config_adapter.js new file mode 100644 index 000000000..70335df5b --- /dev/null +++ b/test/unitary/test_config_adapter.js @@ -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'); + }); +}); diff --git a/test/unitary/test_data_persistence.js b/test/unitary/test_data_persistence.js index a20b323ae..ebf5268a0 100644 --- a/test/unitary/test_data_persistence.js +++ b/test/unitary/test_data_persistence.js @@ -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' } } }; diff --git a/test/unitary/test_server.js b/test/unitary/test_server.js index f9252b848..dea277b4d 100644 --- a/test/unitary/test_server.js +++ b/test/unitary/test_server.js @@ -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: { diff --git a/test/unitary/test_server_config.js b/test/unitary/test_server_config.js index 4d6f9212b..6f90f396b 100644 --- a/test/unitary/test_server_config.js +++ b/test/unitary/test_server_config.js @@ -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);