dmail filters: fix filters being case sensitive.

This commit is contained in:
evazion
2018-09-20 19:51:13 -05:00
parent 03abbd0683
commit 237ab9b782
2 changed files with 20 additions and 1 deletions

View File

@@ -1,4 +1,6 @@
class DmailFilter < ApplicationRecord
extend Memoist
belongs_to :user
validates_presence_of :user
before_validation :initialize_user
@@ -18,6 +20,9 @@ class DmailFilter < ApplicationRecord
end
def regexp
@regexp ||= /\b#{Regexp.union(words.split(/[[:space:]]+/))}\b/
union = words.split(/[[:space:]]+/).map { |word| Regexp.escape(word) }.join("|")
/\b#{union}\b/i
end
memoize :regexp
end

View File

@@ -28,6 +28,11 @@ class DmailFilterTest < ActiveSupport::TestCase
create_dmail("okay", "banned")
assert_equal(true, @receiver.dmails.last.is_read?)
end
should "be case insensitive" do
create_dmail("Banned.", "okay")
assert_equal(true, @receiver.dmails.last.is_read?)
end
end
context "a dmail filter for a user name" do
@@ -40,4 +45,13 @@ class DmailFilterTest < ActiveSupport::TestCase
assert_equal(true, @receiver.dmails.last.is_read?)
end
end
context "a dmail filter containing multiple words" do
should "filter dmails containing any of the words" do
@receiver.create_dmail_filter(words: "foo bar spam")
create_dmail("this is a test (not *SPAM*)", "hello world")
assert_equal(true, @receiver.dmails.last.is_read?)
end
end
end