organization_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. RSpec.describe Organization, type: :model do
  9. it_behaves_like 'ApplicationModel', can_assets: { associations: :members }
  10. it_behaves_like 'CanCsvImport', unique_attributes: 'name'
  11. it_behaves_like 'HasHistory'
  12. it_behaves_like 'HasSearchIndexBackend', indexed_factory: :organization
  13. it_behaves_like 'HasXssSanitizedNote', model_factory: :organization
  14. it_behaves_like 'HasObjectManagerAttributesValidation'
  15. subject(:organization) { create(:organization) }
  16. describe 'Class methods:' do
  17. describe '.where_or_cis' do
  18. it 'finds instance by querying multiple attributes case insensitive' do
  19. # search for Zammad Foundation
  20. organizations = described_class.where_or_cis(%i[name note], '%zammad%')
  21. expect(organizations).not_to be_blank
  22. end
  23. end
  24. end
  25. describe 'Callbacks, Observers, & Async Transactions -' do
  26. describe 'Touching associations on update:' do
  27. let!(:member) { create(:customer_user, organization: organization) }
  28. let!(:member_ticket) { create(:ticket, customer: member) }
  29. context 'when basic attributes are updated' do
  30. it 'touches its members and their tickets' do
  31. expect { organization.update(name: 'foo') }
  32. .to change { member.reload.updated_at }
  33. .and change { member_ticket.reload.updated_at }
  34. end
  35. end
  36. context 'when member associations are added' do
  37. let(:user) { create(:customer_user) }
  38. it 'is touched, and touches its other members (but not their tickets)' do
  39. expect { organization.members.push(user) }
  40. .to change { organization.reload.updated_at }
  41. .and change { member.reload.updated_at }
  42. .and not_change { member_ticket.reload.updated_at }
  43. end
  44. end
  45. context 'with 100+ members' do
  46. let!(:members) { create_list(:user, 101, organization: organization) }
  47. let!(:member_ticket) { create(:ticket, customer: members.first) }
  48. # This _should_ be split into two separate examples,
  49. # but setup is slow and expensive.
  50. it 'does not perform any association updates' do
  51. expect { organization.update(name: 'foo') }
  52. .to not_change { members.map(&:reload).map(&:updated_at) }
  53. .and not_change { member_ticket.reload.updated_at }
  54. expect { organization.members.push(member) }
  55. .to not_change { organization.reload.updated_at }
  56. .and not_change { members.map(&:reload).map(&:updated_at) }
  57. end
  58. end
  59. end
  60. end
  61. end