comment_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'zendesk_api'
  4. RSpec.describe Sequencer::Sequence::Import::Zendesk::Ticket::Comment, db_strategy: :reset, required_envs: %w[IMPORT_ZENDESK_ENDPOINT], sequencer: :sequence do
  5. let(:hostname) { URI.parse(ENV['IMPORT_ZENDESK_ENDPOINT']).hostname }
  6. context 'when importing ticket comments from Zendesk' do
  7. let(:customer) { create(:customer) }
  8. let(:ticket) { create(:ticket) }
  9. let(:inline_image_url) { "https://#{hostname}/attachments/token/khRQTQjm8ODhA0FjbS39i4xOb/?name=1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg" }
  10. let(:resource) do
  11. ZendeskAPI::Ticket::Comment.new(
  12. nil,
  13. {
  14. 'id' => 31_964_468_581,
  15. 'type' => 'Comment',
  16. 'author_id' => 1_150_734_731,
  17. 'html_body' => "<div class=\"zd-comment\" dir=\"auto\"><p dir=\"auto\">This is the latest comment for this ticket. You also changed the ticket status to Pending.</p><span style=\"opacity: 1;\"><img src=\"#{inline_image_url}\"></span><a href=\"/agent/tickets/1\" rel=\"ticket\">#1</a></p></div>",
  18. 'public' => true,
  19. 'attachments' => [
  20. {
  21. 'id' => 1_282_310_719,
  22. 'file_name' => '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
  23. 'content_url' => "https://#{hostname}/attachments/token/khRQTQjm8ODhA0FjbS39i4xOb/?name=1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg",
  24. 'mapped_content_url' => "https://#{hostname}/attachments/token/khRQTQjm8ODhA0FjbS39i4xOb/?name=1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg",
  25. 'content_type' => 'image/jpeg',
  26. 'size' => 164_934,
  27. 'width' => 1600,
  28. 'height' => 1200,
  29. 'inline' => false,
  30. 'deleted' => false,
  31. 'thumbnails' => []
  32. }
  33. ],
  34. 'audit_id' => 31_964_468_571,
  35. 'via' => {
  36. 'channel' => 'email',
  37. 'source' => {
  38. 'from' => {
  39. 'address' => 'john.doe@example.com',
  40. 'name' => 'John Doe',
  41. 'original_recipients' => [
  42. 'zendesk@example.com'
  43. ]
  44. },
  45. 'to' => {
  46. 'name' => 'Znuny',
  47. 'address' => 'zendesk@example.com'
  48. },
  49. 'rel' => nil
  50. },
  51. },
  52. 'created_at' => '2018-09-28T12:00:00Z',
  53. 'metadata' => {
  54. 'system' => {},
  55. 'custom' => {}
  56. }
  57. }
  58. )
  59. end
  60. let(:user_map) do
  61. {
  62. 1_150_734_731 => customer.id,
  63. }
  64. end
  65. let(:process_payload) do
  66. {
  67. import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
  68. dry_run: false,
  69. instance: ticket,
  70. resource: resource,
  71. user_map: user_map,
  72. field_map: {},
  73. }
  74. end
  75. let(:imported_attachment) do
  76. {
  77. 'filename' => '1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg',
  78. 'size' => '3',
  79. 'preferences' => {
  80. 'Content-Type' => 'image/jpeg',
  81. 'resizable' => false,
  82. 'content_preview' => true,
  83. }
  84. }
  85. end
  86. def imported_article(inline_image_cid)
  87. {
  88. from: 'john.doe@example.com',
  89. to: 'zendesk@example.com',
  90. body: "\n<div dir=\"auto\">\n<p dir=\"auto\">This is the latest comment for this ticket. You also changed the ticket status to Pending.</p>\n<span><img src=\"cid:#{inline_image_cid}\"></span><a href=\"/#ticket/zoom/1\" rel=\"ticket\">#1</a>\n</div>\n",
  91. created_at: Time.zone.parse('2018-09-28T12:00:00Z'),
  92. updated_at: Time.zone.parse('2018-09-28T12:00:00Z'),
  93. }
  94. end
  95. before do
  96. stub_request(:get, "https://#{hostname}/attachments/token/khRQTQjm8ODhA0FjbS39i4xOb/?name=1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg").to_return(status: 200, body: '123', headers: {})
  97. end
  98. context 'with an email article' do
  99. it 'imports article correctly' do
  100. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  101. end
  102. it 'imports ticket data correctly' do
  103. process(process_payload)
  104. attachment_list = Store.list(
  105. object: 'Ticket::Article',
  106. o_id: Ticket::Article.last.id,
  107. )
  108. expect(Ticket::Article.last).to have_attributes(imported_article(attachment_list.first[:preferences]['Content-ID']))
  109. end
  110. it 'adds correct number of attachments' do
  111. process(process_payload)
  112. expect(Ticket::Article.last.attachments.size).to eq 2
  113. end
  114. it 'adds attachment content' do
  115. process(process_payload)
  116. expect(Ticket::Article.last.attachments.last).to have_attributes(imported_attachment)
  117. end
  118. end
  119. context 'when attachment request has an error' do
  120. before do
  121. allow_any_instance_of(Sequencer::Unit::Import::Zendesk::Ticket::Comment::Attachment::Request).to receive(:sleep)
  122. stub_request(:get, "https://#{hostname}/attachments/token/khRQTQjm8ODhA0FjbS39i4xOb/?name=1a3496b9-53d9-494d-bbb0-e1d2e22074f8.jpeg").to_return(status: 503, headers: {}).then.to_return(status: 200, body: '123', headers: {})
  123. end
  124. it 'adds attachment content after one request error' do
  125. process(process_payload)
  126. expect(Ticket::Article.last.attachments.last).to have_attributes(imported_attachment)
  127. end
  128. end
  129. end
  130. end