2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-21 07:30:26 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
require_dependency 'user_email'
|
|
|
|
|
|
|
|
describe UserEmail do
|
|
|
|
context "validation" do
|
|
|
|
it "allows only one primary email" do
|
2019-04-23 05:49:05 -04:00
|
|
|
user = Fabricate(:user)
|
2017-12-21 07:30:26 -05:00
|
|
|
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
|
2019-04-23 05:49:05 -04:00
|
|
|
user = Fabricate(:user)
|
2018-07-03 07:51:22 -04:00
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
|
|
|
Fabricate(:secondary_email, user: user, primary: false)
|
2017-12-21 07:30:26 -05:00
|
|
|
expect(user.user_emails.count).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "indexes" do
|
|
|
|
it "allows only one primary email" do
|
2019-04-23 05:49:05 -04:00
|
|
|
user = Fabricate(:user)
|
2017-12-21 07:30:26 -05:00
|
|
|
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
|
2019-04-23 05:49:05 -04:00
|
|
|
user = Fabricate(:user)
|
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
|