ticket_spec.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Sequence::Import::Freshdesk::Ticket, db_strategy: :reset, sequencer: :sequence do
  4. context 'when importing tickets from Freshdesk' do
  5. let(:group) { create(:group) }
  6. let(:resource) do
  7. {
  8. 'cc_emails' => [],
  9. 'fwd_emails' => [],
  10. 'reply_cc_emails' => [],
  11. 'ticket_cc_emails' => [],
  12. 'fr_escalated' => false,
  13. 'spam' => false,
  14. 'email_config_id' => nil,
  15. 'group_id' => 80_000_374_718,
  16. 'priority' => 1,
  17. 'requester_id' => 80_014_400_475,
  18. 'responder_id' => 80_014_400_475,
  19. 'source' => 3,
  20. 'company_id' => nil,
  21. 'status' => 2,
  22. 'subject' => 'Inline Images Failing?',
  23. 'association_type' => nil,
  24. 'support_email' => nil,
  25. 'to_emails' => ['info@zammad.org'],
  26. 'product_id' => nil,
  27. 'id' => 13,
  28. 'type' => 'Incident',
  29. 'due_by' => '2021-05-17T12:29:27Z',
  30. 'fr_due_by' => '2021-05-15T12:29:27Z',
  31. 'is_escalated' => false,
  32. 'custom_fields' => {
  33. 'cf_test_checkbox' => true,
  34. 'cf_custom_integer' => 999,
  35. 'cf_custom_dropdown' => 'key_2',
  36. 'cf_custom_decimal' => '1.1',
  37. 'cf_not_existing' => 'not_existing',
  38. },
  39. 'created_at' => '2021-05-14T12:29:27Z',
  40. 'updated_at' => '2021-05-14T12:30:19Z',
  41. 'associated_tickets_count' => nil,
  42. 'tags' => %w[example test],
  43. 'description' => "<div style=\"font-family:-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, sans-serif; font-size:14px\">\n<div dir=\"ltr\">Inline images in the first article might not be working, see following:</div>\n<div dir=\"ltr\"><img src=\"https://eucattachment.freshdesk.com/inline/attachment?token=secret_token\" style=\"width: auto\" class=\"fr-fil fr-dib\" data-id=\"80012226479\"></div>\n</div>", 'description_text' => 'Inline images in the first article might not be working, see following:'
  44. }
  45. end
  46. let(:field_map) do
  47. {
  48. 'Ticket' => {
  49. 'cf_test_checkbox' => 'cf_test_checkbox',
  50. 'cf_custom_integer' => 'cf_custom_integer',
  51. 'cf_custom_dropdown' => 'cf_custom_dropdown',
  52. 'cf_custom_decimal' => 'cf_custom_decimal'
  53. }
  54. }
  55. end
  56. let(:id_map) do
  57. {
  58. 'User' => {
  59. 80_014_400_475 => owner.id,
  60. },
  61. 'Group' => {
  62. 80_000_374_718 => group.id,
  63. },
  64. }
  65. end
  66. let(:process_payload) do
  67. {
  68. import_job: build_stubbed(:import_job, name: 'Import::Freshdesk', payload: {}),
  69. dry_run: false,
  70. resource: resource,
  71. field_map: field_map,
  72. id_map: id_map,
  73. time_entry_available: true,
  74. skip_initial_contacts: false,
  75. }
  76. end
  77. let(:owner) { create(:agent, group_ids: [group.id]) }
  78. let(:ticket_get_response_payload) do
  79. attachment_payload = {
  80. 'attachments' => [
  81. {
  82. 'id' => 80_012_226_885,
  83. 'name' => 'standalone_attachment.png',
  84. 'content_type' => 'image/png',
  85. 'size' => 11_447,
  86. 'created_at' => '2021-05-14T12:30:16Z',
  87. 'updated_at' => '2021-05-14T12:30:19Z',
  88. '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',
  89. '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'
  90. }
  91. ],
  92. }
  93. resource.merge(attachment_payload)
  94. end
  95. let(:used_urls) do
  96. [
  97. 'https://eucattachment.freshdesk.com/inline/attachment?token=secret_token',
  98. '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',
  99. ]
  100. end
  101. let(:imported_ticket) do
  102. {
  103. title: 'Inline Images Failing?',
  104. note: nil,
  105. create_article_type_id: 5,
  106. create_article_sender_id: 2,
  107. article_count: 1,
  108. state_id: 2,
  109. group_id: group.id,
  110. priority_id: 1,
  111. owner_id: owner.id,
  112. customer_id: User.last.id,
  113. type: 'Incident',
  114. cf_custom_dropdown: 'key_2',
  115. cf_custom_integer: 999,
  116. cf_test_checkbox: true,
  117. cf_custom_decimal: '1.1',
  118. }
  119. end
  120. let(:imported_article_attachments) do
  121. {
  122. 'filename' => 'standalone_attachment.png',
  123. 'size' => '3',
  124. 'preferences' => {
  125. 'Content-Type' => 'image/png',
  126. 'resizable' => false,
  127. }
  128. }
  129. end
  130. before do
  131. create(:object_manager_attribute_select, name: 'cf_custom_dropdown')
  132. create(:object_manager_attribute_integer, name: 'cf_custom_integer')
  133. create(:object_manager_attribute_boolean, name: 'cf_test_checkbox')
  134. create(:object_manager_attribute_text, name: 'cf_custom_decimal')
  135. ObjectManager::Attribute.migration_execute
  136. # Mock the attachment and inline image download requests.
  137. used_urls.each do |used_url|
  138. stub_request(:get, used_url).to_return(status: 200, body: '123', headers: {})
  139. end
  140. # Mock the ticket get request (Import::Freshdesk::Ticket::Fetch).
  141. stub_request(:get, 'https://yours.freshdesk.com/api/v2/tickets/13').to_return(status: 200, body: JSON.generate(ticket_get_response_payload), headers: {})
  142. # We only want to test here the Ticket API, so disable other modules in the sequence
  143. # that make their own HTTP requests.
  144. custom_sequence = described_class.sequence.dup
  145. custom_sequence.delete('Import::Freshdesk::Ticket::TimeEntries')
  146. custom_sequence.delete('Import::Freshdesk::Ticket::Conversations')
  147. allow(described_class).to receive(:sequence) { custom_sequence }
  148. end
  149. it 'adds tickets' do
  150. expect { process(process_payload) }.to change(Ticket, :count).by(1)
  151. end
  152. it 'correct attributes for added ticket' do
  153. process(process_payload)
  154. expect(Ticket.last).to have_attributes(imported_ticket)
  155. end
  156. it 'correct tags for added ticket' do
  157. process(process_payload)
  158. expect(Ticket.last.tag_list).to eq(%w[example test])
  159. end
  160. it 'adds article with inline image' do
  161. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  162. end
  163. it 'correct attributes for added article' do
  164. process(process_payload)
  165. attachment_list = Store.list(
  166. object: 'Ticket::Article',
  167. o_id: Ticket::Article.last.id,
  168. )
  169. expect(Ticket::Article.last).to have_attributes(
  170. to: 'info@zammad.org',
  171. body: "<div>\n<div dir=\"ltr\">Inline images in the first article might not be working, see following:</div>\n<div dir=\"ltr\"><img src=\"cid:#{attachment_list.first[:preferences]['Content-ID']}\" style=\"width: auto;\"></div>\n</div>",
  172. )
  173. end
  174. it 'adds correct number of attachments' do
  175. process(process_payload)
  176. expect(Ticket::Article.last.attachments.size).to eq 2
  177. end
  178. it 'adds attachment content' do
  179. process(process_payload)
  180. expect(Ticket::Article.last.attachments.last).to have_attributes(imported_article_attachments)
  181. end
  182. context 'when ticket is imported twice' do
  183. before do
  184. process(process_payload)
  185. end
  186. it 'updates first article for already existing ticket' do
  187. expect { process(process_payload) }.not_to change(Ticket::Article, :count)
  188. end
  189. it 'correct tags for added ticket' do
  190. process(process_payload)
  191. expect(Ticket.last.tag_list).to eq(%w[example test])
  192. end
  193. end
  194. context 'when importing without a type' do
  195. let(:resource) do
  196. super().merge(
  197. 'type' => nil
  198. )
  199. end
  200. let(:imported_ticket) do
  201. super().merge(
  202. type: nil
  203. )
  204. end
  205. it 'correct attributes for added ticket' do
  206. process(process_payload)
  207. expect(Ticket.last).to have_attributes(imported_ticket)
  208. end
  209. end
  210. end
  211. end