post_spec.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Sequence::Import::Kayako::Post, sequencer: :sequence do
  4. context 'when importing posts from Kayako' do
  5. let(:user) { create(:user) }
  6. let(:customer) { create(:customer) }
  7. let(:ticket) { create(:ticket) }
  8. let(:resource) do
  9. {
  10. 'id' => 99_999,
  11. 'uuid' => '179a033a-7582-4def-ae57-b8f077eaee5b',
  12. 'client_id' => '',
  13. 'subject' => 'Getting comfortable with Kayako: a sample conversation',
  14. 'contents' => "[img src=\"https://yours.kayako.com/media/url/UB6tba5kStQ7pL1i247kJ2blopDsywfn\" class=\"fr-fic fr-dii\" style=\"width: 127px; height: 96.3263px;\" width=\"127\" height=\"96.3263\"]\n\nA Test with a inline image.\n",
  15. 'creator' => {
  16. 'id' => 80_014_400_777,
  17. 'resource_type' => 'user'
  18. },
  19. 'identity' => {
  20. 'id' => 80_014_400_777,
  21. 'email' => customer.email,
  22. 'resource_type' => 'identity_email',
  23. },
  24. 'source_channel' => {
  25. 'uuid' => 'e955e374-8324-4637-97a5-763cd4010997',
  26. 'type' => 'MAIL',
  27. 'character_limit' => nil,
  28. 'account' => {
  29. 'id' => 1,
  30. 'resource_type' => 'mailbox'
  31. },
  32. 'resource_type' => 'channel'
  33. },
  34. 'attachments' => [
  35. {
  36. 'id' => 3,
  37. 'name' => 'example.json',
  38. 'size' => 100,
  39. 'width' => 0,
  40. 'height' => 0,
  41. 'type' => 'application/json',
  42. 'content_id' => nil,
  43. 'alt' => nil,
  44. 'url' => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/3/url',
  45. 'url_download' => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/3/download',
  46. 'thumbnails' => [],
  47. 'created_at' => '2021-08-16T08:43:46+00:00',
  48. 'resource_type' => 'attachment',
  49. },
  50. {
  51. 'id' => 2,
  52. 'name' => 'example.log',
  53. 'size' => 1909,
  54. 'width' => 0,
  55. 'height' => 0,
  56. 'type' => 'text/plain',
  57. 'content_id' => nil,
  58. 'alt' => nil,
  59. 'url' => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/2/url',
  60. 'url_download' => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/2/download',
  61. 'thumbnails' => [],
  62. 'created_at' => '2021-08-16T08:43:46+00:00',
  63. 'resource_type' => 'attachment',
  64. },
  65. ],
  66. 'original' => {
  67. 'id' => 4,
  68. 'uuid' => '179a033a-7582-4def-ae57-b8f077eaee5b',
  69. 'subject' => 'Getting comfortable with Kayako: a sample conversation',
  70. 'body_text' => "[img src=\"https://yours.kayako.com/media/url/UB6tba5kStQ7pL1i247kJ2blopDsywfn\" class=\"fr-fic fr-dii\" style=\"width: 127px; height: 96.3263px;\" width=\"127\" height=\"96.3263\"]\n\nA Test with a inline image.\n",
  71. 'body_html' => '<img src="https://yours.kayako.com/media/url/UB6tba5kStQ7pL1i247kJ2blopDsywfn" class="fr-fic fr-dii" style="width: 127px; height: 96.3263px;" width="127" height="96.3263"><br><br>A Test with a inline image.<br>',
  72. 'recipients' => [],
  73. 'fullname' => customer.fullname,
  74. 'email' => customer.email,
  75. 'creator' => {
  76. 'id' => 80_014_400_777,
  77. 'resource_type' => 'user'
  78. },
  79. 'identity' => {
  80. 'id' => 80_014_400_777,
  81. 'email' => customer.email,
  82. 'resource_type' => 'identity_email',
  83. },
  84. 'mailbox' => {
  85. 'id' => 1,
  86. 'uuid' => 'e955e374-8324-4637-97a5-763cd4010997',
  87. 'address' => 'info@zammad.org',
  88. 'resource_type' => 'mailbox',
  89. },
  90. 'attachments' => [],
  91. 'download_all' => nil,
  92. 'locale' => nil,
  93. 'response_time' => 0,
  94. 'created_at' => '2021-08-16T08:19:40+00:00',
  95. 'updated_at' => '2021-08-16T08:19:40+00:00',
  96. 'resource_type' => 'case_message',
  97. },
  98. 'is_requester' => true,
  99. 'created_at' => '2021-08-16T08:19:40+00:00',
  100. 'updated_at' => '2021-08-16T08:30:11+00:00',
  101. 'resource_type' => 'post',
  102. }
  103. end
  104. let(:used_urls) do
  105. [
  106. 'https://yours.kayako.com/media/url/UB6tba5kStQ7pL1i247kJ2blopDsywfn',
  107. 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/2/download'
  108. ]
  109. end
  110. let(:id_map) do
  111. {
  112. 'Ticket' => {
  113. 1001 => ticket.id,
  114. },
  115. 'User' => {
  116. 80_014_400_745 => user.id,
  117. 80_014_400_777 => customer.id,
  118. }
  119. }
  120. end
  121. let(:process_payload) do
  122. {
  123. import_job: build_stubbed(:import_job, name: 'Import::Kayako', payload: {}),
  124. dry_run: false,
  125. resource: resource,
  126. field_map: {},
  127. id_map: id_map,
  128. instance: ticket,
  129. default_language: 'en-us',
  130. }
  131. end
  132. let(:imported_ticket_article_attachment) do
  133. {
  134. filename: 'example.log',
  135. size: '3',
  136. preferences: {
  137. 'Content-Type': 'text/plain'
  138. }
  139. }
  140. end
  141. let(:imported_ticket_article_attachment_json) do
  142. {
  143. filename: 'example.json',
  144. size: '12',
  145. preferences: {
  146. 'Content-Type': 'application/json'
  147. }
  148. }
  149. end
  150. before do
  151. # Mock the attachment and inline image download requests.
  152. used_urls.each do |used_url|
  153. stub_request(:get, used_url).to_return(status: 200, body: '123', headers: {})
  154. end
  155. # Special handling for json situation
  156. stub_request(:get, 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/3/download').to_return(status: 200, body: '{test: 123,}', headers: { 'content-type': 'application/json' })
  157. end
  158. it 'adds article with inline image' do
  159. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  160. end
  161. it 'correct attributes for added article' do
  162. process(process_payload)
  163. attachment_list = Store.list(
  164. object: 'Ticket::Article',
  165. o_id: Ticket::Article.last.id,
  166. )
  167. expect(Ticket::Article.last).to have_attributes(
  168. to: 'info@zammad.org',
  169. body: "<img src=\"cid:#{attachment_list.first[:preferences]['Content-ID']}\" style=\"width: 127px; height: 96.3263px;\"><br><br>A Test with a inline image.<br>",
  170. )
  171. end
  172. it 'updates already existing article' do
  173. expect do
  174. process(process_payload)
  175. process(process_payload)
  176. end.to change(Ticket::Article, :count).by(1)
  177. end
  178. it 'adds correct number of attachments' do
  179. process(process_payload)
  180. expect(Ticket::Article.last.attachments.size).to eq 3
  181. end
  182. it 'adds attachment content' do
  183. process(process_payload)
  184. expect(Ticket::Article.last.attachments).to include(have_attributes(imported_ticket_article_attachment))
  185. .and include(have_attributes(imported_ticket_article_attachment_json))
  186. end
  187. end
  188. end