From 8b24205e9d17c0b07702e4fa139257bc08c2f6b2 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Wed, 29 Mar 2023 09:37:51 -0700 Subject: [PATCH] replace for i loop with for..of, add test --- index.js | 10 +++++----- test/config/rcpt_to.routes.ini | 11 +++++++++++ test/recipient-routes.js | 15 +++++++++++---- 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 test/config/rcpt_to.routes.ini diff --git a/index.js b/index.js index 549072f..60661ee 100644 --- a/index.js +++ b/index.js @@ -26,9 +26,9 @@ exports.register = function () { exports.load_rcpt_to_routes_ini = function () { const plugin = this; plugin.cfg = plugin.config.get('rcpt_to.routes.ini', { - booleans: [ - '+redis.enabled', - ], + booleans: [ + '+redis.enabled', + ], }, function () { plugin.load_rcpt_to_routes_ini(); @@ -45,8 +45,8 @@ exports.load_rcpt_to_routes_ini = function () { const lowered = {}; if (plugin.cfg.routes) { const keys = Object.keys(plugin.cfg.routes); - for (let i=0; i < keys.length; i++) { - lowered[keys[i].toLowerCase()] = plugin.cfg.routes[keys[i]]; + for (const key of keys) { + lowered[key.toLowerCase()] = plugin.cfg.routes[key]; } plugin.route_list = lowered; } diff --git a/test/config/rcpt_to.routes.ini b/test/config/rcpt_to.routes.ini new file mode 100644 index 0000000..1816819 --- /dev/null +++ b/test/config/rcpt_to.routes.ini @@ -0,0 +1,11 @@ + +; [redis] +; host=127.0.0.1 +; port=6379 +; db=0 +; enabled=true + +[routes] +matt@example.com=192.168.76.66 +bad@example.com=127.0.0.1:26 +mixEd@examPle.com=172.16.1.1 diff --git a/test/recipient-routes.js b/test/recipient-routes.js index 70fafb5..28a24d2 100644 --- a/test/recipient-routes.js +++ b/test/recipient-routes.js @@ -1,6 +1,7 @@ 'use strict'; const assert = require('assert') +const path = require('path') const Address = require('address-rfc2821').Address; const fixtures = require('haraka-test-fixtures'); @@ -23,6 +24,7 @@ const hmail = { function file_setup (done) { this.server = {}; this.plugin = new fixtures.plugin('index'); + this.plugin.config = this.plugin.config.module_config(path.resolve('test')); this.plugin.register(); this.connection = fixtures.connection.createConnection(); @@ -61,12 +63,10 @@ describe('haraka-plugin-recipient-routes', function () { assert.equal(rc, undefined); assert.equal(msg, undefined); done() - }, this.connection, [ new Address('') ]); + }, this.connection, [ new Address('') ]); }) it('hit returns OK', function (done) { - this.plugin.route_list = { 'matt@example.com': '192.168.1.1' }; - this.plugin.rcpt(function (rc, msg) { assert.equal(rc, OK); assert.equal(msg, undefined); @@ -75,7 +75,6 @@ describe('haraka-plugin-recipient-routes', function () { }) it('missing domain', function (done) { - this.plugin.route_list = { 'matt@example.com': '192.168.1.1' }; try { this.plugin.rcpt(function (rc, msg) { assert.ok(false) @@ -87,6 +86,14 @@ describe('haraka-plugin-recipient-routes', function () { done() } }) + + it('lowers mixed case routes', function () { + assert.deepEqual(this.plugin.route_list, { + "bad@example.com": "127.0.0.1:26", + "matt@example.com": "192.168.76.66", + 'mixed@example.com': '172.16.1.1', + }) + }) }) describe('rcpt redis', function () {