organization_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. it 'checks user deletion' do
  35. organization.destroy
  36. expect { user.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  37. end
  38. it 'checks ticket deletion' do
  39. organization.destroy
  40. expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  41. end
  42. describe 'when changes for member_ids' do
  43. let(:agent1) { create(:agent) }
  44. let(:agent2) { create(:agent) }
  45. let(:agent3) { create(:agent) }
  46. let(:organization_agents) { create(:organization, member_ids: [agent1.id, agent2.id, agent3.id]) }
  47. it 'does not delete users' do
  48. organization_agents.update(member_ids: [agent1.id, agent2.id])
  49. expect { agent3.reload }.not_to raise_error
  50. end
  51. end
  52. end
  53. end
  54. describe 'Callbacks, Observers, & Async Transactions -' do
  55. describe 'Touching associations on update:' do
  56. let!(:member) { create(:customer, organization: organization) }
  57. let!(:member_ticket) { create(:ticket, customer: member) }
  58. context 'when member associations are added' do
  59. let(:user) { create(:customer) }
  60. it 'is touched, and touches its other members (but not their tickets)' do
  61. expect { organization.members.push(user) }
  62. .to change { organization.reload.updated_at }
  63. end
  64. end
  65. end
  66. end
  67. describe '#domain_assignment' do
  68. it 'fails if enabled and domain is missing' do
  69. organization.domain_assignment = true
  70. organization.domain = nil
  71. organization.valid?
  72. expect(organization.errors[:domain]).to be_present
  73. end
  74. it 'succeeds if enabled and domain is present' do
  75. organization.domain_assignment = true
  76. organization.domain = 'example.org'
  77. organization.valid?
  78. expect(organization.errors[:domain]).to be_empty
  79. end
  80. it 'succeeds if disabled and domain is missing' do
  81. organization.domain_assignment = false
  82. organization.domain = nil
  83. organization.valid?
  84. expect(organization.errors[:domain]).to be_empty
  85. end
  86. end
  87. end