jobs_controller_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'spec_helper'
  2. describe JobsController do
  3. describe "GET index" do
  4. before do
  5. Delayed::Job.create!
  6. Delayed::Job.create!
  7. expect(Delayed::Job.count).to be > 0
  8. end
  9. it "does not allow normal users" do
  10. expect(users(:bob)).not_to be_admin
  11. sign_in users(:bob)
  12. expect(get(:index)).to redirect_to(root_path)
  13. end
  14. it "returns all jobs" do
  15. expect(users(:jane)).to be_admin
  16. sign_in users(:jane)
  17. get :index
  18. expect(assigns(:jobs).length).to eq(2)
  19. end
  20. end
  21. describe "DELETE destroy" do
  22. before do
  23. @not_running = Delayed::Job.create
  24. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  25. sign_in users(:jane)
  26. end
  27. it "destroy a job which is not running" do
  28. expect { delete :destroy, id: @not_running.id }.to change(Delayed::Job, :count).by(-1)
  29. end
  30. it "does not destroy a running job" do
  31. expect { delete :destroy, id: @running.id }.to change(Delayed::Job, :count).by(0)
  32. end
  33. end
  34. describe "PUT run" do
  35. before do
  36. @not_running = Delayed::Job.create(run_at: Time.now - 1.hour)
  37. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  38. @failed = Delayed::Job.create(run_at: Time.now - 1.hour, locked_at: Time.now, failed_at: Time.now)
  39. sign_in users(:jane)
  40. end
  41. it "queue a job which is not running" do
  42. expect { put :run, id: @not_running.id }.to change { @not_running.reload.run_at }
  43. end
  44. it "queue a job that failed" do
  45. expect { put :run, id: @failed.id }.to change { @failed.reload.run_at }
  46. end
  47. it "not queue a running job" do
  48. expect { put :run, id: @running.id }.not_to change { @not_running.reload.run_at }
  49. end
  50. end
  51. describe "DELETE destroy_failed" do
  52. before do
  53. @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
  54. @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
  55. sign_in users(:jane)
  56. end
  57. it "just destroy failed jobs" do
  58. expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
  59. expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
  60. end
  61. end
  62. end