events_controller_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. require 'rails_helper'
  2. describe EventsController do
  3. before do
  4. expect(Event.where(:user_id => users(:bob).id).count).to be > 0
  5. expect(Event.where(:user_id => users(:jane).id).count).to be > 0
  6. end
  7. describe "GET index" do
  8. it "only returns Events created by Agents of the current user" do
  9. sign_in users(:bob)
  10. get :index
  11. expect(assigns(:events).all? {|i| expect(i.user).to eq(users(:bob)) }).to be_truthy
  12. end
  13. it "can filter by Agent" do
  14. sign_in users(:bob)
  15. get :index, params: {:agent_id => agents(:bob_website_agent)}
  16. expect(assigns(:events).length).to eq(agents(:bob_website_agent).events.length)
  17. expect(assigns(:events).all? {|i| expect(i.agent).to eq(agents(:bob_website_agent)) }).to be_truthy
  18. expect {
  19. get :index, params: {:agent_id => agents(:jane_website_agent)}
  20. }.to raise_error(ActiveRecord::RecordNotFound)
  21. end
  22. end
  23. describe "GET show" do
  24. it "only shows Events for the current user" do
  25. sign_in users(:bob)
  26. get :show, params: {:id => events(:bob_website_agent_event).to_param}
  27. expect(assigns(:event)).to eq(events(:bob_website_agent_event))
  28. expect {
  29. get :show, params: {:id => events(:jane_website_agent_event).to_param}
  30. }.to raise_error(ActiveRecord::RecordNotFound)
  31. end
  32. end
  33. describe "POST reemit" do
  34. before do
  35. request.env["HTTP_REFERER"] = "/events"
  36. sign_in users(:bob)
  37. end
  38. it "clones and re-emits events" do
  39. expect {
  40. post :reemit, params: {:id => events(:bob_website_agent_event).to_param}
  41. }.to change { Event.count }.by(1)
  42. expect(Event.last.payload).to eq(events(:bob_website_agent_event).payload)
  43. expect(Event.last.agent).to eq(events(:bob_website_agent_event).agent)
  44. expect(Event.last.created_at.to_i).to be_within(2).of(Time.now.to_i)
  45. end
  46. it "can only re-emit Events for the current user" do
  47. expect {
  48. post :reemit, params: {:id => events(:jane_website_agent_event).to_param}
  49. }.to raise_error(ActiveRecord::RecordNotFound)
  50. end
  51. end
  52. describe "DELETE destroy" do
  53. it "only deletes events for the current user" do
  54. sign_in users(:bob)
  55. expect {
  56. delete :destroy, params: {:id => events(:bob_website_agent_event).to_param}
  57. }.to change { Event.count }.by(-1)
  58. expect {
  59. delete :destroy, params: {:id => events(:jane_website_agent_event).to_param}
  60. }.to raise_error(ActiveRecord::RecordNotFound)
  61. end
  62. end
  63. end