post_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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' => 1,
  37. 'name' => 'example.log',
  38. 'size' => 1909,
  39. 'width' => 0,
  40. 'height' => 0,
  41. 'type' => 'text/plain',
  42. 'content_id' => nil,
  43. 'alt' => nil,
  44. 'url' => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/2/url',
  45. 'url_download' => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/2/download',
  46. 'thumbnails' => [],
  47. 'created_at' => '2021-08-16T08:43:46+00:00',
  48. 'resource_type' => 'attachment',
  49. }
  50. ],
  51. 'original' => {
  52. 'id' => 4,
  53. 'uuid' => '179a033a-7582-4def-ae57-b8f077eaee5b',
  54. 'subject' => 'Getting comfortable with Kayako: a sample conversation',
  55. '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",
  56. '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>',
  57. 'recipients' => [],
  58. 'fullname' => customer.fullname,
  59. 'email' => customer.email,
  60. 'creator' => {
  61. 'id' => 80_014_400_777,
  62. 'resource_type' => 'user'
  63. },
  64. 'identity' => {
  65. 'id' => 80_014_400_777,
  66. 'email' => customer.email,
  67. 'resource_type' => 'identity_email',
  68. },
  69. 'mailbox' => {
  70. 'id' => 1,
  71. 'uuid' => 'e955e374-8324-4637-97a5-763cd4010997',
  72. 'address' => 'info@zammad.org',
  73. 'resource_type' => 'mailbox',
  74. },
  75. 'attachments' => [],
  76. 'download_all' => nil,
  77. 'locale' => nil,
  78. 'response_time' => 0,
  79. 'created_at' => '2021-08-16T08:19:40+00:00',
  80. 'updated_at' => '2021-08-16T08:19:40+00:00',
  81. 'resource_type' => 'case_message',
  82. },
  83. 'is_requester' => true,
  84. 'created_at' => '2021-08-16T08:19:40+00:00',
  85. 'updated_at' => '2021-08-16T08:30:11+00:00',
  86. 'resource_type' => 'post',
  87. }
  88. end
  89. let(:used_urls) do
  90. [
  91. 'https://yours.kayako.com/media/url/UB6tba5kStQ7pL1i247kJ2blopDsywfn',
  92. 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/2/download'
  93. ]
  94. end
  95. let(:id_map) do
  96. {
  97. 'Ticket' => {
  98. 1001 => ticket.id,
  99. },
  100. 'User' => {
  101. 80_014_400_745 => user.id,
  102. 80_014_400_777 => customer.id,
  103. }
  104. }
  105. end
  106. let(:process_payload) do
  107. {
  108. import_job: build_stubbed(:import_job, name: 'Import::Kayako', payload: {}),
  109. dry_run: false,
  110. resource: resource,
  111. field_map: {},
  112. id_map: id_map,
  113. instance: ticket,
  114. default_language: 'en-us',
  115. }
  116. end
  117. let(:imported_ticket_article_attachment) do
  118. {
  119. filename: 'example.log',
  120. size: '3',
  121. preferences: {
  122. 'Content-Type': 'text/plain'
  123. }
  124. }
  125. end
  126. before do
  127. # Mock the attachment and inline image download requests.
  128. used_urls.each do |used_url|
  129. stub_request(:get, used_url).to_return(status: 200, body: '123', headers: {})
  130. end
  131. end
  132. it 'adds article with inline image' do
  133. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  134. end
  135. it 'correct attributes for added article' do
  136. process(process_payload)
  137. attachment_list = Store.list(
  138. object: 'Ticket::Article',
  139. o_id: Ticket::Article.last.id,
  140. )
  141. expect(Ticket::Article.last).to have_attributes(
  142. to: 'info@zammad.org',
  143. 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>",
  144. )
  145. end
  146. it 'updates already existing article' do
  147. expect do
  148. process(process_payload)
  149. process(process_payload)
  150. end.to change(Ticket::Article, :count).by(1)
  151. end
  152. it 'adds correct number of attachments' do
  153. process(process_payload)
  154. expect(Ticket::Article.last.attachments.size).to eq 2
  155. end
  156. it 'adds attachment content' do
  157. process(process_payload)
  158. expect(Ticket::Article.last.attachments.last).to have_attributes(imported_ticket_article_attachment)
  159. end
  160. end
  161. end