2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-07-26 01:01:27 +08:00
|
|
|
|
|
|
|
describe EmailValidator do
|
2015-06-15 09:28:50 +08:00
|
|
|
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
|
|
|
|
|
2013-07-26 01:01:27 +08:00
|
|
|
context "blocked email" do
|
|
|
|
it "doesn't add an error when email doesn't match a blocked email" do
|
2015-06-15 09:28:50 +08:00
|
|
|
expect(blocks?('sam@sam.com')).to eq(false)
|
2013-07-26 01:01:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds an error when email matches a blocked email" do
|
2015-06-15 09:28:50 +08:00
|
|
|
ScreenedEmail.create!(email: 'sam@sam.com', action_type: ScreenedEmail.actions[:block])
|
|
|
|
expect(blocks?('sam@sam.com')).to eq(true)
|
2017-11-12 08:43:18 +08:00
|
|
|
expect(blocks?('SAM@sam.com')).to eq(true)
|
2015-06-15 09:28:50 +08:00
|
|
|
end
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
it "blocks based on blocked_email_domains" do
|
|
|
|
SiteSetting.blocked_email_domains = "email.com|mail.com|e-mail.com"
|
2016-07-04 22:05:01 +08:00
|
|
|
expect(blocks?('sam@email.com')).to eq(true)
|
2017-11-12 08:43:18 +08:00
|
|
|
expect(blocks?('sam@EMAIL.com')).to eq(true)
|
2016-07-04 22:05:01 +08:00
|
|
|
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)
|
2013-07-26 01:01:27 +08:00
|
|
|
end
|
2017-10-03 17:23:18 +08:00
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
it "blocks based on allowed_email_domains" do
|
|
|
|
SiteSetting.allowed_email_domains = "googlemail.com|email.com"
|
2017-10-03 17:23:18 +08:00
|
|
|
expect(blocks?('sam@email.com')).to eq(false)
|
2017-11-12 08:43:18 +08:00
|
|
|
expect(blocks?('sam@EMAIL.com')).to eq(false)
|
2017-10-03 17:23:18 +08:00
|
|
|
expect(blocks?('sam@bob.email.com')).to eq(false)
|
|
|
|
expect(blocks?('sam@e-mail.com')).to eq(true)
|
|
|
|
expect(blocks?('sam@googlemail.com')).to eq(false)
|
2018-01-18 04:45:32 +08:00
|
|
|
expect(blocks?('sam@email.computers.are.evil.com')).to eq(true)
|
2017-10-03 17:23:18 +08:00
|
|
|
end
|
2013-07-26 01:01:27 +08:00
|
|
|
end
|
|
|
|
|
2020-04-01 02:29:15 +08:00
|
|
|
context "auto approve email domains" do
|
|
|
|
it "works as expected" do
|
|
|
|
SiteSetting.auto_approve_email_domains = "example.com"
|
|
|
|
|
|
|
|
expect(EmailValidator.can_auto_approve_user?("foobar@example.com.fr")).to eq(false)
|
|
|
|
expect(EmailValidator.can_auto_approve_user?("foobar@example.com")).to eq(true)
|
|
|
|
end
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
it "returns false if domain not present in allowed_email_domains" do
|
|
|
|
SiteSetting.allowed_email_domains = "googlemail.com"
|
2020-04-01 02:29:15 +08:00
|
|
|
SiteSetting.auto_approve_email_domains = "example.com|googlemail.com"
|
|
|
|
|
|
|
|
expect(EmailValidator.can_auto_approve_user?("foobar@example.com")).to eq(false)
|
|
|
|
expect(EmailValidator.can_auto_approve_user?("foobar@googlemail.com")).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-21 17:00:45 +08:00
|
|
|
context '.email_regex' do
|
|
|
|
it 'should match valid emails' do
|
|
|
|
expect(!!('test@discourse.org' =~ EmailValidator.email_regex)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not match invalid emails' do
|
|
|
|
['testdiscourse.org', 'test@discourse.org; a@discourse.org', 'random'].each do |email|
|
|
|
|
expect(!!(email =~ EmailValidator.email_regex)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 01:01:27 +08:00
|
|
|
end
|