ticket_field_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Sequence::Import::Freshdesk::TicketField, sequencer: :sequence do
  4. context 'when trying to import ticket fields from Freshdesk', db_strategy: :reset do
  5. let(:process_payload) do
  6. {
  7. import_job: build_stubbed(:import_job, name: 'Import::Freshdesk', payload: {}),
  8. dry_run: false,
  9. resource: resource,
  10. field_map: {},
  11. id_map: {},
  12. }
  13. end
  14. let(:base_resource) do
  15. {
  16. 'id' => 80_000_561_223,
  17. 'label' => 'My custom field',
  18. 'description' => nil,
  19. 'position' => 14,
  20. 'required_for_closure' => false,
  21. 'required_for_agents' => false,
  22. 'default' => false,
  23. 'customers_can_edit' => true,
  24. 'label_for_customers' => 'custom_dropdown',
  25. 'required_for_customers' => false,
  26. 'displayed_to_customers' => true,
  27. 'created_at' => '2021-04-12T20:48:40Z',
  28. 'updated_at' => '2021-04-12T20:48:40Z',
  29. }
  30. end
  31. context 'when field is a dropdown' do
  32. let(:resource) do
  33. base_resource.merge(
  34. {
  35. 'name' => 'cf_custom_dropdown',
  36. 'type' => 'custom_dropdown',
  37. 'choices' => %w[key1 key2],
  38. }
  39. )
  40. end
  41. it 'adds a custom field' do
  42. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_dropdown'])
  43. end
  44. end
  45. context 'when field is a decimal' do
  46. let(:resource) do
  47. base_resource.merge(
  48. {
  49. 'name' => 'cf_custom_integer',
  50. 'type' => 'custom_decimal',
  51. }
  52. )
  53. end
  54. it 'adds a custom field' do
  55. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_integer'])
  56. end
  57. end
  58. context 'when field is a number' do
  59. let(:resource) do
  60. base_resource.merge(
  61. {
  62. 'name' => 'cf_custom_integer',
  63. 'type' => 'custom_number',
  64. }
  65. )
  66. end
  67. it 'adds a custom field' do
  68. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_integer'])
  69. end
  70. end
  71. context 'when field is a date' do
  72. let(:resource) do
  73. base_resource.merge(
  74. {
  75. 'name' => 'cf_custom_date',
  76. 'type' => 'custom_date',
  77. }
  78. )
  79. end
  80. it 'adds a custom field' do
  81. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_date'])
  82. end
  83. end
  84. context 'when field is a datetime' do
  85. let(:resource) do
  86. base_resource.merge(
  87. {
  88. 'name' => 'cf_custom_datetime',
  89. 'type' => 'custom_date_time',
  90. }
  91. )
  92. end
  93. it 'adds a custom field' do
  94. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_datetime'])
  95. end
  96. end
  97. context 'when field is a checkbox' do
  98. let(:resource) do
  99. base_resource.merge(
  100. {
  101. 'name' => 'cf_custom_checkbox',
  102. 'type' => 'custom_checkbox',
  103. }
  104. )
  105. end
  106. it 'adds a custom field' do
  107. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_checkbox'])
  108. end
  109. end
  110. context 'when field is a text' do
  111. let(:resource) do
  112. base_resource.merge(
  113. {
  114. 'name' => 'cf_custom_text',
  115. 'type' => 'custom_text',
  116. }
  117. )
  118. end
  119. it 'adds a custom field' do
  120. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_text'])
  121. end
  122. end
  123. context 'when field is a paragraph' do
  124. let(:resource) do
  125. base_resource.merge(
  126. {
  127. 'name' => 'cf_custom_paragraph',
  128. 'type' => 'custom_paragraph',
  129. }
  130. )
  131. end
  132. it 'adds a custom field' do
  133. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_paragraph'])
  134. end
  135. end
  136. context 'when field is a phone number' do
  137. let(:resource) do
  138. base_resource.merge(
  139. {
  140. 'name' => 'cf_custom_phone_number',
  141. 'type' => 'custom_phone_number',
  142. }
  143. )
  144. end
  145. it 'adds a custom field' do
  146. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_phone_number'])
  147. end
  148. it 'the custom field has type "tel"' do
  149. process(process_payload)
  150. expect(ObjectManager::Attribute.find_by(name: 'cf_custom_phone_number').data_option).to include('type' => 'tel')
  151. end
  152. end
  153. context 'when field is an URL' do
  154. let(:resource) do
  155. base_resource.merge(
  156. {
  157. 'name' => 'cf_custom_url',
  158. 'type' => 'custom_url',
  159. }
  160. )
  161. end
  162. it 'adds a custom field' do
  163. expect { process(process_payload) }.to change(Ticket, :column_names).by(['cf_custom_url'])
  164. end
  165. it 'the custom field has type "url"' do
  166. process(process_payload)
  167. expect(ObjectManager::Attribute.find_by(name: 'cf_custom_url').data_option).to include('type' => 'url')
  168. end
  169. end
  170. context 'when field is invalid' do
  171. let(:resource) do
  172. base_resource.merge(
  173. {
  174. 'name' => 'cf_custom_unknown',
  175. 'type' => 'custom_unknown',
  176. }
  177. )
  178. end
  179. it 'ignore field' do
  180. expect { process(process_payload) }.to not_change(Ticket, :column_names)
  181. end
  182. end
  183. context 'when importing default fields' do
  184. let(:resource) do
  185. base_resource.merge(
  186. {
  187. 'name' => 'ticket_type',
  188. 'label' => 'Type',
  189. 'type' => 'default_ticket_type',
  190. 'default' => true,
  191. 'choices' => [
  192. 'Question',
  193. 'Incident',
  194. 'Problem',
  195. 'Feature Request',
  196. 'Refunds and Returns',
  197. 'Bulk orders',
  198. 'Refund',
  199. 'Request',
  200. ]
  201. }
  202. )
  203. end
  204. it "activate the already existing ticket 'type' field" do
  205. expect { process(process_payload) }.to change { ObjectManager::Attribute.get(object: 'Ticket', name: 'type').active }.from(false).to(true)
  206. end
  207. it "import the fixed option list for the ticket 'type' field" do
  208. process(process_payload)
  209. expect(ObjectManager::Attribute.get(object: 'Ticket', name: 'type').data_option[:options]).to include(resource['choices'].to_h { |choice| [choice, choice] })
  210. end
  211. end
  212. end
  213. end