job_spec.rb 20 KB

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