organization_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2012-2023 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/has_history_examples'
  6. require 'models/concerns/has_search_index_backend_examples'
  7. require 'models/concerns/has_xss_sanitized_note_examples'
  8. require 'models/concerns/has_image_sanitized_note_examples'
  9. require 'models/concerns/has_object_manager_attributes_examples'
  10. require 'models/concerns/has_taskbars_examples'
  11. RSpec.describe Organization, type: :model do
  12. subject(:organization) { create(:organization) }
  13. it_behaves_like 'ApplicationModel', can_assets: { associations: :members }
  14. it_behaves_like 'CanCsvImport', unique_attributes: 'name'
  15. it_behaves_like 'HasHistory'
  16. it_behaves_like 'HasSearchIndexBackend', indexed_factory: :organization
  17. it_behaves_like 'HasXssSanitizedNote', model_factory: :organization
  18. it_behaves_like 'HasImageSanitizedNote', model_factory: :organization
  19. it_behaves_like 'HasObjectManagerAttributes'
  20. it_behaves_like 'HasTaskbars'
  21. describe 'Class methods:' do
  22. describe '.where_or_cis' do
  23. it 'finds instance by querying multiple attributes case insensitive' do
  24. # search for Zammad Foundation
  25. organizations = described_class.where_or_cis(%i[name note], '%zammad%')
  26. expect(organizations).not_to be_blank
  27. end
  28. end
  29. describe '.destroy' do
  30. let!(:refs_known) { { 'Ticket' => { 'organization_id'=> 1 }, 'User' => { 'organization_id'=> 1 } } }
  31. let!(:user) { create(:customer, organization: organization) }
  32. let!(:ticket) { create(:ticket, organization: organization, customer: user) }
  33. it 'checks known refs' do
  34. refs_organization = Models.references('Organization', organization.id, true)
  35. expect(refs_organization).to eq(refs_known)
  36. end
  37. context 'with associations' do
  38. it 'checks user deletion' do
  39. organization.destroy(associations: true)
  40. expect { user.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  41. end
  42. it 'checks ticket deletion' do
  43. organization.destroy(associations: true)
  44. expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  45. end
  46. end
  47. context 'without associations' do
  48. it 'checks user deletion' do
  49. organization.destroy
  50. expect(user.reload.organization_id).to be_nil
  51. end
  52. it 'checks ticket deletion' do
  53. organization.destroy
  54. expect(ticket.reload.organization_id).to be_nil
  55. end
  56. end
  57. describe 'when changes for member_ids' do
  58. let(:agent1) { create(:agent) }
  59. let(:agent2) { create(:agent) }
  60. let(:agent3) { create(:agent) }
  61. let(:organization_agents) { create(:organization, member_ids: [agent1.id, agent2.id, agent3.id]) }
  62. it 'does not delete users' do
  63. organization_agents.update(member_ids: [agent1.id, agent2.id])
  64. expect { agent3.reload }.not_to raise_error
  65. end
  66. end
  67. end
  68. end
  69. describe 'Callbacks, Observers, & Async Transactions -' do
  70. describe 'Touching associations on update:' do
  71. let!(:member) { create(:customer, organization: organization) }
  72. let!(:member_ticket) { create(:ticket, customer: member) }
  73. context 'when member associations are added' do
  74. let(:user) { create(:customer) }
  75. it 'is touched, and touches its other members (but not their tickets)' do
  76. expect { organization.members.push(user) }
  77. .to change { organization.reload.updated_at }
  78. end
  79. end
  80. end
  81. end
  82. describe '#domain_assignment' do
  83. it 'fails if enabled and domain is missing' do
  84. organization.domain_assignment = true
  85. organization.domain = nil
  86. organization.valid?
  87. expect(organization.errors[:domain]).to be_present
  88. end
  89. it 'succeeds if enabled and domain is present' do
  90. organization.domain_assignment = true
  91. organization.domain = 'example.org'
  92. organization.valid?
  93. expect(organization.errors[:domain]).to be_empty
  94. end
  95. it 'succeeds if disabled and domain is missing' do
  96. organization.domain_assignment = false
  97. organization.domain = nil
  98. organization.valid?
  99. expect(organization.errors[:domain]).to be_empty
  100. end
  101. end
  102. end