edit_spec.rb 15 KB

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