mirror of
https://github.com/discourse/discourse.git
synced 2025-06-07 21:44:42 +08:00
FIX: incorrect logic in email blocker
if mail.com was blocked, email.com was automatically blocked
This commit is contained in:
@ -17,7 +17,7 @@ class EmailValidator < ActiveModel::EachValidator
|
|||||||
|
|
||||||
def email_in_restriction_setting?(setting, value)
|
def email_in_restriction_setting?(setting, value)
|
||||||
domains = setting.gsub('.', '\.')
|
domains = setting.gsub('.', '\.')
|
||||||
regexp = Regexp.new("@(.+\.)?(#{domains})", true)
|
regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
|
||||||
value =~ regexp
|
value =~ regexp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -2,21 +2,33 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe EmailValidator do
|
describe EmailValidator do
|
||||||
|
|
||||||
let(:record) { Fabricate.build(:user, email: "bad@spamclub.com") }
|
let(:record) { }
|
||||||
let(:validator) { described_class.new({attributes: :email}) }
|
let(:validator) { described_class.new({attributes: :email}) }
|
||||||
subject(:validate) { validator.validate_each(record,:email,record.email) }
|
subject(:validate) { validator.validate_each(record,:email,record.email) }
|
||||||
|
|
||||||
|
def blocks?(email)
|
||||||
|
user = Fabricate.build(:user, email: email)
|
||||||
|
validator = EmailValidator.new(attributes: :email)
|
||||||
|
validator.validate_each(user, :email, user.email)
|
||||||
|
user.errors[:email].present?
|
||||||
|
end
|
||||||
|
|
||||||
context "blocked email" do
|
context "blocked email" do
|
||||||
it "doesn't add an error when email doesn't match a blocked email" do
|
it "doesn't add an error when email doesn't match a blocked email" do
|
||||||
ScreenedEmail.stubs(:should_block?).with(record.email).returns(false)
|
expect(blocks?('sam@sam.com')).to eq(false)
|
||||||
validate
|
|
||||||
expect(record.errors[:email]).not_to be_present
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "adds an error when email matches a blocked email" do
|
it "adds an error when email matches a blocked email" do
|
||||||
ScreenedEmail.stubs(:should_block?).with(record.email).returns(true)
|
ScreenedEmail.create!(email: 'sam@sam.com', action_type: ScreenedEmail.actions[:block])
|
||||||
validate
|
expect(blocks?('sam@sam.com')).to eq(true)
|
||||||
expect(record.errors[:email]).to be_present
|
end
|
||||||
|
|
||||||
|
it "blocks based on email_domains_blacklist" do
|
||||||
|
SiteSetting.email_domains_blacklist = "email.com|mail.com|e-mail.com"
|
||||||
|
expect(blocks?('sam@email.com')).to eq(true)
|
||||||
|
expect(blocks?('sam@bob.email.com')).to eq(true)
|
||||||
|
expect(blocks?('sam@e-mail.com')).to eq(true)
|
||||||
|
expect(blocks?('sam@googlemail.com')).to eq(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user