diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-22 15:07:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-22 15:07:28 +0300 |
commit | 4220cf46a314ac1c4d88be13608752bc07bb28fb (patch) | |
tree | 169db13fd06c7622bbcf59b707d57d516f44e299 /spec/models/customer_relations/contact_spec.rb | |
parent | 4c7e34071eceb05a9ce271354c21de7487e4ff84 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/customer_relations/contact_spec.rb')
-rw-r--r-- | spec/models/customer_relations/contact_spec.rb | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/spec/models/customer_relations/contact_spec.rb b/spec/models/customer_relations/contact_spec.rb index 18896962261..86f868b269e 100644 --- a/spec/models/customer_relations/contact_spec.rb +++ b/spec/models/customer_relations/contact_spec.rb @@ -25,7 +25,7 @@ RSpec.describe CustomerRelations::Contact, type: :model do it { is_expected.to validate_length_of(:email).is_at_most(255) } it { is_expected.to validate_length_of(:description).is_at_most(1024) } - it { is_expected.to validate_uniqueness_of(:email).scoped_to(:group_id) } + it { is_expected.to validate_uniqueness_of(:email).case_insensitive.scoped_to(:group_id) } it_behaves_like 'an object with RFC3696 compliant email-formatted attributes', :email end @@ -87,6 +87,15 @@ RSpec.describe CustomerRelations::Contact, type: :model do too_many_emails = described_class::MAX_PLUCK + 1 expect { described_class.find_ids_by_emails(group, Array(0..too_many_emails)) }.to raise_error(ArgumentError) end + + it 'finds contacts regardless of email casing' do + new_contact = create(:contact, group: group, email: "UPPERCASE@example.com") + emails = [group_contacts[0].email.downcase, group_contacts[1].email.upcase, new_contact.email] + + contact_ids = described_class.find_ids_by_emails(group, emails) + + expect(contact_ids).to contain_exactly(group_contacts[0].id, group_contacts[1].id, new_contact.id) + end end describe '#self.exists_for_group?' do @@ -104,4 +113,33 @@ RSpec.describe CustomerRelations::Contact, type: :model do end end end + + describe '#self.move_to_root_group' do + let!(:old_root_group) { create(:group) } + let!(:contacts) { create_list(:contact, 4, group: old_root_group) } + let!(:project) { create(:project, group: old_root_group) } + let!(:issue) { create(:issue, project: project) } + let!(:issue_contact1) { create(:issue_customer_relations_contact, issue: issue, contact: contacts[0]) } + let!(:issue_contact2) { create(:issue_customer_relations_contact, issue: issue, contact: contacts[1]) } + let!(:new_root_group) { create(:group) } + let!(:dupe_contact1) { create(:contact, group: new_root_group, email: contacts[1].email) } + let!(:dupe_contact2) { create(:contact, group: new_root_group, email: contacts[3].email.upcase) } + + before do + old_root_group.update!(parent: new_root_group) + CustomerRelations::Contact.move_to_root_group(old_root_group) + end + + it 'moves contacts with unique emails and deletes the rest' do + expect(contacts[0].reload.group_id).to eq(new_root_group.id) + expect(contacts[2].reload.group_id).to eq(new_root_group.id) + expect { contacts[1].reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { contacts[3].reload }.to raise_error(ActiveRecord::RecordNotFound) + end + + it 'updates issue_contact.contact_id for dupes and leaves the rest untouched' do + expect(issue_contact1.reload.contact_id).to eq(contacts[0].id) + expect(issue_contact2.reload.contact_id).to eq(dupe_contact1.id) + end + end end |