ticket_preferences_spec.rb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Escalation::TicketPreferences, time_zone: 'Europe/London' do
  4. let(:instance) { described_class.new ticket }
  5. let(:ticket) { create(:ticket) }
  6. describe '#sla_changed?' do
  7. it 'false when using same sla' do
  8. sla = create(:sla)
  9. instance.hash[:sla_id] = sla.id
  10. instance.hash[:sla_updated_at] = sla.updated_at
  11. expect(instance).not_to be_sla_changed(sla)
  12. end
  13. it 'true when using another sla' do
  14. sla = create(:sla)
  15. sla2 = create(:sla)
  16. instance.hash[:sla_id] = sla.id
  17. instance.hash[:sla_updated_at] = sla.updated_at
  18. expect(instance).to be_sla_changed(sla2)
  19. end
  20. it 'true when using updated sla' do
  21. sla = create(:sla)
  22. instance.hash[:sla_id] = sla.id
  23. instance.hash[:sla_updated_at] = sla.updated_at
  24. sla.touch
  25. expect(instance).to be_sla_changed(sla)
  26. end
  27. it 'doe not fail given nil' do
  28. expect { instance.sla_changed?(nil) }.not_to raise_error
  29. end
  30. end
  31. describe '#calendar_changed?' do
  32. it 'false when using same calendar' do
  33. calendar = create(:calendar)
  34. instance.hash[:calendar_id] = calendar.id
  35. instance.hash[:calendar_updated_at] = calendar.updated_at
  36. expect(instance).not_to be_calendar_changed(calendar)
  37. end
  38. it 'true when using another calendar' do
  39. calendar = create(:calendar)
  40. calendar2 = create(:calendar)
  41. instance.hash[:calendar_id] = calendar.id
  42. instance.hash[:calendar_updated_at] = calendar.updated_at
  43. expect(instance).to be_calendar_changed(calendar2)
  44. end
  45. it 'true when using updated calendar' do
  46. calendar = create(:calendar)
  47. instance.hash[:calendar_id] = calendar.id
  48. instance.hash[:calendar_updated_at] = calendar.updated_at
  49. calendar.touch
  50. expect(instance).to be_calendar_changed(calendar)
  51. end
  52. it 'doe not fail given nil' do
  53. expect { instance.calendar_changed?(nil) }.not_to raise_error
  54. end
  55. end
  56. describe '#first_response_at_changed?' do
  57. before { freeze_time }
  58. it 'true when changed' do
  59. instance.hash[:first_response_at] = 1.day.ago
  60. ticket.update! first_response_at: 1.week.ago
  61. expect(instance).to be_first_response_at_changed(ticket)
  62. end
  63. it 'false when matching' do
  64. instance.hash[:first_response_at] = 7.days.ago
  65. ticket.update! first_response_at: 1.week.ago
  66. expect(instance).not_to be_first_response_at_changed(ticket)
  67. end
  68. end
  69. describe '#last_update_at_changed?' do
  70. before { freeze_time }
  71. it 'true when changed' do
  72. instance.hash[:last_update_at] = 1.day.ago
  73. ticket.update! last_contact_customer_at: 1.week.ago
  74. expect(instance).to be_last_update_at_changed(ticket)
  75. end
  76. it 'false when matching' do
  77. instance.hash[:last_update_at] = 7.days.ago
  78. ticket.update! last_contact_customer_at: 1.week.ago
  79. expect(instance).not_to be_last_update_at_changed(ticket)
  80. end
  81. end
  82. describe '#close_at_changed?' do
  83. before { freeze_time }
  84. it 'true when changed' do
  85. instance.hash[:close_at] = 1.day.ago
  86. ticket.update! close_at: 1.week.ago
  87. expect(instance).to be_close_at_changed(ticket)
  88. end
  89. it 'false when matching' do
  90. instance.hash[:close_at] = 7.days.ago
  91. ticket.update! close_at: 1.week.ago
  92. expect(instance).not_to be_close_at_changed(ticket)
  93. end
  94. end
  95. describe '#property_changes?' do
  96. before { freeze_time }
  97. it 'false when no changes' do
  98. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  99. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  100. expect(instance).not_to be_property_changes(ticket)
  101. end
  102. it 'true when #first_response_at changes' do
  103. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 4.days.ago, close_at: 1.day.ago
  104. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  105. expect(instance).to be_property_changes(ticket)
  106. end
  107. it 'true when last_update_at changes' do
  108. ticket.update! last_contact_customer_at: 2.weeks.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  109. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  110. expect(instance).to be_property_changes(ticket)
  111. end
  112. it 'true when #close_at changes' do
  113. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 2.days.ago
  114. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  115. expect(instance).to be_property_changes(ticket)
  116. end
  117. end
  118. describe '#any_changes?' do
  119. let(:sla) { create(:sla) }
  120. before do
  121. freeze_time
  122. sla
  123. instance.hash[:sla_id] = sla.id
  124. instance.hash[:sla_updated_at] = sla.updated_at
  125. instance.hash[:calendar_id] = sla.calendar.id
  126. instance.hash[:calendar_updated_at] = sla.calendar.updated_at
  127. end
  128. it 'false when no changes' do
  129. instance.hash[:escalation_disabled] = false
  130. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  131. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  132. expect(instance).not_to be_any_changes(ticket, sla, false)
  133. end
  134. it 'true when time property changed' do
  135. instance.hash[:escalation_disabled] = false
  136. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 2.days.ago
  137. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  138. expect(instance).to be_any_changes(ticket, sla, false)
  139. end
  140. it 'true when sla changes' do
  141. sla2 = create(:sla)
  142. instance.hash[:escalation_disabled] = false
  143. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  144. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  145. expect(instance).to be_any_changes(ticket, sla2, false)
  146. end
  147. it 'true when escalability status changes' do
  148. instance.hash[:escalation_disabled] = true
  149. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  150. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  151. expect(instance).to be_any_changes(ticket, sla, false)
  152. end
  153. end
  154. describe '#update_preferences' do
  155. it 'sets escalation_calculation in ticket preferences' do
  156. response = :spec
  157. allow(instance).to receive(:hash_of).and_return(response)
  158. instance.update_preferences(ticket, nil, false)
  159. expect(ticket.preferences[:escalation_calculation]).to eq response
  160. end
  161. end
  162. describe '#hash_of' do
  163. let(:last_contact) { 6.days.ago }
  164. let(:first_response_at) { 1.week.ago }
  165. let(:close_at) { 1.day.ago }
  166. let(:sla) { create(:sla, updated_at: 1.week.from_now) }
  167. let(:escalation_disabled) { false }
  168. let(:result) { instance.hash_of(ticket, sla, escalation_disabled) }
  169. before do
  170. freeze_time
  171. sla
  172. ticket.update! last_contact_customer_at: last_contact, first_response_at: first_response_at, close_at: close_at
  173. end
  174. it { expect(result).to be_a(Hash) }
  175. it { expect(result[:first_response_at]).to eq ticket.first_response_at }
  176. it { expect(result[:last_update_at]).to eq last_contact }
  177. it { expect(result[:close_at]).to eq ticket.close_at }
  178. it { expect(result[:sla_id]).to eq sla.id }
  179. it { expect(result[:sla_updated_at]).to eq sla.updated_at }
  180. it { expect(result[:calendar_id]).to eq sla.calendar.id }
  181. it { expect(result[:calendar_updated_at]).to eq sla.calendar.updated_at }
  182. it { expect(result[:escalation_disabled]).to eq escalation_disabled }
  183. context 'when sla not given' do
  184. let(:sla) { nil }
  185. it 'sla and calendar meta data are not included' do
  186. expect(result.keys).to eq %i[first_response_at last_update_at close_at escalation_disabled]
  187. end
  188. end
  189. end
  190. end