ticket_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'zendesk_api'
  4. RSpec.describe Sequencer::Sequence::Import::Zendesk::Ticket, db_strategy: :reset, sequencer: :sequence do
  5. context 'when importing tickets from Zendesk' do
  6. let(:group) { create(:group) }
  7. let(:owner) { create(:agent, group_ids: [group.id]) }
  8. let(:customer) { create(:customer) }
  9. let(:resource) do
  10. ZendeskAPI::Ticket.new(
  11. nil,
  12. {
  13. 'id' => 2,
  14. 'external_id' => nil,
  15. 'via' => {
  16. 'channel' => 'email',
  17. 'source' => {
  18. 'from' => {
  19. 'address' => 'john.doe@example.com',
  20. 'name' => 'John Doe'
  21. },
  22. 'to' => {
  23. 'name' => 'Zendesk',
  24. 'address' => 'zendesk-user@example.com'
  25. },
  26. 'rel' => nil
  27. }
  28. },
  29. 'created_at' => '2015-07-19 22:44:20 UTC',
  30. 'updated_at' => '2016-05-19 14:00:42 UTC',
  31. 'type' => 'question',
  32. 'subject' => 'test',
  33. 'raw_subject' => 'test',
  34. 'description' => 'test email',
  35. 'priority' => 'urgent',
  36. 'status' => 'pending',
  37. 'recipient' => 'zendesk-user@example.com',
  38. 'requester_id' => 1_202_726_611,
  39. 'submitter_id' => 1_147_801_812,
  40. 'assignee_id' => 1_150_734_731,
  41. 'organization_id' => 154_755_561,
  42. 'group_id' => 24_554_931,
  43. 'collaborator_ids' => [],
  44. 'follower_ids' => [],
  45. 'email_cc_ids' => [],
  46. 'forum_topic_id' => nil,
  47. 'problem_id' => nil,
  48. 'has_incidents' => false,
  49. 'is_public' => true,
  50. 'due_at' => nil,
  51. 'tags' => %w[
  52. anothertag
  53. import
  54. key2
  55. newtag
  56. otrs
  57. zammad
  58. ],
  59. 'custom_fields' => [
  60. { 'id' => 1001,
  61. 'value' => 'key_1' },
  62. { 'id' => 1002,
  63. 'value' => true },
  64. { 'id' => 1003,
  65. 'value' => %w[key_1 key_2] },
  66. ],
  67. 'satisfaction_rating' => nil,
  68. 'sharing_agreement_ids' => [],
  69. 'followup_ids' => [],
  70. 'brand_id' => 670_701,
  71. 'allow_channelback' => false,
  72. 'allow_attachments' => true,
  73. 'generated_timestamp' => 1_463_666_442
  74. }
  75. )
  76. end
  77. let(:group_map) do
  78. {
  79. 24_554_931 => group.id,
  80. }
  81. end
  82. let(:user_map) do
  83. {
  84. 1_150_734_731 => owner.id,
  85. 1_202_726_611 => customer.id,
  86. }
  87. end
  88. let(:organization_map) do
  89. {}
  90. end
  91. let(:ticket_field_map) do
  92. {
  93. 1001 => 'custom_dropdown',
  94. 1002 => 'custom_checkbox',
  95. 1003 => 'custom_multiselect',
  96. }
  97. end
  98. let(:process_payload) do
  99. {
  100. import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
  101. dry_run: false,
  102. resource: resource,
  103. group_map: group_map,
  104. user_map: user_map,
  105. organization_map: organization_map,
  106. ticket_field_map: ticket_field_map,
  107. field_map: {},
  108. }
  109. end
  110. let(:imported_ticket) do
  111. {
  112. title: 'test',
  113. note: nil,
  114. create_article_type_id: 1,
  115. create_article_sender_id: 2,
  116. article_count: nil,
  117. state_id: 3,
  118. group_id: group.id,
  119. priority_id: 3,
  120. owner_id: owner.id,
  121. customer_id: customer.id,
  122. type: 'question',
  123. custom_dropdown: 'key_1',
  124. custom_checkbox: true,
  125. custom_multiselect: %w[key_1 key_2],
  126. }
  127. end
  128. before do
  129. create(:object_manager_attribute_select, object_name: 'Ticket', name: 'custom_dropdown')
  130. create(:object_manager_attribute_boolean, object_name: 'Ticket', name: 'custom_checkbox')
  131. create(:object_manager_attribute_multiselect, object_name: 'Ticket', name: 'custom_multiselect')
  132. ObjectManager::Attribute.migration_execute
  133. # We only want to test here the Ticket API, so disable other modules in the sequence
  134. # that make their own HTTP requests.
  135. custom_sequence = described_class.sequence.dup
  136. custom_sequence.delete('Import::Zendesk::Ticket::Comments')
  137. custom_sequence.delete('Import::Zendesk::Ticket::Tags')
  138. allow(described_class).to receive(:sequence) { custom_sequence }
  139. end
  140. context 'with email ticket' do
  141. it 'imports user correctly (increased ticket count)' do
  142. expect { process(process_payload) }.to change(Ticket, :count).by(1)
  143. end
  144. it 'imports ticket data correctly' do
  145. process(process_payload)
  146. expect(Ticket.last).to have_attributes(imported_ticket)
  147. end
  148. end
  149. context 'with already existing ticket' do
  150. let(:ticket) { Ticket.last }
  151. before do
  152. process(process_payload)
  153. # Get last ticket after first import.
  154. ticket
  155. end
  156. it 'imports updated ticket data correctly' do
  157. resource.subject = 'Different title test'
  158. resource.status = 'on-hold'
  159. resource.priority = 'very low'
  160. imported_ticket[:title] = 'Different title test'
  161. imported_ticket[:state_id] = Ticket::State.find_by(name: 'open')&.id
  162. imported_ticket[:priority_id] = Ticket::Priority.find_by(name: '2 normal')&.id
  163. process(process_payload)
  164. expect(ticket.reload).to have_attributes(imported_ticket)
  165. end
  166. end
  167. end
  168. end