ticket_escalation_test.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. require 'test_helper'
  2. class TicketEscalationTest < ActiveSupport::TestCase
  3. test 'ticket create' do
  4. ticket = Ticket.new(
  5. title: 'some value 123',
  6. group: Group.lookup(name: 'Users'),
  7. customer_id: 2,
  8. updated_by_id: 1,
  9. created_by_id: 1,
  10. )
  11. ticket.save!
  12. assert(ticket, 'ticket created')
  13. assert_not(ticket.escalation_at)
  14. assert_not(ticket.has_changes_to_save?)
  15. article = Ticket::Article.create!(
  16. ticket_id: ticket.id,
  17. type_id: Ticket::Article::Type.find_by(name: 'note').id,
  18. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  19. body: 'some body',
  20. internal: false,
  21. updated_by_id: 1,
  22. created_by_id: 1,
  23. )
  24. assert_not(article.has_changes_to_save?)
  25. assert_not(ticket.has_changes_to_save?)
  26. calendar = Calendar.create_or_update(
  27. name: 'Escalation Test',
  28. timezone: 'Europe/Berlin',
  29. business_hours: {
  30. mon: {
  31. active: true,
  32. timeframes: [ ['00:00', '23:59'] ]
  33. },
  34. tue: {
  35. active: true,
  36. timeframes: [ ['00:00', '23:59'] ]
  37. },
  38. wed: {
  39. active: true,
  40. timeframes: [ ['00:00', '23:59'] ]
  41. },
  42. thu: {
  43. active: true,
  44. timeframes: [ ['00:00', '23:59'] ]
  45. },
  46. fri: {
  47. active: true,
  48. timeframes: [ ['00:00', '23:59'] ]
  49. },
  50. sat: {
  51. active: true,
  52. timeframes: [ ['00:00', '23:59'] ]
  53. },
  54. sun: {
  55. active: true,
  56. timeframes: [ ['00:00', '23:59'] ]
  57. },
  58. },
  59. default: true,
  60. ical_url: nil,
  61. updated_by_id: 1,
  62. created_by_id: 1,
  63. )
  64. sla = Sla.create_or_update(
  65. name: 'test sla 1',
  66. condition: {
  67. 'ticket.title' => {
  68. operator: 'contains',
  69. value: 'some value 123',
  70. },
  71. },
  72. first_response_time: 60,
  73. update_time: 180,
  74. solution_time: 240,
  75. calendar_id: calendar.id,
  76. updated_by_id: 1,
  77. created_by_id: 1,
  78. )
  79. ticket = Ticket.new(
  80. title: 'some value 123',
  81. group: Group.lookup(name: 'Users'),
  82. customer_id: 2,
  83. updated_by_id: 1,
  84. created_by_id: 1,
  85. )
  86. ticket.save!
  87. assert(ticket, 'ticket created')
  88. ticket_escalation_at = ticket.escalation_at
  89. assert(ticket.escalation_at)
  90. assert_not(ticket.has_changes_to_save?)
  91. article = Ticket::Article.create!(
  92. ticket_id: ticket.id,
  93. type_id: Ticket::Article::Type.find_by(name: 'note').id,
  94. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  95. body: 'some body',
  96. internal: false,
  97. updated_by_id: 1,
  98. created_by_id: 1,
  99. )
  100. assert_not(article.has_changes_to_save?)
  101. assert_not(ticket.has_changes_to_save?)
  102. travel 1.second
  103. sla.first_response_time = 30
  104. sla.save!
  105. ticket.save!
  106. assert_not(ticket.has_changes_to_save?)
  107. assert(ticket.escalation_at)
  108. assert_in_delta((ticket_escalation_at - 30.minutes).to_i, ticket.escalation_at.to_i, 90)
  109. sla.destroy!
  110. calendar.destroy!
  111. ticket.save!
  112. assert_not(ticket.has_changes_to_save?)
  113. assert_not(ticket.escalation_at)
  114. end
  115. test 'email process and reply via email' do
  116. calendar = Calendar.create_or_update(
  117. name: 'Escalation Test',
  118. timezone: 'Europe/Berlin',
  119. business_hours: {
  120. mon: {
  121. active: true,
  122. timeframes: [ ['00:00', '23:59'] ]
  123. },
  124. tue: {
  125. active: true,
  126. timeframes: [ ['00:00', '23:59'] ]
  127. },
  128. wed: {
  129. active: true,
  130. timeframes: [ ['00:00', '23:59'] ]
  131. },
  132. thu: {
  133. active: true,
  134. timeframes: [ ['00:00', '23:59'] ]
  135. },
  136. fri: {
  137. active: true,
  138. timeframes: [ ['00:00', '23:59'] ]
  139. },
  140. sat: {
  141. active: true,
  142. timeframes: [ ['00:00', '23:59'] ]
  143. },
  144. sun: {
  145. active: true,
  146. timeframes: [ ['00:00', '23:59'] ]
  147. },
  148. },
  149. default: true,
  150. ical_url: nil,
  151. updated_by_id: 1,
  152. created_by_id: 1,
  153. )
  154. sla = Sla.create_or_update(
  155. name: 'test sla 1',
  156. condition: {
  157. 'ticket.title' => {
  158. operator: 'contains',
  159. value: 'some value 123',
  160. },
  161. },
  162. first_response_time: 60,
  163. update_time: 180,
  164. solution_time: 240,
  165. calendar_id: calendar.id,
  166. updated_by_id: 1,
  167. created_by_id: 1,
  168. )
  169. email = "From: Bob Smith <customer@example.com>
  170. To: zammad@example.com
  171. Subject: some value 123
  172. Some Text"
  173. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email)
  174. ticket_p.reload
  175. assert(ticket_p.escalation_at)
  176. assert_in_delta(ticket_p.first_response_escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  177. assert_in_delta(ticket_p.update_escalation_at.to_i, (ticket_p.created_at + 3.hours).to_i, 90)
  178. assert_in_delta(ticket_p.close_escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  179. assert_in_delta(ticket_p.escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  180. travel 3.hours
  181. article = nil
  182. ticket_p.with_lock do
  183. article = Ticket::Article.create!(
  184. ticket_id: ticket_p.id,
  185. from: 'some_sender@example.com',
  186. to: 'some_recipient@example.com',
  187. subject: 'some subject',
  188. message_id: 'some@id',
  189. body: 'some message',
  190. internal: false,
  191. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  192. type: Ticket::Article::Type.where(name: 'email').first,
  193. updated_by_id: 1,
  194. created_by_id: 1,
  195. )
  196. end
  197. ticket_p.reload
  198. assert_in_delta(ticket_p.first_response_escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  199. assert_in_delta(ticket_p.update_escalation_at.to_i, (ticket_p.last_contact_agent_at + 3.hours).to_i, 90)
  200. assert_in_delta(ticket_p.close_escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  201. assert_in_delta(ticket_p.escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  202. end
  203. end