Welcome to mirror list, hosted at ThFree Co, Russian Federation.

organization_spec.rb « customer_relations « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1833fcf538590e0be3df59bf5d6a3b32bcbbb369 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CustomerRelations::Organization, type: :model do
  let_it_be(:group) { create(:group) }

  describe 'associations' do
    it { is_expected.to belong_to(:group).with_foreign_key('group_id') }
  end

  describe 'validations' do
    subject { build(:organization) }

    it { is_expected.to validate_presence_of(:group) }
    it { is_expected.to validate_presence_of(:name) }
    it { is_expected.to validate_uniqueness_of(:name).case_insensitive.scoped_to([:group_id]) }
    it { is_expected.to validate_length_of(:name).is_at_most(255) }
    it { is_expected.to validate_length_of(:description).is_at_most(1024) }
  end

  describe '#root_group' do
    context 'when root group' do
      subject { build(:organization, group: group) }

      it { is_expected.to be_valid }
    end

    context 'when subgroup' do
      subject { build(:organization, group: create(:group, parent: group)) }

      it { is_expected.to be_invalid }
    end
  end

  describe '#name' do
    it 'strips name' do
      organization = described_class.new(name: '   GitLab   ')
      organization.valid?

      expect(organization.name).to eq('GitLab')
    end
  end

  describe '#find_by_name' do
    let!(:organiztion1) { create(:organization, group: group, name: 'Test') }
    let!(:organiztion2) { create(:organization, group: create(:group), name: 'Test') }

    it 'strips name' do
      expect(described_class.find_by_name(group.id, 'TEST')).to eq([organiztion1])
    end
  end

  describe '#self.move_to_root_group' do
    let!(:old_root_group) { create(:group) }
    let!(:organizations) { create_list(:organization, 4, group: old_root_group) }
    let!(:new_root_group) { create(:group) }
    let!(:contact1) { create(:contact, group: new_root_group, organization: organizations[0]) }
    let!(:contact2) { create(:contact, group: new_root_group, organization: organizations[1]) }

    let!(:dupe_organization1) { create(:organization, group: new_root_group, name: organizations[1].name) }
    let!(:dupe_organization2) { create(:organization, group: new_root_group, name: organizations[3].name.upcase) }

    before do
      old_root_group.update!(parent: new_root_group)
      CustomerRelations::Organization.move_to_root_group(old_root_group)
    end

    it 'moves organizations with unique names and deletes the rest' do
      expect(organizations[0].reload.group_id).to eq(new_root_group.id)
      expect(organizations[2].reload.group_id).to eq(new_root_group.id)
      expect { organizations[1].reload }.to raise_error(ActiveRecord::RecordNotFound)
      expect { organizations[3].reload }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'updates contact.organization_id for dupes and leaves the rest untouched' do
      expect(contact1.reload.organization_id).to eq(organizations[0].id)
      expect(contact2.reload.organization_id).to eq(dupe_organization1.id)
    end
  end

  describe '.search' do
    let_it_be(:organization_a) do
      create(
        :organization,
        group: group,
        name: "DEF",
        description: "ghi_st",
        state: "inactive"
      )
    end

    let_it_be(:organization_b) do
      create(
        :organization,
        group: group,
        name: "ABC_st",
        description: "JKL",
        state: "active"
      )
    end

    subject(:found_organizations) { group.organizations.search(search_term) }

    context 'when search term is empty' do
      let(:search_term) { "" }

      it 'returns all group organizations' do
        expect(found_organizations).to contain_exactly(organization_a, organization_b)
      end
    end

    context 'when search term is not empty' do
      context 'when searching for name' do
        let(:search_term) { "aBc" }

        it { is_expected.to contain_exactly(organization_b) }
      end

      context 'when searching for description' do
        let(:search_term) { "ghI" }

        it { is_expected.to contain_exactly(organization_a) }
      end

      context 'when searching for name and description' do
        let(:search_term) { "_st" }

        it { is_expected.to contain_exactly(organization_a, organization_b) }
      end
    end
  end

  describe '.search_by_state' do
    let_it_be(:organization_a) { create(:organization, group: group, state: "inactive") }
    let_it_be(:organization_b) { create(:organization, group: group, state: "active") }

    context 'when searching for organizations state' do
      it 'returns only inactive organizations' do
        expect(group.organizations.search_by_state(:inactive)).to contain_exactly(organization_a)
      end

      it 'returns only active organizations' do
        expect(group.organizations.search_by_state(:active)).to contain_exactly(organization_b)
      end
    end
  end

  describe '.sort_by_name' do
    let_it_be(:organization_a) { create(:organization, group: group, name: "c") }
    let_it_be(:organization_b) { create(:organization, group: group, name: "a") }
    let_it_be(:organization_c) { create(:organization, group: group, name: "b") }

    context 'when sorting the organizations' do
      it 'sorts them by name in ascendent order' do
        expect(group.organizations.sort_by_name).to eq([organization_b, organization_c, organization_a])
      end
    end
  end
end