From 7bb2f2bcd76548663374e7e1b4aa3a1e480e925c Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 15 Feb 2012 14:39:26 -0800 Subject: [PATCH 01/10] The start of our unit tests. This sets up a submodule for nodeunit and stubs out some of the objects we're going to need in plugins. Finally, I implement unit tests for one of the smallest plugins we have, relay_all. --- .gitignore | 1 + .gitmodules | 3 ++ node_modules/nodeunit | 1 + run_tests | 21 +++++++++ tests/fixtures/stub.js | 13 ++++++ tests/fixtures/stub_plugin.js | 21 +++++++++ tests/plugins/relay_all_test.js | 81 +++++++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+) create mode 100644 .gitmodules create mode 160000 node_modules/nodeunit create mode 100755 run_tests create mode 100644 tests/fixtures/stub.js create mode 100644 tests/fixtures/stub_plugin.js create mode 100644 tests/plugins/relay_all_test.js diff --git a/.gitignore b/.gitignore index 3c3629e64..fb0da4cd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +!node_modules/nodeunit diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..699e144ea --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "node_modules/nodeunit"] + path = node_modules/nodeunit + url = git://github.com/godsflaw/nodeunit.git diff --git a/node_modules/nodeunit b/node_modules/nodeunit new file mode 160000 index 000000000..bdb457a6e --- /dev/null +++ b/node_modules/nodeunit @@ -0,0 +1 @@ +Subproject commit bdb457a6ed100b73329ca38047d41b6ee38fea29 diff --git a/run_tests b/run_tests new file mode 100755 index 000000000..4b0fff67c --- /dev/null +++ b/run_tests @@ -0,0 +1,21 @@ +#!/usr/bin/env node + +require.paths.unshift(__dirname); + +try { + var reporter = require('nodeunit').reporters.default; +} +catch(e) { + console.log("Error: " + e.message); + console.log(""); + console.log("Cannot find nodeunit module."); + console.log("You can download submodules for this project by doing:"); + console.log(""); + console.log(" git submodule init"); + console.log(" git submodule update"); + console.log(""); + process.exit(); +} + +process.chdir(__dirname); +reporter.run(['tests/plugins']); diff --git a/tests/fixtures/stub.js b/tests/fixtures/stub.js new file mode 100644 index 000000000..628d51624 --- /dev/null +++ b/tests/fixtures/stub.js @@ -0,0 +1,13 @@ +module.exports = function (returnValue) { + function stub() { + stub.called = true; + stub.args = arguments; + stub.thisArg = this; + return returnValue; + } + + stub.called = false; + stub.relaying = false; + + return stub; +}; diff --git a/tests/fixtures/stub_plugin.js b/tests/fixtures/stub_plugin.js new file mode 100644 index 000000000..7ef3de5fd --- /dev/null +++ b/tests/fixtures/stub_plugin.js @@ -0,0 +1,21 @@ +"use strict"; + +var stub = require('tests/fixtures/stub'); + +var plugin = exports; + +function Plugin(name) { +} + +plugin.createPlugin = function(name) { + var obj = new Plugin(name); + var plug = require(name); + + for (var k in plug) { + if (plug.hasOwnProperty(k)) { + obj[k] = plug[k]; + } + } + + return obj; +} diff --git a/tests/plugins/relay_all_test.js b/tests/plugins/relay_all_test.js new file mode 100644 index 000000000..fa8891236 --- /dev/null +++ b/tests/plugins/relay_all_test.js @@ -0,0 +1,81 @@ +var stub = require('tests/fixtures/stub'), + constants = require('../../constants'); + Plugin = require('tests/fixtures/stub_plugin'); + +// huge hack here, but plugin tests need constants +constants.import(global); + +function _set_up(callback) { + this.backup = {}; + + // needed for tests + this.plugin = Plugin.createPlugin('plugins/relay_all'); + this.connection = stub(); + this.params = ['foo@bar.com']; + + // backup modifications + this.backup.plugin = {}; + this.backup.plugin.register_hook = this.plugin.register_hook; + + // stub out functions + this.plugin.register_hook = stub(); + this.connection.loginfo = stub(); + + // going to need this in multiple tests + this.plugin.register(); + + callback(); +} + +function _tear_down(callback) { + // restore backed up functions + this.plugin.register_hook = this.backup.plugin.register_hook; + + callback(); +} + +exports.relay_all = { + setUp : _set_up, + tearDown : _tear_down, + 'should have register function' : function (test) { + test.expect(2); + test.isNotNull(this.plugin); + test.isFunction(this.plugin.register); + test.done(); + }, + 'register function should call register_hook()' : function (test) { + test.expect(1); + test.ok(this.plugin.register_hook.called); + test.done(); + }, + 'register_hook() should register for propper hook' : function (test) { + test.expect(1); + test.equals(this.plugin.register_hook.args[0], 'rcpt'); + test.done(); + }, + 'register_hook() should register available function' : function (test) { + test.expect(3); + test.equals(this.plugin.register_hook.args[1], 'confirm_all'); + test.isNotNull(this.plugin.confirm_all); + test.isFunction(this.plugin.confirm_all); + test.done(); + }, + 'confirm_all hook always returns OK' : function (test) { + var next = function (action) { + test.expect(1); + test.equals(action, constants.ok); + test.done(); + }; + + this.plugin.confirm_all(next, this.connection, this.params); + } +}; + + +// exports.confirm_all = function(next, connection, params) { +// var recipient = params.shift(); +// connection.loginfo(this, "confirming recipient " + recipient); +// connection.relaying = 1; +// next(OK); +// }; + From da3b1547e65979d50b2b39fe6d2cf81bdb3724b4 Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 15 Feb 2012 15:31:05 -0800 Subject: [PATCH 02/10] Adding function bind for those times where we need to set this. --- function-bind.js | 31 +++++++++++++++++++++++++++++++ tests/fixtures/stub.js | 1 - tests/fixtures/stub_connection.js | 24 ++++++++++++++++++++++++ tests/fixtures/stub_plugin.js | 2 +- tests/plugins/relay_all_test.js | 29 +++++++++++++++++------------ 5 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 function-bind.js create mode 100644 tests/fixtures/stub_connection.js diff --git a/function-bind.js b/function-bind.js new file mode 100644 index 000000000..c54acf49e --- /dev/null +++ b/function-bind.js @@ -0,0 +1,31 @@ +if (!Function.prototype.bind) { + (function () { + var slice = Array.prototype.slice; + + Function.prototype.bind = function (thisObj) { + var target = this; + + if (arguments.length > 1) { + var args = slice.call(arguments, 1); + + return function () { + var allArgs = args; + + if (arguments.length > 0) { + allArgs = args.concat(slice.call(arguments)); + } + + return target.apply(thisObj, allArgs); + }; + } + + return function () { + if (arguments.length > 0) { + return target.apply(thisObj, arguments); + } + + return target.call(thisObj); + }; + }; + }()); +} diff --git a/tests/fixtures/stub.js b/tests/fixtures/stub.js index 628d51624..9c847c210 100644 --- a/tests/fixtures/stub.js +++ b/tests/fixtures/stub.js @@ -7,7 +7,6 @@ module.exports = function (returnValue) { } stub.called = false; - stub.relaying = false; return stub; }; diff --git a/tests/fixtures/stub_connection.js b/tests/fixtures/stub_connection.js new file mode 100644 index 000000000..0d20bd113 --- /dev/null +++ b/tests/fixtures/stub_connection.js @@ -0,0 +1,24 @@ +"use strict"; + +var stub = require('tests/fixtures/stub'); + +var connection = exports; + +function Connection(client, server) { + this.client = client; + this.server = server; + this.relaying = false; +} + +connection.createConnection = function(client, server) { + if (typeof(client) === 'undefined') { + client = {}; + } + + if (typeof(server) === 'undefined') { + server = {}; + } + + var obj = new Connection(client, server); + return obj; +}; diff --git a/tests/fixtures/stub_plugin.js b/tests/fixtures/stub_plugin.js index 7ef3de5fd..7b7ef3750 100644 --- a/tests/fixtures/stub_plugin.js +++ b/tests/fixtures/stub_plugin.js @@ -18,4 +18,4 @@ plugin.createPlugin = function(name) { } return obj; -} +}; diff --git a/tests/plugins/relay_all_test.js b/tests/plugins/relay_all_test.js index fa8891236..91d53e85d 100644 --- a/tests/plugins/relay_all_test.js +++ b/tests/plugins/relay_all_test.js @@ -1,5 +1,8 @@ +require('function-bind.js'); + var stub = require('tests/fixtures/stub'), - constants = require('../../constants'); + constants = require('../../constants'), + Connection = require('tests/fixtures/stub_connection'), Plugin = require('tests/fixtures/stub_plugin'); // huge hack here, but plugin tests need constants @@ -7,10 +10,12 @@ constants.import(global); function _set_up(callback) { this.backup = {}; + this.client = {}; + this.server = {}; // needed for tests this.plugin = Plugin.createPlugin('plugins/relay_all'); - this.connection = stub(); + this.connection = Connection.createConnection(); this.params = ['foo@bar.com']; // backup modifications @@ -21,7 +26,7 @@ function _set_up(callback) { this.plugin.register_hook = stub(); this.connection.loginfo = stub(); - // going to need this in multiple tests + // going to need these in multiple tests this.plugin.register(); callback(); @@ -67,15 +72,15 @@ exports.relay_all = { test.done(); }; + this.plugin.confirm_all(next, this.connection, this.params); + }, + 'confirm_all hook always sets connection.relaying to 1' : function (test) { + var next = function (action) { + test.expect(1); + test.equals(this.connection.relaying, 1); + test.done(); + }.bind(this); + this.plugin.confirm_all(next, this.connection, this.params); } }; - - -// exports.confirm_all = function(next, connection, params) { -// var recipient = params.shift(); -// connection.loginfo(this, "confirming recipient " + recipient); -// connection.relaying = 1; -// next(OK); -// }; - From c1ecd3fbcd78f84bf095da08aa6e5976ede7105e Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 15 Feb 2012 16:17:03 -0800 Subject: [PATCH 03/10] Started some tests for log.syslog plugin. --- plugins/log.syslog.js | 6 +- tests/fixtures/stub_logger.js | 13 ++ tests/plugins/log.syslog.js | 215 ++++++++++++++++++ .../{relay_all_test.js => relay_all.js} | 2 - 4 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/stub_logger.js create mode 100644 tests/plugins/log.syslog.js rename tests/plugins/{relay_all_test.js => relay_all.js} (98%) diff --git a/plugins/log.syslog.js b/plugins/log.syslog.js index a1d9e8411..c6d3c7bb8 100644 --- a/plugins/log.syslog.js +++ b/plugins/log.syslog.js @@ -1,3 +1,5 @@ +require('function-bind'); + // send logs to syslog var Syslog = require('node-syslog'); @@ -84,7 +86,7 @@ exports.register = function() { } this.register_hook('log', 'syslog'); -} +}; exports.syslog = function (next, logger, log) { switch(log.level.toUpperCase()) { @@ -117,4 +119,4 @@ exports.syslog = function (next, logger, log) { } return next(); -} +}; diff --git a/tests/fixtures/stub_logger.js b/tests/fixtures/stub_logger.js new file mode 100644 index 000000000..49df7ced5 --- /dev/null +++ b/tests/fixtures/stub_logger.js @@ -0,0 +1,13 @@ +"use strict"; + +var stub = require('tests/fixtures/stub'); + +var logger = exports; + +function Logger() { +} + +logger.createLogger = function() { + var obj = new Logger(); + return obj; +}; diff --git a/tests/plugins/log.syslog.js b/tests/plugins/log.syslog.js new file mode 100644 index 000000000..588c627ff --- /dev/null +++ b/tests/plugins/log.syslog.js @@ -0,0 +1,215 @@ +require('function-bind.js'); + +var stub = require('tests/fixtures/stub'), + constants = require('../../constants'), + Logger = require('tests/fixtures/stub_logger'), + Plugin = require('tests/fixtures/stub_plugin'); + +// huge hack here, but plugin tests need constants +constants.import(global); + +function _set_up(callback) { + this.backup = {}; + + // needed for tests + this.plugin = Plugin.createPlugin('plugins/log.syslog'); + this.logger = Logger.createLogger(); + + // backup modifications + this.backup.plugin = {}; + this.backup.plugin.register_hook = this.plugin.register_hook; + + // stub out functions + this.plugin.register_hook = stub(); + this.plugin.config = stub(); + this.log = stub(); + this.log.level = stub(); + this.log.level.toUpperCase = stub(); + + // some test data + this.plugin.config.get = function (file) { + return { + general : { + name : 'haraka', + facility : 'MAIL', + log_pid : 1, + log_odelay : 1, + log_cons : 0, + log_ndelay : 0, + log_nowait : 0 + } + }; + }; + + // going to need these in multiple tests + this.plugin.register(); + + callback(); +} + +function _tear_down(callback) { + // restore backed up functions + this.plugin.register_hook = this.backup.plugin.register_hook; + + callback(); +} + +exports.log_syslog = { + setUp : _set_up, + tearDown : _tear_down, + 'should have register function' : function (test) { + test.expect(2); + test.isNotNull(this.plugin); + test.isFunction(this.plugin.register); + test.done(); + }, + 'register function should call register_hook()' : function (test) { + test.expect(1); + test.ok(this.plugin.register_hook.called); + test.done(); + }, + 'register_hook() should register for propper hook' : function (test) { + test.expect(1); + test.equals(this.plugin.register_hook.args[0], 'log'); + test.done(); + }, + 'register_hook() should register available function' : function (test) { + test.expect(3); + test.equals(this.plugin.register_hook.args[1], 'syslog'); + test.isNotNull(this.plugin.syslog); + test.isFunction(this.plugin.syslog); + test.done(); + }, + 'hook always returns next()' : function (test) { + var next = function (action) { + test.expect(1); + test.isUndefined(action); + test.done(); + }; + + this.plugin.syslog(next, this.logger, this.log); + } +}; + + + +// send logs to syslog +//var Syslog = require('node-syslog'); +// +//exports.register = function() { +// var options = 0; +// var ini = this.config.get('log.syslog.ini'); +// var name = ini.general && (ini.general['name'] || 'haraka'); +// var facility = ini.general && (ini.general['facility'] || 'MAIL'); +// var pid = ini.general && (ini.general['log_pid'] || 1); +// var odelay = ini.general && (ini.general['log_odelay'] || 1); +// var cons = ini.general && (ini.general['log_cons'] || 0); +// var ndelay = ini.general && (ini.general['log_ndelay'] || 0); +// var nowait = ini.general && (ini.general['log_nowait'] || 0); +// +// if (pid) +// options |= Syslog.LOG_PID; +// +// if (odelay) +// options |= Syslog.LOG_ODELAY; +// +// if (cons) +// options |= Syslog.LOG_CONS; +// +// if (ndelay) +// options |= Syslog.LOG_NDELAY; +// +// if (nowait) +// options |= Syslog.LOG_NOWAIT; +// +// switch(facility.toUpperCase()) { +// case 'MAIL': +// Syslog.init(name, options, Syslog.LOG_MAIL); +// break; +// case 'KERN': +// Syslog.init(name, options, Syslog.LOG_KERN); +// break; +// case 'USER': +// Syslog.init(name, options, Syslog.LOG_USER); +// break; +// case 'DAEMON': +// Syslog.init(name, options, Syslog.LOG_DAEMON); +// break; +// case 'AUTH': +// Syslog.init(name, options, Syslog.LOG_AUTH); +// break; +// case 'SYSLOG': +// Syslog.init(name, options, Syslog.LOG_SYSLOG); +// break; +// case 'LPR': +// Syslog.init(name, options, Syslog.LOG_LPR); +// break; +// case 'NEWS': +// Syslog.init(name, options, Syslog.LOG_NEWS); +// break; +// case 'UUCP': +// Syslog.init(name, options, Syslog.LOG_UUCP); +// break; +// case 'LOCAL0': +// Syslog.init(name, options, Syslog.LOG_LOCAL0); +// break; +// case 'LOCAL1': +// Syslog.init(name, options, Syslog.LOG_LOCAL1); +// break; +// case 'LOCAL2': +// Syslog.init(name, options, Syslog.LOG_LOCAL2); +// break; +// case 'LOCAL3': +// Syslog.init(name, options, Syslog.LOG_LOCAL3); +// break; +// case 'LOCAL4': +// Syslog.init(name, options, Syslog.LOG_LOCAL4); +// break; +// case 'LOCAL5': +// Syslog.init(name, options, Syslog.LOG_LOCAL5); +// break; +// case 'LOCAL6': +// Syslog.init(name, options, Syslog.LOG_LOCAL6); +// break; +// case 'LOCAL7': +// Syslog.init(name, options, Syslog.LOG_LOCAL7); +// break; +// default: +// Syslog.init(name, options, Syslog.LOG_MAIL); +// } +// +// this.register_hook('log', 'syslog'); +//} +// +//exports.syslog = function (next, logger, log) { +// switch(log.level.toUpperCase()) { +// case 'INFO': +// Syslog.log(Syslog.LOG_INFO, log.data); +// break; +// case 'NOTICE': +// Syslog.log(Syslog.LOG_NOTICE, log.data); +// break; +// case 'WARN': +// Syslog.log(Syslog.LOG_WARNING, log.data); +// break; +// case 'ERROR': +// Syslog.log(Syslog.LOG_ERR, log.data); +// break; +// case 'CRIT': +// Syslog.log(Syslog.LOG_CRIT, log.data); +// break; +// case 'ALERT': +// Syslog.log(Syslog.LOG_ALERT, log.data); +// break; +// case 'EMERG': +// Syslog.log(Syslog.LOG_EMERG, log.data); +// break; +// case 'DATA': +// case 'PROTOCOL': +// case 'DEBUG': +// default: +// Syslog.log(Syslog.LOG_DEBUG, log.data); +// } +// +// return next(); +//} diff --git a/tests/plugins/relay_all_test.js b/tests/plugins/relay_all.js similarity index 98% rename from tests/plugins/relay_all_test.js rename to tests/plugins/relay_all.js index 91d53e85d..6bc5b73cc 100644 --- a/tests/plugins/relay_all_test.js +++ b/tests/plugins/relay_all.js @@ -10,8 +10,6 @@ constants.import(global); function _set_up(callback) { this.backup = {}; - this.client = {}; - this.server = {}; // needed for tests this.plugin = Plugin.createPlugin('plugins/relay_all'); From fddbe8101a6b9c964513bdf08f250d9fa8e6f8ae Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Thu, 16 Feb 2012 16:21:45 -0800 Subject: [PATCH 04/10] finished unit tests for log.syslog and added always_ok option. --- config/log.syslog.ini | 1 + docs/plugins/log.syslog.md | 8 ++ plugins/log.syslog.js | 31 +++-- tests/plugins/log.syslog.js | 220 ++++++++++++++---------------------- 4 files changed, 112 insertions(+), 148 deletions(-) diff --git a/config/log.syslog.ini b/config/log.syslog.ini index bf8cf9b87..90b81593a 100644 --- a/config/log.syslog.ini +++ b/config/log.syslog.ini @@ -6,3 +6,4 @@ odelay=1 cons=0 ndelay=0 nowait=0 +always_ok=false diff --git a/docs/plugins/log.syslog.md b/docs/plugins/log.syslog.md index 68fb0b6d1..20975ec56 100644 --- a/docs/plugins/log.syslog.md +++ b/docs/plugins/log.syslog.md @@ -61,3 +61,11 @@ chosen for you. Don't wait for child processes that may have been created while logging the message. + + +* log.syslog.general.always_ok (default: false) + + If false, then this plugin will return with just next() allowing other + plugins that have registered for the log hook to run. To speed things up, + if no other log hooks need to run (daemon), then one can make this true. + This will case the plugin to always call next(OK). diff --git a/plugins/log.syslog.js b/plugins/log.syslog.js index c6d3c7bb8..6aa0578f2 100644 --- a/plugins/log.syslog.js +++ b/plugins/log.syslog.js @@ -1,18 +1,21 @@ require('function-bind'); // send logs to syslog -var Syslog = require('node-syslog'); +var Syslog = exports.Syslog = require('node-syslog'); exports.register = function() { - var options = 0; - var ini = this.config.get('log.syslog.ini'); - var name = ini.general && (ini.general['name'] || 'haraka'); - var facility = ini.general && (ini.general['facility'] || 'MAIL'); - var pid = ini.general && (ini.general['log_pid'] || 1); - var odelay = ini.general && (ini.general['log_odelay'] || 1); - var cons = ini.general && (ini.general['log_cons'] || 0); - var ndelay = ini.general && (ini.general['log_ndelay'] || 0); - var nowait = ini.general && (ini.general['log_nowait'] || 0); + var options = 0; + var ini = this.config.get('log.syslog.ini'); + var name = ini.general && (ini.general['name'] || 'haraka'); + var facility = ini.general && (ini.general['facility'] || 'MAIL'); + var pid = ini.general && (ini.general['log_pid'] || 1); + var odelay = ini.general && (ini.general['log_odelay'] || 1); + var cons = ini.general && (ini.general['log_cons'] || 0); + var ndelay = ini.general && (ini.general['log_ndelay'] || 0); + var nowait = ini.general && (ini.general['log_nowait'] || 0); + var always_ok = ini.general && (ini.general['always_ok'] || false); + + this.always_ok = always_ok; if (pid) options |= Syslog.LOG_PID; @@ -89,6 +92,8 @@ exports.register = function() { }; exports.syslog = function (next, logger, log) { + var plugin = this; + switch(log.level.toUpperCase()) { case 'INFO': Syslog.log(Syslog.LOG_INFO, log.data); @@ -118,5 +123,9 @@ exports.syslog = function (next, logger, log) { Syslog.log(Syslog.LOG_DEBUG, log.data); } - return next(); + if (plugin.always_ok) { + return next(OK); + } else { + return next(); + } }; diff --git a/tests/plugins/log.syslog.js b/tests/plugins/log.syslog.js index 588c627ff..56bce11b6 100644 --- a/tests/plugins/log.syslog.js +++ b/tests/plugins/log.syslog.js @@ -2,6 +2,7 @@ require('function-bind.js'); var stub = require('tests/fixtures/stub'), constants = require('../../constants'), + util = require('util'), Logger = require('tests/fixtures/stub_logger'), Plugin = require('tests/fixtures/stub_plugin'); @@ -17,29 +18,32 @@ function _set_up(callback) { // backup modifications this.backup.plugin = {}; + this.backup.plugin.Syslog = {}; this.backup.plugin.register_hook = this.plugin.register_hook; // stub out functions this.plugin.register_hook = stub(); this.plugin.config = stub(); this.log = stub(); - this.log.level = stub(); - this.log.level.toUpperCase = stub(); + this.log.level = 'info'; + this.log.data = "this is a test log message"; // some test data - this.plugin.config.get = function (file) { - return { - general : { - name : 'haraka', - facility : 'MAIL', - log_pid : 1, - log_odelay : 1, - log_cons : 0, - log_ndelay : 0, - log_nowait : 0 - } - }; + this.configfile = { + general : { + name : 'haraka', + facility : 'MAIL', + log_pid : 1, + log_odelay : 1, + log_cons : 0, + log_ndelay : 0, + log_nowait : 0, + always_ok : false + } }; + this.plugin.config.get = function (file) { + return this.configfile; + }.bind(this); // going to need these in multiple tests this.plugin.register(); @@ -80,7 +84,39 @@ exports.log_syslog = { test.isFunction(this.plugin.syslog); test.done(); }, - 'hook always returns next()' : function (test) { + 'register calls Syslog.init()' : function (test) { + // local setup + this.backup.plugin.Syslog.init = this.plugin.Syslog.init; + this.plugin.Syslog.init = stub(); + this.plugin.register(); + + test.expect(1); + test.ok(this.plugin.Syslog.init.called); + test.done(); + + // local teardown + this.plugin.Syslog.init = this.backup.plugin.Syslog.init; + }, + 'register calls Syslog.init() with correct args' : function (test) { + // local setup + this.backup.plugin.Syslog.init = this.plugin.Syslog.init; + this.plugin.Syslog.init = stub(); + this.plugin.register(); + + test.expect(4); + test.ok(this.plugin.Syslog.init.called); + test.equals(this.plugin.Syslog.init.args[0], + this.plugin.config.get("test").general.name); + test.equals(this.plugin.Syslog.init.args[1], + this.plugin.Syslog.LOG_PID | this.plugin.Syslog.LOG_ODELAY); + test.equals(this.plugin.Syslog.init.args[2], + this.plugin.Syslog.LOG_MAIL); + test.done(); + + // local teardown + this.plugin.Syslog.init = this.backup.plugin.Syslog.init; + }, + 'hook returns just next() if configured to do so' : function (test) { var next = function (action) { test.expect(1); test.isUndefined(action); @@ -88,128 +124,38 @@ exports.log_syslog = { }; this.plugin.syslog(next, this.logger, this.log); - } -}; + }, + 'hook returns next(OK) if configured to do so' : function (test) { + // local setup + this.backup.configfile = this.configfile; + this.configfile.general.always_ok = true; + this.plugin.register(); + var next = function (action) { + test.expect(1); + test.equals(action, constants.ok); + test.done(); + }; + this.plugin.syslog(next, this.logger, this.log); -// send logs to syslog -//var Syslog = require('node-syslog'); -// -//exports.register = function() { -// var options = 0; -// var ini = this.config.get('log.syslog.ini'); -// var name = ini.general && (ini.general['name'] || 'haraka'); -// var facility = ini.general && (ini.general['facility'] || 'MAIL'); -// var pid = ini.general && (ini.general['log_pid'] || 1); -// var odelay = ini.general && (ini.general['log_odelay'] || 1); -// var cons = ini.general && (ini.general['log_cons'] || 0); -// var ndelay = ini.general && (ini.general['log_ndelay'] || 0); -// var nowait = ini.general && (ini.general['log_nowait'] || 0); -// -// if (pid) -// options |= Syslog.LOG_PID; -// -// if (odelay) -// options |= Syslog.LOG_ODELAY; -// -// if (cons) -// options |= Syslog.LOG_CONS; -// -// if (ndelay) -// options |= Syslog.LOG_NDELAY; -// -// if (nowait) -// options |= Syslog.LOG_NOWAIT; -// -// switch(facility.toUpperCase()) { -// case 'MAIL': -// Syslog.init(name, options, Syslog.LOG_MAIL); -// break; -// case 'KERN': -// Syslog.init(name, options, Syslog.LOG_KERN); -// break; -// case 'USER': -// Syslog.init(name, options, Syslog.LOG_USER); -// break; -// case 'DAEMON': -// Syslog.init(name, options, Syslog.LOG_DAEMON); -// break; -// case 'AUTH': -// Syslog.init(name, options, Syslog.LOG_AUTH); -// break; -// case 'SYSLOG': -// Syslog.init(name, options, Syslog.LOG_SYSLOG); -// break; -// case 'LPR': -// Syslog.init(name, options, Syslog.LOG_LPR); -// break; -// case 'NEWS': -// Syslog.init(name, options, Syslog.LOG_NEWS); -// break; -// case 'UUCP': -// Syslog.init(name, options, Syslog.LOG_UUCP); -// break; -// case 'LOCAL0': -// Syslog.init(name, options, Syslog.LOG_LOCAL0); -// break; -// case 'LOCAL1': -// Syslog.init(name, options, Syslog.LOG_LOCAL1); -// break; -// case 'LOCAL2': -// Syslog.init(name, options, Syslog.LOG_LOCAL2); -// break; -// case 'LOCAL3': -// Syslog.init(name, options, Syslog.LOG_LOCAL3); -// break; -// case 'LOCAL4': -// Syslog.init(name, options, Syslog.LOG_LOCAL4); -// break; -// case 'LOCAL5': -// Syslog.init(name, options, Syslog.LOG_LOCAL5); -// break; -// case 'LOCAL6': -// Syslog.init(name, options, Syslog.LOG_LOCAL6); -// break; -// case 'LOCAL7': -// Syslog.init(name, options, Syslog.LOG_LOCAL7); -// break; -// default: -// Syslog.init(name, options, Syslog.LOG_MAIL); -// } -// -// this.register_hook('log', 'syslog'); -//} -// -//exports.syslog = function (next, logger, log) { -// switch(log.level.toUpperCase()) { -// case 'INFO': -// Syslog.log(Syslog.LOG_INFO, log.data); -// break; -// case 'NOTICE': -// Syslog.log(Syslog.LOG_NOTICE, log.data); -// break; -// case 'WARN': -// Syslog.log(Syslog.LOG_WARNING, log.data); -// break; -// case 'ERROR': -// Syslog.log(Syslog.LOG_ERR, log.data); -// break; -// case 'CRIT': -// Syslog.log(Syslog.LOG_CRIT, log.data); -// break; -// case 'ALERT': -// Syslog.log(Syslog.LOG_ALERT, log.data); -// break; -// case 'EMERG': -// Syslog.log(Syslog.LOG_EMERG, log.data); -// break; -// case 'DATA': -// case 'PROTOCOL': -// case 'DEBUG': -// default: -// Syslog.log(Syslog.LOG_DEBUG, log.data); -// } -// -// return next(); -//} + // local teardown + this.configfile = this.backup.configfile; + }, + 'syslog hook logs correct thing' : function (test) { + // local setup + var next = stub(); + this.backup.plugin.Syslog.log = this.plugin.Syslog.log; + this.plugin.Syslog.log = stub(); + this.plugin.syslog(next, this.logger, this.log); + + test.expect(3); + test.ok(this.plugin.Syslog.log.called); + test.equals(this.plugin.Syslog.log.args[0], this.plugin.Syslog.LOG_INFO); + test.equals(this.plugin.Syslog.log.args[1], this.log.data); + test.done(); + + // local teardown + this.plugin.Syslog.log = this.backup.plugin.Syslog.log; + } +}; From 04575132b64df2dade8af314b189cda7e61121b9 Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Thu, 16 Feb 2012 16:24:44 -0800 Subject: [PATCH 05/10] This was only there for testing. --- plugins/log.syslog.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/log.syslog.js b/plugins/log.syslog.js index 6aa0578f2..e8925c151 100644 --- a/plugins/log.syslog.js +++ b/plugins/log.syslog.js @@ -1,5 +1,3 @@ -require('function-bind'); - // send logs to syslog var Syslog = exports.Syslog = require('node-syslog'); From 1c96d3687d1dd30a4e4e42d2282fd061024cc85b Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 22 Feb 2012 13:07:56 -0800 Subject: [PATCH 06/10] no need for function-bind. --- function-bind.js | 31 ------------------------------- tests/plugins/log.syslog.js | 3 --- tests/plugins/relay_all.js | 2 -- 3 files changed, 36 deletions(-) delete mode 100644 function-bind.js diff --git a/function-bind.js b/function-bind.js deleted file mode 100644 index c54acf49e..000000000 --- a/function-bind.js +++ /dev/null @@ -1,31 +0,0 @@ -if (!Function.prototype.bind) { - (function () { - var slice = Array.prototype.slice; - - Function.prototype.bind = function (thisObj) { - var target = this; - - if (arguments.length > 1) { - var args = slice.call(arguments, 1); - - return function () { - var allArgs = args; - - if (arguments.length > 0) { - allArgs = args.concat(slice.call(arguments)); - } - - return target.apply(thisObj, allArgs); - }; - } - - return function () { - if (arguments.length > 0) { - return target.apply(thisObj, arguments); - } - - return target.call(thisObj); - }; - }; - }()); -} diff --git a/tests/plugins/log.syslog.js b/tests/plugins/log.syslog.js index 56bce11b6..fb69a1850 100644 --- a/tests/plugins/log.syslog.js +++ b/tests/plugins/log.syslog.js @@ -1,8 +1,5 @@ -require('function-bind.js'); - var stub = require('tests/fixtures/stub'), constants = require('../../constants'), - util = require('util'), Logger = require('tests/fixtures/stub_logger'), Plugin = require('tests/fixtures/stub_plugin'); diff --git a/tests/plugins/relay_all.js b/tests/plugins/relay_all.js index 6bc5b73cc..a17b44daf 100644 --- a/tests/plugins/relay_all.js +++ b/tests/plugins/relay_all.js @@ -1,5 +1,3 @@ -require('function-bind.js'); - var stub = require('tests/fixtures/stub'), constants = require('../../constants'), Connection = require('tests/fixtures/stub_connection'), From 3c09231e819e0e2390eb01a0fc13eb2360a6a72c Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 22 Feb 2012 13:23:19 -0800 Subject: [PATCH 07/10] a little less code for general.ini. --- plugins/log.syslog.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/plugins/log.syslog.js b/plugins/log.syslog.js index e8925c151..8bdf3a10c 100644 --- a/plugins/log.syslog.js +++ b/plugins/log.syslog.js @@ -4,14 +4,15 @@ var Syslog = exports.Syslog = require('node-syslog'); exports.register = function() { var options = 0; var ini = this.config.get('log.syslog.ini'); - var name = ini.general && (ini.general['name'] || 'haraka'); - var facility = ini.general && (ini.general['facility'] || 'MAIL'); - var pid = ini.general && (ini.general['log_pid'] || 1); - var odelay = ini.general && (ini.general['log_odelay'] || 1); - var cons = ini.general && (ini.general['log_cons'] || 0); - var ndelay = ini.general && (ini.general['log_ndelay'] || 0); - var nowait = ini.general && (ini.general['log_nowait'] || 0); - var always_ok = ini.general && (ini.general['always_ok'] || false); + ini.general ||= {}; + var name = ini.general['name'] || 'haraka'; + var facility = ini.general['facility'] || 'MAIL'; + var pid = ini.general['log_pid'] || 1; + var odelay = ini.general['log_odelay'] || 1; + var cons = ini.general['log_cons'] || 0; + var ndelay = ini.general['log_ndelay'] || 0; + var nowait = ini.general['log_nowait'] || 0; + var always_ok = ini.general['always_ok'] || false; this.always_ok = always_ok; From 810025f50ad8bed753633e16946d8ee5b0bda2a3 Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 22 Feb 2012 13:28:58 -0800 Subject: [PATCH 08/10] that last push was a syntax error --- plugins/log.syslog.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/log.syslog.js b/plugins/log.syslog.js index 8bdf3a10c..bc49fac2f 100644 --- a/plugins/log.syslog.js +++ b/plugins/log.syslog.js @@ -4,7 +4,7 @@ var Syslog = exports.Syslog = require('node-syslog'); exports.register = function() { var options = 0; var ini = this.config.get('log.syslog.ini'); - ini.general ||= {}; + ini.general = ini.general || {}; var name = ini.general['name'] || 'haraka'; var facility = ini.general['facility'] || 'MAIL'; var pid = ini.general['log_pid'] || 1; From 72dc530b11e4e74827136acb7391e0ce46f12a45 Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 22 Feb 2012 13:38:57 -0800 Subject: [PATCH 09/10] removed hte else cuddle. --- plugins/log.syslog.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/log.syslog.js b/plugins/log.syslog.js index bc49fac2f..aad004e52 100644 --- a/plugins/log.syslog.js +++ b/plugins/log.syslog.js @@ -124,7 +124,8 @@ exports.syslog = function (next, logger, log) { if (plugin.always_ok) { return next(OK); - } else { + } + else { return next(); } }; From 319e47e2b0af215ce9c48cc81a7ec88a2b1b05e7 Mon Sep 17 00:00:00 2001 From: Christopher Mooney Date: Wed, 22 Feb 2012 14:19:25 -0800 Subject: [PATCH 10/10] four spaces instead of two. --- run_tests | 20 +-- tests/fixtures/stub.js | 16 +- tests/fixtures/stub_connection.js | 22 +-- tests/fixtures/stub_logger.js | 4 +- tests/fixtures/stub_plugin.js | 14 +- tests/plugins/log.syslog.js | 279 +++++++++++++++--------------- tests/plugins/relay_all.js | 116 ++++++------- 7 files changed, 236 insertions(+), 235 deletions(-) diff --git a/run_tests b/run_tests index 4b0fff67c..e4f03db8b 100755 --- a/run_tests +++ b/run_tests @@ -3,18 +3,18 @@ require.paths.unshift(__dirname); try { - var reporter = require('nodeunit').reporters.default; + var reporter = require('nodeunit').reporters.default; } catch(e) { - console.log("Error: " + e.message); - console.log(""); - console.log("Cannot find nodeunit module."); - console.log("You can download submodules for this project by doing:"); - console.log(""); - console.log(" git submodule init"); - console.log(" git submodule update"); - console.log(""); - process.exit(); + console.log("Error: " + e.message); + console.log(""); + console.log("Cannot find nodeunit module."); + console.log("You can download submodules for this project by doing:"); + console.log(""); + console.log(" git submodule init"); + console.log(" git submodule update"); + console.log(""); + process.exit(); } process.chdir(__dirname); diff --git a/tests/fixtures/stub.js b/tests/fixtures/stub.js index 9c847c210..ec1af39d4 100644 --- a/tests/fixtures/stub.js +++ b/tests/fixtures/stub.js @@ -1,12 +1,12 @@ module.exports = function (returnValue) { - function stub() { - stub.called = true; - stub.args = arguments; - stub.thisArg = this; - return returnValue; - } + function stub() { + stub.called = true; + stub.args = arguments; + stub.thisArg = this; + return returnValue; + } - stub.called = false; + stub.called = false; - return stub; + return stub; }; diff --git a/tests/fixtures/stub_connection.js b/tests/fixtures/stub_connection.js index 0d20bd113..479b6e94a 100644 --- a/tests/fixtures/stub_connection.js +++ b/tests/fixtures/stub_connection.js @@ -5,20 +5,20 @@ var stub = require('tests/fixtures/stub'); var connection = exports; function Connection(client, server) { - this.client = client; - this.server = server; - this.relaying = false; + this.client = client; + this.server = server; + this.relaying = false; } connection.createConnection = function(client, server) { - if (typeof(client) === 'undefined') { - client = {}; - } + if (typeof(client) === 'undefined') { + client = {}; + } - if (typeof(server) === 'undefined') { - server = {}; - } + if (typeof(server) === 'undefined') { + server = {}; + } - var obj = new Connection(client, server); - return obj; + var obj = new Connection(client, server); + return obj; }; diff --git a/tests/fixtures/stub_logger.js b/tests/fixtures/stub_logger.js index 49df7ced5..dff8aad25 100644 --- a/tests/fixtures/stub_logger.js +++ b/tests/fixtures/stub_logger.js @@ -8,6 +8,6 @@ function Logger() { } logger.createLogger = function() { - var obj = new Logger(); - return obj; + var obj = new Logger(); + return obj; }; diff --git a/tests/fixtures/stub_plugin.js b/tests/fixtures/stub_plugin.js index 7b7ef3750..d74ce5485 100644 --- a/tests/fixtures/stub_plugin.js +++ b/tests/fixtures/stub_plugin.js @@ -8,14 +8,14 @@ function Plugin(name) { } plugin.createPlugin = function(name) { - var obj = new Plugin(name); - var plug = require(name); + var obj = new Plugin(name); + var plug = require(name); - for (var k in plug) { - if (plug.hasOwnProperty(k)) { - obj[k] = plug[k]; + for (var k in plug) { + if (plug.hasOwnProperty(k)) { + obj[k] = plug[k]; + } } - } - return obj; + return obj; }; diff --git a/tests/plugins/log.syslog.js b/tests/plugins/log.syslog.js index fb69a1850..d20e2affd 100644 --- a/tests/plugins/log.syslog.js +++ b/tests/plugins/log.syslog.js @@ -7,152 +7,153 @@ var stub = require('tests/fixtures/stub'), constants.import(global); function _set_up(callback) { - this.backup = {}; - - // needed for tests - this.plugin = Plugin.createPlugin('plugins/log.syslog'); - this.logger = Logger.createLogger(); - - // backup modifications - this.backup.plugin = {}; - this.backup.plugin.Syslog = {}; - this.backup.plugin.register_hook = this.plugin.register_hook; - - // stub out functions - this.plugin.register_hook = stub(); - this.plugin.config = stub(); - this.log = stub(); - this.log.level = 'info'; - this.log.data = "this is a test log message"; - - // some test data - this.configfile = { - general : { - name : 'haraka', - facility : 'MAIL', - log_pid : 1, - log_odelay : 1, - log_cons : 0, - log_ndelay : 0, - log_nowait : 0, - always_ok : false - } - }; - this.plugin.config.get = function (file) { - return this.configfile; - }.bind(this); + this.backup = {}; + + // needed for tests + this.plugin = Plugin.createPlugin('plugins/log.syslog'); + this.logger = Logger.createLogger(); + + // backup modifications + this.backup.plugin = {}; + this.backup.plugin.Syslog = {}; + this.backup.plugin.register_hook = this.plugin.register_hook; + + // stub out functions + this.plugin.register_hook = stub(); + this.plugin.config = stub(); + this.log = stub(); + this.log.level = 'info'; + this.log.data = "this is a test log message"; + + // some test data + this.configfile = { + general : { + name : 'haraka', + facility : 'MAIL', + log_pid : 1, + log_odelay : 1, + log_cons : 0, + log_ndelay : 0, + log_nowait : 0, + always_ok : false + } + }; + this.plugin.config.get = function (file) { + return this.configfile; + }.bind(this); - // going to need these in multiple tests - this.plugin.register(); + // going to need these in multiple tests + this.plugin.register(); - callback(); + callback(); } function _tear_down(callback) { - // restore backed up functions - this.plugin.register_hook = this.backup.plugin.register_hook; + // restore backed up functions + this.plugin.register_hook = this.backup.plugin.register_hook; - callback(); + callback(); } exports.log_syslog = { - setUp : _set_up, - tearDown : _tear_down, - 'should have register function' : function (test) { - test.expect(2); - test.isNotNull(this.plugin); - test.isFunction(this.plugin.register); - test.done(); - }, - 'register function should call register_hook()' : function (test) { - test.expect(1); - test.ok(this.plugin.register_hook.called); - test.done(); - }, - 'register_hook() should register for propper hook' : function (test) { - test.expect(1); - test.equals(this.plugin.register_hook.args[0], 'log'); - test.done(); - }, - 'register_hook() should register available function' : function (test) { - test.expect(3); - test.equals(this.plugin.register_hook.args[1], 'syslog'); - test.isNotNull(this.plugin.syslog); - test.isFunction(this.plugin.syslog); - test.done(); - }, - 'register calls Syslog.init()' : function (test) { - // local setup - this.backup.plugin.Syslog.init = this.plugin.Syslog.init; - this.plugin.Syslog.init = stub(); - this.plugin.register(); - - test.expect(1); - test.ok(this.plugin.Syslog.init.called); - test.done(); - - // local teardown - this.plugin.Syslog.init = this.backup.plugin.Syslog.init; - }, - 'register calls Syslog.init() with correct args' : function (test) { - // local setup - this.backup.plugin.Syslog.init = this.plugin.Syslog.init; - this.plugin.Syslog.init = stub(); - this.plugin.register(); - - test.expect(4); - test.ok(this.plugin.Syslog.init.called); - test.equals(this.plugin.Syslog.init.args[0], - this.plugin.config.get("test").general.name); - test.equals(this.plugin.Syslog.init.args[1], - this.plugin.Syslog.LOG_PID | this.plugin.Syslog.LOG_ODELAY); - test.equals(this.plugin.Syslog.init.args[2], - this.plugin.Syslog.LOG_MAIL); - test.done(); - - // local teardown - this.plugin.Syslog.init = this.backup.plugin.Syslog.init; - }, - 'hook returns just next() if configured to do so' : function (test) { - var next = function (action) { - test.expect(1); - test.isUndefined(action); - test.done(); - }; - - this.plugin.syslog(next, this.logger, this.log); - }, - 'hook returns next(OK) if configured to do so' : function (test) { - // local setup - this.backup.configfile = this.configfile; - this.configfile.general.always_ok = true; - this.plugin.register(); - - var next = function (action) { - test.expect(1); - test.equals(action, constants.ok); - test.done(); - }; - - this.plugin.syslog(next, this.logger, this.log); - - // local teardown - this.configfile = this.backup.configfile; - }, - 'syslog hook logs correct thing' : function (test) { - // local setup - var next = stub(); - this.backup.plugin.Syslog.log = this.plugin.Syslog.log; - this.plugin.Syslog.log = stub(); - this.plugin.syslog(next, this.logger, this.log); - - test.expect(3); - test.ok(this.plugin.Syslog.log.called); - test.equals(this.plugin.Syslog.log.args[0], this.plugin.Syslog.LOG_INFO); - test.equals(this.plugin.Syslog.log.args[1], this.log.data); - test.done(); - - // local teardown - this.plugin.Syslog.log = this.backup.plugin.Syslog.log; - } + setUp : _set_up, + tearDown : _tear_down, + 'should have register function' : function (test) { + test.expect(2); + test.isNotNull(this.plugin); + test.isFunction(this.plugin.register); + test.done(); + }, + 'register function should call register_hook()' : function (test) { + test.expect(1); + test.ok(this.plugin.register_hook.called); + test.done(); + }, + 'register_hook() should register for propper hook' : function (test) { + test.expect(1); + test.equals(this.plugin.register_hook.args[0], 'log'); + test.done(); + }, + 'register_hook() should register available function' : function (test) { + test.expect(3); + test.equals(this.plugin.register_hook.args[1], 'syslog'); + test.isNotNull(this.plugin.syslog); + test.isFunction(this.plugin.syslog); + test.done(); + }, + 'register calls Syslog.init()' : function (test) { + // local setup + this.backup.plugin.Syslog.init = this.plugin.Syslog.init; + this.plugin.Syslog.init = stub(); + this.plugin.register(); + + test.expect(1); + test.ok(this.plugin.Syslog.init.called); + test.done(); + + // local teardown + this.plugin.Syslog.init = this.backup.plugin.Syslog.init; + }, + 'register calls Syslog.init() with correct args' : function (test) { + // local setup + this.backup.plugin.Syslog.init = this.plugin.Syslog.init; + this.plugin.Syslog.init = stub(); + this.plugin.register(); + + test.expect(4); + test.ok(this.plugin.Syslog.init.called); + test.equals(this.plugin.Syslog.init.args[0], + this.plugin.config.get("test").general.name); + test.equals(this.plugin.Syslog.init.args[1], + this.plugin.Syslog.LOG_PID | this.plugin.Syslog.LOG_ODELAY); + test.equals(this.plugin.Syslog.init.args[2], + this.plugin.Syslog.LOG_MAIL); + test.done(); + + // local teardown + this.plugin.Syslog.init = this.backup.plugin.Syslog.init; + }, + 'hook returns just next() if configured to do so' : function (test) { + var next = function (action) { + test.expect(1); + test.isUndefined(action); + test.done(); + }; + + this.plugin.syslog(next, this.logger, this.log); + }, + 'hook returns next(OK) if configured to do so' : function (test) { + // local setup + this.backup.configfile = this.configfile; + this.configfile.general.always_ok = true; + this.plugin.register(); + + var next = function (action) { + test.expect(1); + test.equals(action, constants.ok); + test.done(); + }; + + this.plugin.syslog(next, this.logger, this.log); + + // local teardown + this.configfile = this.backup.configfile; + }, + 'syslog hook logs correct thing' : function (test) { + // local setup + var next = stub(); + this.backup.plugin.Syslog.log = this.plugin.Syslog.log; + this.plugin.Syslog.log = stub(); + this.plugin.syslog(next, this.logger, this.log); + + test.expect(3); + test.ok(this.plugin.Syslog.log.called); + test.equals(this.plugin.Syslog.log.args[0], + this.plugin.Syslog.LOG_INFO); + test.equals(this.plugin.Syslog.log.args[1], this.log.data); + test.done(); + + // local teardown + this.plugin.Syslog.log = this.backup.plugin.Syslog.log; + } }; diff --git a/tests/plugins/relay_all.js b/tests/plugins/relay_all.js index a17b44daf..08ef62009 100644 --- a/tests/plugins/relay_all.js +++ b/tests/plugins/relay_all.js @@ -7,76 +7,76 @@ var stub = require('tests/fixtures/stub'), constants.import(global); function _set_up(callback) { - this.backup = {}; + this.backup = {}; - // needed for tests - this.plugin = Plugin.createPlugin('plugins/relay_all'); - this.connection = Connection.createConnection(); - this.params = ['foo@bar.com']; + // needed for tests + this.plugin = Plugin.createPlugin('plugins/relay_all'); + this.connection = Connection.createConnection(); + this.params = ['foo@bar.com']; - // backup modifications - this.backup.plugin = {}; - this.backup.plugin.register_hook = this.plugin.register_hook; + // backup modifications + this.backup.plugin = {}; + this.backup.plugin.register_hook = this.plugin.register_hook; - // stub out functions - this.plugin.register_hook = stub(); - this.connection.loginfo = stub(); + // stub out functions + this.plugin.register_hook = stub(); + this.connection.loginfo = stub(); - // going to need these in multiple tests - this.plugin.register(); + // going to need these in multiple tests + this.plugin.register(); - callback(); + callback(); } function _tear_down(callback) { - // restore backed up functions - this.plugin.register_hook = this.backup.plugin.register_hook; + // restore backed up functions + this.plugin.register_hook = this.backup.plugin.register_hook; - callback(); + callback(); } exports.relay_all = { - setUp : _set_up, - tearDown : _tear_down, - 'should have register function' : function (test) { - test.expect(2); - test.isNotNull(this.plugin); - test.isFunction(this.plugin.register); - test.done(); - }, - 'register function should call register_hook()' : function (test) { - test.expect(1); - test.ok(this.plugin.register_hook.called); - test.done(); - }, - 'register_hook() should register for propper hook' : function (test) { - test.expect(1); - test.equals(this.plugin.register_hook.args[0], 'rcpt'); - test.done(); - }, - 'register_hook() should register available function' : function (test) { - test.expect(3); - test.equals(this.plugin.register_hook.args[1], 'confirm_all'); - test.isNotNull(this.plugin.confirm_all); - test.isFunction(this.plugin.confirm_all); - test.done(); - }, - 'confirm_all hook always returns OK' : function (test) { - var next = function (action) { - test.expect(1); - test.equals(action, constants.ok); - test.done(); - }; + setUp : _set_up, + tearDown : _tear_down, + 'should have register function' : function (test) { + test.expect(2); + test.isNotNull(this.plugin); + test.isFunction(this.plugin.register); + test.done(); + }, + 'register function should call register_hook()' : function (test) { + test.expect(1); + test.ok(this.plugin.register_hook.called); + test.done(); + }, + 'register_hook() should register for propper hook' : function (test) { + test.expect(1); + test.equals(this.plugin.register_hook.args[0], 'rcpt'); + test.done(); + }, + 'register_hook() should register available function' : function (test) { + test.expect(3); + test.equals(this.plugin.register_hook.args[1], 'confirm_all'); + test.isNotNull(this.plugin.confirm_all); + test.isFunction(this.plugin.confirm_all); + test.done(); + }, + 'confirm_all hook always returns OK' : function (test) { + var next = function (action) { + test.expect(1); + test.equals(action, constants.ok); + test.done(); + }; - this.plugin.confirm_all(next, this.connection, this.params); - }, - 'confirm_all hook always sets connection.relaying to 1' : function (test) { - var next = function (action) { - test.expect(1); - test.equals(this.connection.relaying, 1); - test.done(); - }.bind(this); + this.plugin.confirm_all(next, this.connection, this.params); + }, + 'confirm_all hook always sets connection.relaying to 1' : function (test) { + var next = function (action) { + test.expect(1); + test.equals(this.connection.relaying, 1); + test.done(); + }.bind(this); - this.plugin.confirm_all(next, this.connection, this.params); - } + this.plugin.confirm_all(next, this.connection, this.params); + } };