core_workflow_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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, mariadb: true, type: :model do
  5. include_context 'with core workflow base'
  6. describe '.perform - No assets' do
  7. let(:result) { described_class.perform(payload: payload, user: action_user, assets: false) }
  8. it 'does not contain assets' do
  9. expect(result[:assets]).to be_blank
  10. end
  11. end
  12. describe '.matches_selector?' do
  13. let(:result) { described_class.matches_selector?(id: ticket.id, user: action_user, selector: condition) }
  14. context 'when matching open tickets' do
  15. let(:condition) do
  16. { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => Ticket::State.by_category(:open).map { |x| x.id.to_s } } }
  17. end
  18. it 'does match' do
  19. expect(result).to be(true)
  20. end
  21. end
  22. context 'when matching closed tickets' do
  23. let(:condition) do
  24. { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => Ticket::State.by_category(:closed).map { |x| x.id.to_s } } }
  25. end
  26. it 'does not match' do
  27. expect(result).to be(false)
  28. end
  29. end
  30. end
  31. describe 'Core Workflow "is not" operator is working unexpected #3752' do
  32. let(:approval_role) { create(:role) }
  33. let!(:workflow) do
  34. create(:core_workflow,
  35. object: 'Ticket',
  36. condition_selected: {
  37. 'session.role_ids': {
  38. operator: 'is_not',
  39. value: [ approval_role.id.to_s ]
  40. },
  41. })
  42. end
  43. context 'when not action user has approval role' do
  44. let(:action_user) { create(:agent, roles: [Role.find_by(name: 'Agent'), approval_role]) }
  45. it 'does not match' do
  46. expect(result[:matched_workflows]).not_to include(workflow.id)
  47. end
  48. end
  49. context 'when action user has not approval role' do
  50. let(:action_user) { create(:agent) }
  51. it 'does match' do
  52. expect(result[:matched_workflows]).to include(workflow.id)
  53. end
  54. end
  55. end
  56. describe 'Saved conditions break on selections without reloading #3758', db_strategy: :reset do
  57. let(:field_name) { SecureRandom.uuid }
  58. let(:screens) do
  59. {
  60. edit: {
  61. 'ticket.agent' => {
  62. shown: true,
  63. },
  64. },
  65. }
  66. end
  67. let!(:workflow) do
  68. create(:core_workflow,
  69. object: 'Ticket',
  70. condition_saved: {
  71. "ticket.#{field_name}": {
  72. operator: 'is_not',
  73. value: 'true',
  74. },
  75. })
  76. end
  77. let(:payload) do
  78. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  79. end
  80. before do
  81. create(:object_manager_attribute_boolean, object_name: 'Ticket', name: field_name, display: field_name, screens: screens)
  82. ObjectManager::Attribute.migration_execute
  83. end
  84. it 'does match the workflow because saved value is false' do
  85. expect(result[:matched_workflows]).to include(workflow.id)
  86. end
  87. context 'when params contain boolean field true' do
  88. let(:payload) do
  89. base_payload.merge('params' => { 'id' => ticket.id, field_name => true }, 'screen' => 'edit')
  90. end
  91. it 'does match the workflow because saved value is false' do
  92. expect(result[:matched_workflows]).to include(workflow.id)
  93. end
  94. end
  95. end
  96. describe 'Core Workflow: Add organization condition attributes for object User #3779' do
  97. let(:organization) { create(:organization, note: 'hello') }
  98. let!(:base_payload) do
  99. {
  100. 'event' => 'core_workflow',
  101. 'request_id' => 'default',
  102. 'class_name' => 'User',
  103. 'screen' => 'create',
  104. 'params' => {},
  105. }
  106. end
  107. let!(:workflow) do
  108. create(:core_workflow,
  109. object: 'User',
  110. condition_selected: {
  111. 'organization.note': {
  112. operator: 'is',
  113. value: 'hello',
  114. },
  115. })
  116. end
  117. context 'when new user has no organization' do
  118. it 'does not match the workflow' do
  119. expect(result[:matched_workflows]).not_to include(workflow.id)
  120. end
  121. end
  122. context 'when new user is part of the organization' do
  123. let(:payload) do
  124. base_payload.merge('params' => { 'organization_id' => organization.id.to_s })
  125. end
  126. it 'does match the workflow' do
  127. expect(result[:matched_workflows]).to include(workflow.id)
  128. end
  129. end
  130. end
  131. describe 'Ticket owner selection is not updated if owner selection should be empty #3809' do
  132. let(:group_no_owners) { create(:group) }
  133. let(:payload) do
  134. base_payload.merge('params' => { 'group_id' => group_no_owners.id })
  135. end
  136. before do
  137. action_user.group_names_access_map = {
  138. group_no_owners.name => %w[create read change overview],
  139. }
  140. end
  141. it 'does not show any owners because no one has full permissions' do
  142. expect(result[:restrict_values]['owner_id']).to eq([''])
  143. end
  144. end
  145. describe 'If selected value is not part of the restriction of set_fixed_to it should recalculate it with the new value #3822', db_strategy: :reset do
  146. let(:field_name1) { SecureRandom.uuid }
  147. let(:screens) do
  148. {
  149. 'create_middle' => {
  150. 'ticket.agent' => {
  151. 'shown' => false,
  152. 'required' => false,
  153. }
  154. }
  155. }
  156. end
  157. let!(:workflow1) do
  158. create(:core_workflow,
  159. object: 'Ticket',
  160. perform: { "ticket.#{field_name1}" => { 'operator' => 'set_fixed_to', 'set_fixed_to' => ['key_3'] } })
  161. end
  162. let!(:workflow2) do
  163. create(:core_workflow,
  164. object: 'Ticket',
  165. condition_selected: {
  166. "ticket.#{field_name1}": {
  167. operator: 'is',
  168. value: 'key_3',
  169. },
  170. })
  171. end
  172. before do
  173. create(:object_manager_attribute_select, name: field_name1, display: field_name1, screens: screens)
  174. ObjectManager::Attribute.migration_execute
  175. end
  176. it 'does select key_3 as new param value and based on this executes workflow 2' do
  177. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  178. end
  179. end
  180. describe 'Add clear selection action or has changed condition #3821' do
  181. let!(:workflow_has_changed) do
  182. create(:core_workflow,
  183. object: 'Ticket',
  184. condition_selected: {
  185. 'ticket.priority_id': {
  186. operator: 'has_changed',
  187. },
  188. })
  189. end
  190. let!(:workflow_changed_to) do
  191. create(:core_workflow,
  192. object: 'Ticket',
  193. condition_selected: {
  194. 'ticket.priority_id': {
  195. operator: 'changed_to',
  196. value: [ Ticket::Priority.find_by(name: '3 high').id.to_s ]
  197. },
  198. })
  199. end
  200. context 'when priority changed' do
  201. let(:payload) do
  202. base_payload.merge('last_changed_attribute' => 'priority_id', 'params' => { 'priority_id' => Ticket::Priority.find_by(name: '3 high').id.to_s })
  203. end
  204. it 'does match on condition has changed' do
  205. expect(result[:matched_workflows]).to include(workflow_has_changed.id)
  206. end
  207. it 'does match on condition changed to' do
  208. expect(result[:matched_workflows]).to include(workflow_changed_to.id)
  209. end
  210. end
  211. context 'when nothing changed' do
  212. it 'does not match on condition has changed' do
  213. expect(result[:matched_workflows]).not_to include(workflow_has_changed.id)
  214. end
  215. it 'does not match on condition changed to' do
  216. expect(result[:matched_workflows]).not_to include(workflow_changed_to.id)
  217. end
  218. end
  219. context 'when state changed' do
  220. let(:payload) do
  221. base_payload.merge('last_changed_attribute' => 'state_id')
  222. end
  223. it 'does not match on condition has changed' do
  224. expect(result[:matched_workflows]).not_to include(workflow_has_changed.id)
  225. end
  226. it 'does not match on condition changed to' do
  227. expect(result[:matched_workflows]).not_to include(workflow_changed_to.id)
  228. end
  229. end
  230. end
  231. describe 'Wrong core workflow execution because of missing relation defaults #4541' do
  232. let!(:workflow) do
  233. create(:core_workflow,
  234. object: 'Ticket',
  235. condition_selected: {
  236. 'ticket.priority_id': {
  237. operator: 'is',
  238. value: [ Ticket::Priority.find_by(name: '1 low').id.to_s ]
  239. },
  240. })
  241. end
  242. before do
  243. Ticket::Priority.find_by(name: '2 normal').update(note: 'Test')
  244. workflow
  245. end
  246. it 'does not execute the core workflow because the default priority is 2 normal and not 1 low' do
  247. expect(result[:matched_workflows]).not_to include(workflow.id)
  248. end
  249. end
  250. describe 'Core Workflow - Action "Fill text if empty" will always be executed even if text field is not empty #4825' do
  251. let(:payload) do
  252. base_payload.merge('params' => { 'article' => { 'body' => 'test123' } })
  253. end
  254. let!(:workflow) do
  255. create(:core_workflow,
  256. object: 'Ticket',
  257. perform: {
  258. 'ticket.body': {
  259. operator: 'fill_in_empty',
  260. fill_in_empty: 'test',
  261. },
  262. })
  263. end
  264. before do
  265. workflow
  266. end
  267. it 'does not prefill if body is set already' do
  268. expect(result[:fill_in]['body']).to be_blank
  269. end
  270. end
  271. describe 'Core-Workflows: Removing groups with re-adding some discards all permissions the user has #5002' do
  272. let(:payload) do
  273. base_payload.merge('params' => { 'group_id' => Group.first.id })
  274. end
  275. let!(:workflow1) do
  276. create(:core_workflow,
  277. object: 'Ticket',
  278. perform: {
  279. 'ticket.group_id': {
  280. operator: 'remove_option',
  281. remove_option: Group.all.map { |x| x.id.to_s },
  282. },
  283. })
  284. end
  285. let!(:workflow2) do
  286. create(:core_workflow,
  287. object: 'Ticket',
  288. perform: {
  289. 'ticket.group_id': {
  290. operator: 'add_option',
  291. add_option: [Group.first.id.to_s],
  292. },
  293. })
  294. end
  295. before do
  296. action_user.group_names_access_map = {
  297. Group.first.name => %w[full],
  298. }
  299. workflow1
  300. workflow2
  301. end
  302. it 'does readd the group' do
  303. expect(result[:restrict_values]['group_id']).to eq(['', Group.first.id.to_s])
  304. end
  305. it 'does keep owners' do
  306. expect(result[:restrict_values]['owner_id']).to include(action_user.id.to_s)
  307. end
  308. it 'does not endless loop because of removing and adding the same element' do
  309. expect(result[:rerun_count]).to be < CoreWorkflow::Result::MAX_RERUN
  310. end
  311. end
  312. end