2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UserEmail do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2020-06-03 10:13:25 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Validations" do
|
2017-12-21 20:30:26 +08:00
|
|
|
it "allows only one primary email" do
|
2018-07-03 19:51:22 +08:00
|
|
|
expect { Fabricate(:secondary_email, user: user, primary: true) }.to raise_error(
|
2017-12-21 20:30:26 +08:00
|
|
|
ActiveRecord::RecordInvalid,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple secondary emails" do
|
2020-07-16 20:51:30 +08:00
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
2020-07-16 16:50:41 +08:00
|
|
|
|
2017-12-21 20:30:26 +08:00
|
|
|
expect(user.user_emails.count).to eq 3
|
|
|
|
end
|
2020-06-03 10:13:25 +08: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 20:30:26 +08:00
|
|
|
end
|
|
|
|
|
2021-11-24 17:30:06 +08: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
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Indexes" do
|
2017-12-21 20:30:26 +08:00
|
|
|
it "allows only one primary email" do
|
|
|
|
expect {
|
2018-07-03 19:51:22 +08:00
|
|
|
Fabricate.build(:secondary_email, user: user, primary: true).save(validate: false)
|
2017-12-21 20:30:26 +08:00
|
|
|
}.to raise_error(ActiveRecord::RecordNotUnique)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple secondary emails" do
|
2018-07-03 19:51:22 +08: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 20:30:26 +08:00
|
|
|
expect(user.user_emails.count).to eq 3
|
|
|
|
end
|
|
|
|
end
|
2024-12-16 19:48:25 +08:00
|
|
|
|
|
|
|
describe ".ensure_consistency!" do
|
|
|
|
context "when some users have no primary emails" do
|
|
|
|
it "creates primary emails for the users without a primary email" do
|
|
|
|
user_with_primary_email = Fabricate(:user)
|
|
|
|
user_without_primary_email = Fabricate(:user)
|
|
|
|
user_without_any_email = Fabricate(:user)
|
|
|
|
|
|
|
|
user_without_primary_email.primary_email.update_column(:primary, false)
|
|
|
|
user_without_any_email.user_emails.delete_all
|
|
|
|
original_email_of_user_with_primary_email = user_with_primary_email.primary_email.email
|
|
|
|
|
|
|
|
described_class.ensure_consistency!
|
|
|
|
|
|
|
|
expect(user_without_primary_email.reload.primary_email).to be_present
|
|
|
|
expect(user_without_any_email.reload.primary_email).to be_present
|
|
|
|
expect(
|
|
|
|
user_with_primary_email.reload.primary_email.email,
|
|
|
|
).to eq original_email_of_user_with_primary_email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-21 20:30:26 +08:00
|
|
|
end
|