notification_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/concerns/checks_human_changes_examples'
  4. RSpec.describe Transaction::Notification, type: :model do
  5. describe 'pending ticket reminder repeats after midnight at selected time zone' do
  6. let(:group) { create(:group) }
  7. let(:user) { create(:agent) }
  8. let(:ticket) { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) }
  9. before do
  10. travel_to Time.use_zone('UTC') { Time.current.noon }
  11. user.groups << group
  12. ticket
  13. Setting.set('timezone_default', 'America/Santiago')
  14. run(ticket, user, 'reminder_reached')
  15. OnlineNotification.destroy_all
  16. end
  17. it 'notification not sent at UTC midnight' do
  18. travel_to Time.use_zone('UTC') { Time.current.end_of_day + 1.minute }
  19. expect { run(ticket, user, 'reminder_reached') }.not_to change(OnlineNotification, :count)
  20. end
  21. it 'notification sent at selected time zone midnight' do
  22. travel_to Time.use_zone('America/Santiago') { Time.current.end_of_day + 1.minute }
  23. expect { run(ticket, user, 'reminder_reached') }.to change(OnlineNotification, :count).by(1)
  24. end
  25. end
  26. # https://github.com/zammad/zammad/issues/4066
  27. describe 'notification sending reason may be fully translated' do
  28. let(:group) { create(:group) }
  29. let(:user) { create(:agent, groups: [group]) }
  30. let(:ticket) { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) }
  31. let(:reason_en) { 'You are receiving this because you are the owner of this ticket.' }
  32. let(:reason_de) do
  33. Translation.translate('de-de', reason_en).tap do |translated|
  34. expect(translated).not_to eq(reason_en) # rubocop:disable RSpec/ExpectInLet
  35. end
  36. end
  37. before do
  38. allow(NotificationFactory::Mailer).to receive(:deliver)
  39. end
  40. it 'notification includes English footer' do
  41. run(ticket, user, 'reminder_reached')
  42. expect(NotificationFactory::Mailer)
  43. .to have_received(:deliver)
  44. .with hash_including body: %r{#{reason_en}}
  45. end
  46. context 'when locale set to Deutsch' do
  47. before do
  48. user.preferences[:locale] = 'de-de'
  49. user.save
  50. end
  51. it 'notification includes German footer' do
  52. run(ticket, user, 'reminder_reached')
  53. expect(NotificationFactory::Mailer)
  54. .to have_received(:deliver)
  55. .with hash_including body: %r{#{reason_de}}
  56. end
  57. end
  58. end
  59. describe '#ooo_replacements' do
  60. subject(:notification_instance) { build(ticket, user) }
  61. let(:group) { create(:group) }
  62. let(:user) { create(:agent, :ooo, :groupable, ooo_agent: replacement_1, group: group) }
  63. let(:ticket) { create(:ticket, owner: user, group: group, state_name: 'open', pending_time: Time.current) }
  64. context 'when replacement has access' do
  65. let(:replacement_1) { create(:agent, :groupable, group: group) }
  66. it 'is added to list' do
  67. replacements = Set.new
  68. ooo(notification_instance, user, replacements: replacements)
  69. expect(replacements).to include replacement_1
  70. end
  71. context 'when replacement has replacement' do
  72. let(:replacement_1) { create(:agent, :ooo, :groupable, ooo_agent: replacement_2, group: group) }
  73. let(:replacement_2) { create(:agent, :groupable, group: group) }
  74. it 'replacement\'s replacement added to list' do
  75. replacements = Set.new
  76. ooo(notification_instance, user, replacements: replacements)
  77. expect(replacements).to include replacement_2
  78. end
  79. it 'intermediary replacement is not in list' do
  80. replacements = Set.new
  81. ooo(notification_instance, user, replacements: replacements)
  82. expect(replacements).not_to include replacement_1
  83. end
  84. end
  85. end
  86. context 'when replacement does not have access' do
  87. let(:replacement_1) { create(:agent) }
  88. it 'is not added to list' do
  89. replacements = Set.new
  90. ooo(notification_instance, user, replacements: replacements)
  91. expect(replacements).not_to include replacement_1
  92. end
  93. context 'when replacement has replacement with access' do
  94. let(:replacement_1) { create(:agent, :ooo, ooo_agent: replacement_2) }
  95. let(:replacement_2) { create(:agent, :groupable, group: group) }
  96. it 'his replacement may be added' do
  97. replacements = Set.new
  98. ooo(notification_instance, user, replacements: replacements)
  99. expect(replacements).to include replacement_2
  100. end
  101. end
  102. end
  103. end
  104. it_behaves_like 'ChecksHumanChanges'
  105. def run(ticket, user, type)
  106. build(ticket, user, type).perform
  107. end
  108. def build(ticket, user, type = 'reminder_reached')
  109. described_class.new(
  110. object: ticket.class.name,
  111. type: type,
  112. object_id: ticket.id,
  113. interface_handle: 'scheduler',
  114. changes: nil,
  115. created_at: Time.current,
  116. user_id: user.id
  117. )
  118. end
  119. def ooo(instance, user, replacements: Set.new, reasons: [])
  120. instance.send(:ooo_replacements, user: user, replacements: replacements, ticket: ticket, reasons: reasons)
  121. end
  122. end