updates_ticket_organization_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe User::UpdatesTicketOrganization, type: :model do
  4. subject(:ticket) { build(:ticket, customer: customer, organization: nil) }
  5. let(:customer) { create(:customer, :with_org) }
  6. context 'when ticket is created' do
  7. it 'automatically adopts the organization of its #customer' do
  8. expect { ticket.save }
  9. .to change(ticket, :organization).to(customer.organization)
  10. end
  11. end
  12. context 'when #customer.organization is updated' do
  13. context 'when set to nil' do
  14. it "automatically updates to #customer's new value" do
  15. ticket.save
  16. expect { customer.update(organization: nil) }.to change { ticket.reload.organization }.to(nil)
  17. end
  18. end
  19. context 'when #customer.organization is updated to a different organization' do
  20. let!(:old_org) { customer.organization }
  21. let!(:new_org) { create(:organization) }
  22. context "when 'ticket_organization_reassignment' is set to false" do
  23. before { Setting.set('ticket_organization_reassignment', false) }
  24. it "does not automatically update to #customer's new value" do
  25. ticket.save
  26. customer.update!(organization: new_org)
  27. expect(ticket.reload.organization).to eq(old_org)
  28. end
  29. end
  30. it "automatically updates to #customer's new value" do
  31. ticket.save
  32. expect { customer.update(organization: new_org) }.to change { ticket.reload.organization }.to(new_org)
  33. end
  34. it 'has made all changes with user id 1' do
  35. expect(ticket.updated_by.id).to eq 1
  36. end
  37. # https://github.com/zammad/zammad/issues/3952
  38. it 'does not send notifications' do
  39. allow(NotificationFactory::Mailer).to receive(:deliver)
  40. customer.update(organization: old_org)
  41. expect(NotificationFactory::Mailer).not_to have_received(:deliver)
  42. end
  43. end
  44. context 'when customer has secondary organizations' do
  45. let(:customer) { create(:customer, organization: old_primary_organization, organizations: [secondary_organization]) }
  46. let(:old_primary_organization) { create(:organization) }
  47. let(:new_primary_organization) { create(:organization) }
  48. let(:secondary_organization) { create(:organization) }
  49. let(:tickets) do
  50. ticket_primary_org = create(:ticket, customer: customer, organization: old_primary_organization)
  51. ticket_secondary_org = create(:ticket, customer: customer, organization: secondary_organization)
  52. [ticket_primary_org, ticket_secondary_org]
  53. end
  54. before { tickets }
  55. it 'updates ticket with primary organization and does not change ticket with secondary organization', :aggregate_failures do
  56. customer.update!(organization: new_primary_organization)
  57. expect(tickets.first.reload.organization).to eq(new_primary_organization)
  58. expect(tickets.last.reload.organization).to eq(secondary_organization)
  59. end
  60. context 'when customer has no organization assigned at all' do
  61. let(:customer) { create(:customer) }
  62. let(:tickets) do
  63. ticket_primary_org = create(:ticket, customer: customer)
  64. ticket_secondary_org = create(:ticket, customer: customer)
  65. [ticket_primary_org, ticket_secondary_org]
  66. end
  67. it 'updates all ticket to contain the primary organization', :aggregate_failures do
  68. customer.update!(organization: new_primary_organization, organizations: [secondary_organization])
  69. expect(tickets.first.reload.organization).to eq(new_primary_organization)
  70. expect(tickets.last.reload.organization).to eq(new_primary_organization)
  71. end
  72. end
  73. end
  74. end
  75. end