organization_spec.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. end
  43. end
  44. describe 'Callbacks, Observers, & Async Transactions -' do
  45. describe 'Touching associations on update:' do
  46. let!(:member) { create(:customer, organization: organization) }
  47. let!(:member_ticket) { create(:ticket, customer: member) }
  48. context 'when member associations are added' do
  49. let(:user) { create(:customer) }
  50. it 'is touched, and touches its other members (but not their tickets)' do
  51. expect { organization.members.push(user) }
  52. .to change { organization.reload.updated_at }
  53. end
  54. end
  55. end
  56. end
  57. describe '#domain_assignment' do
  58. it 'fails if enabled and domain is missing' do
  59. organization.domain_assignment = true
  60. organization.domain = nil
  61. organization.valid?
  62. expect(organization.errors[:domain]).to be_present
  63. end
  64. it 'succeeds if enabled and domain is present' do
  65. organization.domain_assignment = true
  66. organization.domain = 'example.org'
  67. organization.valid?
  68. expect(organization.errors[:domain]).to be_empty
  69. end
  70. it 'succeeds if disabled and domain is missing' do
  71. organization.domain_assignment = false
  72. organization.domain = nil
  73. organization.valid?
  74. expect(organization.errors[:domain]).to be_empty
  75. end
  76. end
  77. end