Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add received_header test, cleaned up PR #59 #61

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
58 changes: 34 additions & 24 deletions test/geoip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
})
}
})
Expand Down Expand Up @@ -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)
})
})
Loading