debian-mirror-gitlab/spec/models/customer_relations/organization_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
6.3 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe CustomerRelations::Organization, type: :model do
2022-05-07 20:08:51 +05:30
let_it_be(:group) { create(:group) }
2021-10-27 15:23:28 +05:30
describe 'associations' do
it { is_expected.to belong_to(:group).with_foreign_key('group_id') }
end
describe 'validations' do
2023-07-09 08:55:56 +05:30
subject { build(:crm_organization) }
2021-10-27 15:23:28 +05:30
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
2022-05-07 20:08:51 +05:30
describe '#root_group' do
context 'when root group' do
2023-07-09 08:55:56 +05:30
subject { build(:crm_organization, group: group) }
2022-05-07 20:08:51 +05:30
it { is_expected.to be_valid }
end
context 'when subgroup' do
2023-07-09 08:55:56 +05:30
subject { build(:crm_organization, group: create(:group, parent: group)) }
2022-05-07 20:08:51 +05:30
it { is_expected.to be_invalid }
end
end
2021-10-27 15:23:28 +05:30
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
2023-07-09 08:55:56 +05:30
let!(:crm_organiztion1) { create(:crm_organization, group: group, name: 'Test') }
let!(:crm_organiztion2) { create(:crm_organization, group: create(:group), name: 'Test') }
2021-10-27 15:23:28 +05:30
it 'strips name' do
2023-07-09 08:55:56 +05:30
expect(described_class.find_by_name(group.id, 'TEST')).to eq([crm_organiztion1])
2021-10-27 15:23:28 +05:30
end
end
2022-06-21 17:19:12 +05:30
describe '#self.move_to_root_group' do
let!(:old_root_group) { create(:group) }
2023-07-09 08:55:56 +05:30
let!(:crm_organizations) { create_list(:crm_organization, 4, group: old_root_group) }
2022-06-21 17:19:12 +05:30
let!(:new_root_group) { create(:group) }
2023-07-09 08:55:56 +05:30
let!(:contact1) { create(:contact, group: new_root_group, organization: crm_organizations[0]) }
let!(:contact2) { create(:contact, group: new_root_group, organization: crm_organizations[1]) }
2022-06-21 17:19:12 +05:30
2023-07-09 08:55:56 +05:30
let!(:dupe_crm_organization1) { create(:crm_organization, group: new_root_group, name: crm_organizations[1].name) }
let!(:dupe_crm_organization2) do
create(:crm_organization, group: new_root_group, name: crm_organizations[3].name.upcase)
end
2022-06-21 17:19:12 +05:30
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
2023-07-09 08:55:56 +05:30
expect(crm_organizations[0].reload.group_id).to eq(new_root_group.id)
expect(crm_organizations[2].reload.group_id).to eq(new_root_group.id)
expect { crm_organizations[1].reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { crm_organizations[3].reload }.to raise_error(ActiveRecord::RecordNotFound)
2022-06-21 17:19:12 +05:30
end
it 'updates contact.organization_id for dupes and leaves the rest untouched' do
2023-07-09 08:55:56 +05:30
expect(contact1.reload.organization_id).to eq(crm_organizations[0].id)
expect(contact2.reload.organization_id).to eq(dupe_crm_organization1.id)
2022-06-21 17:19:12 +05:30
end
end
2022-07-23 23:45:48 +05:30
describe '.search' do
2023-07-09 08:55:56 +05:30
let_it_be(:crm_organization_a) do
2022-07-23 23:45:48 +05:30
create(
2023-07-09 08:55:56 +05:30
:crm_organization,
2022-07-23 23:45:48 +05:30
group: group,
name: "DEF",
description: "ghi_st",
state: "inactive"
)
end
2023-07-09 08:55:56 +05:30
let_it_be(:crm_organization_b) do
2022-07-23 23:45:48 +05:30
create(
2023-07-09 08:55:56 +05:30
:crm_organization,
2022-07-23 23:45:48 +05:30
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
2023-07-09 08:55:56 +05:30
expect(found_organizations).to contain_exactly(crm_organization_a, crm_organization_b)
2022-07-23 23:45:48 +05:30
end
end
context 'when search term is not empty' do
context 'when searching for name' do
let(:search_term) { "aBc" }
2023-07-09 08:55:56 +05:30
it { is_expected.to contain_exactly(crm_organization_b) }
2022-07-23 23:45:48 +05:30
end
context 'when searching for description' do
let(:search_term) { "ghI" }
2023-07-09 08:55:56 +05:30
it { is_expected.to contain_exactly(crm_organization_a) }
2022-07-23 23:45:48 +05:30
end
context 'when searching for name and description' do
let(:search_term) { "_st" }
2023-07-09 08:55:56 +05:30
it { is_expected.to contain_exactly(crm_organization_a, crm_organization_b) }
2022-07-23 23:45:48 +05:30
end
end
end
describe '.search_by_state' do
2023-07-09 08:55:56 +05:30
let_it_be(:crm_organization_a) { create(:crm_organization, group: group, state: "inactive") }
let_it_be(:crm_organization_b) { create(:crm_organization, group: group, state: "active") }
2022-07-23 23:45:48 +05:30
context 'when searching for organizations state' do
it 'returns only inactive organizations' do
2023-07-09 08:55:56 +05:30
expect(group.organizations.search_by_state(:inactive)).to contain_exactly(crm_organization_a)
2022-07-23 23:45:48 +05:30
end
it 'returns only active organizations' do
2023-07-09 08:55:56 +05:30
expect(group.organizations.search_by_state(:active)).to contain_exactly(crm_organization_b)
2022-07-23 23:45:48 +05:30
end
end
end
2022-10-11 01:57:18 +05:30
describe '.counts_by_state' do
before do
2023-07-09 08:55:56 +05:30
create_list(:crm_organization, 3, group: group)
create_list(:crm_organization, 2, group: group, state: 'inactive')
2022-10-11 01:57:18 +05:30
end
it 'returns correct organization counts' do
counts = group.organizations.counts_by_state
expect(counts['active']).to be(3)
expect(counts['inactive']).to be(2)
end
it 'returns 0 with no results' do
counts = group.organizations.where(id: non_existing_record_id).counts_by_state
expect(counts['active']).to be(0)
expect(counts['inactive']).to be(0)
end
end
describe 'sorting' do
2023-07-09 08:55:56 +05:30
let_it_be(:crm_organization_a) { create(:crm_organization, group: group, name: "c", description: "1") }
let_it_be(:crm_organization_b) { create(:crm_organization, group: group, name: "a") }
let_it_be(:crm_organization_c) { create(:crm_organization, group: group, name: "b", description: "2") }
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
describe '.sort_by_name' do
2022-07-23 23:45:48 +05:30
it 'sorts them by name in ascendent order' do
2023-07-09 08:55:56 +05:30
expect(group.organizations.sort_by_name).to eq([crm_organization_b, crm_organization_c, crm_organization_a])
2022-07-23 23:45:48 +05:30
end
end
2022-10-11 01:57:18 +05:30
describe '.sort_by_field' do
it 'sorts them by description in descending order' do
expect(group.organizations.sort_by_field('description', :desc))
2023-07-09 08:55:56 +05:30
.to eq([crm_organization_c, crm_organization_a, crm_organization_b])
2022-10-11 01:57:18 +05:30
end
end
2022-07-23 23:45:48 +05:30
end
2021-10-27 15:23:28 +05:30
end