organization_spec.rb 4.2 KB

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