notification_spec.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. describe 'SMTP errors' do
  105. let(:group) { create(:group) }
  106. let(:user) { create(:agent, groups: [group]) }
  107. let(:ticket) { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) }
  108. let(:response) { Net::SMTP::Response.new(response_status_code, 'mocked SMTP response') }
  109. let(:error) { Net::SMTPFatalError.new(response) }
  110. before do
  111. allow_any_instance_of(Net::SMTP).to receive(:start).and_raise(error)
  112. Service::System::SetEmailNotificationConfiguration
  113. .new(
  114. adapter: 'smtp',
  115. new_configuration: {}
  116. ).execute
  117. end
  118. context 'when there is a problem with the sending SMTP server' do
  119. let(:response_status_code) { 535 }
  120. it 'raises an eroror' do
  121. expect { run(ticket, user, 'reminder_reached') }
  122. .to raise_error(Channel::DeliveryError)
  123. end
  124. end
  125. context 'when there is a problem with the receiving SMTP server' do
  126. let(:response_status_code) { 550 }
  127. it 'logs the information about failed email delivery' do
  128. allow(Rails.logger).to receive(:info)
  129. run(ticket, user, 'reminder_reached')
  130. expect(Rails.logger).to have_received(:info)
  131. end
  132. end
  133. end
  134. it_behaves_like 'ChecksHumanChanges'
  135. def run(ticket, user, type)
  136. build(ticket, user, type).perform
  137. end
  138. def build(ticket, user, type = 'reminder_reached')
  139. described_class.new(
  140. object: ticket.class.name,
  141. type: type,
  142. object_id: ticket.id,
  143. interface_handle: 'scheduler',
  144. changes: nil,
  145. created_at: Time.current,
  146. user_id: user.id
  147. )
  148. end
  149. def ooo(instance, user, replacements: Set.new, reasons: [])
  150. instance.send(:ooo_replacements, user: user, replacements: replacements, ticket: ticket, reasons: reasons)
  151. end
  152. end