From 40614c687d7aa303b45aa00cb73ac57bf173416f Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 10 Nov 2017 19:51:38 +0100 Subject: [PATCH] Even more tests for apply-config; --- bin/kresus.js | 6 +- tests/test-apply-config.js | 187 ++++++++++++++++++++++++++++++++++++- 2 files changed, 187 insertions(+), 6 deletions(-) diff --git a/bin/kresus.js b/bin/kresus.js index f1c5c33f5..23e281299 100755 --- a/bin/kresus.js +++ b/bin/kresus.js @@ -84,11 +84,7 @@ if (numActualArgs >= 1) { var root = path.join(path.dirname(fs.realpathSync(__filename)), '..', 'build'); var standalone = true; -try { - require(path.join(root, 'server', 'apply-config.js'))(standalone, config); -} catch (e) { - console.error(e); -} +require(path.join(root, 'server', 'apply-config.js'))(standalone, config); // Then only, import the server. var server = require(path.join(root, 'server')); diff --git a/tests/test-apply-config.js b/tests/test-apply-config.js index 32cd7371e..3e33a7d2a 100644 --- a/tests/test-apply-config.js +++ b/tests/test-apply-config.js @@ -61,6 +61,54 @@ describe('Test default configuration', () => { process.kresus.dataDir.should.equal(path.join(ospath.home(), '.kresus')); process.kresus.urlPrefix.should.equal('/'); }); + + it('a partially incomplete configuration should get the default keys', () => { + process.kresus = {}; + + // Empty configuration object. + let config = { }; + prepareProcessKresus(true, config); + checkHasConfigKeys(process.kresus); + checkCommonDefaultConfig(process.kresus); + + process.kresus = {}; + + // Empty sub-config object. + config = { + email: {} + }; + + prepareProcessKresus(true, config); + checkHasConfigKeys(process.kresus); + checkCommonDefaultConfig(process.kresus); + + process.kresus = {}; + + // Only one key is defined. + config = { + kresus: { + port: 4242 + } + }; + + prepareProcessKresus(true, config); + checkHasConfigKeys(process.kresus); + + process.kresus.port.should.equal(4242); + process.kresus.host.should.equal('127.0.0.1'); + process.kresus.pythonExec.should.equal('python2'); + should.not.exist(process.kresus.weboobDir); + should.not.exist(process.kresus.weboobSourcesList); + should.not.exist(process.kresus.emailTransport); + should.not.exist(process.kresus.emailSendmailBin); + should.not.exist(process.kresus.emailFrom); + should.not.exist(process.kresus.smtpHost); + should.not.exist(process.kresus.smtpPort); + should.not.exist(process.kresus.smtpUser); + should.not.exist(process.kresus.smtpPassword); + process.kresus.smtpForceTLS.should.equal(false); + process.kresus.smtpRejectUnauthorizedTLS.should.equal(true); + }); }); describe('Test config.example.ini matches default configuration', () => { @@ -84,7 +132,7 @@ describe('Test config.example.ini matches default configuration', () => { describe('Test overloading configuration', () => { it('shall return correct overloaded config in standalone mode', () => { process.kresus = {}; - let config = { + let config = { kresus: { datadir: 'dataDir', host: '0.0.0.0', @@ -131,6 +179,129 @@ describe('Test overloading configuration', () => { process.kresus.dataDir.should.equal('dataDir'); process.kresus.urlPrefix.should.equal('/foobar'); }); + + it('shall let environment variables define config keys', () => { + process.kresus = {}; + + let previousEnv = process.env; + process.env = { + PORT: '8080', + HOST: '0.0.0.0', + KRESUS_DIR: 'dataDir', + KRESUS_PYTHON_EXEC: 'pythonExec', + KRESUS_URL_PREFIX: 'foobar', + KRESUS_WEBOOB_DIR: 'weboobDir', + KRESUS_WEBOOB_SOURCES_LIST: 'weboobSourcesList', + KRESUS_EMAIL_TRANSPORT: 'smtp', + KRESUS_EMAIL_SENDMAIL_BIN: 'sendmailBin', + KRESUS_EMAIL_FROM: 'emailFrom', + KRESUS_EMAIL_HOST: 'smtpHost', + KRESUS_EMAIL_PORT: '4242', + KRESUS_EMAIL_USER: 'smtpUser', + KRESUS_EMAIL_PASSWORD: 'smtpPassword', + KRESUS_EMAIL_FORCE_TLS: 'true', + KRESUS_EMAIL_REJECT_UNAUTHORIZED_TLS: 'false', + }; + + prepareProcessKresus(true); + + checkHasConfigKeys(process.kresus); + + process.kresus.port.should.equal(8080); + process.kresus.host.should.equal('0.0.0.0'); + process.kresus.pythonExec.should.equal('pythonExec'); + process.kresus.weboobDir.should.equal('weboobDir'); + process.kresus.weboobSourcesList.should.equal('weboobSourcesList'); + process.kresus.emailTransport.should.equal('smtp'); + process.kresus.emailSendmailBin.should.equal('sendmailBin'); + process.kresus.emailFrom.should.equal('emailFrom'); + process.kresus.smtpHost.should.equal('smtpHost'); + process.kresus.smtpPort.should.equal(4242); + process.kresus.smtpUser.should.equal('smtpUser'); + process.kresus.smtpPassword.should.equal('smtpPassword'); + process.kresus.smtpForceTLS.should.equal(true); + process.kresus.smtpRejectUnauthorizedTLS.should.equal(false); + + process.kresus.standalone.should.equal(true); + process.kresus.dataDir.should.equal('dataDir'); + process.kresus.urlPrefix.should.equal('/foobar'); + + process.env = previousEnv; + }); + + it('shall let environment variables override configuration', () => { + process.kresus = {}; + + let previousEnv = process.env; + process.env = { + PORT: '8080', + HOST: '0.0.0.0', + KRESUS_DIR: 'dataDir', + KRESUS_PYTHON_EXEC: 'pythonExec', + KRESUS_URL_PREFIX: 'foobar', + KRESUS_WEBOOB_DIR: 'weboobDir', + KRESUS_WEBOOB_SOURCES_LIST: 'weboobSourcesList', + KRESUS_EMAIL_TRANSPORT: 'smtp', + KRESUS_EMAIL_SENDMAIL_BIN: 'sendmailBin', + KRESUS_EMAIL_FROM: 'emailFrom', + KRESUS_EMAIL_HOST: 'smtpHost', + KRESUS_EMAIL_PORT: '4242', + KRESUS_EMAIL_USER: 'smtpUser', + KRESUS_EMAIL_PASSWORD: 'smtpPassword', + KRESUS_EMAIL_FORCE_TLS: 'true', + KRESUS_EMAIL_REJECT_UNAUTHORIZED_TLS: 'false', + }; + + let config = { + kresus: { + datadir: 'dites', + host: 'non', + port: 'à', + url_prefix: 'la', + python_exec: 'drogue' + }, + weboob: { + srcdir: 'salut', + sources_list: "c'est cool" + }, + email: { + transport: 'il', + sendmail_bin: 'était', + from: 'une', + host: 'fois', + port: 'un', + user: 'bonhomme', + password: 'de', + force_tls: 'foi', + reject_unauthorized_tls: '.' + } + }; + + prepareProcessKresus(true, config); + + checkHasConfigKeys(process.kresus); + + process.kresus.port.should.equal(8080); + process.kresus.host.should.equal('0.0.0.0'); + process.kresus.pythonExec.should.equal('pythonExec'); + process.kresus.weboobDir.should.equal('weboobDir'); + process.kresus.weboobSourcesList.should.equal('weboobSourcesList'); + process.kresus.emailTransport.should.equal('smtp'); + process.kresus.emailSendmailBin.should.equal('sendmailBin'); + process.kresus.emailFrom.should.equal('emailFrom'); + process.kresus.smtpHost.should.equal('smtpHost'); + process.kresus.smtpPort.should.equal(4242); + process.kresus.smtpUser.should.equal('smtpUser'); + process.kresus.smtpPassword.should.equal('smtpPassword'); + process.kresus.smtpForceTLS.should.equal(true); + process.kresus.smtpRejectUnauthorizedTLS.should.equal(false); + + process.kresus.standalone.should.equal(true); + process.kresus.dataDir.should.equal('dataDir'); + process.kresus.urlPrefix.should.equal('/foobar'); + + process.env = previousEnv; + }); }); describe('Test invalid configurations', () => { @@ -140,11 +311,25 @@ describe('Test invalid configurations', () => { prepareProcessKresus(true, { kresus: { port: -1 } }); }).should.throw(); + (function negativePortEnv(){ + process.kresus = {}; + process.env.PORT = '-1'; + prepareProcessKresus(true); + }).should.throw(); + delete process.env.PORT; + (function zeroPort(){ process.kresus = {}; prepareProcessKresus(true, { kresus: { port: 0 } }); }).should.throw(); + (function zeroPortEnv(){ + process.kresus = {}; + process.env.PORT = '0'; + prepareProcessKresus(true); + }).should.throw(); + delete process.env.PORT; + (function overflowPort(){ process.kresus = {}; prepareProcessKresus(true, { kresus: { port: 65536 } }); -- GitLab