diff --git a/spec/models/channel/filter/match/email_regex_spec.rb b/spec/models/channel/filter/match/email_regex_spec.rb new file mode 100644 index 000000000..88319319c --- /dev/null +++ b/spec/models/channel/filter/match/email_regex_spec.rb @@ -0,0 +1,122 @@ +require 'rails_helper' + +RSpec.describe Channel::Filter::Match::EmailRegex do + describe '.match' do + subject(:match) { Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: check_mode) } + let(:from) { 'foobar@foo.bar' } + + context 'in normal (error-suppressing) mode (default)' do + let(:check_mode) { false } + + context 'with exact match' do + let(:sender) { 'foobar@foo.bar' } + + it { is_expected.to be(true) } + end + + context 'with wildcard *' do + let(:sender) { '*' } + + it { is_expected.to be(true) } + end + + context 'with empty string' do + let(:sender) { '' } + + it { is_expected.to be(true) } + end + + context 'with `regex` operator' do + context 'and matching regex' do + let(:sender) { 'regex:foobar@.*' } + + it { is_expected.to be(true) } + end + + context 'and non-matching regex' do + let(:sender) { 'regex:nagios@.*' } + + it { is_expected.to be(false) } + end + + context 'and invalid regex (misused ? repeat operator)' do + let(:sender) { 'regex:??' } + + it { is_expected.to be(false) } + end + + context 'and invalid regex (misused ? repeat operator)' do + let(:sender) { 'regex:*' } + + it { is_expected.to be(false) } + end + + context 'and invalid regex (empty char class)' do + let(:sender) { 'regex:[]' } + + it { is_expected.to be(false) } + end + end + end + + context 'in check (error-raising) mode' do + let(:check_mode) { true } + + context 'with exact match' do + let(:sender) { 'foobar@foo.bar' } + + it { is_expected.to be(true) } + end + + context 'with wildcard *' do + let(:sender) { '*' } + + it { is_expected.to be(true) } + end + + context 'with empty string' do + let(:sender) { '' } + + it { is_expected.to be(true) } + end + + context 'with `regex` operator' do + context 'and matching regex' do + let(:sender) { 'regex:foobar@.*' } + + it { is_expected.to be(true) } + end + + context 'and non-matching regex' do + let(:sender) { 'regex:nagios@.*' } + + it { is_expected.to be(false) } + end + + context 'and invalid regex (misused ? repeat operator)' do + let(:sender) { 'regex:??' } + + it { expect { subject }.to raise_error(<<~ERR.chomp) } + Can't use regex '??' on 'foobar@foo.bar': target of repeat operator is not specified: /??/i + ERR + end + + context 'and invalid regex (misused ? repeat operator)' do + let(:sender) { 'regex:*' } + + it { expect { subject }.to raise_error(<<~ERR.chomp) } + Can't use regex '*' on 'foobar@foo.bar': target of repeat operator is not specified: /*/i + ERR + end + + context 'and invalid regex (empty char class)' do + let(:sender) { 'regex:[]' } + + it { expect { subject }.to raise_error(<<~ERR.chomp) } + Can't use regex '[]' on 'foobar@foo.bar': empty char-class: /[]/i + ERR + end + end + end + end +end diff --git a/test/unit/email_regex_test.rb b/test/unit/email_regex_test.rb deleted file mode 100644 index 0389d06e8..000000000 --- a/test/unit/email_regex_test.rb +++ /dev/null @@ -1,116 +0,0 @@ -require 'test_helper' - -class EmailRegexTest < ActiveSupport::TestCase - - test 'should be able to detect valid/invalid the regex filter' do - # check with exact email, check_mode = true - sender = 'foobar@foo.bar' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - assert_equal(true, regex) - - # check with exact email - sender = 'foobar@foo.bar' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(true, regex) - - # check with regex: filter check_mode = true - sender = 'regex:foobar@.*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - assert_equal(true, regex) - - # check with regex: filter - sender = 'regex:foobar@.*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(true, regex) - - # check regex with regex: filter - sender = 'regex:??' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(false, regex) - - # check regex with raise error (check_mode = true) - assert_raises("Can't use regex '??' on 'foobar@foo.bar'") do - sender = 'regex:??' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - end - - sender = 'regex:[]' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(false, regex) - - # check regex with raise error, (check_mode = true) - assert_raises("Can't use regex '[]' on 'foobar@foo.bar'") do - sender = 'regex:[]' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - end - - # check regex with empty field - sender = '{}' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(false, regex) - - # check regex with empty field and raise error (check_mode = true) - sender = '{}' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - assert_equal(false, regex) - - # check regex with empty field - sender = '' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(true, regex) - - # check regex with empty field - sender = '' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - assert_equal(true, regex) - - # check regex with regex: wildcard - sender = 'regex:*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(false, regex) - - # check regex with regex: wildcard and raise error (check_mode = true) - assert_raises("Can't use regex '*' on 'foobar@foo.bar'") do - sender = 'regex:*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - end - - # check email with wildcard - sender = '*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(true, regex) - - # check email with wildcard (check_mode = true) - sender = '*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - assert_equal(true, regex) - - # check email with a different sender - sender = 'regex:nagios@.*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false) - assert_equal(false, regex) - - # check email with a different sender with checkmode = true - sender = 'regex:nagios@.*' - from = 'foobar@foo.bar' - regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true) - assert_equal(false, regex) - end -end