tickets_controller_escalation_test.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. require 'test_helper'
  2. class TicketsControllerEscalationTest < ActionDispatch::IntegrationTest
  3. setup do
  4. # set accept header
  5. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  6. # create agent
  7. roles = Role.where(name: %w[Admin Agent])
  8. groups = Group.all
  9. UserInfo.current_user_id = 1
  10. @admin = User.create!(
  11. login: 'tickets-admin',
  12. firstname: 'Tickets',
  13. lastname: 'Admin',
  14. email: 'tickets-admin@example.com',
  15. password: 'adminpw',
  16. active: true,
  17. roles: roles,
  18. groups: groups,
  19. )
  20. # create agent
  21. roles = Role.where(name: 'Agent')
  22. @agent = User.create!(
  23. login: 'tickets-agent@example.com',
  24. firstname: 'Tickets',
  25. lastname: 'Agent',
  26. email: 'tickets-agent@example.com',
  27. password: 'agentpw',
  28. active: true,
  29. roles: roles,
  30. groups: groups,
  31. )
  32. # create customer without org
  33. roles = Role.where(name: 'Customer')
  34. @customer_without_org = User.create!(
  35. login: 'tickets-customer1@example.com',
  36. firstname: 'Tickets',
  37. lastname: 'Customer1',
  38. email: 'tickets-customer1@example.com',
  39. password: 'customer1pw',
  40. active: true,
  41. roles: roles,
  42. )
  43. @calendar = Calendar.create!(
  44. name: 'Escalation Test',
  45. timezone: 'Europe/Berlin',
  46. business_hours: {
  47. mon: {
  48. active: true,
  49. timeframes: [ ['00:00', '23:59'] ]
  50. },
  51. tue: {
  52. active: true,
  53. timeframes: [ ['00:00', '23:59'] ]
  54. },
  55. wed: {
  56. active: true,
  57. timeframes: [ ['00:00', '23:59'] ]
  58. },
  59. thu: {
  60. active: true,
  61. timeframes: [ ['00:00', '23:59'] ]
  62. },
  63. fri: {
  64. active: true,
  65. timeframes: [ ['00:00', '23:59'] ]
  66. },
  67. sat: {
  68. active: true,
  69. timeframes: [ ['00:00', '23:59'] ]
  70. },
  71. sun: {
  72. active: true,
  73. timeframes: [ ['00:00', '23:59'] ]
  74. },
  75. },
  76. default: true,
  77. ical_url: nil,
  78. )
  79. @sla = Sla.create!(
  80. name: 'test sla 1',
  81. condition: {
  82. 'ticket.title' => {
  83. operator: 'contains',
  84. value: 'some value 123',
  85. },
  86. },
  87. first_response_time: 60,
  88. update_time: 180,
  89. solution_time: 240,
  90. calendar_id: @calendar.id,
  91. )
  92. UserInfo.current_user_id = nil
  93. end
  94. test '01.01 ticket created via web' do
  95. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  96. params = {
  97. title: 'some value 123',
  98. group: 'Users',
  99. article: {
  100. body: 'some test 123',
  101. },
  102. }
  103. post '/api/v1/tickets', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  104. assert_response(201)
  105. result = JSON.parse(@response.body)
  106. assert_equal(Hash, result.class)
  107. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  108. assert_equal('some value 123', result['title'])
  109. assert_equal(@customer_without_org.id, result['updated_by_id'])
  110. assert_equal(@customer_without_org.id, result['created_by_id'])
  111. ticket_p = Ticket.find(result['id'])
  112. assert_equal(ticket_p['escalation_at'].iso8601, result['escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  113. assert_equal(ticket_p['first_response_escalation_at'].iso8601, result['first_response_escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  114. assert_equal(ticket_p['update_escalation_at'].iso8601, result['update_escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  115. assert_equal(ticket_p['close_escalation_at'].iso8601, result['close_escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  116. assert(ticket_p.escalation_at)
  117. assert_in_delta(ticket_p.first_response_escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  118. assert_in_delta(ticket_p.update_escalation_at.to_i, (ticket_p.created_at + 3.hours).to_i, 90)
  119. assert_in_delta(ticket_p.close_escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  120. assert_in_delta(ticket_p.escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  121. end
  122. test '01.02 ticket got created via email - reply by agent via web' do
  123. email = "From: Bob Smith <customer@example.com>
  124. To: zammad@example.com
  125. Subject: some value 123
  126. Some Text"
  127. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email)
  128. ticket_p.reload
  129. assert(ticket_p.escalation_at)
  130. assert_in_delta(ticket_p.first_response_escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  131. assert_in_delta(ticket_p.update_escalation_at.to_i, (ticket_p.created_at + 3.hours).to_i, 90)
  132. assert_in_delta(ticket_p.close_escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  133. assert_in_delta(ticket_p.escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  134. travel 3.hours
  135. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  136. params = {
  137. title: 'some value 123 - update',
  138. article: {
  139. body: 'some test 123',
  140. type: 'email',
  141. to: 'customer@example.com',
  142. },
  143. }
  144. put "/api/v1/tickets/#{ticket_p.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  145. assert_response(200)
  146. result = JSON.parse(@response.body)
  147. assert_equal(Hash, result.class)
  148. assert_equal(Ticket::State.lookup(name: 'open').id, result['state_id'])
  149. assert_equal('some value 123 - update', result['title'])
  150. assert_equal(@agent.id, result['updated_by_id'])
  151. assert_equal(user_p.id, result['created_by_id'])
  152. ticket_p.reload
  153. assert_equal(ticket_p['escalation_at'].iso8601, result['escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  154. assert_equal(ticket_p['first_response_escalation_at'].iso8601, result['first_response_escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  155. assert_equal(ticket_p['update_escalation_at'].iso8601, result['update_escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  156. assert_equal(ticket_p['close_escalation_at'].iso8601, result['close_escalation_at'].sub(/.\d\d\dZ$/, 'Z'))
  157. assert_in_delta(ticket_p.first_response_escalation_at.to_i, (ticket_p.created_at + 1.hour).to_i, 90)
  158. assert_in_delta(ticket_p.update_escalation_at.to_i, (ticket_p.last_contact_agent_at + 3.hours).to_i, 90)
  159. assert_in_delta(ticket_p.close_escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  160. assert_in_delta(ticket_p.escalation_at.to_i, (ticket_p.created_at + 4.hours).to_i, 90)
  161. end
  162. end