2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-21 20:30:26 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe UserEmail do
|
2020-06-03 10:13:25 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2017-12-21 20:30:26 +08:00
|
|
|
context "validation" do
|
|
|
|
it "allows only one primary email" do
|
|
|
|
expect {
|
2018-07-03 19:51:22 +08:00
|
|
|
Fabricate(:secondary_email, user: user, primary: true)
|
2017-12-21 20:30:26 +08:00
|
|
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple secondary emails" do
|
2018-07-03 19:51:22 +08:00
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
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
|
|
|
|
|
|
|
|
context "indexes" do
|
|
|
|
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
|
|
|
|
end
|