edit_spec.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/form_updater/concerns/checks_core_workflow_examples'
  4. require 'models/form_updater/concerns/has_security_options_examples'
  5. require 'models/form_updater/concerns/applies_ticket_shared_draft_examples'
  6. require 'models/form_updater/concerns/stores_taskbar_state_examples'
  7. require 'models/form_updater/concerns/applies_taskbar_state_examples'
  8. RSpec.describe(FormUpdater::Updater::Ticket::Edit) do
  9. subject(:resolved_result) do
  10. described_class.new(
  11. context: context,
  12. relation_fields: relation_fields,
  13. meta: meta,
  14. data: data,
  15. id: id,
  16. )
  17. end
  18. let(:group) { create(:group) }
  19. let(:user) { create(:agent, groups: [group]) }
  20. let(:context) { { current_user: user } }
  21. let(:meta) { { initial: true, form_id: SecureRandom.uuid } }
  22. let(:data) { {} }
  23. let(:id) { nil }
  24. let(:relation_fields) do
  25. [
  26. {
  27. name: 'group_id',
  28. relation: 'group',
  29. },
  30. {
  31. name: 'state_id',
  32. relation: 'TicketState',
  33. },
  34. {
  35. name: 'priority_id',
  36. relation: 'TicketPriority',
  37. },
  38. ]
  39. end
  40. let(:expected_result) do
  41. {
  42. 'group_id' => {
  43. options: [ { value: group.id, label: group.name } ],
  44. },
  45. 'state_id' => {
  46. options: Ticket::State.by_category(:viewable_agent_edit).reorder(name: :asc).map { |state| { value: state.id, label: state.name } },
  47. },
  48. 'priority_id' => {
  49. options: Ticket::Priority.where(active: true).reorder(id: :asc).map { |priority| { value: priority.id, label: priority.name } },
  50. },
  51. }
  52. end
  53. context 'when resolving' do
  54. it 'returns all resolved relation fields with correct value + label' do
  55. expect(resolved_result.resolve[:fields]).to include(
  56. 'group_id' => include(expected_result['group_id']),
  57. 'state_id' => include(expected_result['state_id']),
  58. 'priority_id' => include(expected_result['priority_id']),
  59. )
  60. end
  61. context 'when body is used' do
  62. before do
  63. create(:core_workflow, object: 'Ticket',
  64. perform: {
  65. body: {
  66. operator: 'set_readonly',
  67. set_readonly: true
  68. },
  69. })
  70. end
  71. it 'body (and also attachments) should be disabled' do
  72. expect(resolved_result.resolve[:fields]).to include(
  73. 'body' => include({
  74. disabled: true,
  75. }),
  76. 'attachments' => include({
  77. disabled: true,
  78. }),
  79. )
  80. end
  81. end
  82. context 'when ticket has object attribute value with a historical value', db_strategy: :reset do
  83. let(:field_name) { SecureRandom.uuid }
  84. let(:screens) do
  85. {
  86. create_middle: {
  87. '-all-' => {
  88. shown: true,
  89. required: false,
  90. },
  91. },
  92. create: {
  93. '-all-' => {
  94. shown: true,
  95. required: false,
  96. },
  97. },
  98. edit: {
  99. '-all-' => {
  100. shown: true,
  101. required: false,
  102. },
  103. },
  104. }
  105. end
  106. let(:object_attribute_field_type) { :object_manager_attribute_select }
  107. let(:object_attribute) do
  108. create(object_attribute_field_type, object_name: 'Ticket', name: field_name, display: field_name, screens: screens)
  109. end
  110. let(:ticket_object_attribute_value) { 'key_3' }
  111. let(:id) do
  112. Gql::ZammadSchema.id_from_object(create(:ticket, group: group, object_attribute.name => ticket_object_attribute_value))
  113. end
  114. before do
  115. object_attribute
  116. ObjectManager::Attribute.migration_execute
  117. object_attribute.reload
  118. end
  119. shared_examples 'resolve fields' do
  120. it 'checks that "rejectNonExistentValues" is false' do
  121. # Trigger first object authorization check.
  122. resolved_result.authorized?
  123. result = resolved_result.resolve[:fields]
  124. expect(result[field_name]).to include(expected_result)
  125. end
  126. end
  127. context 'when object attribute is a select field' do
  128. before do
  129. object_attribute.update!(data_option: object_attribute[:data_option].merge({ options: { key_1: 'value_1', key_2: 'value_2' } }))
  130. ObjectManager::Attribute.migration_execute
  131. end
  132. let(:expected_result) do
  133. {
  134. options: [
  135. {
  136. value: 'key_1',
  137. label: 'value_1',
  138. },
  139. {
  140. value: 'key_2',
  141. label: 'value_2',
  142. },
  143. ],
  144. clearable: true,
  145. rejectNonExistentValues: false
  146. }
  147. end
  148. include_examples 'resolve fields'
  149. end
  150. context 'when object attribute is a tree select field' do
  151. let(:object_attribute_field_type) { :object_manager_attribute_tree_select }
  152. let(:ticket_object_attribute_value) { 'Incident::Softwareproblem::MS Office' }
  153. let(:expected_result) do
  154. {
  155. options: [
  156. {
  157. label: 'Incident',
  158. value: 'Incident',
  159. disabled: false,
  160. children: [
  161. {
  162. label: 'Hardware',
  163. value: 'Incident::Hardware',
  164. disabled: false,
  165. children: [
  166. {
  167. label: 'Monitor',
  168. value: 'Incident::Hardware::Monitor',
  169. disabled: false,
  170. },
  171. {
  172. label: 'Mouse',
  173. value: 'Incident::Hardware::Mouse',
  174. disabled: false,
  175. },
  176. ]
  177. },
  178. ]
  179. },
  180. {
  181. label: 'Change request',
  182. value: 'Change request',
  183. disabled: false,
  184. }
  185. ],
  186. clearable: true,
  187. rejectNonExistentValues: false
  188. }
  189. end
  190. before do
  191. object_attribute.update!(data_option: object_attribute[:data_option].merge({ options: [
  192. {
  193. 'name' => 'Incident',
  194. 'value' => 'Incident',
  195. 'children' => [
  196. {
  197. 'name' => 'Hardware',
  198. 'value' => 'Incident::Hardware',
  199. 'children' => [
  200. {
  201. 'name' => 'Monitor',
  202. 'value' => 'Incident::Hardware::Monitor'
  203. },
  204. {
  205. 'name' => 'Mouse',
  206. 'value' => 'Incident::Hardware::Mouse'
  207. },
  208. ]
  209. },
  210. ]
  211. },
  212. {
  213. 'name' => 'Change request',
  214. 'value' => 'Change request'
  215. }
  216. ] }))
  217. ObjectManager::Attribute.migration_execute
  218. end
  219. include_examples 'resolve fields'
  220. end
  221. end
  222. context 'when time accounting should be triggered' do
  223. let(:id) do
  224. Gql::ZammadSchema.id_from_object(create(:ticket, group: group))
  225. end
  226. before do
  227. Setting.set('time_accounting', true)
  228. end
  229. it 'checks that time_accounting flag is not present' do
  230. # Trigger first object authorization check.
  231. resolved_result.authorized?
  232. # Time accounting check was moved into a separate validator, the flag should be absent.
  233. flags = resolved_result.resolve[:flags]
  234. expect(flags).not_to have_key(:time_accounting)
  235. end
  236. end
  237. context 'when auto save should be applied' do
  238. let(:taskbar_key) { 'TicketZoom-1234' }
  239. let(:taskbar) { create(:taskbar, key: taskbar_key, callback: 'Ticket', user_id: user.id, state: taskbar_state) }
  240. let(:taskbar_state) { { 'title' => 'test', 'owner_id' => 1 } }
  241. let(:field_name) { 'title' }
  242. let(:field_result) { { value: 'test' } }
  243. let(:additional_data) { { 'taskbarId' => Gql::ZammadSchema.id_from_object(taskbar), 'applyTaskbarState' => true } }
  244. let(:meta) { { additional_data: } }
  245. before do
  246. # Trigger first object authorization check.
  247. resolved_result.authorized?
  248. end
  249. it 'owner_id should be nil for system user' do
  250. fields = resolved_result.resolve[:fields]
  251. expect(fields['owner_id'][:value]).to be_nil
  252. end
  253. context 'when new article exists' do
  254. let(:taskbar_state) { { 'title' => 'test', 'article' => { 'articleType' => 'email' } } }
  255. it 'checks that newArticlePresent flag is present' do
  256. flags = resolved_result.resolve[:flags]
  257. expect(flags[:newArticlePresent]).to be_truthy
  258. end
  259. end
  260. end
  261. context 'when data should be stored' do
  262. let(:taskbar_key) { 'TicketZoom-1234' }
  263. let(:taskbar) { create(:taskbar, key: taskbar_key, callback: 'Ticket', user_id: user.id) }
  264. let(:additional_data) { { 'taskbarId' => Gql::ZammadSchema.id_from_object(taskbar) } }
  265. let(:meta) { { additional_data: } }
  266. let(:id) do
  267. Gql::ZammadSchema.id_from_object(create(:ticket, group: group, state_id: 1))
  268. end
  269. shared_examples 'stores the form value of the field' do
  270. it 'checks that data was stored correctly' do
  271. # Trigger first object authorization check.
  272. resolved_result.authorized?
  273. resolved_result.resolve
  274. state = taskbar.reload.state
  275. expect(state).to include(result)
  276. end
  277. end
  278. context 'when new article is saved' do
  279. let(:result) { { 'ticket' => include({ 'title' => 'test' }), 'article' => { 'type' => 'email' } } }
  280. let(:data) { { 'title' => 'test', 'article' => { 'articleType' => 'email' } } }
  281. include_examples 'stores the form value of the field'
  282. end
  283. context 'when data should be skipped' do
  284. let(:data) { { 'title' => 'test', 'state_id' => 1 } }
  285. let(:result) { { 'ticket' => { 'title' => 'test' } } }
  286. include_examples 'stores the form value of the field'
  287. end
  288. end
  289. end
  290. include_examples 'FormUpdater::ChecksCoreWorkflow', object_name: 'Ticket'
  291. include_examples 'FormUpdater::HasSecurityOptions', type: 'edit'
  292. include_examples 'FormUpdater::AppliesTicketSharedDraft', draft_type: 'detail-view'
  293. context 'when data should be stored and applied' do
  294. let(:id) do
  295. Gql::ZammadSchema.id_from_object(create(:ticket, group: group))
  296. end
  297. before do
  298. # Trigger creation of the ticket and handover the id.
  299. id
  300. # Trigger first object authorization check.
  301. resolved_result.authorized?
  302. end
  303. include_examples 'FormUpdater::StoresTaskbarState', taskbar_key: 'TicketZoom-1234', taskbar_callback: 'Ticket', store_state_collect_group_key: 'ticket', store_state_group_keys: ['article'] # gitleaks:allow
  304. include_examples 'FormUpdater::AppliesTaskbarState', taskbar_key: 'TicketZoom-1234', taskbar_callback: 'Ticket', apply_state_group_keys: %w[ticket article] # gitleaks:allow
  305. end
  306. end