ticket_field_spec.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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::TicketField, sequencer: :sequence do
  5. context 'when trying to import ticket fields from Zendesk', db_strategy: :reset do
  6. let(:process_payload) do
  7. {
  8. import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
  9. dry_run: false,
  10. resource: resource,
  11. field_map: {},
  12. }
  13. end
  14. let(:base_resource) do
  15. {
  16. 'id' => 24_656_189,
  17. 'raw_title' => 'Custom Decimal',
  18. 'description' => 'A custom Decimal field',
  19. 'raw_description' => 'A custom Decimal field',
  20. 'position' => 7,
  21. 'active' => true,
  22. 'required' => true,
  23. 'collapsed_for_agents' => false,
  24. 'regexp_for_validation' => '\\A[-+]?[0-9]*[.,]?[0-9]+\\z',
  25. 'title_in_portal' => 'Custom Decimal',
  26. 'raw_title_in_portal' => 'Custom Decimal',
  27. 'visible_in_portal' => true,
  28. 'editable_in_portal' => true,
  29. 'required_in_portal' => true,
  30. 'tag' => nil,
  31. 'created_at' => '2015-12-15 12:08:26 UTC',
  32. 'updated_at' => '2015-12-15 12:22:30 UTC',
  33. 'removable' => true,
  34. 'agent_description' => nil
  35. }
  36. end
  37. context 'when field is a decimal' do
  38. let(:resource) do
  39. ZendeskAPI::TicketField.new(
  40. nil,
  41. base_resource.merge(
  42. {
  43. 'title' => 'Custom Decimal',
  44. 'type' => 'decimal',
  45. }
  46. )
  47. )
  48. end
  49. it 'adds a custom field' do
  50. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_decimal'])
  51. end
  52. end
  53. context 'when field is a checkbox' do
  54. let(:resource) do
  55. ZendeskAPI::TicketField.new(
  56. nil,
  57. base_resource.merge(
  58. {
  59. 'title' => 'Custom Checkbox',
  60. 'type' => 'checkbox',
  61. }
  62. )
  63. )
  64. end
  65. it 'adds a custom field' do
  66. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_checkbox'])
  67. end
  68. end
  69. context 'when field is a date' do
  70. let(:resource) do
  71. ZendeskAPI::TicketField.new(
  72. nil,
  73. base_resource.merge(
  74. {
  75. 'title' => 'Custom Date',
  76. 'type' => 'date',
  77. }
  78. )
  79. )
  80. end
  81. it 'adds a custom field' do
  82. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_date'])
  83. end
  84. end
  85. context 'when field is an integer' do
  86. let(:resource) do
  87. ZendeskAPI::TicketField.new(
  88. nil,
  89. base_resource.merge(
  90. {
  91. 'title' => 'Custom Integer',
  92. 'type' => 'integer',
  93. }
  94. )
  95. )
  96. end
  97. it 'adds a custom field' do
  98. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_integer'])
  99. end
  100. end
  101. context 'when field is a regex' do
  102. let(:resource) do
  103. ZendeskAPI::TicketField.new(
  104. nil,
  105. base_resource.merge(
  106. {
  107. 'title' => 'Custom Regex',
  108. 'type' => 'regexp',
  109. }
  110. )
  111. )
  112. end
  113. it 'adds a custom field' do
  114. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_regex'])
  115. end
  116. end
  117. context 'when field is a dropdown' do
  118. let(:resource) do
  119. ZendeskAPI::TicketField.new(
  120. nil,
  121. base_resource.merge(
  122. {
  123. 'title' => 'Custom Dropdown',
  124. 'type' => 'dropdown',
  125. 'custom_field_options' => [
  126. {
  127. 'id' => 28_353_445,
  128. 'name' => 'Another Value',
  129. 'raw_name' => 'Another Value',
  130. 'value' => 'anotherkey',
  131. 'default' => false
  132. },
  133. {
  134. 'id' => 28_353_425,
  135. 'name' => 'Value 1',
  136. 'raw_name' => 'Value 1',
  137. 'value' => 'key1',
  138. 'default' => false
  139. },
  140. {
  141. 'id' => 28_353_435,
  142. 'name' => 'Value 2',
  143. 'raw_name' => 'Value 2',
  144. 'value' => 'key2',
  145. 'default' => false
  146. }
  147. ]
  148. }
  149. )
  150. )
  151. end
  152. it 'adds a custom field' do
  153. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_dropdown'])
  154. end
  155. end
  156. context 'when field is a multiselect' do
  157. let(:resource) do
  158. ZendeskAPI::TicketField.new(
  159. nil,
  160. base_resource.merge(
  161. {
  162. 'title' => 'Custom Multiselect',
  163. 'type' => 'multiselect',
  164. 'custom_field_options' => [
  165. {
  166. 'id' => 28_353_445,
  167. 'name' => 'Another Value',
  168. 'raw_name' => 'Another Value',
  169. 'value' => 'anotherkey',
  170. 'default' => false
  171. },
  172. {
  173. 'id' => 28_353_425,
  174. 'name' => 'Value 1',
  175. 'raw_name' => 'Value 1',
  176. 'value' => 'key1',
  177. 'default' => false
  178. },
  179. {
  180. 'id' => 28_353_435,
  181. 'name' => 'Value 2',
  182. 'raw_name' => 'Value 2',
  183. 'value' => 'key2',
  184. 'default' => false
  185. }
  186. ]
  187. }
  188. )
  189. )
  190. end
  191. it 'adds a custom field' do
  192. expect { process(process_payload) }.to change(Ticket, :column_names).by(['custom_multiselect'])
  193. end
  194. end
  195. context 'when field is the ticket type' do
  196. let(:resource) do
  197. ZendeskAPI::TicketField.new(
  198. nil,
  199. base_resource.merge(
  200. {
  201. 'title' => 'Type',
  202. 'type' => 'tickettype',
  203. 'system_field_options' => [
  204. {
  205. 'name' => 'Question',
  206. 'value' => 'question'
  207. },
  208. {
  209. 'name' => 'Incident',
  210. 'value' => 'incident'
  211. },
  212. {
  213. 'name' => 'Problem',
  214. 'value' => 'problem'
  215. },
  216. {
  217. 'name' => 'Task',
  218. 'value' => 'task'
  219. }
  220. ]
  221. }
  222. )
  223. )
  224. end
  225. it "activate the already existing ticket 'type' field" do
  226. expect { process(process_payload) }.to change { ObjectManager::Attribute.get(object: 'Ticket', name: 'type').active }.from(false).to(true)
  227. end
  228. it "import the fixed option list for the ticket 'type' field" do
  229. process(process_payload)
  230. expect(ObjectManager::Attribute.get(object: 'Ticket', name: 'type').data_option[:options]).to include(resource['system_field_options'].to_h { |choice| [choice['value'], choice['name']] })
  231. end
  232. end
  233. context 'when field is unknown' do
  234. let(:resource) do
  235. ZendeskAPI::TicketField.new(
  236. nil,
  237. base_resource.merge(
  238. {
  239. 'title' => 'Custom Unknown',
  240. 'type' => 'unknown',
  241. }
  242. )
  243. )
  244. end
  245. it 'does not add a custom field' do
  246. expect { process(process_payload) }.not_to change(Ticket, :column_names)
  247. end
  248. end
  249. end
  250. end