escalation_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. require 'rails_helper'
  2. RSpec.describe 'Ticket Escalation', type: :request do
  3. let(:sla_first_response) { 1.hour }
  4. let(:sla_update) { 3.hours }
  5. let(:sla_close) { 4.hours }
  6. let!(:mail_group) { create(:group, email_address: create(:email_address) ) }
  7. let(:calendar) { create(:calendar, :'24/7') }
  8. let(:sla) do
  9. create(:sla,
  10. calendar: calendar,
  11. first_response_time: sla_first_response / 1.minute,
  12. update_time: sla_update / 1.minute,
  13. solution_time: sla_close / 1.minute)
  14. end
  15. define :json_equal_date do
  16. match do
  17. actual&.sub(%r{.\d\d\dZ$}, 'Z') == expected&.iso8601
  18. end
  19. end
  20. shared_examples 'response matching object' do
  21. %w[escalation_at first_response_escalation_at update_escalation_at close_escalation_at].each do |attribute|
  22. it "#{attribute} is representing the same time" do
  23. expect(json_response[attribute]).to json_equal_date ticket[attribute]
  24. end
  25. end
  26. end
  27. before do
  28. freeze_time
  29. sla
  30. end
  31. context 'when customer creates ticket via web', authenticated_as: :customer do
  32. subject(:ticket) { Ticket.find(json_response['id']) }
  33. let(:customer) { create(:customer) }
  34. before do
  35. params = {
  36. title: 'some value 123',
  37. group: mail_group.name,
  38. article: {
  39. type_id: Ticket::Article::Type.find_by(name: 'web').id,
  40. body: 'some test 123',
  41. },
  42. }
  43. post '/api/v1/tickets', params: params, as: :json
  44. end
  45. it_behaves_like 'response matching object'
  46. it 'first response escalation in 1h' do
  47. expect(ticket.first_response_escalation_at).to eq 1.hour.from_now
  48. end
  49. it 'update_escalation in 3h' do
  50. expect(ticket.update_escalation_at).to eq 3.hours.from_now
  51. end
  52. it 'close escalation in 4h' do
  53. expect(ticket.close_escalation_at).to eq 4.hours.from_now
  54. end
  55. it 'next escalation is closest escalation' do
  56. expect(ticket.escalation_at).to eq 1.hour.from_now
  57. end
  58. end
  59. context 'when customer sends email' do
  60. subject(:ticket) { ticket_mail_in }
  61. before { ticket }
  62. it 'first response escalation in 1h' do
  63. expect(ticket.first_response_escalation_at).to eq 1.hour.from_now
  64. end
  65. it 'update_escalation in 3h' do
  66. expect(ticket.update_escalation_at).to eq 3.hours.from_now
  67. end
  68. it 'close escalation in 4h' do
  69. expect(ticket.close_escalation_at).to eq 4.hours.from_now
  70. end
  71. it 'next escalation is closest escalation' do
  72. expect(ticket.escalation_at).to eq 1.hour.from_now
  73. end
  74. end
  75. context 'when agent responds via web', authenticated_as: :agent do
  76. subject(:ticket) { ticket_mail_in }
  77. let(:agent) { create(:agent, groups: Group.all) }
  78. before { ticket && travel(3.hours) }
  79. it_behaves_like 'response matching object' do
  80. before { ticket_respond_web }
  81. end
  82. it 'clears first response escalation' do
  83. expect { ticket_respond_web }.to change(ticket, :first_response_escalation_at).to(nil)
  84. end
  85. it 'changes update escalation' do
  86. expect { ticket_respond_web }.to change(ticket, :update_escalation_at)
  87. end
  88. it 'update escalation is nil since agent responded' do
  89. ticket_respond_web
  90. expect(ticket.update_escalation_at).to be_nil
  91. end
  92. it 'does not change close escalation' do
  93. expect { ticket_respond_web }.not_to change(ticket, :close_escalation_at)
  94. end
  95. it 'change next escalation' do
  96. expect { ticket_respond_web }.to change(ticket, :escalation_at)
  97. end
  98. it 'next escalation is closest escalation which is close escalation' do
  99. ticket_respond_web
  100. expect(ticket.escalation_at).to eq 1.hour.from_now
  101. end
  102. def ticket_respond_web
  103. params = {
  104. title: 'some value 123 - update',
  105. article: {
  106. type_id: Ticket::Article::Type.find_by(name: 'email').id,
  107. body: 'some test 123',
  108. type: 'email',
  109. to: 'customer@example.com',
  110. },
  111. }
  112. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  113. ticket.reload
  114. end
  115. end
  116. def ticket_mail_in
  117. email = <<~EMAIL
  118. From: Bob Smith <customer@example.com>
  119. To: #{mail_group.email_address.email}
  120. Subject: some value 123
  121. Some Text
  122. EMAIL
  123. ticket, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email)
  124. ticket
  125. end
  126. end