job_spec.rb 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. require 'models/concerns/has_xss_sanitized_note_examples'
  5. require 'models/concerns/has_timeplan_examples'
  6. RSpec.describe Job, type: :model do
  7. subject(:job) { create(:job) }
  8. it_behaves_like 'ApplicationModel', can_assets: { selectors: %i[condition perform] }
  9. it_behaves_like 'HasXssSanitizedNote', model_factory: :job
  10. it_behaves_like 'HasTimeplan'
  11. describe 'validation' do
  12. it 'uses Validations::VerifyPerformRulesValidator' do
  13. expect(described_class).to have_validator(Validations::VerifyPerformRulesValidator).on(:perform)
  14. end
  15. end
  16. describe 'Class methods:' do
  17. describe '.run' do
  18. let!(:executable_jobs) { jobs.select(&:executable?).select(&:in_timeplan?) }
  19. let!(:nonexecutable_jobs) { jobs - executable_jobs }
  20. let!(:jobs) do
  21. [
  22. # executable
  23. create(:job, :always_on, updated_at: 2.minutes.ago),
  24. # not executable (updated too recently)
  25. create(:job),
  26. # not executable (inactive)
  27. create(:job, updated_at: 2.minutes.ago, active: false),
  28. # not executable (started too recently)
  29. create(:job, :always_on, updated_at: 2.minutes.ago, last_run_at: 5.minutes.ago),
  30. # executable
  31. create(:job, :always_on, updated_at: 2.minutes.ago, last_run_at: 15.minutes.ago),
  32. # not executable (still running, started too recently)
  33. create(:job, :always_on, updated_at: 2.minutes.ago, running: true, last_run_at: 23.hours.ago),
  34. # executable
  35. create(:job, :always_on, updated_at: 2.minutes.ago, running: true, last_run_at: 25.hours.ago),
  36. ]
  37. end
  38. it 'runs all executable jobs (and no others)' do
  39. expect { described_class.run }
  40. .to change { executable_jobs.map(&:reload).map(&:last_run_at).any?(&:nil?) }.to(false)
  41. .and not_change { nonexecutable_jobs.map(&:reload).map(&:last_run_at).all?(&:nil?) }
  42. end
  43. end
  44. end
  45. describe 'Instance methods:' do
  46. describe '#run' do
  47. subject(:job) { create(:job, condition: condition, perform: perform) }
  48. let(:condition) do
  49. {
  50. 'ticket.state_id' => {
  51. 'operator' => 'is',
  52. 'value' => Ticket::State.where(name: %w[new open]).pluck(:id).map(&:to_s)
  53. },
  54. 'ticket.created_at' => {
  55. 'operator' => 'before (relative)',
  56. 'value' => '2',
  57. 'range' => 'day'
  58. },
  59. }
  60. end
  61. let(:perform) do
  62. { 'ticket.state_id' => { 'value' => Ticket::State.find_by(name: 'closed').id.to_s } }
  63. end
  64. let!(:matching_ticket) do
  65. create(:ticket, state: Ticket::State.lookup(name: 'new'), created_at: 3.days.ago)
  66. end
  67. let!(:nonmatching_ticket) do
  68. create(:ticket, state: Ticket::State.lookup(name: 'new'), created_at: 1.day.ago)
  69. end
  70. context 'when job is not #executable?' do
  71. before { allow(job).to receive(:executable?).and_return(false) }
  72. it 'does not perform changes on matching tickets' do
  73. expect { job.run }.not_to change { matching_ticket.reload.state }
  74. end
  75. it 'does not update #last_run_at' do
  76. expect { job.run }.to not_change { job.reload.last_run_at }
  77. end
  78. context 'but "force" flag is given' do
  79. it 'performs changes on matching tickets' do
  80. expect { job.run(true) }
  81. .to change { matching_ticket.reload.state }
  82. .and not_change { nonmatching_ticket.reload.state }
  83. end
  84. it 'updates #last_run_at' do
  85. expect { job.run(true) }
  86. .to change { job.reload.last_run_at }
  87. end
  88. end
  89. end
  90. context 'when job is #executable?' do
  91. before { allow(job).to receive(:executable?).and_return(true) }
  92. context 'and due (#in_timeplan?)' do
  93. before { allow(job).to receive(:in_timeplan?).and_return(true) }
  94. it 'updates #last_run_at' do
  95. expect { job.run }.to change { job.reload.last_run_at }
  96. end
  97. it 'performs changes on matching tickets' do
  98. expect { job.run }
  99. .to change { matching_ticket.reload.state }
  100. .and not_change { nonmatching_ticket.reload.state }
  101. end
  102. context 'and already marked #running' do
  103. before { job.update(running: true) }
  104. it 'resets #running to false' do
  105. expect { job.run }.to change { job.reload.running }.to(false)
  106. end
  107. end
  108. context 'but condition doesn’t match any tickets' do
  109. before { job.update(condition: invalid_condition) }
  110. let(:invalid_condition) do
  111. { 'ticket.state_id' => { 'operator' => 'is', 'value' => '9999' } }
  112. end
  113. it 'performs no changes' do
  114. expect { job.run }
  115. .not_to change { matching_ticket.reload.state }
  116. end
  117. end
  118. describe 'use case: deleting tickets based on tag' do
  119. let(:condition) { { 'ticket.tags' => { 'operator' => 'contains one', 'value' => 'spam' } } }
  120. let(:perform) { { 'ticket.action' => { 'value' => 'delete' } } }
  121. let!(:matching_ticket) { create(:ticket).tap { |t| t.tag_add('spam', 1) } }
  122. let!(:nonmatching_ticket) { create(:ticket) }
  123. it 'deletes tickets matching the specified tags' do
  124. job.run
  125. expect { matching_ticket.reload }.to raise_error(ActiveRecord::RecordNotFound)
  126. expect { nonmatching_ticket.reload }.not_to raise_error
  127. end
  128. end
  129. describe 'use case: deleting tickets based on group' do
  130. let(:condition) { { 'ticket.group_id' => { 'operator' => 'is', 'value' => matching_ticket.group.id } } }
  131. let(:perform) { { 'ticket.action' => { 'value' => 'delete' } } }
  132. let!(:matching_ticket) { create(:ticket) }
  133. let!(:nonmatching_ticket) { create(:ticket) }
  134. it 'deletes tickets matching the specified groups' do
  135. job.run
  136. expect { matching_ticket.reload }.to raise_error(ActiveRecord::RecordNotFound)
  137. expect { nonmatching_ticket.reload }.not_to raise_error
  138. end
  139. end
  140. end
  141. context 'and not due yet' do
  142. before { allow(job).to receive(:in_timeplan?).and_return(false) }
  143. it 'does not perform changes on matching tickets' do
  144. expect { job.run }.not_to change { matching_ticket.reload.state }
  145. end
  146. it 'does not update #last_run_at' do
  147. expect { job.run }.to not_change { job.reload.last_run_at }
  148. end
  149. it 'updates #next_run_at' do
  150. travel_to(Time.current.last_week) # force new value for #next_run_at
  151. expect { job.run }.to change { job.reload.next_run_at }
  152. end
  153. context 'but "force" flag is given' do
  154. it 'performs changes on matching tickets' do
  155. expect { job.run(true) }
  156. .to change { matching_ticket.reload.state }
  157. .and not_change { nonmatching_ticket.reload.state }
  158. end
  159. it 'updates #last_run_at' do
  160. expect { job.run(true) }.to change { job.reload.last_run_at }
  161. end
  162. it 'updates #next_run_at' do
  163. travel_to(Time.current.last_week) # force new value for #next_run_at
  164. expect { job.run }.to change { job.reload.next_run_at }
  165. end
  166. end
  167. end
  168. end
  169. context 'when job has pre_condition:current_user.id in selector' do
  170. let!(:matching_ticket) { create(:ticket, owner_id: 1) }
  171. let!(:nonmatching_ticket) { create(:ticket, owner_id: create(:agent).id) }
  172. let(:condition) do
  173. {
  174. 'ticket.owner_id' => {
  175. 'operator' => 'is',
  176. 'pre_condition' => 'current_user.id',
  177. 'value' => '',
  178. 'value_completion' => ''
  179. },
  180. }
  181. end
  182. before do
  183. UserInfo.current_user_id = create(:admin).id
  184. job
  185. UserInfo.current_user_id = nil
  186. end
  187. it 'performs changes on matching tickets' do
  188. expect { job.run(true) }
  189. .to change { matching_ticket.reload.state }
  190. .and not_change { nonmatching_ticket.reload.state }
  191. end
  192. end
  193. end
  194. describe '#executable?' do
  195. context 'for an inactive Job' do
  196. subject(:job) { create(:job, active: false) }
  197. it 'returns false' do
  198. expect(job.executable?).to be(false)
  199. end
  200. end
  201. context 'for an active job' do
  202. context 'updated in the last minute' do
  203. subject(:job) do
  204. create(:job, active: true,
  205. updated_at: 59.seconds.ago)
  206. end
  207. it 'returns false' do
  208. expect(job.executable?).to be(false)
  209. end
  210. end
  211. context 'updated over a minute ago' do
  212. context 'that has never run before' do
  213. subject(:job) do
  214. create(:job, active: true,
  215. updated_at: 60.seconds.ago)
  216. end
  217. it 'returns true' do
  218. expect(job.executable?).to be(true)
  219. end
  220. end
  221. context 'that was started in the last 10 minutes' do
  222. subject(:job) do
  223. create(:job, active: true,
  224. updated_at: 60.seconds.ago,
  225. last_run_at: 9.minutes.ago)
  226. end
  227. it 'returns false' do
  228. expect(job.executable?).to be(false)
  229. end
  230. context '(or, given an argument, up to 10 minutes before that)' do
  231. subject(:job) do
  232. create(:job, active: true,
  233. updated_at: 60.seconds.ago,
  234. last_run_at: 9.minutes.before(Time.current.yesterday))
  235. end
  236. it 'returns false' do
  237. expect(job.executable?(Time.current.yesterday)).to be(false)
  238. end
  239. end
  240. end
  241. context 'that was started over 10 minutes ago' do
  242. subject(:job) do
  243. create(:job, active: true,
  244. updated_at: 60.seconds.ago,
  245. last_run_at: 10.minutes.ago)
  246. end
  247. it 'returns true' do
  248. expect(job.executable?).to be(true)
  249. end
  250. context '(or, given an argument, over 10 minutes before that)' do
  251. subject(:job) do
  252. create(:job, active: true,
  253. updated_at: 60.seconds.ago,
  254. last_run_at: 10.minutes.before(Time.current.yesterday))
  255. end
  256. it 'returns true' do
  257. expect(job.executable?(Time.current.yesterday)).to be(true)
  258. end
  259. end
  260. context 'but is still running, up to 24 hours later' do
  261. subject(:job) do
  262. create(:job, active: true,
  263. updated_at: 60.seconds.ago,
  264. running: true,
  265. last_run_at: 23.hours.ago)
  266. end
  267. it 'returns false' do
  268. expect(job.executable?).to be(false)
  269. end
  270. end
  271. context 'but is still running, over 24 hours later' do
  272. subject(:job) do
  273. create(:job, active: true,
  274. updated_at: 60.seconds.ago,
  275. running: true,
  276. last_run_at: 24.hours.ago)
  277. end
  278. it 'returns true' do
  279. expect(job.executable?).to be(true)
  280. end
  281. end
  282. end
  283. end
  284. end
  285. end
  286. end
  287. describe 'Attributes:' do
  288. describe '#next_run_at' do
  289. subject(:job) { build(:job) }
  290. it 'is set automatically on save (cannot be set manually)' do
  291. job.next_run_at = 1.day.from_now
  292. expect { job.save }.to change(job, :next_run_at)
  293. end
  294. context 'for an inactive Job' do
  295. subject(:job) { build(:job, active: false) }
  296. it 'is nil' do
  297. expect { job.save }
  298. .not_to change(job, :next_run_at).from(nil)
  299. end
  300. end
  301. context 'for a never-on Job (all #timeplan values are false)' do
  302. subject(:job) { build(:job, :never_on) }
  303. it 'is nil' do
  304. expect { job.save }
  305. .not_to change(job, :next_run_at).from(nil)
  306. end
  307. end
  308. context 'when #timeplan contains at least one true value for :day, :hour, and :minute' do
  309. subject(:job) { build(:job, :never_on) }
  310. let(:base_time) { Time.current.beginning_of_week }
  311. # Tuesday & Thursday @ 12:00a, 12:30a, 6:00p, and 6:30p
  312. before do
  313. job.assign_attributes(
  314. timeplan: {
  315. days: job.timeplan[:days].merge(Tue: true, Thu: true),
  316. hours: job.timeplan[:hours].merge(0 => true, 18 => true),
  317. minutes: job.timeplan[:minutes].merge(0 => true, 30 => true),
  318. }
  319. )
  320. end
  321. let(:valid_timeslots) do
  322. [
  323. base_time + 1.day, # Tue 12:00a
  324. base_time + 1.day + 30.minutes, # Tue 12:30a
  325. base_time + 1.day + 18.hours, # Tue 6:00p
  326. base_time + 1.day + 18.hours + 30.minutes, # Tue 6:30p
  327. base_time + 3.days, # Thu 12:00a
  328. base_time + 3.days + 30.minutes, # Thu 12:30a
  329. base_time + 3.days + 18.hours, # Thu 6:00p
  330. base_time + 3.days + 18.hours + 30.minutes, # Thu 6:30p
  331. ]
  332. end
  333. context 'for a Job that has never been run before' do
  334. context 'when record is saved at the start of the week' do
  335. before { travel_to(base_time) }
  336. it 'is set to the first valid timeslot of the week' do
  337. expect { job.save }
  338. .to change { job.next_run_at.to_i } # comparing times is hard;
  339. .to(valid_timeslots.first.to_i) # integers are less precise
  340. end
  341. end
  342. context 'when record is saved between two valid timeslots' do
  343. before { travel_to(valid_timeslots.third - 1.second) }
  344. it 'is set to the latter timeslot' do
  345. expect { job.save }
  346. .to change { job.next_run_at.to_i } # comparing times is hard;
  347. .to(valid_timeslots.third.to_i) # integers are less precise
  348. end
  349. end
  350. context 'when record is saved during a valid timeslot' do
  351. before { travel_to(valid_timeslots.fifth + 9.minutes + 59.seconds) }
  352. it 'is set to that timeslot' do
  353. expect { job.save }
  354. .to change { job.next_run_at.to_i } # comparing times is hard;
  355. .to(valid_timeslots.fifth.to_i) # integers are less precise
  356. end
  357. end
  358. end
  359. context 'for a Job that been run before' do
  360. context 'when record is saved in the same timeslot as #last_run_at' do
  361. before do
  362. job.assign_attributes(last_run_at: valid_timeslots.fourth + 5.minutes)
  363. travel_to(valid_timeslots.fourth + 7.minutes)
  364. end
  365. it 'is set to the next valid timeslot' do
  366. expect { job.save }
  367. .to change { job.next_run_at.to_i } # comparing times is hard;
  368. .to(valid_timeslots.fifth.to_i) # integers are less precise
  369. end
  370. end
  371. end
  372. end
  373. context 'when updating #next_run_at' do
  374. before do
  375. travel_to Time.zone.parse('2020-12-27 00:00')
  376. job.timeplan = { days: { Mon: true }, hours: { 0 => true }, minutes: { 0 => true } }
  377. end
  378. it 'sets #next_run_at time in selected time zone' do
  379. Setting.set 'timezone_default', 'Europe/Vilnius'
  380. expect { job.save }.to change(job, :next_run_at).to(Time.zone.parse('2020-12-27 22:00'))
  381. end
  382. it 'sets #next_run_at time in UTC' do
  383. expect { job.save }.to change(job, :next_run_at).to(Time.zone.parse('2020-12-28 00:00'))
  384. end
  385. end
  386. end
  387. describe '#perform' do
  388. describe 'Validations:' do
  389. describe '"article.note" key' do
  390. let(:perform) do
  391. { 'article.note' => { 'subject' => 'foo', 'internal' => 'true', 'body' => '' } }
  392. end
  393. it 'fails if an empty "body" is given' do
  394. expect { create(:job, perform: perform) }.to raise_error(ActiveRecord::RecordInvalid, %r{body is missing})
  395. end
  396. end
  397. describe '"notification.email" key' do
  398. let(:perform) do
  399. { 'notification.email' => { 'body' => 'foo', 'recipient' => '', 'subject' => 'bar' } }
  400. end
  401. it 'fails if an empty "recipient" is given' do
  402. expect { create(:job, perform: perform) }.to raise_error(ActiveRecord::RecordInvalid, %r{recipient is missing})
  403. end
  404. end
  405. describe '"notification.sms" key' do
  406. let(:perform) do
  407. { 'notification.sms' => { 'body' => 'foo', 'recipient' => '' } }
  408. end
  409. it 'fails if an empty "recipient" is given' do
  410. expect { create(:job, perform: perform) }.to raise_error(ActiveRecord::RecordInvalid, %r{recipient is missing})
  411. end
  412. end
  413. end
  414. end
  415. end
  416. # when running a very large job, tickets may change during the job
  417. # if tickets are fetched once, their action may be performed later on
  418. # when it no longer matches the conditions
  419. # https://github.com/zammad/zammad/issues/3329
  420. context 'job re-checks conditions' do
  421. let(:job) { create(:job, condition: condition, perform: perform) }
  422. let(:ticket) { create(:ticket, title: initial_title) }
  423. let(:initial_title) { 'initial 3329' }
  424. let(:changed_title) { 'performed 3329' }
  425. let(:condition) do
  426. { 'ticket.title' => { 'value' => initial_title, 'operator' => 'is' } }
  427. end
  428. let(:perform) do
  429. { 'ticket.title' => { 'value'=> changed_title } }
  430. end
  431. it 'condition matches ticket' do
  432. ticket
  433. expect(job.send(:start_job, Time.zone.now, true)).to eq [ticket.id]
  434. end
  435. it 'action is performed' do
  436. ticket
  437. ticket_ids = job.send(:start_job, Time.zone.now, true)
  438. job.send(:run_slice, ticket_ids)
  439. expect(ticket.reload.title).to eq changed_title
  440. end
  441. it 'checks conditions' do
  442. ticket
  443. ticket_ids = job.send(:start_job, Time.zone.now, true)
  444. ticket.update! title: 'another title'
  445. job.send(:run_slice, ticket_ids)
  446. expect(ticket.reload.title).not_to eq changed_title
  447. end
  448. end
  449. describe 'Scheduler ignores "disable notifications == no" #3684', sends_notification_emails: true, performs_jobs: true do
  450. let!(:group) { create(:group) }
  451. let!(:agent) { create(:agent, groups: [group]) }
  452. let!(:ticket) { create(:ticket, group: group, owner: agent) }
  453. let(:perform) do
  454. { 'article.note' => { 'body' => 'ccc', 'internal' => 'true', 'subject' => 'ccc' }, 'ticket.state_id' => { 'value' => 4 } }
  455. end
  456. context 'with disable_notification true' do
  457. let!(:notify_job) { create(:job, :always_on) }
  458. it 'does modify the ticket' do
  459. expect { notify_job.run(true) }.to change { ticket.reload.state }
  460. end
  461. it 'does not send a notification to the owner of the ticket' do # rubocop:disable RSpec/ExampleLength
  462. check_notification do
  463. notify_job.run(true)
  464. perform_enqueued_jobs
  465. not_sent(
  466. template: 'ticket_update',
  467. user: agent,
  468. objects: hash_including({ article: nil })
  469. )
  470. end
  471. end
  472. end
  473. context 'with disable_notification false' do
  474. let!(:notify_job) { create(:job, :always_on, disable_notification: false, perform: perform) }
  475. it 'does modify the ticket' do
  476. expect { notify_job.run(true) }.to change { ticket.reload.state }
  477. end
  478. it 'does send a notification to the owner of the ticket with trigger note in notification body' do # rubocop:disable RSpec/ExampleLength
  479. check_notification do
  480. notify_job.run(true)
  481. perform_enqueued_jobs
  482. sent(
  483. template: 'ticket_update',
  484. user: agent,
  485. objects: hash_including({ article: ticket.reload.articles.first })
  486. )
  487. end
  488. end
  489. end
  490. end
  491. end