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

rspamd: refactor complex condition into function #1840

Merged
merged 2 commits into from Mar 2, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions plugins/rspamd.js
Expand Up @@ -174,9 +174,8 @@ exports.hook_data_post = function (next, connection) {
}
if (cfg.soft_reject.enabled && r.data.default.action === 'soft reject') {
return callNext(DENYSOFT, DSN.sec_unauthorized(cfg.soft_reject.message, 451));
} else if (cfg.main.add_headers !== 'never' && (
cfg.main.add_headers === 'always' ||
(r.data.default.action === 'add header' && cfg.main.add_headers === 'sometimes'))) {
}
if (plugin.wants_headers_added(r.data)) {
plugin.add_headers(connection, r.data);
}
return callNext();
Expand All @@ -198,6 +197,17 @@ exports.hook_data_post = function (next, connection) {
});
};

exports.wants_headers_added = function (rspamd_data) {
var plugin = this;

if (plugin.cfg.main.add_headers === 'never') return false;
if (plugin.cfg.main.add_headers === 'always') return true;

// implicit add_headers=sometimes, based on rspamd response
if (rspamd_data.default.action === 'add header') return true;
return false;
};

exports.parse_response = function (rawData, connection) {
var plugin = this;

Expand Down
35 changes: 35 additions & 0 deletions tests/plugins/rspamd.js
Expand Up @@ -87,3 +87,38 @@ exports.add_headers = {
test.done();
}
};

exports.wants_headers_added = {
setUp : _set_up,
'wants no headers when add_headers=never': function (test) {
test.expect(1);
this.plugin.cfg.main.add_headers='never';
test.equal(
this.plugin.wants_headers_added({ default: { action: 'add header' }}),
false
);
test.done();
},
'always wants no headers when add_headers=always': function (test) {
test.expect(1);
this.plugin.cfg.main.add_headers='always';
test.equal(
this.plugin.wants_headers_added({ default: { action: 'beat it' }}),
true
);
test.done();
},
'wants headers when rspamd response indicates, add_headers=sometimes': function (test) {
test.expect(2);
this.plugin.cfg.main.add_headers='sometimes';
test.equal(
this.plugin.wants_headers_added({ default: { action: 'add header' }}),
true
);
test.equal(
this.plugin.wants_headers_added({ default: { action: 'brownlist' }}),
false
);
test.done();
}
}