mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Merge pull request #4618 from tgxworld/fix_invalid_emails
FIX: Don't allow invalid email to be saved.
This commit is contained in:
@@ -32,4 +32,16 @@ describe EmailValidator do
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
end
|
||||
|
||||
6
spec/fabricators/email_change_request_fabricator.rb
Normal file
6
spec/fabricators/email_change_request_fabricator.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
Fabricator(:email_change_request) do
|
||||
user
|
||||
old_email { sequence(:old_email) { |i| "bruce#{i}@wayne.com" } }
|
||||
new_email { sequence(:new_email) { |i| "super#{i}@man.com" } }
|
||||
change_state EmailChangeRequest.states[:authorizing_old]
|
||||
end
|
||||
14
spec/models/email_change_request_spec.rb
Normal file
14
spec/models/email_change_request_spec.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe EmailChangeRequest do
|
||||
context 'validations' do
|
||||
describe '#new_email' do
|
||||
describe 'when email is invalid' do
|
||||
it 'should not be valid' do
|
||||
email_change_request = Fabricate.build(:email_change_request, new_email: 'testdiscourse.org')
|
||||
expect(email_change_request).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,8 +3,29 @@ require_dependency 'user'
|
||||
|
||||
describe User do
|
||||
|
||||
it { is_expected.to validate_presence_of :username }
|
||||
it { is_expected.to validate_presence_of :email }
|
||||
context 'validations' do
|
||||
it { is_expected.to validate_presence_of :username }
|
||||
|
||||
describe 'emails' do
|
||||
let(:user) { Fabricate.build(:user) }
|
||||
|
||||
it { is_expected.to validate_presence_of :email }
|
||||
|
||||
describe 'when record has a valid email' do
|
||||
it "should be valid" do
|
||||
user.email = 'test@gmail.com'
|
||||
expect(user).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe 'when record has an invalid email' do
|
||||
it 'should not be valid' do
|
||||
user.email = 'test@gmailcom'
|
||||
expect(user).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#count_by_signup_date' do
|
||||
before(:each) do
|
||||
|
||||
@@ -28,14 +28,5 @@ describe UserActivator do
|
||||
expect(user.email_tokens.last.created_at).to be_within_one_second_of(Time.zone.now)
|
||||
end
|
||||
|
||||
it "escapes the email in the message" do
|
||||
user = Fabricate(:user, email: '<strike>eviltrout@example.com</strike>')
|
||||
activator = EmailActivator.new(user, nil, nil, nil)
|
||||
msg = activator.activate
|
||||
|
||||
expect(msg).to match(/eviltrout@example.com/)
|
||||
expect(msg).not_to match(/<strike>/)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user