Skip to content
Open
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
144 changes: 85 additions & 59 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')

const Address = require('address-rfc2821').Address
const fixtures = require('haraka-test-fixtures')
Expand Down Expand Up @@ -27,57 +27,65 @@ describe('is_authenticated', function () {
this.connection.init_transaction()
})

it('returns empty when no auth found', function (done) {
it('returns empty when no auth found', async function () {
this.connection.transaction.mail_from = new Address('<johndoe@test.com>')
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, undefined)
done()
await new Promise((resolve) => {
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, undefined)
resolve()
}, this.connection)
}, this.connection)
})

it('finds OD from FCrDNS match', function (done) {
it('finds OD from FCrDNS match', async function () {
this.connection.transaction.mail_from = new Address(
'<johndoe@validated-test.com>',
)
this.connection.results.add(
{ name: 'fcrdns' },
{ fcrdns: 'validated-test.com' },
)
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, 'validated-test.com')
done()
await new Promise((resolve) => {
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, 'validated-test.com')
resolve()
}, this.connection)
}, this.connection)
})

it('finds OD from FCrDNS array match', function (done) {
it('finds OD from FCrDNS array match', async function () {
this.connection.transaction.mail_from = new Address(
'<johndoe@validated-test.com>',
)
this.connection.results.add(
{ name: 'fcrdns' },
{ fcrdns: ['validated-test.com'] },
)
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, 'validated-test.com')
done()
await new Promise((resolve) => {
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, 'validated-test.com')
resolve()
}, this.connection)
}, this.connection)
})

it('misses OD on FCrDNS miss', function (done) {
it('misses OD on FCrDNS miss', async function () {
this.connection.transaction.mail_from = new Address(
'<johndoe@invalid-test.com>',
)
this.connection.results.add(
{ name: 'fcrdns' },
{ fcrdns: 'valid-test.com' },
)
plugin.is_authenticated((null1, null2, sender_od) => {
assert.equal(sender_od, undefined)
done()
await new Promise((resolve) => {
plugin.is_authenticated((null1, null2, sender_od) => {
assert.equal(sender_od, undefined)
resolve()
}, this.connection)
}, this.connection)
})

it('finds OD on SPF mfrom match', function (done) {
it('finds OD on SPF mfrom match', async function () {
this.connection.transaction.mail_from = new Address(
'<johndoe@spf-mfrom.com>',
)
Expand All @@ -90,13 +98,15 @@ describe('is_authenticated', function () {
},
)

plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, 'spf-mfrom.com')
done()
await new Promise((resolve) => {
plugin.is_authenticated(function (null1, null2, sender_od) {
assert.equal(sender_od, 'spf-mfrom.com')
resolve()
}, this.connection)
}, this.connection)
})

it('finds OD on SPF helo match', function (done) {
it('finds OD on SPF helo match', async function () {
this.connection.transaction.mail_from = new Address(
'<johndoe@helo-pass.com>',
)
Expand All @@ -109,9 +119,11 @@ describe('is_authenticated', function () {
},
)

plugin.is_authenticated((null1, null2, sender_od) => {
assert.equal(sender_od, 'helo-pass.com')
done()
await new Promise((resolve) => {
plugin.is_authenticated((null1, null2, sender_od) => {
assert.equal(sender_od, 'helo-pass.com')
resolve()
}, this.connection)
}, this.connection)
})
})
Expand All @@ -124,68 +136,78 @@ describe('check_recipient', function () {
connection.init_transaction()
})

it('reduces domain to OD', function (done) {
it('reduces domain to OD', async function () {
const plugin = new fixtures.plugin('index')
plugin.validated_sender_od = 'example.com'

plugin.check_recipient(
() => {
const res = connection.transaction.results.get(plugin.name)
assert.equal(res.rcpt_ods[0], 'example.com')
done()
},
connection,
new Address('<user@host.example.com>'),
)
await new Promise((resolve) => {
plugin.check_recipient(
() => {
const res = connection.transaction.results.get(plugin.name)
assert.equal(res.rcpt_ods[0], 'example.com')
resolve()
},
connection,
new Address('<user@host.example.com>'),
)
})
})
})

describe('update_sender', function () {
beforeEach(function (done) {
beforeEach(async function () {
this.connection = fixtures.connection.createConnection()
this.connection.relaying = true
this.connection.init_transaction()

this.plugin = new fixtures.plugin('index')
this.plugin.inherits('haraka-plugin-redis')
this.plugin.load_sender_ini()
this.plugin.init_redis_plugin(done)
await new Promise((resolve) => {
this.plugin.init_redis_plugin(resolve)
})
})

after(function () {
this.plugin.shutdown()
})

it('gets the sender domain', function (done) {
it('gets the sender domain', async function () {
this.connection.transaction.mail_from = new Address('<johndoe@example.com>')

this.plugin.update_sender(function (null1, null2, sender_dom, rcpt_doms) {
assert.equal(sender_dom, 'example.com')
assert.deepEqual(rcpt_doms, [])
done()
await new Promise((resolve) => {
this.plugin.update_sender(function (null1, null2, sender_dom, rcpt_doms) {
assert.equal(sender_dom, 'example.com')
assert.deepEqual(rcpt_doms, [])
resolve()
}, this.connection)
}, this.connection)
})

it('gets the recipient domain', function (done) {
it('gets the recipient domain', async function () {
this.connection.transaction.mail_from = new Address('<johndoe@example.com>')
this.connection.transaction.rcpt_to.push(new Address('<jane@test1.com>'))

this.plugin.update_sender(function (null1, null2, sender_dom, rcpt_doms) {
assert.equal(sender_dom, 'example.com')
assert.deepEqual(rcpt_doms, ['test1.com'])
done()
await new Promise((resolve) => {
this.plugin.update_sender(function (null1, null2, sender_dom, rcpt_doms) {
assert.equal(sender_dom, 'example.com')
assert.deepEqual(rcpt_doms, ['test1.com'])
resolve()
}, this.connection)
}, this.connection)
})

it('gets the recipient domains', function (done) {
it('gets the recipient domains', async function () {
this.connection.transaction.mail_from = new Address('<johndoe@example.com>')
this.connection.transaction.rcpt_to.push(new Address('<jane@test1.com>'))
this.connection.transaction.rcpt_to.push(new Address('<jane@test2.com>'))

this.plugin.update_sender(function (null1, null2, sender_dom, rcpt_doms) {
assert.equal(sender_dom, 'example.com')
assert.deepEqual(rcpt_doms, ['test1.com', 'test2.com'])
done()
await new Promise((resolve) => {
this.plugin.update_sender(function (null1, null2, sender_dom, rcpt_doms) {
assert.equal(sender_dom, 'example.com')
assert.deepEqual(rcpt_doms, ['test1.com', 'test2.com'])
resolve()
}, this.connection)
}, this.connection)
})
})
Expand Down Expand Up @@ -264,20 +286,22 @@ describe('get_recipient_domains_by_txn', function () {
})

describe('is_dkim_authenticated', function () {
beforeEach(function (done) {
beforeEach(async function () {
this.connection = new fixtures.connection.createConnection()
this.connection.init_transaction()

this.plugin = new fixtures.plugin('known-senders')
this.plugin.register()
this.plugin.init_redis_plugin(done)
await new Promise((resolve) => {
this.plugin.init_redis_plugin(resolve)
})
})

after(function () {
this.plugin.shutdown()
})

it('finds dkim results', function (done) {
it('finds dkim results', async function () {
const plugin = this.plugin
const connection = this.connection
const txn = this.connection.transaction
Expand All @@ -286,10 +310,12 @@ describe('is_dkim_authenticated', function () {
txn.results.push(plugin, { rcpt_ods: 'rcpt.com' })
txn.results.add({ name: 'dkim_verify' }, { pass: 'sender.com' })

plugin.is_dkim_authenticated(() => {
const res = txn.results.get(plugin.name)
assert.equal('dkim', res.auth)
done()
await new Promise((resolve) => {
plugin.is_dkim_authenticated(() => {
const res = txn.results.get(plugin.name)
assert.equal('dkim', res.auth)
resolve()
}, connection)
}, connection)
})
})
Loading