2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-06-05 07:15:48 +00:00
|
|
|
require 'rails_helper'
|
2019-02-26 11:00:46 +00:00
|
|
|
require 'models/application_model_examples'
|
2019-10-21 10:44:52 +00:00
|
|
|
require 'models/concerns/can_csv_import_examples'
|
2019-06-28 13:07:14 +00:00
|
|
|
require 'models/concerns/has_history_examples'
|
2019-01-15 12:32:14 +00:00
|
|
|
require 'models/concerns/has_search_index_backend_examples'
|
2019-02-12 07:38:59 +00:00
|
|
|
require 'models/concerns/has_xss_sanitized_note_examples'
|
2021-08-17 16:37:16 +00:00
|
|
|
require 'models/concerns/has_object_manager_attributes_examples'
|
2020-09-08 15:06:23 +00:00
|
|
|
require 'models/concerns/has_taskbars_examples'
|
2018-06-05 07:15:48 +00:00
|
|
|
|
2019-01-22 09:20:14 +00:00
|
|
|
RSpec.describe Organization, type: :model do
|
2020-09-30 09:07:01 +00:00
|
|
|
subject(:organization) { create(:organization) }
|
|
|
|
|
2019-02-26 11:00:46 +00:00
|
|
|
it_behaves_like 'ApplicationModel', can_assets: { associations: :members }
|
2019-10-21 10:44:52 +00:00
|
|
|
it_behaves_like 'CanCsvImport', unique_attributes: 'name'
|
2019-06-28 13:07:14 +00:00
|
|
|
it_behaves_like 'HasHistory'
|
2019-01-24 10:13:04 +00:00
|
|
|
it_behaves_like 'HasSearchIndexBackend', indexed_factory: :organization
|
2019-02-12 07:38:59 +00:00
|
|
|
it_behaves_like 'HasXssSanitizedNote', model_factory: :organization
|
2021-08-17 16:37:16 +00:00
|
|
|
it_behaves_like 'HasObjectManagerAttributes'
|
2020-09-08 15:06:23 +00:00
|
|
|
it_behaves_like 'HasTaskbars'
|
2018-06-05 07:15:48 +00:00
|
|
|
|
2019-04-02 14:43:11 +00:00
|
|
|
describe 'Class methods:' do
|
|
|
|
describe '.where_or_cis' do
|
|
|
|
it 'finds instance by querying multiple attributes case insensitive' do
|
|
|
|
# search for Zammad Foundation
|
|
|
|
organizations = described_class.where_or_cis(%i[name note], '%zammad%')
|
|
|
|
expect(organizations).not_to be_blank
|
|
|
|
end
|
2018-06-05 07:15:48 +00:00
|
|
|
end
|
2020-09-08 15:06:23 +00:00
|
|
|
|
|
|
|
describe '.destroy' do
|
|
|
|
|
|
|
|
let!(:refs_known) { { 'Ticket' => { 'organization_id'=> 1 }, 'User' => { 'organization_id'=> 1 } } }
|
|
|
|
let!(:user) { create(:customer, organization: organization) }
|
|
|
|
let!(:ticket) { create(:ticket, organization: organization, customer: user) }
|
|
|
|
|
|
|
|
it 'checks known refs' do
|
|
|
|
refs_organization = Models.references('Organization', organization.id, true)
|
|
|
|
expect(refs_organization).to eq(refs_known)
|
|
|
|
end
|
|
|
|
|
2021-08-09 08:47:42 +00:00
|
|
|
context 'with associations' do
|
|
|
|
it 'checks user deletion' do
|
|
|
|
organization.destroy(associations: true)
|
|
|
|
expect { user.reload }.to raise_exception(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks ticket deletion' do
|
|
|
|
organization.destroy(associations: true)
|
|
|
|
expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
2021-08-09 08:47:42 +00:00
|
|
|
context 'without associations' do
|
|
|
|
it 'checks user deletion' do
|
|
|
|
organization.destroy
|
|
|
|
expect(user.reload.organization_id).to be nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks ticket deletion' do
|
|
|
|
organization.destroy
|
|
|
|
expect(ticket.reload.organization_id).to be nil
|
|
|
|
end
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
2020-11-11 10:07:41 +00:00
|
|
|
|
|
|
|
describe 'when changes for member_ids' do
|
|
|
|
let(:agent1) { create(:agent) }
|
|
|
|
let(:agent2) { create(:agent) }
|
|
|
|
let(:agent3) { create(:agent) }
|
|
|
|
let(:organization_agents) { create(:organization, member_ids: [agent1.id, agent2.id, agent3.id]) }
|
|
|
|
|
|
|
|
it 'does not delete users' do
|
|
|
|
organization_agents.update(member_ids: [agent1.id, agent2.id])
|
|
|
|
expect { agent3.reload }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
2018-06-05 07:15:48 +00:00
|
|
|
end
|
2019-02-12 07:38:59 +00:00
|
|
|
|
2019-04-02 14:43:11 +00:00
|
|
|
describe 'Callbacks, Observers, & Async Transactions -' do
|
|
|
|
describe 'Touching associations on update:' do
|
2020-06-19 09:17:18 +00:00
|
|
|
let!(:member) { create(:customer, organization: organization) }
|
2019-04-02 14:43:11 +00:00
|
|
|
let!(:member_ticket) { create(:ticket, customer: member) }
|
|
|
|
|
|
|
|
context 'when member associations are added' do
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:user) { create(:customer) }
|
2019-04-02 14:43:11 +00:00
|
|
|
|
|
|
|
it 'is touched, and touches its other members (but not their tickets)' do
|
|
|
|
expect { organization.members.push(user) }
|
|
|
|
.to change { organization.reload.updated_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-27 12:02:20 +00:00
|
|
|
|
|
|
|
describe '#domain_assignment' do
|
|
|
|
it 'fails if enabled and domain is missing' do
|
|
|
|
organization.domain_assignment = true
|
|
|
|
organization.domain = nil
|
|
|
|
organization.valid?
|
|
|
|
|
|
|
|
expect(organization.errors[:domain]).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds if enabled and domain is present' do
|
|
|
|
organization.domain_assignment = true
|
|
|
|
organization.domain = 'example.org'
|
|
|
|
organization.valid?
|
|
|
|
|
|
|
|
expect(organization.errors[:domain]).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds if disabled and domain is missing' do
|
|
|
|
organization.domain_assignment = false
|
|
|
|
organization.domain = nil
|
|
|
|
organization.valid?
|
|
|
|
|
|
|
|
expect(organization.errors[:domain]).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2018-06-05 07:15:48 +00:00
|
|
|
end
|