]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Final cases for selectors - register_* and functional for regexps
authorMikhail Galanin <mgalanin@mimecast.com>
Mon, 24 Sep 2018 12:23:17 +0000 (13:23 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Mon, 24 Sep 2018 12:23:17 +0000 (13:23 +0100)
test/functional/cases/220_http.robot
test/functional/configs/selector.conf [new file with mode: 0644]
test/functional/lua/selector_test.lua [new file with mode: 0644]
test/functional/messages/subject1.eml [new file with mode: 0644]
test/lua/unit/selectors.custom.lua [new file with mode: 0644]

index b1ac67bd91e85d757e002ef8ac779fce864c1161..b4292897e765a9e02c396884707c5d5ea4c1d0b8 100644 (file)
@@ -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 (file)
index 0000000..d4e872d
--- /dev/null
@@ -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 (file)
index 0000000..f1bb452
--- /dev/null
@@ -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 (file)
index 0000000..473bf98
--- /dev/null
@@ -0,0 +1,3 @@
+Subject: Some subject\r
+\r
+Hello\r
diff --git a/test/lua/unit/selectors.custom.lua b/test/lua/unit/selectors.custom.lua
new file mode 100644 (file)
index 0000000..cf82fe6
--- /dev/null
@@ -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: <whoknows@nowhere.com>
+To: <nobody@example.com>, <no-one@example.com>
+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
+]]