From fbfbd1a920699cba86b1c848eb479ea01780276f Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 2 Apr 2024 15:19:00 -0700 Subject: [PATCH] add received_header test, cleaned up PR #59 (#61) - deps: pin versions - test: load DBs so we get test results - test: use correct assert syntax - index: turn off watchForUpdates, due to warning: - (node:52697) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [StatWatcher]. Use emitter.setMaxListeners() to increase limit Closes #59 - [x] tests updated --- index.js | 4 ++-- package.json | 14 ++++++------- test/geoip.js | 58 ++++++++++++++++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 95ccbb2..26f0dae 100644 --- a/index.js +++ b/index.js @@ -88,8 +88,8 @@ exports.load_dbs = async function () { } this[`${db}Lookup`] = await this.maxmind.open(dbPath, { - // this causes tests to hang, which is why mocha runs with --exit - watchForUpdates: true, + // watchForUpdates causes tests to hang unless mocha runs with --exit + watchForUpdates: false, cache: { max: 1000, // max items in cache maxAge: 1000 * 60 * 60 // life time in milliseconds diff --git a/package.json b/package.json index 15d629a..8402fa0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "node": ">=14" }, "scripts": { - "test": "npx mocha --exit", + "test": "npx mocha", "lint": "npx eslint index.js test/*.js", "lintfix": "npx eslint --fix index.js test/*.js", "cover": "NODE_ENV=cov npx nyc --reporter=lcovonly npm run test", @@ -34,13 +34,13 @@ }, "homepage": "https://github.com/haraka/haraka-plugin-geoip#readme", "devDependencies": { - "eslint": ">=8", - "eslint-plugin-haraka": "*", - "haraka-test-fixtures": "*", - "mocha": "*" + "eslint": "^8.57.0", + "eslint-plugin-haraka": "^1.0.15", + "haraka-test-fixtures": "^1.3.3", + "mocha": "^10.4.0" }, "dependencies": { - "maxmind": "^4.3.8", - "haraka-net-utils": "*" + "maxmind": "^4.3.18", + "haraka-net-utils": "^1.5.4" } } diff --git a/test/geoip.js b/test/geoip.js index 44468e2..1140207 100644 --- a/test/geoip.js +++ b/test/geoip.js @@ -8,61 +8,52 @@ const fixtures = require('haraka-test-fixtures') const plugin_name = 'geoip' describe('register', function () { - beforeEach(function (done) { + beforeEach(async function () { this.plugin = new fixtures.plugin('geoip') - this.plugin.register().then(done) + await this.plugin.register() }) - it('config loaded', function (done) { + it('config loaded', function () { assert.ok(this.plugin.cfg) assert.ok(this.plugin.cfg.main) - done() }) if (plugin_name === 'geoip') { - it('maxmind module loaded', function (done) { + it('maxmind module loaded', function () { assert.ok(this.plugin.maxmind) - done() }) } if (plugin_name === 'geoip-lite') { - it('geoip-lite module loads', function (done) { + it('geoip-lite module loads', function () { assert.ok(this.plugin.geoip) - done() }) } }) describe('database lookups', function () { - beforeEach(function (done) { + beforeEach(async function () { this.plugin = new fixtures.plugin('geoip') - this.plugin.register().then(() => { - this.connection = fixtures.connection.createConnection() - }) + await this.plugin.register() + this.connection = fixtures.connection.createConnection() if (plugin_name === 'geoip') { this.plugin.cfg.main.dbdir = path.resolve('test','fixtures') - this.plugin.load_dbs().then(done) - } - else { - done() + await this.plugin.load_dbs() } }) describe('get_geoip', function () { - it('no IP fails', function (done) { + it('no IP fails', function () { assert.ok(!this.plugin.get_geoip()) - done() }) - it('ipv4 private fails', function (done) { + it('ipv4 private fails', function () { assert.ok(!this.plugin.get_geoip('192.168.2.3')) - done() }) - it('ipv4 public passes', function (done) { + it('ipv4 public passes', function () { const r = this.plugin.get_geoip('192.48.85.146') if (plugin_name === 'geoip') { assert.equal(r.continent.code, 'NA') @@ -71,15 +62,13 @@ describe('database lookups', function () { if (plugin_name === 'geoip-lite') { assert.equal(r.country, 'US') } - done() }) if (plugin_name === 'geoip') { - it('ipv6 public passes', function (done) { + it('ipv6 public passes', function () { const r = this.plugin.get_geoip('2607:f060:b008:feed::6') assert.equal(r.continent.code, 'NA') assert.equal(r.country.iso_code, 'US') - done() }) } }) @@ -160,3 +149,24 @@ describe('haversine', function () { done() }) }) + +describe('received_headers', function () { + beforeEach(async function () { + this.plugin = new fixtures.plugin('geoip') + await this.plugin.register() + this.connection = fixtures.connection.createConnection() + this.connection.transaction = fixtures.transaction.createTransaction() + + if (plugin_name === 'geoip') { + this.plugin.cfg.main.dbdir = path.resolve('test','fixtures') + await this.plugin.load_dbs() + } + }) + + it('generates results for each received header', function () { + this.connection.transaction.header.add_end('Received', 'from [199.176.179.3]') + this.connection.transaction.header.add_end('Received', 'from [192.48.85.146]') + const results = this.plugin.received_headers(this.connection) + assert.equal(results.length, 2) + }) +}) \ No newline at end of file