case_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/sequencer/sequence/import/kayako/examples/object_custom_field_values_examples'
  4. RSpec.describe Sequencer::Sequence::Import::Kayako::Case, db_strategy: :reset, sequencer: :sequence do
  5. context 'when importing cases from Kayako' do
  6. let(:group) { create(:group) }
  7. let(:owner) { create(:agent, group_ids: [group.id]) }
  8. let(:organization) { create(:organization) }
  9. let(:customer) { create(:customer, organization: organization) }
  10. let(:resource) do
  11. {
  12. 'id' => 9999,
  13. 'legacy_id' => nil,
  14. 'subject' => 'Getting comfortable with Kayako: a sample conversation',
  15. 'portal' => 'SETUP',
  16. 'source_channel' => {
  17. 'uuid' => 'e955e374-8324-4637-97a5-763cd4010997',
  18. 'type' => 'MAIL',
  19. 'character_limit' => nil,
  20. 'resource_type' => 'channel'
  21. },
  22. 'requester' => {
  23. 'id' => 80_014_400_777,
  24. 'organization' => {
  25. 'id' => 80_014_400_111,
  26. 'resource_type' => 'organization'
  27. },
  28. 'resource_type' => 'user',
  29. },
  30. 'creator' => {
  31. 'id' => 80_014_400_777,
  32. 'resource_type' => 'user',
  33. },
  34. 'identity' => {
  35. 'id' => 80_014_400_777,
  36. 'resource_type' => 'identity_email'
  37. },
  38. 'assigned_agent' => {
  39. 'id' => 80_014_400_475,
  40. 'resource_type' => 'user',
  41. },
  42. 'assigned_team' => {
  43. 'id' => 80_000_374_718,
  44. 'resource_type' => 'team'
  45. },
  46. 'status' => {
  47. 'id' => 2,
  48. 'label' => 'Open',
  49. 'type' => 'OPEN',
  50. 'sort_order' => 2,
  51. 'is_sla_active' => true,
  52. 'is_deleted' => false,
  53. 'created_at' => '2021-08-12T11:48:51+00:00',
  54. 'updated_at' => '2021-08-12T11:48:51+00:00',
  55. 'resource_type' => 'case_status',
  56. },
  57. 'priority' => {
  58. 'id' => 1,
  59. 'label' => 'Low',
  60. 'level' => 1,
  61. 'created_at' => '2021-08-12T11:48:51+00:00',
  62. 'updated_at' => '2021-08-12T11:48:51+00:00',
  63. 'resource_type' => 'case_priority',
  64. },
  65. 'type' => {
  66. 'id' => 1,
  67. 'label' => 'Question',
  68. 'type' => 'QUESTION',
  69. 'created_at' => '2021-08-12T11:48:51+00:00',
  70. 'updated_at' => '2021-08-12T11:48:51+00:00',
  71. },
  72. 'last_updated_by' => {
  73. 'id' => 80_000_374_718,
  74. 'resource_type' => 'user',
  75. },
  76. 'state' => 'ACTIVE',
  77. 'tags' => [
  78. {
  79. 'id' => 1,
  80. 'name' => 'example',
  81. 'resource_type' => 'tag'
  82. },
  83. {
  84. 'id' => 2,
  85. 'name' => 'test',
  86. 'resource_type' => 'tag'
  87. }
  88. ],
  89. 'created_at' => '2018-08-18T12:00:00+00:00',
  90. 'updated_at' => '2021-08-24T06:30:00+00:00',
  91. 'resource_type' => 'case',
  92. }
  93. end
  94. let(:id_map) do
  95. {
  96. 'Organization' => {
  97. 80_014_400_111 => organization.id,
  98. },
  99. 'User' => {
  100. 80_014_400_475 => owner.id,
  101. 80_014_400_777 => customer.id,
  102. },
  103. 'Group' => {
  104. 80_000_374_718 => group.id,
  105. },
  106. }
  107. end
  108. let(:process_payload) do
  109. {
  110. import_job: build_stubbed(:import_job, name: 'Import::Kayako', payload: {}),
  111. dry_run: false,
  112. resource: resource,
  113. field_map: {},
  114. id_map: id_map,
  115. default_language: 'en-us',
  116. }
  117. end
  118. let(:posts_response_payload) do
  119. {
  120. 'data' => [
  121. {
  122. 'id' => 99_999,
  123. 'uuid' => '179a033a-7582-4def-ae57-b8f077eaee5b',
  124. 'client_id' => '',
  125. 'subject' => 'Getting comfortable with Kayako: a sample conversation',
  126. 'contents' => 'Some text conent\n',
  127. 'creator' => {
  128. 'id' => 80_014_400_777,
  129. 'resource_type' => 'user'
  130. },
  131. 'identity' => {
  132. 'id' => 80_014_400_777,
  133. 'email' => customer.email,
  134. 'resource_type' => 'identity_email',
  135. },
  136. 'source_channel' => {
  137. 'uuid' => 'e955e374-8324-4637-97a5-763cd4010997',
  138. 'type' => 'MAIL',
  139. 'character_limit' => nil,
  140. 'account' => {
  141. 'id' => 1,
  142. 'resource_type' => 'mailbox'
  143. },
  144. 'resource_type' => 'channel'
  145. },
  146. 'attachments' => [],
  147. 'original' => {
  148. 'id' => 4,
  149. 'uuid' => '179a033a-7582-4def-ae57-b8f077eaee5b',
  150. 'subject' => 'Getting comfortable with Kayako: a sample conversation',
  151. 'body_text' => 'Some text conent\n',
  152. 'body_html' => '<div dir=\'ltr\'>Some text conent<br></div>',
  153. 'recipients' => [],
  154. 'fullname' => customer.fullname,
  155. 'email' => customer.email,
  156. 'creator' => {
  157. 'id' => 80_014_400_777,
  158. 'resource_type' => 'user'
  159. },
  160. 'identity' => {
  161. 'id' => 80_014_400_777,
  162. 'email' => customer.email,
  163. 'resource_type' => 'identity_email',
  164. },
  165. 'mailbox' => {
  166. 'id' => 1,
  167. 'uuid' => 'e955e374-8324-4637-97a5-763cd4010997',
  168. 'address' => 'info@zammad.org',
  169. 'resource_type' => 'mailbox',
  170. },
  171. 'attachments' => [],
  172. 'download_all' => nil,
  173. 'locale' => nil,
  174. 'response_time' => 0,
  175. 'created_at' => '2021-08-16T08:19:40+00:00',
  176. 'updated_at' => '2021-08-16T08:19:40+00:00',
  177. 'resource_type' => 'case_message',
  178. },
  179. 'is_requester' => true,
  180. 'created_at' => '2021-08-16T08:19:40+00:00',
  181. 'updated_at' => '2021-08-16T08:30:11+00:00',
  182. 'resource_type' => 'post',
  183. }
  184. ]
  185. }
  186. end
  187. let(:imported_ticket) do
  188. {
  189. title: 'Getting comfortable with Kayako: a sample conversation',
  190. note: nil,
  191. create_article_type_id: 1,
  192. create_article_sender_id: 2,
  193. article_count: 1,
  194. state_id: 2,
  195. group_id: group.id,
  196. priority_id: 1,
  197. owner_id: owner.id,
  198. customer_id: customer.id,
  199. organization_id: organization.id,
  200. type: 'Question'
  201. }
  202. end
  203. before do
  204. # Mock the posts get request (Import::Kayako::Case::Posts).
  205. stub_request(:get, 'https://yours.kayako.com/api/v1/cases/9999/posts?include=mailbox,message_recipient,channel,attachment,case_message,note,chat_message,identity_email,identity_twitter,identity_facebook,facebook_message,facebook_post,facebook_post_comment,twitter_message,twitter_tweet&limit=100').to_return(status: 200, body: JSON.generate(posts_response_payload), headers: {})
  206. end
  207. it 'adds tickets' do
  208. expect { process(process_payload) }.to change(Ticket, :count).by(1)
  209. end
  210. it 'correct attributes for added ticket' do
  211. process(process_payload)
  212. expect(Ticket.last).to have_attributes(imported_ticket)
  213. end
  214. it 'correct tags for added ticket' do
  215. process(process_payload)
  216. expect(Ticket.last.tag_list).to eq(%w[example test])
  217. end
  218. it 'adds article' do
  219. expect { process(process_payload) }.to change(Ticket::Article, :count).by(1)
  220. end
  221. it 'correct attributes for added article' do
  222. process(process_payload)
  223. expect(Ticket::Article.last).to have_attributes(
  224. to: 'info@zammad.org',
  225. body: "<div dir=\"ltr\">Some text conent<br>\n</div>",
  226. )
  227. end
  228. context 'when ticket is imported twice' do
  229. let(:ticket) { Ticket.last }
  230. before do
  231. process(process_payload)
  232. # Get imported ticket after first import
  233. ticket
  234. end
  235. it 'updates first article for already existing ticket' do
  236. expect { process(process_payload) }.not_to change(Ticket::Article, :count)
  237. end
  238. it 'updates ticket data' do
  239. resource['subject'] = 'Different title test'
  240. process(process_payload)
  241. imported_ticket[:title] = 'Different title test'
  242. expect(ticket.reload).to have_attributes(imported_ticket)
  243. end
  244. end
  245. context 'when importing without a type' do
  246. before do
  247. resource['type'] = nil
  248. imported_ticket[:type] = nil
  249. end
  250. it 'correct attributes for added ticket' do
  251. process(process_payload)
  252. expect(Ticket.last).to have_attributes(imported_ticket)
  253. end
  254. end
  255. context "when status is 'PENDING'" do
  256. before do
  257. resource['status'] = {
  258. 'id' => 3,
  259. 'label' => 'Pending',
  260. 'type' => 'PENDING',
  261. 'sort_order' => 3,
  262. 'is_sla_active' => false,
  263. 'is_deleted' => false,
  264. 'created_at' => '2021-08-12T11:48:51+00:00',
  265. 'updated_at' => '2021-08-12T11:48:51+00:00',
  266. }
  267. imported_ticket[:state_id] = 3
  268. end
  269. it 'correct attributes for added ticket' do
  270. process(process_payload)
  271. expect(Ticket.last).to have_attributes(imported_ticket)
  272. end
  273. end
  274. context 'when importing custom fields' do
  275. include_examples 'Object custom field values', object_name: 'Ticket', klass: Ticket
  276. end
  277. end
  278. end