Refactoring: Migrate email_regex_test to RSpec

This commit is contained in:
Ryan Lue 2019-03-04 15:32:34 +01:00 committed by Martin Edenhofer
parent 365e8db792
commit 909f4b7eaf
2 changed files with 122 additions and 116 deletions

View file

@ -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

View file

@ -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