1
0

jobs_controller_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require 'rails_helper'
  2. describe JobsController do
  3. describe "GET index" do
  4. before do
  5. async_handler_yaml =
  6. "--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n job_class: AgentCheckJob\n job_id: 123id\n queue_name: default\n arguments:\n - %d\n"
  7. Delayed::Job.create!(handler: async_handler_yaml % [agents(:jane_website_agent).id])
  8. Delayed::Job.create!(handler: async_handler_yaml % [agents(:bob_website_agent).id])
  9. Delayed::Job.create!(handler: async_handler_yaml % [agents(:jane_weather_agent).id])
  10. agents(:jane_website_agent).destroy
  11. Delayed::Job.create!(handler: async_handler_yaml % [agents(:bob_weather_agent).id], locked_at: Time.now, locked_by: 'test')
  12. expect(Delayed::Job.count).to be > 0
  13. end
  14. it "does not allow normal users" do
  15. expect(users(:bob)).not_to be_admin
  16. sign_in users(:bob)
  17. expect(get(:index)).to redirect_to(root_path)
  18. end
  19. it "returns all jobs" do
  20. expect(users(:jane)).to be_admin
  21. sign_in users(:jane)
  22. get :index
  23. expect(assigns(:jobs).length).to eq(4)
  24. end
  25. end
  26. describe "DELETE destroy" do
  27. before do
  28. @not_running = Delayed::Job.create
  29. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  30. sign_in users(:jane)
  31. end
  32. it "destroy a job which is not running" do
  33. expect { delete :destroy, params: {id: @not_running.id} }.to change(Delayed::Job, :count).by(-1)
  34. end
  35. it "does not destroy a running job" do
  36. expect { delete :destroy, params: {id: @running.id} }.to change(Delayed::Job, :count).by(0)
  37. end
  38. end
  39. describe "PUT run" do
  40. before do
  41. @not_running = Delayed::Job.create(run_at: Time.now - 1.hour)
  42. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  43. @failed = Delayed::Job.create(run_at: Time.now - 1.hour, locked_at: Time.now, failed_at: Time.now)
  44. sign_in users(:jane)
  45. end
  46. it "queue a job which is not running" do
  47. expect { put :run, params: {id: @not_running.id} }.to change { @not_running.reload.run_at }
  48. end
  49. it "queue a job that failed" do
  50. expect { put :run, params: {id: @failed.id} }.to change { @failed.reload.run_at }
  51. end
  52. it "not queue a running job" do
  53. expect { put :run, params: {id: @running.id} }.not_to change { @not_running.reload.run_at }
  54. end
  55. end
  56. describe "DELETE destroy_failed" do
  57. before do
  58. @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
  59. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  60. @pending = Delayed::Job.create
  61. sign_in users(:jane)
  62. end
  63. it "just destroy failed jobs" do
  64. expect { delete :destroy_failed }.to change(Delayed::Job, :count).by(-1)
  65. end
  66. end
  67. describe "DELETE destroy_all" do
  68. before do
  69. @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
  70. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  71. @pending = Delayed::Job.create
  72. sign_in users(:jane)
  73. end
  74. it "destroys all jobs" do
  75. expect { delete :destroy_all }.to change(Delayed::Job, :count).by(-2)
  76. expect(Delayed::Job.find(@running.id)).to be
  77. end
  78. end
  79. describe "POST retry_queued" do
  80. before do
  81. @not_running = Delayed::Job.create(run_at: Time.zone.now - 1.hour)
  82. @not_running.update_attribute(:attempts, 1)
  83. sign_in users(:jane)
  84. end
  85. it "run the queued job" do
  86. expect(Delayed::Job.last.run_at.to_i).not_to be_within(2).of(Time.zone.now.to_i)
  87. post :retry_queued
  88. expect(Delayed::Job.last.run_at.to_i).to be_within(2).of(Time.zone.now.to_i)
  89. end
  90. end
  91. end