organization_spec.rb 4.1 KB

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