defaults_spec.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/core_workflow/base'
  4. RSpec.describe 'CoreWorkflow > Defaults', mariadb: true, type: :model do
  5. include_context 'with core workflow base'
  6. describe '.perform - Default - Group' do
  7. let!(:group_change) { create(:group) }
  8. let!(:group_create) { create(:group) }
  9. describe 'for agent with full permissions on screen create_middle' do
  10. let(:action_user) { create(:agent) }
  11. before do
  12. action_user.group_names_access_map = {
  13. group_create.name => ['full'],
  14. group_change.name => ['change'],
  15. }
  16. end
  17. it 'does show group_create for agent with all permissions' do
  18. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  19. end
  20. it 'does not show group_change for agent with all permissions' do
  21. expect(result[:restrict_values]['group_id']).not_to include(group_change.id.to_s)
  22. end
  23. end
  24. describe 'for agent with full permissions on screen edit' do
  25. let(:payload) do
  26. base_payload.merge('screen' => 'edit')
  27. end
  28. let(:action_user) { create(:agent) }
  29. before do
  30. action_user.group_names_access_map = {
  31. group_create.name => ['full'],
  32. group_change.name => ['change'],
  33. }
  34. end
  35. it 'does show group_create for agent with all permissions' do
  36. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  37. end
  38. it 'does show group_change for agent with all permissions' do
  39. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  40. end
  41. end
  42. describe 'for agent with change permissions on screen create_middle' do
  43. let(:action_user) { create(:agent) }
  44. before do
  45. action_user.group_names_access_map = {
  46. group_create.name => ['change'],
  47. group_change.name => ['change'],
  48. }
  49. end
  50. it 'does not show group_create for agent with change permissions' do
  51. expect(result[:restrict_values]['group_id']).not_to include(group_create.id.to_s)
  52. end
  53. it 'does not show group_change for agent with change permissions' do
  54. expect(result[:restrict_values]['group_id']).not_to include(group_change.id.to_s)
  55. end
  56. end
  57. describe 'for agent with change permissions on screen edit' do
  58. let(:payload) do
  59. base_payload.merge('screen' => 'edit')
  60. end
  61. let(:action_user) { create(:agent) }
  62. before do
  63. action_user.group_names_access_map = {
  64. group_create.name => ['change'],
  65. group_change.name => ['change'],
  66. }
  67. end
  68. it 'does show group_create for agent with change permissions' do
  69. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  70. end
  71. it 'does show group_change for agent with change permissions' do
  72. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  73. end
  74. end
  75. describe 'for customer on screen create_middle' do
  76. let(:action_user) { create(:customer) }
  77. it 'does show group_create for customer' do
  78. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  79. end
  80. it 'does show group_change for customer' do
  81. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  82. end
  83. end
  84. describe 'for customer on screen edit' do
  85. let(:payload) do
  86. base_payload.merge('screen' => 'edit')
  87. end
  88. let(:action_user) { create(:customer) }
  89. it 'does show group_create for customer' do
  90. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  91. end
  92. it 'does show group_change for customer' do
  93. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  94. end
  95. end
  96. end
  97. describe '.perform - Default - Owner' do
  98. before do
  99. another_group = create(:group)
  100. action_user.group_names_access_map = {
  101. ticket.group.name => ['full'],
  102. another_group.name => ['full'],
  103. }
  104. end
  105. it 'does not show any owners for no group' do
  106. expect(result[:restrict_values]['owner_id']).to eq([''])
  107. end
  108. describe 'on group' do
  109. let(:payload) do
  110. base_payload.merge('params' => { 'group_id' => ticket.group.id })
  111. end
  112. it 'does show ticket agent' do
  113. expect(result[:restrict_values]['owner_id']).to eq(['', action_user.id.to_s])
  114. end
  115. end
  116. describe 'on group save' do
  117. let(:payload) do
  118. base_payload.merge('request_id' => 'ChecksCoreWorkflow.validate_workflows', 'params' => { 'group_id' => ticket.group.id })
  119. end
  120. it 'does show ticket agent and system user' do
  121. expect(result[:restrict_values]['owner_id']).to eq(['', '1', action_user.id.to_s])
  122. end
  123. end
  124. end
  125. describe '.perform - Default - Bulk Owner' do
  126. let(:payload) do
  127. base_payload.merge('screen' => 'overview_bulk')
  128. end
  129. it 'does not show any owners for no group' do
  130. expect(result[:restrict_values]['owner_id']).to eq([''])
  131. end
  132. describe 'on ticket ids' do
  133. let(:payload) do
  134. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => ticket.id.to_s })
  135. end
  136. it 'does show ticket agent' do
  137. expect(result[:restrict_values]['owner_id']).to eq(['', action_user.id.to_s])
  138. end
  139. end
  140. describe 'on ticket ids with no group overlap' do
  141. let(:ticket2) { create(:ticket) }
  142. let(:payload) do
  143. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => "#{ticket.id},#{ticket2.id}" })
  144. end
  145. it 'does not show ticket agent' do
  146. expect(result[:restrict_values]['owner_id']).to eq([''])
  147. end
  148. end
  149. describe 'on ticket ids with group overlap' do
  150. let(:ticket2) { create(:ticket, group: ticket.group) }
  151. let(:payload) do
  152. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => "#{ticket.id},#{ticket2.id}" })
  153. end
  154. it 'does show ticket agent' do
  155. expect(result[:restrict_values]['owner_id']).to eq(['', action_user.id.to_s])
  156. end
  157. end
  158. describe 'Ticket owner selection is not updated if owner selection should be empty #3809' do
  159. let(:group_no_owners) { create(:group) }
  160. let(:ticket2) { create(:ticket, group: group_no_owners) }
  161. let(:payload) do
  162. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => ticket2.id.to_s })
  163. end
  164. before do
  165. action_user.group_names_access_map = {
  166. group_no_owners.name => %w[create read change overview],
  167. }
  168. end
  169. it 'does not show any owners for group with no full permitted users' do
  170. expect(result[:restrict_values]['owner_id']).to eq([''])
  171. end
  172. end
  173. end
  174. describe '.perform - Default - State' do
  175. it 'does show state type new for create_middle' do
  176. expect(result[:restrict_values]['state_id']).to include(Ticket::State.find_by(name: 'new').id.to_s)
  177. end
  178. describe 'on edit' do
  179. let(:payload) do
  180. base_payload.merge('screen' => 'edit')
  181. end
  182. it 'does not show state type new' do
  183. expect(result[:restrict_values]['state_id']).not_to include(Ticket::State.find_by(name: 'new').id.to_s)
  184. end
  185. end
  186. it 'does show empty value for create_middle' do
  187. expect(result[:restrict_values]['state_id']).to include('')
  188. end
  189. context 'with customer user' do
  190. let(:action_user) { create(:customer) }
  191. it 'does not show empty value for create_middle' do
  192. expect(result[:restrict_values]['state_id']).not_to include('')
  193. end
  194. end
  195. end
  196. describe '.perform - Default - Priority' do
  197. let(:prio_invalid) { create(:ticket_priority, active: false) }
  198. it 'does show valid priority' do
  199. expect(result[:restrict_values]['priority_id']).to include(Ticket::Priority.find_by(name: '3 high').id.to_s)
  200. end
  201. it 'does not show invalid priority' do
  202. expect(result[:restrict_values]['priority_id']).not_to include(prio_invalid.id.to_s)
  203. end
  204. end
  205. describe '.perform - Default - Customer setting customer_ticket_create_group_ids' do
  206. let(:action_user) { create(:customer) }
  207. let!(:group1) { create(:group) }
  208. let!(:group2) { create(:group) }
  209. let!(:group3) { create(:group) }
  210. it 'does show group 1' do
  211. expect(result[:restrict_values]['group_id']).to include(group1.id.to_s)
  212. end
  213. context 'with customer_ticket_create_group_ids set' do
  214. before do
  215. Setting.set('customer_ticket_create_group_ids', [group2.id.to_s, group3.id.to_s])
  216. end
  217. it 'does not show group 1' do
  218. expect(result[:restrict_values]['group_id']).not_to include(group1.id.to_s)
  219. end
  220. end
  221. end
  222. describe '.perform - Default - #3721 - Fields are falsey displayed as mandatory if they contain historic screen values', db_strategy: :reset do
  223. let(:field_name) { SecureRandom.uuid }
  224. let(:screens) do
  225. {
  226. create_middle: {
  227. 'ticket.agent' => {
  228. shown: true,
  229. },
  230. },
  231. }
  232. end
  233. before do
  234. create(:object_manager_attribute_text, object_name: 'Ticket', name: field_name, display: field_name, screens: screens)
  235. ObjectManager::Attribute.migration_execute
  236. end
  237. it 'does show the field as optional because it has no required value' do
  238. expect(result[:mandatory][field_name]).to be(false)
  239. end
  240. end
  241. describe '.perform - Default - Certain offered states can not be set (never) via the bulk action #4848' do
  242. let(:payload) do
  243. base_payload.merge('screen' => 'overview_bulk')
  244. end
  245. it 'does not show non-edit states in the bulk edit of the ticket overview' do
  246. expect(result[:restrict_values]['state_id']).not_to include(*Ticket::State.where(name: %w[new removed]).pluck(:id).map(&:to_s))
  247. end
  248. end
  249. end