organization_spec.rb 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. require 'models/concerns/can_csv_import_examples'
  5. require 'models/concerns/can_csv_import_organization_examples'
  6. require 'models/concerns/has_history_examples'
  7. require 'models/concerns/has_search_index_backend_examples'
  8. require 'models/concerns/has_xss_sanitized_note_examples'
  9. require 'models/concerns/has_image_sanitized_note_examples'
  10. require 'models/concerns/has_object_manager_attributes_examples'
  11. require 'models/concerns/has_taskbars_examples'
  12. RSpec.describe Organization, type: :model do
  13. subject(:organization) { create(:organization) }
  14. it_behaves_like 'ApplicationModel', can_assets: { associations: :members }
  15. it_behaves_like 'CanCsvImport', unique_attributes: 'name'
  16. include_examples 'CanCsvImport - Organization specific tests'
  17. it_behaves_like 'HasHistory'
  18. it_behaves_like 'HasSearchIndexBackend', indexed_factory: :organization
  19. it_behaves_like 'HasXssSanitizedNote', model_factory: :organization
  20. it_behaves_like 'HasImageSanitizedNote', model_factory: :organization
  21. it_behaves_like 'HasObjectManagerAttributes'
  22. it_behaves_like 'HasTaskbars'
  23. describe 'Class methods:' do
  24. describe '.where_or_cis' do
  25. it 'finds instance by querying multiple attributes case insensitive' do
  26. # search for Zammad Foundation
  27. organizations = described_class.where_or_cis(%i[name note], '%zammad%')
  28. expect(organizations).not_to be_blank
  29. end
  30. end
  31. describe '.destroy' do
  32. let!(:refs_known) { { 'Ticket' => { 'organization_id'=> 1 }, 'User' => { 'organization_id'=> 1 } } }
  33. let!(:user) { create(:customer, organization: organization) }
  34. let!(:ticket) { create(:ticket, organization: organization, customer: user) }
  35. it 'checks known refs' do
  36. refs_organization = Models.references('Organization', organization.id, true)
  37. expect(refs_organization).to eq(refs_known)
  38. end
  39. context 'with associations' do
  40. it 'checks user deletion' do
  41. organization.destroy(associations: true)
  42. expect { user.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  43. end
  44. it 'checks ticket deletion' do
  45. organization.destroy(associations: true)
  46. expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  47. end
  48. end
  49. context 'without associations' do
  50. it 'checks user deletion' do
  51. organization.destroy
  52. expect(user.reload.organization_id).to be_nil
  53. end
  54. it 'checks ticket deletion' do
  55. organization.destroy
  56. expect(ticket.reload.organization_id).to be_nil
  57. end
  58. end
  59. describe 'when changes for member_ids' do
  60. let(:agent1) { create(:agent) }
  61. let(:agent2) { create(:agent) }
  62. let(:agent3) { create(:agent) }
  63. let(:organization_agents) { create(:organization, member_ids: [agent1.id, agent2.id, agent3.id]) }
  64. it 'does not delete users' do
  65. organization_agents.update(member_ids: [agent1.id, agent2.id])
  66. expect { agent3.reload }.not_to raise_error
  67. end
  68. end
  69. end
  70. end
  71. describe 'Callbacks, Observers, & Async Transactions -' do
  72. describe 'Touching associations on update:' do
  73. let!(:member) { create(:customer, organization: organization) }
  74. let!(:member_ticket) { create(:ticket, customer: member) }
  75. context 'when member associations are added' do
  76. let(:user) { create(:customer) }
  77. it 'is touched, and touches its other members (but not their tickets)' do
  78. expect { organization.members.push(user) }
  79. .to change { organization.reload.updated_at }
  80. end
  81. end
  82. end
  83. end
  84. describe '#domain_assignment' do
  85. it 'fails if enabled and domain is missing' do
  86. organization.domain_assignment = true
  87. organization.domain = nil
  88. organization.valid?
  89. expect(organization.errors[:domain]).to be_present
  90. end
  91. it 'succeeds if enabled and domain is present' do
  92. organization.domain_assignment = true
  93. organization.domain = 'example.org'
  94. organization.valid?
  95. expect(organization.errors[:domain]).to be_empty
  96. end
  97. it 'succeeds if disabled and domain is missing' do
  98. organization.domain_assignment = false
  99. organization.domain = nil
  100. organization.valid?
  101. expect(organization.errors[:domain]).to be_empty
  102. end
  103. end
  104. describe 'Updating organization members' do
  105. context 'when member gets removed' do
  106. let(:customer) { create(:customer, organization: organization) }
  107. before do
  108. customer.attributes_with_association_ids
  109. organization.attributes_with_association_ids
  110. end
  111. it 'does clear cache of customer after user unassignment' do
  112. organization.update(member_ids: [])
  113. expect(customer.reload.attributes_with_association_ids['organization_id']).to be_nil
  114. end
  115. it 'does touch customer after user unassignment' do
  116. expect { organization.update(member_ids: []) }.to change { customer.reload.updated_at }
  117. end
  118. it 'does clear cache of organization after user unassignment' do
  119. organization.update(member_ids: [])
  120. expect(organization.reload.attributes_with_association_ids['member_ids']).not_to include(customer.id)
  121. end
  122. it 'does touch organization after user unassignment' do
  123. expect { organization.update(member_ids: []) }.to change { organization.reload.updated_at }
  124. end
  125. end
  126. context 'when member gets added' do
  127. let(:customer) { create(:customer) }
  128. before do
  129. customer.attributes_with_association_ids
  130. organization.attributes_with_association_ids
  131. end
  132. it 'does clear cache of customer after user assignment' do
  133. organization.update(member_ids: [customer.id])
  134. expect(customer.reload.attributes_with_association_ids['organization_id']).not_to be_nil
  135. end
  136. it 'does touch customer after user assignment' do
  137. expect { organization.update(member_ids: [customer.id]) }.to change { customer.reload.updated_at }
  138. end
  139. it 'does clear cache of organization after user assignment' do
  140. organization.update(member_ids: [customer.id])
  141. expect(organization.reload.attributes_with_association_ids['member_ids']).to include(customer.id)
  142. end
  143. it 'does touch organization after user assignment' do
  144. expect { organization.update(member_ids: [customer.id]) }.to change { organization.reload.updated_at }
  145. end
  146. end
  147. end
  148. describe 'Updating secondary organization members' do
  149. context 'when member gets removed' do
  150. let(:customer) { create(:customer, organization: organization) }
  151. let(:secondary_organization) { create(:organization) }
  152. before do
  153. secondary_organization.update(member_ids: [customer.id])
  154. customer.attributes_with_association_ids
  155. organization.attributes_with_association_ids
  156. secondary_organization.attributes_with_association_ids
  157. end
  158. it 'does clear cache of customer after user unassignment' do
  159. secondary_organization.update(member_ids: [])
  160. expect(customer.reload.attributes_with_association_ids['organization_id']).to be_nil
  161. end
  162. it 'does touch customer after user unassignment' do
  163. expect { secondary_organization.update(member_ids: []) }.to change { customer.reload.updated_at }
  164. end
  165. it 'does clear cache of organization after user unassignment' do
  166. secondary_organization.update(member_ids: [])
  167. expect(secondary_organization.reload.attributes_with_association_ids['member_ids']).not_to include(customer.id)
  168. end
  169. it 'does touch organization after user unassignment' do
  170. expect { secondary_organization.update(member_ids: []) }.to change { secondary_organization.reload.updated_at }
  171. end
  172. end
  173. context 'when member gets added' do
  174. let(:customer) { create(:customer, organization: organization) }
  175. let(:secondary_organization) { create(:organization) }
  176. before do
  177. customer.attributes_with_association_ids
  178. organization.attributes_with_association_ids
  179. secondary_organization.attributes_with_association_ids
  180. end
  181. it 'does clear cache of customer after user assignment' do
  182. secondary_organization.update(secondary_member_ids: [customer.id])
  183. expect(customer.reload.attributes_with_association_ids['organization_id']).not_to be_nil
  184. end
  185. it 'does touch customer after user assignment' do
  186. expect { secondary_organization.update(secondary_member_ids: [customer.id]) }.to change { customer.reload.updated_at }
  187. end
  188. it 'does clear cache of organization after user assignment' do
  189. secondary_organization.update(member_ids: [customer.id])
  190. expect(secondary_organization.reload.attributes_with_association_ids['member_ids']).to include(customer.id)
  191. end
  192. it 'does touch organization after user assignment' do
  193. expect { secondary_organization.update(member_ids: [customer.id]) }.to change { secondary_organization.reload.updated_at }
  194. end
  195. end
  196. end
  197. end