notification_spec.rb 5.1 KB

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