123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068 |
- require 'test_helper'
- class JobTest < ActiveSupport::TestCase
- test 'case 1' do
- # create ticket
- group1 = Group.lookup(name: 'Users')
- group2 = Group.create_or_update(
- name: 'JobTest2',
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket1 = Ticket.create!(
- title: 'job test 1',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 3.days,
- updated_at: Time.zone.now - 3.days,
- created_by_id: 1,
- updated_by_id: 1,
- )
- ticket2 = Ticket.create!(
- title: 'job test 2',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 1.day,
- created_by_id: 1,
- updated_at: Time.zone.now - 1.day,
- updated_by_id: 1,
- )
- ticket3 = Ticket.create!(
- title: 'job test 3',
- group: group2,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'open'),
- priority: Ticket::Priority.lookup(name: '3 high'),
- created_at: Time.zone.now - 1.day,
- created_by_id: 1,
- updated_at: Time.zone.now - 1.day,
- updated_by_id: 1,
- )
- ticket4 = Ticket.create!(
- title: 'job test 4',
- group: group2,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'closed'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 3.days,
- created_by_id: 1,
- updated_at: Time.zone.now - 3.days,
- updated_by_id: 1,
- )
- ticket5 = Ticket.create!(
- title: 'job test 5',
- group: group2,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'open'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 3.days,
- created_by_id: 1,
- updated_by_id: 1,
- updated_at: Time.zone.now - 3.days,
- )
- # create jobs
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: false,
- Tue: false,
- Wed: false,
- Thu: false,
- Fri: false,
- Sat: false,
- Sun: false,
- },
- hours: {
- 0 => false,
- 1 => false,
- 2 => false,
- 3 => false,
- 4 => false,
- 5 => false,
- 6 => false,
- 7 => false,
- 8 => false,
- 9 => false,
- 10 => false,
- 11 => false,
- 12 => false,
- 13 => false,
- 14 => false,
- 15 => false,
- 16 => false,
- 17 => false,
- 18 => false,
- 19 => false,
- 20 => false,
- 21 => false,
- 22 => false,
- 23 => false,
- },
- minutes: {
- 0 => false,
- 10 => false,
- 20 => false,
- 30 => false,
- 40 => false,
- 50 => false,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
- 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
- },
- perform: {
- 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
- },
- disable_notification: true,
- last_run_at: nil,
- active: true,
- created_by_id: 1,
- created_at: Time.zone.now,
- updated_by_id: 1,
- updated_at: Time.zone.now,
- )
- assert_not(job1.next_run_at)
- assert_not(job1.executable?)
- job1.last_run_at = Time.zone.now - 15.minutes
- job1.save!
- assert_not(job1.executable?)
- job1.updated_at = Time.zone.now - 15.minutes
- job1.save!
- assert(job1.executable?)
- job1.active = false
- job1.save!
- assert_not(job1.executable?)
- job1.active = true
- job1.save!
- assert_not(job1.executable?)
- assert_not(job1.in_timeplan?)
- time = Time.zone.now
- # "freeze" time to avoid timing issues
- travel_to(time)
- day_map = {
- 0 => 'Sun',
- 1 => 'Mon',
- 2 => 'Tue',
- 3 => 'Wed',
- 4 => 'Thu',
- 5 => 'Fri',
- 6 => 'Sat',
- }
- job1.timeplan['days'][day_map[time.wday]] = true
- job1.save!
- assert_not(job1.in_timeplan?(time))
- job1.timeplan['hours'][time.hour.to_s] = true
- job1.save!
- assert_not(job1.in_timeplan?(time))
- min = time.min
- if min < 9
- min = 0
- elsif min < 20
- min = 10
- elsif min < 30
- min = 20
- elsif min < 40
- min = 30
- elsif min < 50
- min = 40
- elsif min < 60
- min = 50
- end
- job1.timeplan['minutes'][min.to_s] = true
- job1.save!
- assert(job1.in_timeplan?(time))
- job1.timeplan['hours'][time.hour] = true
- job1.save!
- job1.timeplan['minutes'][min] = true
- job1.save!
- assert(job1.in_timeplan?(time))
- # execute jobs
- job1.updated_at = Time.zone.now - 15.minutes
- job1.save!
- Job.run
- assert(job1.next_run_at)
- assert(job1.executable?)
- assert(job1.in_timeplan?)
- # verify changes on tickets
- ticket1_later = Ticket.find(ticket1.id)
- assert_equal('closed', ticket1_later.state.name)
- assert_not_equal(ticket1.updated_at.to_s, ticket1_later.updated_at.to_s)
- ticket2_later = Ticket.find(ticket2.id)
- assert_equal('new', ticket2_later.state.name)
- assert_equal(ticket2.updated_at.to_s, ticket2_later.updated_at.to_s)
- ticket3_later = Ticket.find(ticket3.id)
- assert_equal('open', ticket3_later.state.name)
- assert_equal(ticket3.updated_at.to_s, ticket3_later.updated_at.to_s)
- ticket4_later = Ticket.find(ticket4.id)
- assert_equal('closed', ticket4_later.state.name)
- assert_equal(ticket4.updated_at.to_s, ticket4_later.updated_at.to_s)
- ticket5_later = Ticket.find(ticket5.id)
- assert_equal('closed', ticket5_later.state.name)
- assert_not_equal(ticket5.updated_at.to_s, ticket5_later.updated_at.to_s)
- # execute jobs again
- job1.updated_at = Time.zone.now - 15.minutes
- job1.save!
- Job.run
- # verify changes on tickets
- ticket1_later_next = Ticket.find(ticket1.id)
- assert_equal('closed', ticket1_later_next.state.name)
- assert_equal(ticket1_later.updated_at.to_s, ticket1_later_next.updated_at.to_s)
- ticket2_later_next = Ticket.find(ticket2.id)
- assert_equal('new', ticket2_later_next.state.name)
- assert_equal(ticket2_later.updated_at.to_s, ticket2_later_next.updated_at.to_s)
- ticket3_later_next = Ticket.find(ticket3.id)
- assert_equal('open', ticket3_later_next.state.name)
- assert_equal(ticket3_later.updated_at.to_s, ticket3_later_next.updated_at.to_s)
- ticket4_later_next = Ticket.find(ticket4.id)
- assert_equal('closed', ticket4_later_next.state.name)
- assert_equal(ticket4_later.updated_at.to_s, ticket4_later_next.updated_at.to_s)
- ticket5_later_next = Ticket.find(ticket5.id)
- assert_equal('closed', ticket5_later_next.state.name)
- assert_equal(ticket5_later.updated_at.to_s, ticket5_later_next.updated_at.to_s)
- end
- test 'with invalid state_id' do
- # create ticket
- group1 = Group.lookup(name: 'Users')
- group2 = Group.create_or_update(
- name: 'JobTest2',
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket1 = Ticket.create!(
- title: 'job test 1',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 3.days,
- updated_at: Time.zone.now - 3.days,
- created_by_id: 1,
- updated_by_id: 1,
- )
- ticket2 = Ticket.create!(
- title: 'job test 2',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 1.day,
- created_by_id: 1,
- updated_at: Time.zone.now - 1.day,
- updated_by_id: 1,
- )
- # create jobs
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: true,
- Tue: true,
- Wed: true,
- Thu: true,
- Fri: true,
- Sat: true,
- Sun: true,
- },
- hours: {
- 0 => true,
- 1 => true,
- 2 => true,
- 3 => true,
- 4 => true,
- 5 => true,
- 6 => true,
- 7 => true,
- 8 => true,
- 9 => true,
- 10 => true,
- 11 => true,
- 12 => true,
- 13 => true,
- 14 => true,
- 15 => true,
- 16 => true,
- 17 => true,
- 18 => true,
- 19 => true,
- 20 => true,
- 21 => true,
- 22 => true,
- 23 => true,
- },
- minutes: {
- 0 => true,
- 10 => true,
- 20 => true,
- 30 => true,
- 40 => true,
- 50 => true,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => '9999' },
- 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
- },
- perform: {
- 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
- },
- disable_notification: true,
- last_run_at: nil,
- updated_at: Time.zone.now - 15.minutes,
- active: true,
- updated_by_id: 1,
- created_by_id: 1,
- )
- assert(job1.executable?)
- assert(job1.in_timeplan?)
- Job.run
- # verify changes on tickets
- ticket1_later = Ticket.find(ticket1.id)
- assert_equal('new', ticket1_later.state.name)
- assert_equal(ticket1.updated_at.to_s, ticket1_later.updated_at.to_s)
- ticket2_later = Ticket.find(ticket2.id)
- assert_equal('new', ticket2_later.state.name)
- assert_equal(ticket2.updated_at.to_s, ticket2_later.updated_at.to_s)
- end
- test 'case 3' do
- # create jobs
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: true,
- Tue: false,
- Wed: false,
- Thu: false,
- Fri: true,
- Sat: false,
- Sun: false,
- },
- hours: {
- 0 => false,
- 1 => true,
- 2 => false,
- 3 => false,
- 4 => false,
- 5 => false,
- 6 => false,
- 7 => false,
- 8 => false,
- 9 => false,
- 10 => true,
- 11 => false,
- 12 => false,
- 13 => false,
- 14 => false,
- 15 => false,
- 16 => false,
- 17 => false,
- 18 => false,
- 19 => false,
- 20 => false,
- 21 => false,
- 22 => false,
- 23 => false,
- },
- minutes: {
- 0 => true,
- 10 => false,
- 20 => false,
- 30 => false,
- 40 => true,
- 50 => false,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
- 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
- },
- perform: {
- 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
- },
- disable_notification: true,
- last_run_at: nil,
- active: true,
- created_by_id: 1,
- created_at: Time.zone.now,
- updated_by_id: 1,
- updated_at: Time.zone.now,
- )
- time_now = Time.zone.parse('2016-03-18 09:17:13 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 10:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-18 10:37:13 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 10:40:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-17 09:17:13 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-17 11:17:13 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-19 11:17:13 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-21 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-22 00:59:59 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-25 00:59:59 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-24 00:59:59 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-24 23:59:59 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-25 01:00:01 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-25 01:09:01 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:40:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-25 01:09:59 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-25 01:40:00 UTC', next_run_at.to_s)
- job1.last_run_at = Time.zone.parse('2016-03-18 10:00:01 UTC')
- job1.save!
- time_now = Time.zone.parse('2016-03-18 10:00:02 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 10:40:00 UTC', next_run_at.to_s)
- job1.last_run_at = Time.zone.parse('2016-03-18 10:40:01 UTC')
- job1.save!
- time_now = Time.zone.parse('2016-03-18 10:40:02 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-21 01:00:00 UTC', next_run_at.to_s)
- end
- test 'case 4' do
- # create jobs
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: true,
- Tue: false,
- Wed: false,
- Thu: false,
- Fri: true,
- Sat: false,
- Sun: false,
- },
- hours: {
- 0 => true,
- 1 => false,
- 2 => false,
- 3 => false,
- 4 => false,
- 5 => false,
- 6 => false,
- 7 => false,
- 8 => false,
- 9 => false,
- 10 => true,
- 11 => false,
- 12 => false,
- 13 => false,
- 14 => false,
- 15 => false,
- 16 => false,
- 17 => false,
- 18 => false,
- 19 => false,
- 20 => false,
- 21 => false,
- 22 => false,
- 23 => false,
- },
- minutes: {
- 0 => true,
- 10 => false,
- 20 => false,
- 30 => false,
- 40 => true,
- 50 => false,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
- 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
- },
- perform: {
- 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
- },
- disable_notification: true,
- last_run_at: nil,
- active: true,
- created_by_id: 1,
- created_at: Time.zone.now,
- updated_by_id: 1,
- updated_at: Time.zone.now,
- )
- time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 00:00:00 UTC', next_run_at.to_s)
- job1.last_run_at = Time.zone.parse('2016-03-17 23:45:01 UTC')
- job1.save!
- time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 00:00:00 UTC', next_run_at.to_s)
- job1.last_run_at = Time.zone.parse('2016-03-17 23:59:01 UTC')
- job1.save!
- time_now = Time.zone.parse('2016-03-17 23:59:23 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-18 00:40:00 UTC', next_run_at.to_s)
- time_now = Time.zone.parse('2016-03-17 23:59:23 UTC')
- assert_not(job1.in_timeplan?(time_now))
- time_now = Time.zone.parse('2016-03-18 00:01:23 UTC')
- assert(job1.in_timeplan?(time_now))
- end
- test 'check next_run_at' do
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: true,
- Tue: false,
- Wed: false,
- Thu: false,
- Fri: false,
- Sat: false,
- Sun: false,
- },
- hours: {
- '0' => true,
- '1' => false,
- '2' => false,
- '3' => false,
- '4' => false,
- '5' => false,
- '6' => false,
- '7' => false,
- '8' => false,
- '9' => false,
- '10' => false,
- '11' => false,
- '12' => false,
- '13' => false,
- '14' => false,
- '15' => false,
- '16' => false,
- '17' => false,
- '18' => false,
- '19' => false,
- '20' => false,
- '21' => false,
- '22' => false,
- '23' => false,
- },
- minutes: {
- '0' => true,
- '10' => false,
- '20' => false,
- '30' => false,
- '40' => false,
- '50' => false,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
- 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
- },
- perform: {
- 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
- },
- disable_notification: true,
- last_run_at: nil,
- active: true,
- created_by_id: 1,
- created_at: Time.zone.now,
- updated_by_id: 1,
- updated_at: Time.zone.now,
- )
- time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
- next_run_at = job1.next_run_at_calculate(time_now)
- assert_equal('2016-03-21 00:00:00 UTC', next_run_at.to_s)
- end
- test 'update next run at' do
- travel_to Time.zone.local(2017, 11, 10, 22, 0o4, 44)
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: false,
- Tue: false,
- Wed: false,
- Thu: false,
- Fri: false,
- Sat: true,
- Sun: false,
- },
- hours: {
- '0' => false,
- '1' => false,
- '2' => false,
- '3' => false,
- '4' => false,
- '5' => false,
- '6' => false,
- '7' => false,
- '8' => false,
- '9' => false,
- '10' => false,
- '11' => false,
- '12' => false,
- '13' => false,
- '14' => false,
- '15' => false,
- '16' => false,
- '17' => false,
- '18' => false,
- '19' => false,
- '20' => false,
- '21' => false,
- '22' => false,
- '23' => true,
- },
- minutes: {
- '0' => true,
- '10' => false,
- '20' => false,
- '30' => false,
- '40' => false,
- '50' => false,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
- },
- perform: {
- 'ticket.action' => { 'value' => 'delete' },
- },
- disable_notification: true,
- last_run_at: nil,
- active: true,
- created_by_id: 1,
- created_at: Time.zone.now,
- updated_by_id: 1,
- updated_at: Time.zone.now,
- )
- assert_equal('2017-11-11 23:00:00 UTC', job1.next_run_at.to_s)
- assert_not(job1.last_run_at)
- travel_to Time.zone.local(2017, 11, 16, 22, 0o4, 44)
- Job.run
- job1.reload
- assert_equal('2017-11-18 23:00:00 UTC', job1.next_run_at.to_s)
- assert_not(job1.last_run_at)
- travel_back
- end
- test 'execute on certain time' do
- travel_to Time.zone.local(2017, 11, 16, 22, 0o4, 44)
- group1 = Group.lookup(name: 'Users')
- ticket1 = Ticket.create!(
- title: 'job test 1',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_by_id: 1,
- updated_by_id: 1,
- )
- ticket2 = Ticket.create!(
- title: 'job test 2',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_by_id: 1,
- updated_by_id: 1,
- )
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: false,
- Tue: false,
- Wed: false,
- Thu: true,
- Fri: false,
- Sat: false,
- Sun: false,
- },
- hours: {
- '0' => false,
- '1' => false,
- '2' => false,
- '3' => false,
- '4' => false,
- '5' => false,
- '6' => false,
- '7' => false,
- '8' => false,
- '9' => false,
- '10' => false,
- '11' => false,
- '12' => false,
- '13' => false,
- '14' => false,
- '15' => false,
- '16' => false,
- '17' => false,
- '18' => false,
- '19' => false,
- '20' => false,
- '21' => false,
- '22' => false,
- '23' => true,
- },
- minutes: {
- '0' => true,
- '10' => false,
- '20' => false,
- '30' => false,
- '40' => false,
- '50' => false,
- },
- },
- condition: {
- 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
- },
- perform: {
- 'ticket.action' => { 'value' => 'delete' },
- },
- disable_notification: true,
- last_run_at: nil,
- active: true,
- created_by_id: 1,
- created_at: Time.zone.now,
- updated_by_id: 1,
- updated_at: Time.zone.now,
- )
- Job.run
- assert(Ticket.find_by(id: ticket1.id))
- assert(Ticket.find_by(id: ticket2.id))
- travel_to Time.zone.local(2017, 11, 16, 23, 0o4, 44)
- Job.run
- assert_not(Ticket.find_by(id: ticket1.id))
- assert_not(Ticket.find_by(id: ticket2.id))
- travel_back
- end
- test 'delete based on tag' do
- # create ticket
- group1 = Group.lookup(name: 'Users')
- group2 = Group.create_or_update(
- name: 'JobTest2',
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket1 = Ticket.create!(
- title: 'job test 1',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 3.days,
- updated_at: Time.zone.now - 3.days,
- created_by_id: 1,
- updated_by_id: 1,
- )
- ticket1.tag_add('spam', 1)
- ticket1.tag_add('test1 ', 1)
- ticket2 = Ticket.create!(
- title: 'job test 2',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_at: Time.zone.now - 1.day,
- created_by_id: 1,
- updated_at: Time.zone.now - 1.day,
- updated_by_id: 1,
- )
- job1 = Job.create_or_update(
- name: 'Test Job1',
- timeplan: {
- days: {
- Mon: true,
- Tue: true,
- Wed: true,
- Thu: true,
- Fri: true,
- Sat: true,
- Sun: true,
- },
- hours: {
- 0 => true,
- 1 => true,
- 2 => true,
- 3 => true,
- 4 => true,
- 5 => true,
- 6 => true,
- 7 => true,
- 8 => true,
- 9 => true,
- 10 => true,
- 11 => true,
- 12 => true,
- 13 => true,
- 14 => true,
- 15 => true,
- 16 => true,
- 17 => true,
- 18 => true,
- 19 => true,
- 20 => true,
- 21 => true,
- 22 => true,
- 23 => true,
- },
- minutes: {
- 0 => true,
- 10 => true,
- 20 => true,
- 30 => true,
- 40 => true,
- 50 => true,
- },
- },
- condition: {
- 'ticket.tags' => { 'operator' => 'contains one', 'value' => 'spam' },
- },
- perform: {
- 'ticket.action' => { 'value' => 'delete' },
- },
- disable_notification: true,
- last_run_at: nil,
- updated_at: Time.zone.now - 15.minutes,
- active: true,
- updated_by_id: 1,
- created_by_id: 1,
- )
- assert(job1.executable?)
- assert(job1.in_timeplan?)
- Job.run
- assert_not(Ticket.find_by(id: ticket1.id))
- assert(Ticket.find_by(id: ticket2.id))
- end
- test 'delete spam on time' do
- # create ticket
- group1 = Group.lookup(name: 'Users')
- group2 = Group.create_or_update(
- name: 'JobTest2',
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket1 = Ticket.create!(
- title: 'job test 1',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'closed'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_by_id: 1,
- updated_by_id: 1,
- )
- ticket2 = Ticket.create!(
- title: 'job test 2',
- group: group2,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'closed'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_by_id: 1,
- updated_by_id: 1,
- )
- travel_to Time.zone.parse('2018-08-13T23:01:01Z')
- job1 = Job.create_or_update(
- name: 'Spam entfernen',
- timeplan: {
- days: {
- Mon: true,
- Tue: false,
- Wed: true,
- Thu: false,
- Fri: true,
- Sat: false,
- Sun: false,
- },
- hours: {
- '0' => false,
- '1' => false,
- '2' => false,
- '3' => false,
- '4' => false,
- '5' => false,
- '6' => false,
- '7' => false,
- '8' => false,
- '9' => false,
- '10' => false,
- '11' => false,
- '12' => false,
- '13' => false,
- '14' => false,
- '15' => false,
- '16' => false,
- '17' => false,
- '18' => false,
- '19' => false,
- '20' => false,
- '21' => false,
- '22' => false,
- '23' => true,
- },
- minutes: {
- '0' => true,
- '10' => false,
- '20' => false,
- '30' => false,
- '40' => false,
- '50' => false,
- }
- },
- condition: {
- 'ticket.group_id' => { 'operator' => 'is', 'value' => group1.id },
- 'ticket.state_id' => { 'operator' => 'is', 'value' => Ticket::State.find_by(name: 'closed').id }
- },
- perform: {
- 'ticket.action' => { 'value' => 'delete' }
- },
- disable_notification: true,
- last_run_at: nil,
- next_run_at: '2018-08-13 23:00:00',
- running: false,
- processed: 19_393,
- matching: 19_428,
- updated_at: Time.zone.now - 15.minutes,
- active: true,
- updated_by_id: 1,
- created_by_id: 1,
- )
- assert(job1.executable?)
- assert(job1.in_timeplan?)
- Job.run
- assert_not(Ticket.find_by(id: ticket1.id))
- assert(Ticket.find_by(id: ticket2.id))
- ticket1 = Ticket.create!(
- title: 'job test 1',
- group: group1,
- customer_id: 2,
- state: Ticket::State.lookup(name: 'closed'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- created_by_id: 1,
- updated_by_id: 1,
- )
- travel_to Time.zone.parse('2018-08-15T23:01:01Z')
- job1.running = true
- job1.save!
- assert_not(job1.executable?)
- assert(job1.in_timeplan?)
- Job.run
- assert(Ticket.find_by(id: ticket1.id))
- assert(Ticket.find_by(id: ticket2.id))
- travel_to Time.zone.parse('2018-08-17T23:08:01Z')
- assert(job1.executable?)
- assert(job1.in_timeplan?)
- Job.run
- assert_not(Ticket.find_by(id: ticket1.id))
- assert(Ticket.find_by(id: ticket2.id))
- end
- end
|