2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-07-25 13:01:27 -04:00
|
|
|
|
|
|
|
describe EmailValidator do
|
2015-06-14 21:28:50 -04: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-25 13:01:27 -04:00
|
|
|
context "blocked email" do
|
|
|
|
it "doesn't add an error when email doesn't match a blocked email" do
|
2015-06-14 21:28:50 -04:00
|
|
|
expect(blocks?('sam@sam.com')).to eq(false)
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds an error when email matches a blocked email" do
|
2015-06-14 21:28:50 -04:00
|
|
|
ScreenedEmail.create!(email: 'sam@sam.com', action_type: ScreenedEmail.actions[:block])
|
|
|
|
expect(blocks?('sam@sam.com')).to eq(true)
|
2017-11-11 19:43:18 -05:00
|
|
|
expect(blocks?('SAM@sam.com')).to eq(true)
|
2015-06-14 21:28:50 -04:00
|
|
|
end
|
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
it "blocks based on blocked_email_domains" do
|
|
|
|
SiteSetting.blocked_email_domains = "email.com|mail.com|e-mail.com"
|
2016-07-04 10:05:01 -04:00
|
|
|
expect(blocks?('sam@email.com')).to eq(true)
|
2017-11-11 19:43:18 -05:00
|
|
|
expect(blocks?('sam@EMAIL.com')).to eq(true)
|
2016-07-04 10:05:01 -04: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-25 13:01:27 -04:00
|
|
|
end
|
2017-10-03 05:23:18 -04:00
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
it "blocks based on allowed_email_domains" do
|
|
|
|
SiteSetting.allowed_email_domains = "googlemail.com|email.com"
|
2017-10-03 05:23:18 -04:00
|
|
|
expect(blocks?('sam@email.com')).to eq(false)
|
2017-11-11 19:43:18 -05:00
|
|
|
expect(blocks?('sam@EMAIL.com')).to eq(false)
|
2017-10-03 05:23:18 -04: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-17 15:45:32 -05:00
|
|
|
expect(blocks?('sam@email.computers.are.evil.com')).to eq(true)
|
2017-10-03 05:23:18 -04:00
|
|
|
end
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
|
2020-03-31 14:29:15 -04: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-26 20:23:54 -04:00
|
|
|
it "returns false if domain not present in allowed_email_domains" do
|
|
|
|
SiteSetting.allowed_email_domains = "googlemail.com"
|
2020-03-31 14:29:15 -04: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 04:00:45 -05: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-25 13:01:27 -04:00
|
|
|
end
|