2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-21 07:30:26 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe UserEmail do
|
2020-06-02 22:13:25 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2017-12-21 07:30:26 -05:00
|
|
|
context "validation" do
|
|
|
|
it "allows only one primary email" do
|
|
|
|
expect {
|
2018-07-03 07:51:22 -04:00
|
|
|
Fabricate(:secondary_email, user: user, primary: true)
|
2017-12-21 07:30:26 -05:00
|
|
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple secondary emails" do
|
2020-07-16 08:51:30 -04:00
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
2020-07-16 04:50:41 -04:00
|
|
|
|
2017-12-21 07:30:26 -05:00
|
|
|
expect(user.user_emails.count).to eq 3
|
|
|
|
end
|
2020-06-02 22:13:25 -04:00
|
|
|
|
|
|
|
it "does not allow an invalid email" do
|
|
|
|
user_email = Fabricate.build(:user_email, user: user, email: "asjdaiosd")
|
|
|
|
expect(user_email.valid?).to eq(false)
|
|
|
|
expect(user_email.errors.details[:email].first[:error]).to eq(I18n.t("user.email.invalid"))
|
|
|
|
end
|
2017-12-21 07:30:26 -05:00
|
|
|
end
|
|
|
|
|
2021-11-24 04:30:06 -05:00
|
|
|
describe 'normalized_email' do
|
|
|
|
it 'checks if normalized email is unique' do
|
|
|
|
SiteSetting.normalize_emails = true
|
|
|
|
|
|
|
|
user_email = user.user_emails.create(email: "a.b+c@example.com", primary: false)
|
|
|
|
expect(user_email.normalized_email).to eq("ab@example.com")
|
|
|
|
expect(user_email).to be_valid
|
|
|
|
|
|
|
|
user_email = user.user_emails.create(email: "a.b+d@example.com", primary: false)
|
|
|
|
expect(user_email.normalized_email).to eq("ab@example.com")
|
|
|
|
expect(user_email).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not check uniqueness if email normalization is not enabled' do
|
|
|
|
SiteSetting.normalize_emails = false
|
|
|
|
|
|
|
|
user_email = user.user_emails.create(email: "a.b+c@example.com", primary: false)
|
|
|
|
expect(user_email.normalized_email).to eq("ab@example.com")
|
|
|
|
expect(user_email).to be_valid
|
|
|
|
|
|
|
|
user_email = user.user_emails.create(email: "a.b+d@example.com", primary: false)
|
|
|
|
expect(user_email.normalized_email).to eq("ab@example.com")
|
|
|
|
expect(user_email).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-21 07:30:26 -05:00
|
|
|
context "indexes" do
|
|
|
|
it "allows only one primary email" do
|
|
|
|
expect {
|
2018-07-03 07:51:22 -04:00
|
|
|
Fabricate.build(:secondary_email, user: user, primary: true).save(validate: false)
|
2017-12-21 07:30:26 -05:00
|
|
|
}.to raise_error(ActiveRecord::RecordNotUnique)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple secondary emails" do
|
2018-07-03 07:51:22 -04:00
|
|
|
Fabricate.build(:secondary_email, user: user, primary: false).save(validate: false)
|
|
|
|
Fabricate.build(:secondary_email, user: user, primary: false).save(validate: false)
|
2017-12-21 07:30:26 -05:00
|
|
|
expect(user.user_emails.count).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|