trigger_spec.rb 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. require 'rails_helper'
  2. require 'models/application_model_examples'
  3. require 'models/concerns/has_xss_sanitized_note_examples'
  4. RSpec.describe Trigger, type: :model do
  5. subject(:trigger) { create(:trigger, condition: condition, perform: perform) }
  6. it_behaves_like 'ApplicationModel', can_assets: { selectors: %i[condition perform] }
  7. it_behaves_like 'HasXssSanitizedNote', model_factory: :trigger
  8. describe 'validation' do
  9. let(:condition) do
  10. { 'ticket.action' => { 'operator' => 'is', 'value' => 'create' } }
  11. end
  12. let(:perform) do
  13. { 'ticket.title' => { 'value'=>'triggered' } }
  14. end
  15. context 'notification.email' do
  16. context 'missing recipient' do
  17. let(:perform) do
  18. {
  19. 'notification.email' => {
  20. 'subject' => 'Hello',
  21. 'body' => 'World!'
  22. }
  23. }
  24. end
  25. it 'raises an error' do
  26. expect { trigger.save! }.to raise_error(Exceptions::UnprocessableEntity, 'Invalid perform notification.email, recipient is missing!')
  27. end
  28. end
  29. end
  30. end
  31. describe 'Send-email triggers' do
  32. before do
  33. described_class.destroy_all # Default DB state includes three sample triggers
  34. trigger # create subject trigger
  35. end
  36. let(:perform) do
  37. {
  38. 'notification.email' => {
  39. 'recipient' => 'ticket_customer',
  40. 'subject' => 'foo',
  41. 'body' => 'some body with >snip<#{article.body_as_html}>/snip<', # rubocop:disable Lint/InterpolationCheck
  42. }
  43. }
  44. end
  45. context 'for condition "ticket created"' do
  46. let(:condition) do
  47. { 'ticket.action' => { 'operator' => 'is', 'value' => 'create' } }
  48. end
  49. context 'when ticket is created directly' do
  50. let!(:ticket) { create(:ticket) }
  51. it 'fires (without altering ticket state)' do
  52. expect { TransactionDispatcher.commit }
  53. .to change(Ticket::Article, :count).by(1)
  54. .and not_change { ticket.reload.state.name }.from('new')
  55. end
  56. end
  57. context 'when ticket has tags' do
  58. let(:tag1) { create(:'tag/item', name: 't1') }
  59. let(:tag2) { create(:'tag/item', name: 't2') }
  60. let(:tag3) { create(:'tag/item', name: 't3') }
  61. let!(:ticket) do
  62. ticket = create(:ticket)
  63. create(:tag, o: ticket, tag_item: tag1)
  64. create(:tag, o: ticket, tag_item: tag2)
  65. create(:tag, o: ticket, tag_item: tag3)
  66. ticket
  67. end
  68. let(:perform) do
  69. {
  70. 'notification.email' => {
  71. 'recipient' => 'ticket_customer',
  72. 'subject' => 'foo',
  73. 'body' => 'some body with #{ticket.tags}', # rubocop:disable Lint/InterpolationCheck
  74. }
  75. }
  76. end
  77. it 'fires body with replaced tags' do
  78. TransactionDispatcher.commit
  79. expect(Ticket::Article.last.body).to eq('some body with t1, t2, t3')
  80. end
  81. end
  82. context 'when ticket is created via Channel::EmailParser.process' do
  83. before { create(:email_address, groups: [Group.first]) }
  84. let(:raw_email) { File.read(Rails.root.join('test/data/mail/mail001.box')) }
  85. it 'fires (without altering ticket state)' do
  86. expect { Channel::EmailParser.new.process({}, raw_email) }
  87. .to change(Ticket, :count).by(1)
  88. .and change { Ticket::Article.count }.by(2)
  89. expect(Ticket.last.state.name).to eq('new')
  90. end
  91. end
  92. context 'when ticket is created via Channel::EmailParser.process with inline image' do
  93. before { create(:email_address, groups: [Group.first]) }
  94. let(:raw_email) { File.read(Rails.root.join('test/data/mail/mail010.box')) }
  95. it 'fires (without altering ticket state)' do
  96. expect { Channel::EmailParser.new.process({}, raw_email) }
  97. .to change(Ticket, :count).by(1)
  98. .and change { Ticket::Article.count }.by(2)
  99. expect(Ticket.last.state.name).to eq('new')
  100. article = Ticket::Article.last
  101. expect(article.type.name).to eq('email')
  102. expect(article.sender.name).to eq('System')
  103. expect(article.attachments.count).to eq(1)
  104. expect(article.attachments[0].filename).to eq('image001.jpg')
  105. expect(article.attachments[0].preferences['Content-ID']).to eq('image001.jpg@01CDB132.D8A510F0')
  106. expect(article.body).to eq(<<~RAW.chomp
  107. some body with &gt;snip&lt;<div>
  108. <p>Herzliche Grüße aus Oberalteich sendet Herrn Smith</p>
  109. <p> </p>
  110. <p>Sepp Smith - Dipl.Ing. agr. (FH)</p>
  111. <p>Geschäftsführer der example Straubing-Bogen</p>
  112. <p>Klosterhof 1 | 94327 Bogen-Oberalteich</p>
  113. <p>Tel: 09422-505601 | Fax: 09422-505620</p>
  114. <p><span>Internet: <a href="http://example-straubing-bogen.de/" rel="nofollow noreferrer noopener" target="_blank"><span style="color:blue;">http://example-straubing-bogen.de</span></a></span></p>
  115. <p><span lang="EN-US">Facebook: </span><a href="http://facebook.de/examplesrbog" rel="nofollow noreferrer noopener" target="_blank"><span lang="EN-US" style="color:blue;">http://facebook.de/examplesrbog</span></a><span lang="EN-US"></span></p>
  116. <p><b><span style="color:navy;"><img border="0" src="cid:image001.jpg@01CDB132.D8A510F0" alt="Beschreibung: Beschreibung: efqmLogo" style="width:60px;height:19px;"></span></b><b><span lang="EN-US" style="color:navy;"> - European Foundation für Quality Management</span></b><span lang="EN-US"></span></p>
  117. <p><span lang="EN-US"><p> </p></span></p>
  118. </div>&gt;/snip&lt;
  119. RAW
  120. )
  121. end
  122. end
  123. context 'notification.email recipient' do
  124. let!(:ticket) { create(:ticket) }
  125. let!(:recipient1) { create(:user, email: 'test1@zammad-test.com') }
  126. let!(:recipient2) { create(:user, email: 'test2@zammad-test.com') }
  127. let!(:recipient3) { create(:user, email: 'test3@zammad-test.com') }
  128. let(:perform) do
  129. {
  130. 'notification.email' => {
  131. 'recipient' => recipient,
  132. 'subject' => 'Hello',
  133. 'body' => 'World!'
  134. }
  135. }
  136. end
  137. before { TransactionDispatcher.commit }
  138. context 'mix of recipient group keyword and single recipient users' do
  139. let(:recipient) { [ 'ticket_customer', "userid_#{recipient1.id}", "userid_#{recipient2.id}", "userid_#{recipient3.id}" ] }
  140. it 'contains all recipients' do
  141. expect(ticket.articles.last.to).to eq("#{ticket.customer.email}, #{recipient1.email}, #{recipient2.email}, #{recipient3.email}")
  142. end
  143. context 'duplicate recipient' do
  144. let(:recipient) { [ 'ticket_customer', "userid_#{ticket.customer.id}" ] }
  145. it 'contains only one recipient' do
  146. expect(ticket.articles.last.to).to eq(ticket.customer.email.to_s)
  147. end
  148. end
  149. end
  150. context 'list of single users only' do
  151. let(:recipient) { [ "userid_#{recipient1.id}", "userid_#{recipient2.id}", "userid_#{recipient3.id}" ] }
  152. it 'contains all recipients' do
  153. expect(ticket.articles.last.to).to eq("#{recipient1.email}, #{recipient2.email}, #{recipient3.email}")
  154. end
  155. context 'assets' do
  156. it 'resolves Users from recipient list' do
  157. expect(trigger.assets({})[:User].keys).to include(recipient1.id, recipient2.id, recipient3.id)
  158. end
  159. context 'single entry' do
  160. let(:recipient) { "userid_#{recipient1.id}" }
  161. it 'resolves User from recipient list' do
  162. expect(trigger.assets({})[:User].keys).to include(recipient1.id)
  163. end
  164. end
  165. end
  166. end
  167. context 'recipient group keyword only' do
  168. let(:recipient) { 'ticket_customer' }
  169. it 'contains matching recipient' do
  170. expect(ticket.articles.last.to).to eq(ticket.customer.email.to_s)
  171. end
  172. end
  173. end
  174. context 'active S/MIME integration' do
  175. before do
  176. Setting.set('smime_integration', true)
  177. create(:smime_certificate, :with_private, fixture: system_email_address)
  178. create(:smime_certificate, fixture: customer_email_address)
  179. end
  180. let(:system_email_address) { 'smime1@example.com' }
  181. let(:customer_email_address) { 'smime2@example.com' }
  182. let(:email_address) { create(:email_address, email: system_email_address) }
  183. let(:group) { create(:group, email_address: email_address) }
  184. let(:customer) { create(:customer, email: customer_email_address) }
  185. let(:security_preferences) { Ticket::Article.last.preferences[:security] }
  186. let(:perform) do
  187. {
  188. 'notification.email' => {
  189. 'recipient' => 'ticket_customer',
  190. 'subject' => 'Subject dummy.',
  191. 'body' => 'Body dummy.',
  192. }.merge(security_configuration)
  193. }
  194. end
  195. let!(:ticket) { create(:ticket, group: group, customer: customer) }
  196. context 'sending articles' do
  197. before do
  198. TransactionDispatcher.commit
  199. end
  200. context 'expired certificate' do
  201. let(:system_email_address) { 'expiredsmime1@example.com' }
  202. let(:security_configuration) do
  203. {
  204. 'sign' => 'always',
  205. 'encryption' => 'always',
  206. }
  207. end
  208. it 'creates unsigned article' do
  209. expect(security_preferences[:sign][:success]).to be false
  210. expect(security_preferences[:encryption][:success]).to be true
  211. end
  212. end
  213. context 'sign and encryption not set' do
  214. let(:security_configuration) { {} }
  215. it 'does not sign or encrypt' do
  216. expect(security_preferences[:sign][:success]).to be false
  217. expect(security_preferences[:encryption][:success]).to be false
  218. end
  219. end
  220. context 'sign and encryption disabled' do
  221. let(:security_configuration) do
  222. {
  223. 'sign' => 'no',
  224. 'encryption' => 'no',
  225. }
  226. end
  227. it 'does not sign or encrypt' do
  228. expect(security_preferences[:sign][:success]).to be false
  229. expect(security_preferences[:encryption][:success]).to be false
  230. end
  231. end
  232. context 'sign is enabled' do
  233. let(:security_configuration) do
  234. {
  235. 'sign' => 'always',
  236. 'encryption' => 'no',
  237. }
  238. end
  239. it 'signs' do
  240. expect(security_preferences[:sign][:success]).to be true
  241. expect(security_preferences[:encryption][:success]).to be false
  242. end
  243. end
  244. context 'encryption enabled' do
  245. let(:security_configuration) do
  246. {
  247. 'sign' => 'no',
  248. 'encryption' => 'always',
  249. }
  250. end
  251. it 'encrypts' do
  252. expect(security_preferences[:sign][:success]).to be false
  253. expect(security_preferences[:encryption][:success]).to be true
  254. end
  255. end
  256. context 'sign and encryption enabled' do
  257. let(:security_configuration) do
  258. {
  259. 'sign' => 'always',
  260. 'encryption' => 'always',
  261. }
  262. end
  263. it 'signs and encrypts' do
  264. expect(security_preferences[:sign][:success]).to be true
  265. expect(security_preferences[:encryption][:success]).to be true
  266. end
  267. end
  268. end
  269. context 'discard' do
  270. context 'sign' do
  271. let(:security_configuration) do
  272. {
  273. 'sign' => 'discard',
  274. }
  275. end
  276. context 'group without certificate' do
  277. let(:group) { create(:group) }
  278. it 'does not fire' do
  279. expect { TransactionDispatcher.commit }
  280. .to change(Ticket::Article, :count).by(0)
  281. end
  282. end
  283. end
  284. context 'encryption' do
  285. let(:security_configuration) do
  286. {
  287. 'encryption' => 'discard',
  288. }
  289. end
  290. context 'customer without certificate' do
  291. let(:customer) { create(:customer) }
  292. it 'does not fire' do
  293. expect { TransactionDispatcher.commit }
  294. .to change(Ticket::Article, :count).by(0)
  295. end
  296. end
  297. end
  298. context 'mixed' do
  299. context 'sign' do
  300. let(:security_configuration) do
  301. {
  302. 'encryption' => 'always',
  303. 'sign' => 'discard',
  304. }
  305. end
  306. context 'group without certificate' do
  307. let(:group) { create(:group) }
  308. it 'does not fire' do
  309. expect { TransactionDispatcher.commit }
  310. .to change(Ticket::Article, :count).by(0)
  311. end
  312. end
  313. end
  314. context 'encryption' do
  315. let(:security_configuration) do
  316. {
  317. 'encryption' => 'discard',
  318. 'sign' => 'always',
  319. }
  320. end
  321. context 'customer without certificate' do
  322. let(:customer) { create(:customer) }
  323. it 'does not fire' do
  324. expect { TransactionDispatcher.commit }
  325. .to change(Ticket::Article, :count).by(0)
  326. end
  327. end
  328. end
  329. end
  330. end
  331. end
  332. end
  333. context 'for condition "ticket updated"' do
  334. let(:condition) do
  335. { 'ticket.action' => { 'operator' => 'is', 'value' => 'update' } }
  336. end
  337. let!(:ticket) { create(:ticket).tap { TransactionDispatcher.commit } }
  338. context 'when new article is created directly' do
  339. context 'with empty #preferences hash' do
  340. let!(:article) { create(:ticket_article, ticket: ticket) }
  341. it 'fires (without altering ticket state)' do
  342. expect { TransactionDispatcher.commit }
  343. .to change { ticket.reload.articles.count }.by(1)
  344. .and not_change { ticket.reload.state.name }.from('new')
  345. end
  346. end
  347. context 'with #preferences { "send-auto-response" => false }' do
  348. let!(:article) do
  349. create(:ticket_article,
  350. ticket: ticket,
  351. preferences: { 'send-auto-response' => false })
  352. end
  353. it 'does not fire' do
  354. expect { TransactionDispatcher.commit }
  355. .not_to change { ticket.reload.articles.count }
  356. end
  357. end
  358. end
  359. context 'when new article is created via Channel::EmailParser.process' do
  360. context 'with a regular message' do
  361. let!(:article) do
  362. create(:ticket_article,
  363. ticket: ticket,
  364. message_id: raw_email[%r{(?<=^References: )\S*}],
  365. subject: raw_email[%r{(?<=^Subject: Re: ).*$}])
  366. end
  367. let(:raw_email) { File.read(Rails.root.join('test/data/mail/mail005.box')) }
  368. it 'fires (without altering ticket state)' do
  369. expect { Channel::EmailParser.new.process({}, raw_email) }
  370. .to not_change { Ticket.count }
  371. .and change { ticket.reload.articles.count }.by(2)
  372. .and not_change { ticket.reload.state.name }.from('new')
  373. end
  374. end
  375. context 'with delivery-failed "bounce message"' do
  376. let!(:article) do
  377. create(:ticket_article,
  378. ticket: ticket,
  379. message_id: raw_email[%r{(?<=^Message-ID: )\S*}])
  380. end
  381. let(:raw_email) { File.read(Rails.root.join('test/data/mail/mail055.box')) }
  382. it 'does not fire' do
  383. expect { Channel::EmailParser.new.process({}, raw_email) }
  384. .to change { ticket.reload.articles.count }.by(1)
  385. end
  386. end
  387. end
  388. end
  389. context 'with condition execution_time.calendar_id' do
  390. let(:calendar) { create(:calendar) }
  391. let(:perform) do
  392. { 'ticket.title'=>{ 'value'=>'triggered' } }
  393. end
  394. let!(:ticket) { create(:ticket, title: 'Test Ticket') }
  395. context 'is in working time' do
  396. let(:condition) do
  397. { 'ticket.state_id' => { 'operator' => 'is', 'value' => Ticket::State.all.pluck(:id) }, 'execution_time.calendar_id' => { 'operator' => 'is in working time', 'value' => calendar.id } }
  398. end
  399. it 'does trigger only in working time' do
  400. travel_to Time.zone.parse('2020-02-12T12:00:00Z0')
  401. expect { TransactionDispatcher.commit }.to change { ticket.reload.title }.to('triggered')
  402. end
  403. it 'does not trigger out of working time' do
  404. travel_to Time.zone.parse('2020-02-12T02:00:00Z0')
  405. TransactionDispatcher.commit
  406. expect(ticket.reload.title).to eq('Test Ticket')
  407. end
  408. end
  409. context 'is not in working time' do
  410. let(:condition) do
  411. { 'execution_time.calendar_id' => { 'operator' => 'is not in working time', 'value' => calendar.id } }
  412. end
  413. it 'does not trigger in working time' do
  414. travel_to Time.zone.parse('2020-02-12T12:00:00Z0')
  415. TransactionDispatcher.commit
  416. expect(ticket.reload.title).to eq('Test Ticket')
  417. end
  418. it 'does trigger out of working time' do
  419. travel_to Time.zone.parse('2020-02-12T02:00:00Z0')
  420. expect { TransactionDispatcher.commit }.to change { ticket.reload.title }.to('triggered')
  421. end
  422. end
  423. end
  424. context 'with article last sender equals system address' do
  425. let!(:ticket) { create(:ticket) }
  426. let(:perform) do
  427. {
  428. 'notification.email' => {
  429. 'recipient' => 'article_last_sender',
  430. 'subject' => 'foo last sender',
  431. 'body' => 'some body with &gt;snip&lt;#{article.body_as_html}&gt;/snip&lt;', # rubocop:disable Lint/InterpolationCheck
  432. }
  433. }
  434. end
  435. let(:condition) do
  436. { 'ticket.state_id' => { 'operator' => 'is', 'value' => Ticket::State.all.pluck(:id) } }
  437. end
  438. let!(:system_address) do
  439. create(:email_address)
  440. end
  441. context 'article with from equal to the a system address' do
  442. let!(:article) do
  443. create(:ticket_article,
  444. ticket: ticket,
  445. from: system_address.email,)
  446. end
  447. it 'does not trigger because of the last article is created my system address' do
  448. expect { TransactionDispatcher.commit }.to change { ticket.reload.articles.count }.by(0)
  449. expect(Ticket::Article.where(ticket: ticket).last.subject).not_to eq('foo last sender')
  450. expect(Ticket::Article.where(ticket: ticket).last.to).not_to eq(system_address.email)
  451. end
  452. end
  453. context 'article with reply_to equal to the a system address' do
  454. let!(:article) do
  455. create(:ticket_article,
  456. ticket: ticket,
  457. from: system_address.email,
  458. reply_to: system_address.email,)
  459. end
  460. it 'does not trigger because of the last article is created my system address' do
  461. expect { TransactionDispatcher.commit }.to change { ticket.reload.articles.count }.by(0)
  462. expect(Ticket::Article.where(ticket: ticket).last.subject).not_to eq('foo last sender')
  463. expect(Ticket::Article.where(ticket: ticket).last.to).not_to eq(system_address.email)
  464. end
  465. end
  466. end
  467. end
  468. context 'with pre condition current_user.id' do
  469. let(:perform) do
  470. { 'ticket.title'=>{ 'value'=>'triggered' } }
  471. end
  472. let(:user) do
  473. user = create(:agent)
  474. user.roles.first.groups << group
  475. user
  476. end
  477. let(:group) { Group.first }
  478. let(:ticket) do
  479. create(:ticket,
  480. title: 'Test Ticket', group: group,
  481. owner_id: user.id, created_by_id: user.id, updated_by_id: user.id)
  482. end
  483. shared_examples 'successful trigger' do |attribute:|
  484. let(:attribute) { attribute }
  485. let(:condition) do
  486. { attribute => { operator: 'is', pre_condition: 'current_user.id', value: '', value_completion: '' } }
  487. end
  488. it "for #{attribute}" do
  489. ticket && trigger
  490. expect { TransactionDispatcher.commit }.to change { ticket.reload.title }.to('triggered')
  491. end
  492. end
  493. it_behaves_like 'successful trigger', attribute: 'ticket.updated_by_id'
  494. it_behaves_like 'successful trigger', attribute: 'ticket.owner_id'
  495. end
  496. describe 'Multi-trigger interactions:' do
  497. let(:ticket) { create(:ticket) }
  498. context 'cascading (i.e., trigger A satisfies trigger B satisfies trigger C)' do
  499. subject!(:triggers) do
  500. [
  501. create(:trigger, condition: initial_state, perform: first_change, name: 'A'),
  502. create(:trigger, condition: first_change, perform: second_change, name: 'B'),
  503. create(:trigger, condition: second_change, perform: third_change, name: 'C')
  504. ]
  505. end
  506. context 'in a chain' do
  507. let(:initial_state) do
  508. {
  509. 'ticket.state_id' => {
  510. 'operator' => 'is',
  511. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  512. }
  513. }
  514. end
  515. let(:first_change) do
  516. {
  517. 'ticket.state_id' => {
  518. 'operator' => 'is',
  519. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  520. }
  521. }
  522. end
  523. let(:second_change) do
  524. {
  525. 'ticket.state_id' => {
  526. 'operator' => 'is',
  527. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  528. }
  529. }
  530. end
  531. let(:third_change) do
  532. {
  533. 'ticket.state_id' => {
  534. 'operator' => 'is',
  535. 'value' => Ticket::State.lookup(name: 'merged').id.to_s,
  536. }
  537. }
  538. end
  539. context 'in alphabetical order (by name)' do
  540. it 'fires all triggers in sequence' do
  541. expect { TransactionDispatcher.commit }
  542. .to change { ticket.reload.state.name }.to('merged')
  543. end
  544. end
  545. context 'out of alphabetical order (by name)' do
  546. before do
  547. triggers.first.update(name: 'E')
  548. triggers.second.update(name: 'F')
  549. triggers.third.update(name: 'D')
  550. end
  551. context 'with Setting ticket_trigger_recursive: true' do
  552. before { Setting.set('ticket_trigger_recursive', true) }
  553. it 'evaluates triggers in sequence, then loops back to the start and re-evalutes skipped triggers' do
  554. expect { TransactionDispatcher.commit }
  555. .to change { ticket.reload.state.name }.to('merged')
  556. end
  557. end
  558. context 'with Setting ticket_trigger_recursive: false' do
  559. before { Setting.set('ticket_trigger_recursive', false) }
  560. it 'evaluates triggers in sequence, firing only the ones that match' do
  561. expect { TransactionDispatcher.commit }
  562. .to change { ticket.reload.state.name }.to('closed')
  563. end
  564. end
  565. end
  566. end
  567. context 'in circular reference (i.e., trigger A satisfies trigger B satisfies trigger C satisfies trigger A...)' do
  568. let(:initial_state) do
  569. {
  570. 'ticket.priority_id' => {
  571. 'operator' => 'is',
  572. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  573. }
  574. }
  575. end
  576. let(:first_change) do
  577. {
  578. 'ticket.priority_id' => {
  579. 'operator' => 'is',
  580. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  581. }
  582. }
  583. end
  584. let(:second_change) do
  585. {
  586. 'ticket.priority_id' => {
  587. 'operator' => 'is',
  588. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  589. }
  590. }
  591. end
  592. let(:third_change) do
  593. {
  594. 'ticket.priority_id' => {
  595. 'operator' => 'is',
  596. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  597. }
  598. }
  599. end
  600. context 'with Setting ticket_trigger_recursive: true' do
  601. before { Setting.set('ticket_trigger_recursive', true) }
  602. it 'fires each trigger once, without being caught in an endless loop' do
  603. expect { Timeout.timeout(2) { TransactionDispatcher.commit } }
  604. .to not_change { ticket.reload.priority.name }
  605. .and not_raise_error
  606. end
  607. end
  608. context 'with Setting ticket_trigger_recursive: false' do
  609. before { Setting.set('ticket_trigger_recursive', false) }
  610. it 'fires each trigger once, without being caught in an endless loop' do
  611. expect { Timeout.timeout(2) { TransactionDispatcher.commit } }
  612. .to not_change { ticket.reload.priority.name }
  613. .and not_raise_error
  614. end
  615. end
  616. end
  617. end
  618. context 'competing (i.e., trigger A un-satisfies trigger B)' do
  619. subject!(:triggers) do
  620. [
  621. create(:trigger, condition: initial_state, perform: change_a, name: 'A'),
  622. create(:trigger, condition: initial_state, perform: change_b, name: 'B')
  623. ]
  624. end
  625. let(:initial_state) do
  626. {
  627. 'ticket.state_id' => {
  628. 'operator' => 'is',
  629. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  630. }
  631. }
  632. end
  633. let(:change_a) do
  634. {
  635. 'ticket.state_id' => {
  636. 'operator' => 'is',
  637. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  638. }
  639. }
  640. end
  641. let(:change_b) do
  642. {
  643. 'ticket.priority_id' => {
  644. 'operator' => 'is',
  645. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  646. }
  647. }
  648. end
  649. it 'evaluates triggers in sequence, firing only the ones that match' do
  650. expect { TransactionDispatcher.commit }
  651. .to change { ticket.reload.state.name }.to('open')
  652. .and not_change { ticket.reload.priority.name }
  653. end
  654. end
  655. end
  656. end