1local msg
2context("Selectors test", function()
3  local rspamd_task = require "rspamd_task"
4  local logger = require "rspamd_logger"
5  local lua_selectors = require "lua_selectors"
6  local test_helper = require "rspamd_test_helper"
7  local cfg = rspamd_config
8  local task
9
10  test_helper.init_url_parser()
11
12  before(function()
13    local res
14    res,task = rspamd_task.load_from_string(msg, cfg)
15    if not res then
16      assert_true(false, "failed to load message")
17    end
18  end)
19
20  local function check_selector(selector_string)
21    local sels = lua_selectors.parse_selector(cfg, selector_string)
22    local elts = lua_selectors.process_selectors(task, sels)
23    return elts
24  end
25
26  test("custom selector", function()
27    lua_selectors.register_extractor(rspamd_config, "get_something", {
28      get_value = function(task, args) -- mandatory field
29        return 'simple value','string' -- result + type
30      end,
31      description = 'Sample extractor' -- optional
32    })
33
34    local elts = check_selector('get_something')
35    assert_not_nil(elts)
36    assert_rspamd_table_eq({actual = elts, expect = {'simple value'}})
37  end)
38
39  test("custom transform", function()
40    lua_selectors.register_extractor(rspamd_config, "get_something", {
41      get_value = function(task, args) -- mandatory field
42        return 'simple value','string' -- result + type
43      end,
44      description = 'Sample extractor' -- optional
45    })
46
47    lua_selectors.register_transform(rspamd_config, "append_string", {
48      types = {['string'] = true}, -- accepted types
49      process = function(input, type, args)
50        return input .. table.concat(args or {}),'string' -- result + type
51      end,
52      map_type = 'string', -- can be used in map like invocation, always return 'string' type
53      description = 'Adds all arguments to the input string'
54    })
55
56    local elts = check_selector('get_something.append_string(" and a simple tail")')
57    assert_not_nil(elts)
58    assert_rspamd_table_eq({actual = elts, expect = {'simple value and a simple tail'}})
59
60    local elts = check_selector('get_something.append_string(" and", " a", " simple", " nail")')
61    assert_not_nil(elts)
62    assert_rspamd_table_eq({actual = elts, expect = {'simple value and a simple nail'}})
63  end)
64end)
65
66
67--[=========[ *******************  message  ******************* ]=========]
68msg = [[
69From: <whoknows@nowhere.com>
70To: <nobody@example.com>, <no-one@example.com>
71Date: Wed, 19 Sep 2018 14:36:51 +0100 (BST)
72Subject: Test subject
73Content-Type: multipart/alternative;
74    boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
75
76--_000_6be055295eab48a5af7ad4022f33e2d0_
77Content-Type: text/plain; charset="utf-8"
78Content-Transfer-Encoding: base64
79
80Hello world
81]]
82