create_spec.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_template_examples'
  6. require 'models/form_updater/concerns/applies_ticket_shared_draft_examples'
  7. require 'models/form_updater/concerns/applies_split_ticket_article_examples'
  8. require 'models/form_updater/concerns/stores_taskbar_state_examples'
  9. require 'models/form_updater/concerns/applies_taskbar_state_examples'
  10. RSpec.describe(FormUpdater::Updater::Ticket::Create) do
  11. subject(:resolved_result) do
  12. described_class.new(
  13. context: context,
  14. relation_fields: relation_fields,
  15. meta: meta,
  16. data: data,
  17. id: nil
  18. )
  19. end
  20. let(:group) { create(:group, name: 'Example 1') }
  21. let(:group2) { create(:group, name: 'Example 2') }
  22. let(:level1_group) { create(:group, name: 'Depth 1-G1', parent: group) }
  23. let(:level1_group2) { create(:group, name: 'Depth 1-G2', parent: group) }
  24. let(:level2_group) { create(:group, name: 'Depth 2-G1', parent: level1_group) }
  25. let(:user) { create(:agent, groups: [group, group2, level1_group2, level2_group]) }
  26. let(:context) { { current_user: user } }
  27. let(:meta) { { initial: true, form_id: SecureRandom.uuid } }
  28. let(:data) { {} }
  29. let(:relation_fields) do
  30. [
  31. {
  32. name: 'group_id',
  33. relation: 'group',
  34. },
  35. {
  36. name: 'state_id',
  37. relation: 'TicketState',
  38. },
  39. {
  40. name: 'priority_id',
  41. relation: 'TicketPriority',
  42. },
  43. ]
  44. end
  45. let(:expected_group_options) do
  46. [
  47. {
  48. value: group.id,
  49. label: group.name_last,
  50. disabled: false,
  51. children: [
  52. {
  53. value: level1_group.id,
  54. label: level1_group.name_last,
  55. disabled: true,
  56. children: [
  57. {
  58. value: level2_group.id,
  59. label: level2_group.name_last,
  60. disabled: false
  61. }
  62. ]
  63. },
  64. {
  65. value: level1_group2.id,
  66. label: level1_group2.name_last,
  67. disabled: false,
  68. },
  69. ]
  70. },
  71. {
  72. value: group2.id,
  73. label: group2.name_last,
  74. disabled: false
  75. }
  76. ]
  77. end
  78. let(:expected_result) do
  79. {
  80. 'group_id' => {
  81. options: expected_group_options
  82. },
  83. 'state_id' => {
  84. options: Ticket::State.by_category(:viewable_agent_new).reorder(name: :asc).map { |state| { value: state.id, label: state.name } },
  85. },
  86. 'priority_id' => {
  87. options: Ticket::Priority.where(active: true).reorder(id: :asc).map { |priority| { value: priority.id, label: priority.name } },
  88. },
  89. }
  90. end
  91. context 'when resolving' do
  92. it 'returns all resolved relation fields with correct value + label' do
  93. expect(resolved_result.resolve[:fields]).to include(
  94. 'group_id' => include(expected_result['group_id']),
  95. 'state_id' => include(expected_result['state_id']),
  96. 'priority_id' => include(expected_result['priority_id']),
  97. )
  98. end
  99. context 'with only one group for user' do
  100. let(:user) { create(:agent, groups: [level2_group]) }
  101. it 'returns group_id as integer' do
  102. expect(resolved_result.resolve[:fields]).to include(
  103. 'group_id' => include(value: level2_group.id)
  104. )
  105. end
  106. end
  107. context 'when no permission on all parent groups' do
  108. let(:user) { create(:agent, groups: [group2, level2_group]) }
  109. let(:expected_group_options) do
  110. [
  111. {
  112. value: group.id,
  113. label: group.name_last,
  114. disabled: true,
  115. children: [
  116. {
  117. value: level1_group.id,
  118. label: level1_group.name_last,
  119. disabled: true,
  120. children: [
  121. {
  122. value: level2_group.id,
  123. label: level2_group.name_last,
  124. disabled: false
  125. }
  126. ]
  127. },
  128. ]
  129. },
  130. {
  131. value: group2.id,
  132. label: group2.name_last,
  133. disabled: false
  134. }
  135. ]
  136. end
  137. it 'returns current group options' do
  138. expect(resolved_result.resolve[:fields]).to include(
  139. 'group_id' => include(expected_result['group_id']),
  140. )
  141. end
  142. end
  143. context 'when group_id is given in data' do
  144. let(:data) { { 'group_id' => group.id } }
  145. it 'returns no new value for group' do
  146. expect(resolved_result.resolve[:fields]).to not_include(
  147. 'group_id' => include(value: group.id)
  148. )
  149. end
  150. end
  151. context 'with different default priority' do
  152. before do
  153. Ticket::Priority.find(1).update!(default_create: true)
  154. end
  155. it 'returns initial value for priority_id' do
  156. expect(resolved_result.resolve[:fields]).to include(
  157. 'priority_id' => include(initialValue: 1)
  158. )
  159. end
  160. end
  161. context 'when customer_id should be prefilled' do
  162. let(:meta) { { initial: true, form_id: SecureRandom.uuid, additional_data: { 'customer_id' => customer.id } } }
  163. let(:customer) { create(:customer, organization: create(:organization)) }
  164. it 'returns initial value for customer_id' do
  165. expect(resolved_result.resolve[:fields]).to include(
  166. 'customer_id' => include(
  167. initialValue: customer.id,
  168. options: [{
  169. value: customer.id,
  170. label: customer.fullname,
  171. heading: customer.organization.name,
  172. object: customer.attributes
  173. .slice('active', 'email', 'firstname', 'fullname', 'image', 'lastname', 'mobile', 'out_of_office', 'out_of_office_end_at', 'out_of_office_start_at', 'phone', 'source', 'vip')
  174. .merge({
  175. '__typename' => 'User',
  176. 'id' => Gql::ZammadSchema.id_from_internal_id('User', customer.id),
  177. })
  178. }]
  179. )
  180. )
  181. end
  182. end
  183. end
  184. include_examples 'FormUpdater::ChecksCoreWorkflow', object_name: 'Ticket'
  185. include_examples 'FormUpdater::HasSecurityOptions', type: 'create'
  186. include_examples 'FormUpdater::AppliesTicketTemplate'
  187. include_examples 'FormUpdater::AppliesTicketSharedDraft', draft_type: 'start'
  188. include_examples 'FormUpdater::AppliesSplitTicketArticle'
  189. include_examples 'FormUpdater::StoresTaskbarState', taskbar_key: 'TicketCreateScreen-1234', taskbar_callback: 'TicketCreate', store_state_collect_group_key: nil, store_state_group_keys: nil
  190. include_examples 'FormUpdater::AppliesTaskbarState', taskbar_key: 'TicketCreateScreen-1234', taskbar_callback: 'TicketCreate', apply_state_group_keys: nil
  191. end