From: Mikhail Galanin Date: Mon, 24 Sep 2018 12:23:17 +0000 (+0100) Subject: [Test] Final cases for selectors - register_* and functional for regexps X-Git-Tag: 1.8.0~2^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31270b9fa06db3e630a32fa68fa6cec505a66007;p=thirdparty%2Frspamd.git [Test] Final cases for selectors - register_* and functional for regexps --- diff --git a/test/functional/cases/220_http.robot b/test/functional/cases/220_http.robot index b1ac67bd91..b4292897e7 100644 --- a/test/functional/cases/220_http.robot +++ b/test/functional/cases/220_http.robot @@ -52,7 +52,6 @@ Http Teardown Normal Teardown Run Dummy Http - [Arguments] ${result} = Start Process ${TESTDIR}/util/dummy_http.py Wait Until Created /tmp/dummy_http.pid diff --git a/test/functional/configs/selector.conf b/test/functional/configs/selector.conf new file mode 100644 index 0000000000..d4e872d36b --- /dev/null +++ b/test/functional/configs/selector.conf @@ -0,0 +1,8 @@ +regexp { + CONFIG_SELECTOR_RE_RCPT_SUBJECT { + re = 'test=/test@user.com some subject/$', + score = 100500, + } +} + +lua = "${TESTDIR}/lua/selector_test.lua" diff --git a/test/functional/lua/selector_test.lua b/test/functional/lua/selector_test.lua new file mode 100644 index 0000000000..f1bb45269b --- /dev/null +++ b/test/functional/lua/selector_test.lua @@ -0,0 +1,6 @@ +rspamd_config:register_re_selector('test', 'user.lower;header(Subject).lower', ' ') + +config['regexp']['LUA_SELECTOR_RE'] = { + re = 'test=/^test@user\\.com some subject$/{selector}', + score = 100500, +} diff --git a/test/functional/messages/subject1.eml b/test/functional/messages/subject1.eml new file mode 100644 index 0000000000..473bf98b9d --- /dev/null +++ b/test/functional/messages/subject1.eml @@ -0,0 +1,3 @@ +Subject: Some subject + +Hello diff --git a/test/lua/unit/selectors.custom.lua b/test/lua/unit/selectors.custom.lua new file mode 100644 index 0000000000..cf82fe6a5d --- /dev/null +++ b/test/lua/unit/selectors.custom.lua @@ -0,0 +1,81 @@ +local msg +context("Selectors test", function() + local rspamd_task = require "rspamd_task" + local logger = require "rspamd_logger" + local lua_selectors = require "lua_selectors" + local test_helper = require "rspamd_test_helper" + local cfg = rspamd_config + local task + + test_helper.init_url_parser() + + before(function() + local res + res,task = rspamd_task.load_from_string(msg, cfg) + if not res then + assert_true(false, "failed to load message") + end + end) + + local function check_selector(selector_string) + local sels = lua_selectors.parse_selector(cfg, selector_string) + local elts = lua_selectors.process_selectors(task, sels) + return elts + end + + test("custom selector", function() + lua_selectors.register_extractor(rspamd_config, "get_something", { + get_value = function(task, args) -- mandatory field + return 'simple value','string' -- result + type + end, + description = 'Sample extractor' -- optional + }) + + local elts = check_selector('get_something') + assert_not_nil(elts) + assert_rspamd_table_eq({actual = elts, expect = {'simple value'}}) + end) + + test("custom transform", function() + lua_selectors.register_extractor(rspamd_config, "get_something", { + get_value = function(task, args) -- mandatory field + return 'simple value','string' -- result + type + end, + description = 'Sample extractor' -- optional + }) + + lua_selectors.register_transform(rspamd_config, "append_string", { + types = {['string'] = true}, -- accepted types + process = function(input, type, args) + return input .. table.concat(args or {}),'string' -- result + type + end, + map_type = 'string', -- can be used in map like invocation, always return 'string' type + description = 'Adds all arguments to the input string' + }) + + local elts = check_selector('get_something.append_string(" and a simple tail")') + assert_not_nil(elts) + assert_rspamd_table_eq({actual = elts, expect = {'simple value and a simple tail'}}) + + local elts = check_selector('get_something.append_string(" and", " a", " simple", " nail")') + assert_not_nil(elts) + assert_rspamd_table_eq({actual = elts, expect = {'simple value and a simple nail'}}) + end) +end) + + +--[=========[ ******************* message ******************* ]=========] +msg = [[ +From: +To: , +Date: Wed, 19 Sep 2018 14:36:51 +0100 (BST) +Subject: Test subject +Content-Type: multipart/alternative; + boundary="_000_6be055295eab48a5af7ad4022f33e2d0_" + +--_000_6be055295eab48a5af7ad4022f33e2d0_ +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: base64 + +Hello world +]]