jobs_controller_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. require 'spec_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, 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, 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, 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, id: @failed.id }.to change { @failed.reload.run_at }
  51. end
  52. it "not queue a running job" do
  53. expect { put :run, 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. sign_in users(:jane)
  61. end
  62. it "just destroy failed jobs" do
  63. expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
  64. expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
  65. end
  66. end
  67. end