diff --git a/.codeclimate.yml b/.codeclimate.yml index 75cf8a1..49f182d 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -1,9 +1,12 @@ -engines: +engines: eslint: enabled: true - channel: "eslint-3" + channel: "eslint-6" config: - config: ".eslintrc.json" + config: ".eslintrc.yaml" + checks: + complexity: + enabled: false ratings: paths: diff --git a/.github/workflows/ci-test-win.yml b/.github/workflows/ci-test-win.yml new file mode 100644 index 0000000..8c2686a --- /dev/null +++ b/.github/workflows/ci-test-win.yml @@ -0,0 +1,37 @@ +name: CI Tests - Windows + +on: [ push, pull_request ] + +# no docker/images support on Windows (currently), so run w/o Redis +# also, stack run commands so test doesn't begin before install completes + +jobs: + + ci-test-win: + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ windows-latest ] + node-version: [10.x, 12.x, 13.x] + fail-fast: false + + steps: + - uses: actions/checkout@v1 + name: Checkout haraka-plugin-access + with: + fetch-depth: 1 + + - uses: actions/setup-node@v1 + name: Use Node.js ${{ matrix.node-version }} + with: + node-version: ${{ matrix.node-version }} + + - name: npm install and test + run: | + npm install + npm run test + + env: + CI: true diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml new file mode 100644 index 0000000..d87066f --- /dev/null +++ b/.github/workflows/ci-test.yml @@ -0,0 +1,35 @@ +name: CI Tests + +on: [ push, pull_request ] + +jobs: + + ci-test: + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ ubuntu-latest ] + node-version: [10.x, 12.x] + fail-fast: false + + steps: + - uses: actions/checkout@v1 + name: Checkout haraka-plugin-access + with: + fetch-depth: 1 + + - uses: actions/setup-node@v1 + name: Use Node.js ${{ matrix.node-version }} + with: + node-version: ${{ matrix.node-version }} + + - name: npm install + run: npm install + + - name: Run test suite + run: npm run test + + env: + CI: true diff --git a/.github/workflows/coveralls.yml b/.github/workflows/coveralls.yml new file mode 100644 index 0000000..1fdd213 --- /dev/null +++ b/.github/workflows/coveralls.yml @@ -0,0 +1,32 @@ +on: [ pull_request ] + +name: Test Coverage + +jobs: + + coverage: + name: Codecov + runs-on: ubuntu-latest + + steps: + + - uses: actions/checkout@master + name: Checkout haraka-plugin-access + with: + fetch-depth: 1 + + - name: Use Node.js 10 + uses: actions/setup-node@master + with: + node-version: 10.x + + - name: install, run + run: | + npm install + npm install --no-save nyc codecov + npm run cover + + - name: Coveralls + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.github_token }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..aa47cfa --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,33 @@ +name: Lint + +on: [ push, pull_request ] + +jobs: + + lint: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [ 12.x ] + + steps: + - uses: actions/checkout@v1 + name: Checkout haraka-plugin-access + with: + fetch-depth: 1 + + - uses: actions/setup-node@v1 + name: Use Node.js ${{ matrix.node-version }} + with: + node-version: ${{ matrix.node-version }} + + - name: npm install + run: npm install + + - name: Lint using eslint + run: npm run lint + + env: + CI: true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 6d15570..0000000 --- a/.travis.yml +++ /dev/null @@ -1,28 +0,0 @@ -language: node_js -node_js: - - "8" - - "10" - - "12" - -services: -# https://docs.travis-ci.com/user/database-setup/ -# - mongodb -# - elasticsearch -# - redis-server - -before_script: - -script: - - npm run lint - - npm test - -after_success: -# -# enable codecov, which doesn't currently work for plugins because -# plugins are run in vm.runInNewContext() -# -# - npm install istanbul codecov -# - npm run cover -# - ./node_modules/.bin/codecov - -sudo: false diff --git a/Changes.md b/Changes.md index 9a57f35..bac816e 100644 --- a/Changes.md +++ b/Changes.md @@ -1,4 +1,8 @@ +# 1.1.4 - 2020-04-09 + +- wrap from parsing in a try #20 + # 1.1.3 - 2018-11-16 - check if OD was found before attemping to use it diff --git a/README.md b/README.md index 7777dac..8228284 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ [![Build Status][ci-img]][ci-url] [![Code Climate][clim-img]][clim-url] -[![Greenkeeper badge][gk-img]][gk-url] -[![NPM][npm-img]][npm-url] [![Windows Build Status][ci-win-img]][ci-win-url] +[![NPM][npm-img]][npm-url] + # haraka-plugin-access - ACLs This plugin applies Access Control Lists during the connect, helo, mail, and @@ -222,7 +222,5 @@ OD is *bbc.co.uk*. [ci-win-url]: https://ci.appveyor.com/project/msimerson/haraka-plugin-access [clim-img]: https://codeclimate.com/github/haraka/haraka-plugin-access/badges/gpa.svg [clim-url]: https://codeclimate.com/github/haraka/haraka-plugin-access -[gk-img]: https://badges.greenkeeper.io/haraka/haraka-plugin-access.svg -[gk-url]: https://greenkeeper.io/ [npm-img]: https://nodei.co/npm/haraka-plugin-access.png [npm-url]: https://www.npmjs.com/package/haraka-plugin-access diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index edbfbe0..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,20 +0,0 @@ -environment: - nodejs_version: "8" - -install: - - ps: Install-Product node $env:nodejs_version - - npm install - -before_build: -build: off -after_build: - -before_test: - - node --version - - npm --version - -test_script: - - npm test - -after_test: - diff --git a/index.js b/index.js index 933afa1..9994c1d 100644 --- a/index.js +++ b/index.js @@ -386,7 +386,15 @@ exports.data_any = function (next, connection) { return next(); } - const hdr_addr = haddr.parse(hdr_from)[0]; + let hdr_addr; + try { + hdr_addr = haddr.parse(hdr_from)[0]; + } + catch (e) { + connection.transaction.results.add(plugin, {fail: `data(unparsable_from:${hdr_from})`}); + return next(); + } + if (!hdr_addr) { connection.transaction.results.add(plugin, {fail: `data(unparsable_from:${hdr_from})`}); return next(); @@ -439,11 +447,9 @@ exports.in_re_list = function (type, phase, address) { exports.in_file = function (file_name, address, connection) { const plugin = this; - // using indexOf on an array here is about 20x slower than testing against - // a key in an object + // using indexOf on an array here is about 20x slower than testing against a key in an object connection.logdebug(plugin, `checking ${address} against ${file_name}`); - return (plugin.config.get(file_name, 'list') - .indexOf(address) === -1) ? false : true; + return (plugin.config.get(file_name, 'list').indexOf(address) === -1) ? false : true; } exports.in_re_file = function (file_name, address) { @@ -462,7 +468,7 @@ exports.in_re_file = function (file_name, address) { exports.load_file = function (type, phase) { const plugin = this; if (!plugin.cfg.check[phase]) { - plugin.loginfo(plugin, `skipping ${plugin.cfg[type][phase]}`); + plugin.logdebug(plugin, `skipping ${plugin.cfg[type][phase]}`); return; } @@ -489,7 +495,7 @@ exports.load_file = function (type, phase) { exports.load_re_file = function (type, phase) { const plugin = this; if (!plugin.cfg.check[phase]) { - plugin.loginfo(plugin, `skipping ${plugin.cfg.re[type][phase]}`); + plugin.logdebug(plugin, `skipping ${plugin.cfg.re[type][phase]}`); return; } @@ -514,7 +520,7 @@ exports.load_re_file = function (type, phase) { exports.load_domain_file = function (type, phase) { const plugin = this; if (!plugin.cfg.check[phase]) { - plugin.loginfo(plugin, `skipping ${plugin.cfg[type][phase]}`); + plugin.logdebug(plugin, `skipping ${plugin.cfg[type][phase]}`); return; } diff --git a/package.json b/package.json index dd9296e..679d2dc 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "haraka-plugin-access", - "version": "1.1.3", + "version": "1.1.4", "description": "Haraka plugin for ACLs for email connections", "main": "index.js", "scripts": { - "lint": "./node_modules/.bin/eslint *.js test/**/*.js", - "lintfix": "./node_modules/.bin/eslint --fix *.js test/**/*.js", - "test": "./node_modules/.bin/nodeunit" + "lint": "npx eslint *.js test/*.js", + "lintfix": "npx eslint --fix *.js test/*.js", + "test": "npx mocha", + "cover": "NODE_ENV=cov npx nyc --reporter=lcovonly -x tests npm run test" }, "repository": { "type": "git", @@ -24,11 +25,10 @@ }, "homepage": "https://github.com/haraka/haraka-plugin-access#readme", "devDependencies": { - "eslint": ">=4", + "eslint": ">=6", "eslint-plugin-haraka": "*", "haraka-test-fixtures": "*", - "mocha": "*", - "nodeunit": "*" + "mocha": "*" }, "dependencies": { "address-rfc2822": "*", diff --git a/redress.sh b/redress.sh deleted file mode 100755 index e858af4..0000000 --- a/redress.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -if [ -z "$1" ]; then - echo "$0 something" - exit -fi - -sed -i '' -e "s/template/${1}/g" README.md - -sed -i '' \ - -e "s/template/${1}/g" \ - -e "s/template\.ini/$1.ini/" \ - test/index.js - -sed -i '' -e "s/template/${1}/g" package.json -sed -i '' \ - -e "s/_template/_${1}/g" \ - -e "s/template\.ini/$1.ini/" \ - index.js - -mv config/template.ini config/$1.ini - -git add package.json README.md index.js test/index.js -git commit -m "renamed template to $1" -npm run lint && npm test || exit 1 -rm redress.sh - -echo "success!" -echo "" -echo "Next Steps: update package.json and force push this onto your repo:" -echo "" -echo " \$EDITOR package.json" -echo " git push --set-upstream origin master -f" -echo "" diff --git a/test/.eslintrc.json b/test/.eslintrc.json deleted file mode 100644 index 8d978a0..0000000 --- a/test/.eslintrc.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "env": { - "mocha": true, - "node": true, - "es6": true - }, - "globals": { - "OK": true, - "CONT": true, - "DENY": true, - "DENYSOFT": true, - "DENYDISCONNECT": true, - "DENYSOFTDISCONNECT": true - } -} diff --git a/test/access.js b/test/access.js index 83080a0..03ce482 100644 --- a/test/access.js +++ b/test/access.js @@ -1,431 +1,462 @@ 'use strict'; -const path = require('path'); +const assert = require('assert') +const path = require('path') const Address = require('address-rfc2821').Address; const fixtures = require('haraka-test-fixtures'); -const _set_up = function (done) { +describe('in_list', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + done() + }) - this.plugin = new fixtures.plugin('access'); - this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); - - this.plugin.register(); - - this.connection = fixtures.connection.createConnection(); - this.connection.init_transaction(); - - done(); -} - -exports.in_list = { - setUp : _set_up, - 'white, mail': function (test) { + it('white, mail', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { white: { mail: 'test no file' }}; this.plugin.list = { white: { mail: list }}; - test.expect(3); - test.equal(true, this.plugin.in_list('white', 'mail', 'matt@exam.ple')); - test.equal(true, this.plugin.in_list('white', 'mail', 'matt@example.com')); - test.equal(false, this.plugin.in_list('white', 'mail', 'matt@non-exist')); - test.done(); - }, - 'white, mail, case': function (test) { + assert.equal(true, this.plugin.in_list('white', 'mail', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_list('white', 'mail', 'matt@example.com')); + assert.equal(false, this.plugin.in_list('white', 'mail', 'matt@non-exist')); + done(); + }) + + it('white, mail, case', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { white: { mail: 'test no file' }}; this.plugin.list = { white: { mail: list }}; - test.expect(1); - test.equal(true, this.plugin.in_list('white', 'mail', 'MATT@exam.ple')); - test.done(); - }, - 'white, rcpt': function (test) { + assert.equal(true, this.plugin.in_list('white', 'mail', 'MATT@exam.ple')); + done(); + }) + + it('white, rcpt', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { re: { white: { rcpt: 'test file name' }}}; this.plugin.list = { white: { rcpt: list }}; - test.expect(3); - test.equal(true, this.plugin.in_list('white', 'rcpt', 'matt@exam.ple')); - test.equal(true, this.plugin.in_list('white', 'rcpt', 'matt@example.com')); - test.equal(false, this.plugin.in_list('white', 'rcpt', 'matt@non-exist')); - test.done(); - }, - 'white, helo': function (test) { + assert.equal(true, this.plugin.in_list('white', 'rcpt', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_list('white', 'rcpt', 'matt@example.com')); + assert.equal(false, this.plugin.in_list('white', 'rcpt', 'matt@non-exist')); + done(); + }) + + it('white, helo', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { re: { white: { helo: 'test file name' }}}; this.plugin.list = { white: { helo: list }}; - test.expect(3); - test.equal(true, this.plugin.in_list('white', 'helo', 'matt@exam.ple')); - test.equal(true, this.plugin.in_list('white', 'helo', 'matt@example.com')); - test.equal(false, this.plugin.in_list('white', 'helo', 'matt@non-exist')); - test.done(); - }, - 'black, mail': function (test) { + assert.equal(true, this.plugin.in_list('white', 'helo', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_list('white', 'helo', 'matt@example.com')); + assert.equal(false, this.plugin.in_list('white', 'helo', 'matt@non-exist')); + done(); + }) + + it('black, mail', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { re: { black: { mail: 'test file name' }}}; this.plugin.list = { black: { mail: list }}; - test.expect(3); - test.equal(true, this.plugin.in_list('black', 'mail', 'matt@exam.ple')); - test.equal(true, this.plugin.in_list('black', 'mail', 'matt@example.com')); - test.equal(false, this.plugin.in_list('black', 'mail', 'matt@non-exist')); - test.done(); - }, - 'black, rcpt': function (test) { + assert.equal(true, this.plugin.in_list('black', 'mail', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_list('black', 'mail', 'matt@example.com')); + assert.equal(false, this.plugin.in_list('black', 'mail', 'matt@non-exist')); + done(); + }) + + it('black, rcpt', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { re: { black: { rcpt: 'test file name' }}}; this.plugin.list = { black: { rcpt: list }}; - test.expect(3); - test.equal(true, this.plugin.in_list('black', 'rcpt', 'matt@exam.ple')); - test.equal(true, this.plugin.in_list('black', 'rcpt', 'matt@example.com')); - test.equal(false, this.plugin.in_list('black', 'rcpt', 'matt@non-exist')); - test.done(); - }, - 'black, helo': function (test) { + assert.equal(true, this.plugin.in_list('black', 'rcpt', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_list('black', 'rcpt', 'matt@example.com')); + assert.equal(false, this.plugin.in_list('black', 'rcpt', 'matt@non-exist')); + done(); + }) + + it('black, helo', function (done) { const list = {'matt@exam.ple':true,'matt@example.com':true}; this.plugin.cfg = { re: { black: { helo: 'test file name' }}}; this.plugin.list = { black: { helo: list }}; - test.expect(3); - test.equal(true, this.plugin.in_list('black', 'helo', 'matt@exam.ple')); - test.equal(true, this.plugin.in_list('black', 'helo', 'matt@example.com')); - test.equal(false, this.plugin.in_list('black', 'helo', 'matt@non-exist')); - test.done(); - }, -} - -exports.in_re_list = { - setUp : _set_up, - 'white, mail': function (test) { + assert.equal(true, this.plugin.in_list('black', 'helo', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_list('black', 'helo', 'matt@example.com')); + assert.equal(false, this.plugin.in_list('black', 'helo', 'matt@non-exist')); + done(); + }) +}) + +describe('in_re_list', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + done() + }) + + it('white, mail', function (done) { const list = ['.*exam.ple','.*example.com']; this.plugin.cfg = { re: { white: { mail: 'test file name' }}}; this.plugin.list_re = { white: { mail: new RegExp(`^(${list.join('|')})$`, 'i') }}; - test.expect(3); - test.equal(true, this.plugin.in_re_list('white', 'mail', 'matt@exam.ple')); - test.equal(true, this.plugin.in_re_list('white', 'mail', 'matt@example.com')); - test.equal(false, this.plugin.in_re_list('white', 'mail', 'matt@non-exist')); - test.done(); - }, - 'white, rcpt': function (test) { + assert.equal(true, this.plugin.in_re_list('white', 'mail', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_re_list('white', 'mail', 'matt@example.com')); + assert.equal(false, this.plugin.in_re_list('white', 'mail', 'matt@non-exist')); + done(); + }) + + it('white, rcpt', function (done) { const list = ['.*exam.ple','.*example.com']; this.plugin.cfg = { re: { white: { rcpt: 'test file name' }}}; this.plugin.list_re = { white: { rcpt: new RegExp(`^(${list.join('|')})$`, 'i') }}; - test.expect(3); - test.equal(true, this.plugin.in_re_list('white', 'rcpt', 'matt@exam.ple')); - test.equal(true, this.plugin.in_re_list('white', 'rcpt', 'matt@example.com')); - test.equal(false, this.plugin.in_re_list('white', 'rcpt', 'matt@non-exist')); - test.done(); - }, - 'white, helo': function (test) { + assert.equal(true, this.plugin.in_re_list('white', 'rcpt', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_re_list('white', 'rcpt', 'matt@example.com')); + assert.equal(false, this.plugin.in_re_list('white', 'rcpt', 'matt@non-exist')); + done(); + }) + + it('white, helo', function (done) { const list = ['.*exam.ple','.*example.com']; this.plugin.cfg = { re: { white: { helo: 'test file name' }}}; this.plugin.list_re = { white: { helo: new RegExp(`^(${list.join('|')})$`, 'i') }}; - test.expect(3); - test.equal(true, this.plugin.in_re_list('white', 'helo', 'matt@exam.ple')); - test.equal(true, this.plugin.in_re_list('white', 'helo', 'matt@example.com')); - test.equal(false, this.plugin.in_re_list('white', 'helo', 'matt@non-exist')); - test.done(); - }, - 'black, mail': function (test) { + assert.equal(true, this.plugin.in_re_list('white', 'helo', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_re_list('white', 'helo', 'matt@example.com')); + assert.equal(false, this.plugin.in_re_list('white', 'helo', 'matt@non-exist')); + done(); + }) + + it('black, mail', function (done) { const list = ['.*exam.ple','.*example.com']; this.plugin.cfg = { re: { black: { mail: 'test file name' }}}; this.plugin.list_re = { black: { mail: new RegExp(`^(${list.join('|')})$`, 'i') }}; - test.expect(3); - test.equal(true, this.plugin.in_re_list('black', 'mail', 'matt@exam.ple')); - test.equal(true, this.plugin.in_re_list('black', 'mail', 'matt@example.com')); - test.equal(false, this.plugin.in_re_list('black', 'mail', 'matt@non-exist')); - test.done(); - }, - 'black, rcpt': function (test) { + assert.equal(true, this.plugin.in_re_list('black', 'mail', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_re_list('black', 'mail', 'matt@example.com')); + assert.equal(false, this.plugin.in_re_list('black', 'mail', 'matt@non-exist')); + done(); + }) + + it('black, rcpt', function (done) { const list = ['.*exam.ple','.*example.com']; this.plugin.cfg = { re: { black: { rcpt: 'test file name' }}}; this.plugin.list_re = { black: { rcpt: new RegExp(`^(${list.join('|')})$`, 'i') }}; - test.expect(3); - test.equal(true, this.plugin.in_re_list('black', 'rcpt', 'matt@exam.ple')); - test.equal(true, this.plugin.in_re_list('black', 'rcpt', 'matt@example.com')); - test.equal(false, this.plugin.in_re_list('black', 'rcpt', 'matt@non-exist')); - test.done(); - }, - 'black, helo': function (test) { + assert.equal(true, this.plugin.in_re_list('black', 'rcpt', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_re_list('black', 'rcpt', 'matt@example.com')); + assert.equal(false, this.plugin.in_re_list('black', 'rcpt', 'matt@non-exist')); + done(); + }) + + it('black, helo', function (done) { const list = ['.*exam.ple','.*example.com']; this.plugin.cfg = { re: { black: { helo: 'test file name' }}}; this.plugin.list_re = { black: { helo: new RegExp(`^(${list.join('|')})$`, 'i') }}; - test.expect(3); - test.equal(true, this.plugin.in_re_list('black', 'helo', 'matt@exam.ple')); - test.equal(true, this.plugin.in_re_list('black', 'helo', 'matt@example.com')); - test.equal(false, this.plugin.in_re_list('black', 'helo', 'matt@non-exist')); - test.done(); - }, -} - -exports.load_file = { - setUp : _set_up, - 'case normalizing': function (test) { - test.expect(3); + assert.equal(true, this.plugin.in_re_list('black', 'helo', 'matt@exam.ple')); + assert.equal(true, this.plugin.in_re_list('black', 'helo', 'matt@example.com')); + assert.equal(false, this.plugin.in_re_list('black', 'helo', 'matt@non-exist')); + done(); + }) +}) + +describe('load_file', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + done() + }) + + it('case normalizing', function (done) { console.log(this.plugin.config.root_path); this.plugin.load_file('white', 'rcpt'); - test.equal(true, this.plugin.in_list('white', 'rcpt', 'admin2@example.com')); - test.equal(true, this.plugin.in_list('white', 'rcpt', 'admin2@example.com')); // was ADMIN2@EXAMPLE.com - test.equal(true, this.plugin.in_list('white', 'rcpt', 'admin1@example.com')); // was admin3@EXAMPLE.com - test.done(); - } -} - -exports.load_re_file = { - setUp : _set_up, - 'whitelist': function (test) { - test.expect(4); + assert.equal(true, this.plugin.in_list('white', 'rcpt', 'admin2@example.com')); + assert.equal(true, this.plugin.in_list('white', 'rcpt', 'admin2@example.com')); // was ADMIN2@EXAMPLE.com + assert.equal(true, this.plugin.in_list('white', 'rcpt', 'admin1@example.com')); // was admin3@EXAMPLE.com + done(); + }) +}) + +describe('load_re_file', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + done() + }) + + it('whitelist', function (done) { this.plugin.load_re_file('white', 'mail'); - test.ok(this.plugin.list_re); + assert.ok(this.plugin.list_re); // console.log(this.plugin.temp); - test.equal(true, this.plugin.in_re_list('white', 'mail', 'list@harakamail.com')); - test.equal(false, this.plugin.in_re_list('white', 'mail', 'list@harail.com')); - test.equal(false, this.plugin.in_re_list('white', 'mail', 'LIST@harail.com')); - test.done(); - }, -} - -exports.in_file = { - setUp : _set_up, - 'in_file': function (test) { - test.expect(2); + assert.equal(true, this.plugin.in_re_list('white', 'mail', 'list@harakamail.com')); + assert.equal(false, this.plugin.in_re_list('white', 'mail', 'list@harail.com')); + assert.equal(false, this.plugin.in_re_list('white', 'mail', 'LIST@harail.com')); + done(); + }) +}) + +describe('in_file', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + this.connection = fixtures.connection.createConnection(); + this.connection.init_transaction(); + done() + }) + + it('in_file', function (done) { const file = 'mail_from.access.whitelist'; - test.equal(true, this.plugin.in_file(file, 'haraka@harakamail.com', this.connection)); - test.equal(false, this.plugin.in_file(file, 'matt@harakamail.com', this.connection)); - test.done(); - }, -} - -exports.in_re_file = { - setUp : _set_up, - 'in_re_file': function (test) { - test.expect(2); + assert.equal(true, this.plugin.in_file(file, 'haraka@harakamail.com', this.connection)); + assert.equal(false, this.plugin.in_file(file, 'matt@harakamail.com', this.connection)); + done(); + }) +}) + +describe('in_re_file', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + this.connection = fixtures.connection.createConnection(); + this.connection.init_transaction(); + done() + }) + + it('in_re_file', function (done) { const file = 'mail_from.access.whitelist_regex'; // console.log(this.plugin.cfg); - test.equal(true, this.plugin.in_re_file(file, 'list@harakamail.com')); - test.equal(false, this.plugin.in_re_file(file, 'matt@harkatamale.com')); - test.done(); - }, -} - -exports.rdns_access = { - setUp : _set_up, - 'no list': function (test) { - test.expect(2); - const cb = function (rc) { - // console.log(this.connection.results.get('access')); - test.equal(undefined, rc); - test.ok(this.connection.results.get('access').msg.length); - test.done(); - }.bind(this); + assert.equal(true, this.plugin.in_re_file(file, 'list@harakamail.com')); + assert.equal(false, this.plugin.in_re_file(file, 'matt@harkatamale.com')); + done(); + }) +}) + +describe('rdns_access', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + this.connection = fixtures.connection.createConnection(); + this.connection.init_transaction(); + done() + }) + + it('no list', function (done) { this.connection.remote.ip='1.1.1.1'; this.connection.remote.host='host.example.com'; - this.plugin.rdns_access(cb, this.connection); - }, - 'whitelist': function (test) { - test.expect(2); - const cb = function (rc) { + this.plugin.rdns_access((rc) => { // console.log(this.connection.results.get('access')); - test.equal(undefined, rc); - test.ok(this.connection.results.get('access').pass.length); - // test.ok(this.connection.results.has('access', 'pass', /white/)); - test.done(); - }.bind(this); + assert.equal(undefined, rc); + assert.ok(this.connection.results.get('access').msg.length); + done(); + }, this.connection); + }) + + it('whitelist', function (done) { this.connection.remote.ip='1.1.1.1'; this.connection.remote.host='host.example.com'; this.plugin.list.white.conn['host.example.com']=true; - this.plugin.rdns_access(cb, this.connection); - }, - 'blacklist': function (test) { - test.expect(3); - const cb = function (rc, msg) { - // console.log(this.connection.results.get('access')); - test.equal(DENYDISCONNECT, rc); - test.equal("host.example.com [1.1.1.1] You are not allowed to connect", msg); - test.ok(this.connection.results.get('access').fail.length); - test.done(); - }.bind(this); + this.plugin.rdns_access((rc) => { + assert.equal(undefined, rc); + assert.ok(this.connection.results.get('access').pass.length); + // assert.ok(this.connection.results.has('access', 'pass', /white/)); + done(); + }, this.connection); + }) + + it('blacklist', function (done) { this.connection.remote.ip='1.1.1.1'; this.connection.remote.host='host.example.com'; this.plugin.list.black.conn['host.example.com']=true; - this.plugin.rdns_access(cb, this.connection); - }, - 'blacklist regex': function (test) { - test.expect(3); - const cb = function (rc, msg) { - // console.log(this.connection.results.get('access')); - test.equal(DENYDISCONNECT, rc); - test.equal("host.antispam.com [1.1.1.1] You are not allowed to connect", msg); - test.ok(this.connection.results.get('access').fail.length); - test.done(); - }.bind(this); + this.plugin.rdns_access((rc, msg) => { + assert.equal(DENYDISCONNECT, rc); + assert.equal("host.example.com [1.1.1.1] You are not allowed to connect", msg); + assert.ok(this.connection.results.get('access').fail.length); + done(); + }, this.connection); + }) + + it('blacklist regex', function (done) { this.connection.remote.ip='1.1.1.1'; this.connection.remote.host='host.antispam.com'; const black = [ '.*spam.com' ]; this.plugin.list_re.black.conn = new RegExp(`^(${black.join('|')})$`, 'i'); - this.plugin.rdns_access(cb, this.connection); - }, -} - -exports.helo_access = { - setUp : _set_up, - 'no list': function (test) { - test.expect(2); - const cb = function (rc) { - const r = this.connection.results.get('access'); - test.equal(undefined, rc); - test.ok(r && r.msg && r.msg.length); - test.done(); - }.bind(this); + this.plugin.rdns_access( (rc, msg) => { + assert.equal(DENYDISCONNECT, rc); + assert.equal("host.antispam.com [1.1.1.1] You are not allowed to connect", msg); + assert.ok(this.connection.results.get('access').fail.length); + done(); + }, this.connection); + }) +}) + +describe('helo_access', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + this.connection = fixtures.connection.createConnection(); + done() + }) + + it('no list', function (done) { this.plugin.cfg.check.helo=true; - this.plugin.helo_access(cb, this.connection, 'host.example.com'); - }, - 'blacklisted regex': function (test) { - test.expect(2); - const cb = function (rc) { - test.equal(DENY, rc); + this.plugin.helo_access((rc) => { const r = this.connection.results.get('access'); - test.ok(r && r.fail && r.fail.length); - test.done(); - }.bind(this); + assert.equal(undefined, rc); + assert.ok(r && r.msg && r.msg.length); + done(); + }, this.connection, 'host.example.com'); + }) + + it('blacklisted regex', function (done) { const black = [ '.*spam.com' ]; this.plugin.list_re.black.helo = new RegExp(`^(${black.join('|')})$`, 'i'); this.plugin.cfg.check.helo=true; - this.plugin.helo_access(cb, this.connection, 'bad.spam.com'); - }, -} - -exports.mail_from_access = { - setUp : _set_up, - 'no lists populated': function (test) { - test.expect(2); - const cb = function (rc) { - test.equal(undefined, rc); - test.ok(this.connection.transaction.results.get('access').msg.length); - test.done(); - }.bind(this); - this.plugin.mail_from_access(cb, this.connection, [new Address('')]); - }, - 'whitelisted addr': function (test) { - test.expect(2); - const cb = function (rc) { - test.equal(undefined, rc); - test.ok(this.connection.transaction.results.get('access').pass.length); - test.done(); - }.bind(this); + this.plugin.helo_access((rc) => { + assert.equal(DENY, rc); + const r = this.connection.results.get('access'); + assert.ok(r && r.fail && r.fail.length); + done(); + }, this.connection, 'bad.spam.com'); + }) +}) + +describe('mail_from_access', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + this.connection = fixtures.connection.createConnection(); + this.connection.init_transaction(); + done() + }) + + it('no lists populated', function (done) { + this.plugin.mail_from_access( (rc) => { + assert.equal(undefined, rc); + assert.ok(this.connection.transaction.results.get('access').msg.length); + done(); + }, this.connection, [new Address('')]); + }) + + it('whitelisted addr', function (done) { this.plugin.list.white.mail['list@harakamail.com']=true; - this.plugin.mail_from_access(cb, this.connection, [new Address('')]); - }, - 'blacklisted addr': function (test) { - test.expect(2); - const cb = function (rc) { - test.equal(DENY, rc); - test.ok(this.connection.transaction.results.get('access').fail.length); - test.done(); - }.bind(this); + this.plugin.mail_from_access( (rc) => { + assert.equal(undefined, rc); + assert.ok(this.connection.transaction.results.get('access').pass.length); + done(); + }, this.connection, [new Address('')]); + }) + + it('blacklisted addr', function (done) { this.plugin.list.black.mail['list@badmail.com']=true; - this.plugin.mail_from_access(cb, this.connection, [new Address('')]); - }, - 'blacklisted domain': function (test) { - test.expect(2); - const cb = function (rc) { - test.equal(DENY, rc); - test.ok(this.connection.transaction.results.get('access').fail.length); - test.done(); - }.bind(this); + this.plugin.mail_from_access( (rc) => { + assert.equal(DENY, rc); + assert.ok(this.connection.transaction.results.get('access').fail.length); + done(); + }, this.connection, [new Address('')]); + }) + + it('blacklisted domain', function (done) { const black = [ '.*@spam.com' ]; this.plugin.list_re.black.mail = new RegExp(`^(${black.join('|')})$`, 'i'); - this.plugin.mail_from_access(cb, this.connection, [new Address('')]); - }, - 'blacklisted domain, white addr': function (test) { - test.expect(2); - const cb = function (rc) { - test.equal(undefined, rc); - test.ok(this.connection.transaction.results.get('access').pass.length); - test.done(); - }.bind(this); + this.plugin.mail_from_access( (rc) => { + assert.equal(DENY, rc); + assert.ok(this.connection.transaction.results.get('access').fail.length); + done(); + }, this.connection, [new Address('')]); + }) + + it('blacklisted domain, white addr', function (done) { this.plugin.list.white.mail['special@spam.com']=true; const black = [ '.*@spam.com' ]; this.plugin.list_re.black.mail = new RegExp(`^(${black.join('|')})$`, 'i'); - this.plugin.mail_from_access(cb, this.connection, [new Address('')]); - }, -} - -exports.rcpt_to_access = { - setUp : _set_up, - 'no lists populated': function (test) { - test.expect(2); + this.plugin.mail_from_access( (rc) => { + assert.equal(undefined, rc); + assert.ok(this.connection.transaction.results.get('access').pass.length); + done(); + }, this.connection, [new Address('')]); + }) +}) + +describe('rcpt_to_access', function () { + beforeEach(function (done) { + this.plugin = new fixtures.plugin('access'); + this.plugin.config = this.plugin.config.module_config(path.resolve(__dirname)); + this.plugin.register(); + this.connection = fixtures.connection.createConnection(); + this.connection.init_transaction(); + done() + }) + + it('no lists populated', function (done) { const cb = function (rc) { - test.equal(undefined, rc); - test.ok(this.connection.transaction.results.get('access').msg.length); - test.done(); + assert.equal(undefined, rc); + assert.ok(this.connection.transaction.results.get('access').msg.length); + done(); }.bind(this); this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, - 'whitelisted addr': function (test) { - test.expect(4); + }) + + it('whitelisted addr', function (done) { let calls = 0; const cb = function (rc) { - test.equal(undefined, rc); - test.ok(this.connection.transaction.results.get('access').pass.length); + assert.equal(undefined, rc); + assert.ok(this.connection.transaction.results.get('access').pass.length); if (++calls == 2) { - test.done(); + done(); } }.bind(this); this.plugin.list.white.rcpt['user@example.com']=true; this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, - 'whitelisted addr, accept enabled': function (test) { - test.expect(2); + }) + + it('whitelisted addr, accept enabled', function (done) { const cb = function (rc) { - test.equal(OK, rc); - test.ok(this.connection.transaction.results.get('access').pass.length); - test.done(); + assert.equal(OK, rc); + assert.ok(this.connection.transaction.results.get('access').pass.length); + done(); }.bind(this); this.plugin.cfg.rcpt.accept=true; this.plugin.list.white.rcpt['user@example.com']=true; this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, - 'regex whitelisted addr, accept enabled': function (test) { - test.expect(2); + }) + + it('regex whitelisted addr, accept enabled', function (done) { const cb = function (rc) { - test.equal(OK, rc); - test.ok(this.connection.transaction.results.get('access').pass.length); - test.done(); + assert.equal(OK, rc); + assert.ok(this.connection.transaction.results.get('access').pass.length); + done(); }.bind(this); this.plugin.cfg.rcpt.accept=true; this.plugin.list_re.white.rcpt = new RegExp(`^user@example.com$`, 'i'); this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, - 'blacklisted addr': function (test) { - test.expect(2); + }) + + it('blacklisted addr', function (done) { const cb = function (rc) { - test.equal(DENY, rc); - test.ok(this.connection.transaction.results.get('access').fail.length); - test.done(); + assert.equal(DENY, rc); + assert.ok(this.connection.transaction.results.get('access').fail.length); + done(); }.bind(this); this.plugin.list.black.rcpt['user@badmail.com']=true; this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, - 'blacklisted domain': function (test) { - test.expect(2); + }) + + it('blacklisted domain', function (done) { const cb = function (rc) { - test.equal(DENY, rc); - test.ok(this.connection.transaction.results.get('access').fail.length); - test.done(); + assert.equal(DENY, rc); + assert.ok(this.connection.transaction.results.get('access').fail.length); + done(); }.bind(this); const black = [ '.*@spam.com' ]; this.plugin.list_re.black.rcpt = new RegExp(`^(${black.join('|')})$`, 'i'); this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, - 'blacklisted domain, white addr': function (test) { - test.expect(2); + }) + + it('blacklisted domain, white addr', function (done) { const cb = function (rc) { - test.equal(undefined, rc); - test.ok(this.connection.transaction.results.get('access').pass.length); - test.done(); + assert.equal(undefined, rc); + assert.ok(this.connection.transaction.results.get('access').pass.length); + done(); }.bind(this); this.plugin.list.white.rcpt['special@spam.com'] = true; const black = [ '.*@spam.com' ]; this.plugin.list_re.black.rcpt = new RegExp(`^(${black.join('|')})$`, 'i'); this.plugin.rcpt_to_access(cb, this.connection, [new Address('')]); - }, -} + }) +})