summaryrefslogtreecommitdiff
path: root/spec/models/customer_relations/issue_contact_spec.rb
blob: c6373fddbfbb30aabf050ca4a68d59be87e5ec93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CustomerRelations::IssueContact do
  let_it_be(:issue_contact, reload: true) { create(:issue_customer_relations_contact) }
  let_it_be(:group) { create(:group) }
  let_it_be(:subgroup) { create(:group, parent: group) }
  let_it_be(:project) { create(:project, group: subgroup) }
  let_it_be(:issue) { create(:issue, project: project) }

  subject { issue_contact }

  it { expect(subject).to be_valid }

  describe 'associations' do
    it { is_expected.to belong_to(:issue).required }
    it { is_expected.to belong_to(:contact).required }
  end

  describe 'factory' do
    let(:built) { build(:issue_customer_relations_contact) }
    let(:stubbed) { build_stubbed(:issue_customer_relations_contact) }
    let(:created) { create(:issue_customer_relations_contact) }

    let(:contact) { build(:contact, group: group) }
    let(:for_issue) { build(:issue_customer_relations_contact, :for_issue, issue: issue) }
    let(:for_contact) { build(:issue_customer_relations_contact, :for_contact, contact: contact) }

    it 'uses objects from the same group', :aggregate_failures do
      expect(stubbed.contact.group).to eq(stubbed.issue.project.group)
      expect(built.contact.group).to eq(built.issue.project.group)
      expect(created.contact.group).to eq(created.issue.project.group)
    end

    it 'builds using the same group', :aggregate_failures do
      expect(for_issue.contact.group).to eq(subgroup)
      expect(for_contact.issue.project.group).to eq(group)
    end
  end

  describe 'validation' do
    it 'fails when the contact group does not belong to the issue group or ancestors' do
      built = build(:issue_customer_relations_contact, issue: create(:issue), contact: create(:contact))

      expect(built).not_to be_valid
    end

    it 'succeeds when the contact group is the same as the issue group' do
      built = build(:issue_customer_relations_contact, issue: create(:issue, project: project), contact: create(:contact, group: subgroup))

      expect(built).to be_valid
    end

    it 'succeeds when the contact group is an ancestor of the issue group' do
      built = build(:issue_customer_relations_contact, issue: create(:issue, project: project), contact: create(:contact, group: group))

      expect(built).to be_valid
    end
  end

  describe '#self.find_contact_ids_by_emails' do
    let_it_be(:for_issue) { create_list(:issue_customer_relations_contact, 2, :for_issue, issue: issue) }
    let_it_be(:not_for_issue) { create_list(:issue_customer_relations_contact, 2) }

    it 'returns ids of contacts from issue' do
      contact_ids = described_class.find_contact_ids_by_emails(issue.id, for_issue.map(&:contact).pluck(:email))

      expect(contact_ids).to match_array(for_issue.pluck(:contact_id))
    end

    it 'does not return ids of contacts from other issues' do
      contact_ids = described_class.find_contact_ids_by_emails(issue.id, not_for_issue.map(&:contact).pluck(:email))

      expect(contact_ids).to be_empty
    end

    it 'raises ArgumentError when called with too many emails' do
      too_many_emails = described_class::MAX_PLUCK + 1
      expect { described_class.find_contact_ids_by_emails(issue.id, Array(0..too_many_emails)) }.to raise_error(ArgumentError)
    end
  end
end