conversation_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Sequence::Import::Freshdesk::Conversation, sequencer: :sequence do
  4. context 'when importing conversations from Freshdesk' do
  5. let(:inline_image_url) { 'https://eucattachment.freshdesk.com/inline/attachment?token=secret_token' }
  6. let(:resource) do
  7. {
  8. 'body' => "<div style=\"font-family:-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, sans-serif; font-size:14px\">\n<div dir=\"ltr\">Let's see if inline images work in a subsequent article:</div>\n<div dir=\"ltr\"><img src=\"#{inline_image_url}\" style=\"width: auto\" class=\"fr-fil fr-dib\" data-id=\"80012226853\"></div>\n</div>", 'body_text' => "Let's see if inline images work in a subsequent article:",
  9. 'id' => 80_027_218_656,
  10. 'incoming' => false,
  11. 'private' => true,
  12. 'user_id' => 80_014_400_475,
  13. 'support_email' => nil,
  14. 'source' => 2,
  15. 'category' => 2,
  16. 'to_emails' => ['info@zammad.org'],
  17. 'from_email' => nil,
  18. 'cc_emails' => [],
  19. 'bcc_emails' => nil,
  20. 'email_failure_count' => nil,
  21. 'outgoing_failures' => nil,
  22. 'created_at' => '2021-05-14T12:30:19Z',
  23. 'updated_at' => '2021-05-14T12:30:19Z',
  24. 'attachments' => [
  25. {
  26. 'id' => 80_012_226_885,
  27. 'name' => 'standalone_attachment.png',
  28. 'content_type' => 'image/png',
  29. 'size' => 11_447,
  30. 'created_at' => '2021-05-14T12:30:16Z',
  31. 'updated_at' => '2021-05-14T12:30:19Z',
  32. 'attachment_url' => 'https://s3.eu-central-1.amazonaws.com/euc-cdn.freshdesk.com/data/helpdesk/attachments/production/80012226885/original/standalone_attachment.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=secret-amz-credential&X-Amz-Date=20210514T123300Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=750988d37a6f2f43830bfd19c895517aa051aa13b4ab26a1333369d414fef0be',
  33. 'thumb_url' => 'https://s3.eu-central-1.amazonaws.com/euc-cdn.freshdesk.com/data/helpdesk/attachments/production/80012226885/thumb/standalone_attachment.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=secret-amz-credential&X-Amz-Date=20210514T123300Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=40b5fe1d7d418bcbd1e639b273a1038c7a73781c16d9881c2f31a11c6bebfdf9'
  34. }
  35. ],
  36. 'auto_response' => false,
  37. 'ticket_id' => 1001,
  38. 'source_additional_info' => nil
  39. }
  40. end
  41. let(:used_urls) do
  42. [
  43. 'https://eucattachment.freshdesk.com/inline/attachment?token=secret_token',
  44. 'https://s3.eu-central-1.amazonaws.com/euc-cdn.freshdesk.com/data/helpdesk/attachments/production/80012226885/original/standalone_attachment.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=secret-amz-credential&X-Amz-Date=20210514T123300Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=750988d37a6f2f43830bfd19c895517aa051aa13b4ab26a1333369d414fef0be',
  45. ]
  46. end
  47. let(:ticket) { create(:ticket) }
  48. let(:id_map) do
  49. {
  50. 'Ticket' => {
  51. 1001 => ticket.id,
  52. },
  53. 'User' => {
  54. 80_014_400_475 => 1,
  55. }
  56. }
  57. end
  58. let(:process_payload) do
  59. {
  60. import_job: build_stubbed(:import_job, name: 'Import::Freshdesk', payload: {}),
  61. dry_run: false,
  62. resource: resource,
  63. field_map: {},
  64. id_map: id_map,
  65. time_entry_available: false,
  66. skip_initial_contacts: false,
  67. }
  68. end
  69. before do
  70. # Mock the attachment and inline image download requests.
  71. used_urls.each do |used_url|
  72. stub_request(:get, used_url).to_return(status: 200, body: '123', headers: {})
  73. end
  74. end
  75. shared_examples 'import article' do
  76. it 'adds article with inline image' do
  77. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  78. end
  79. it 'correct attributes for added article' do
  80. process(process_payload)
  81. attachment_list = Store.list(
  82. object: 'Ticket::Article',
  83. o_id: Ticket::Article.last.id,
  84. )
  85. expect(Ticket::Article.last).to have_attributes(
  86. to: 'info@zammad.org',
  87. body: "<div>\n<div dir=\"ltr\">Let's see if inline images work in a subsequent article:</div>\n<div dir=\"ltr\"><img src=\"cid:#{attachment_list.first[:preferences]['Content-ID']}\" style=\"width: auto;\"></div>\n</div>",
  88. )
  89. end
  90. end
  91. include_examples 'import article'
  92. it 'updates already existing article' do
  93. expect do
  94. process(process_payload)
  95. process(process_payload)
  96. end.to change(Ticket::Article, :count).by(1)
  97. end
  98. it 'adds correct number of attachments' do
  99. process(process_payload)
  100. expect(Ticket::Article.last.attachments.size).to eq 2
  101. end
  102. it 'adds attachment content' do
  103. process(process_payload)
  104. expect(Ticket::Article.last.attachments.last).to have_attributes(
  105. 'filename' => 'standalone_attachment.png',
  106. 'size' => '3',
  107. 'preferences' => {
  108. 'Content-Type' => 'image/png',
  109. 'resizable' => false,
  110. }
  111. )
  112. end
  113. context 'when handling special inline images' do
  114. context 'when inline image source contains special urls (e.g. "cid:https://...")' do
  115. let(:inline_image_url) { 'cid:https://eucattachment.freshdesk.com/inline/attachment?token=secret_token' }
  116. include_examples 'import article'
  117. end
  118. context 'when inline image source contains broken urls' do
  119. let(:inline_image_url) { 'broken_image_url' }
  120. it 'skips image download with broken inline image url' do
  121. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  122. end
  123. it 'correct attributes for added article' do
  124. process(process_payload)
  125. expect(Ticket::Article.last).to have_attributes(
  126. to: 'info@zammad.org',
  127. body: "<div>\n<div dir=\"ltr\">Let's see if inline images work in a subsequent article:</div>\n<div dir=\"ltr\"><img src=\"broken_image_url\" style=\"width: auto;\"></div>\n</div>",
  128. )
  129. end
  130. end
  131. end
  132. end
  133. end