scenarios_controller_spec.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require 'spec_helper'
  2. describe ScenariosController do
  3. def valid_attributes(options = {})
  4. { :name => "some_name" }.merge(options)
  5. end
  6. before do
  7. sign_in users(:bob)
  8. end
  9. describe "GET index" do
  10. it "only returns Scenarios for the current user" do
  11. get :index
  12. assigns(:scenarios).all? {|i| i.user.should == users(:bob) }.should be_true
  13. end
  14. end
  15. describe "GET show" do
  16. it "only shows Scenarios for the current user" do
  17. get :show, :id => scenarios(:bob_weather).to_param
  18. assigns(:scenario).should eq(scenarios(:bob_weather))
  19. lambda {
  20. get :show, :id => scenarios(:jane_weather).to_param
  21. }.should raise_error(ActiveRecord::RecordNotFound)
  22. end
  23. it "loads Agents for the requested Scenario" do
  24. get :show, :id => scenarios(:bob_weather).to_param
  25. assigns(:agents).pluck(:id).should eq(scenarios(:bob_weather).agents.pluck(:id))
  26. end
  27. end
  28. describe "GET edit" do
  29. it "only shows Scenarios for the current user" do
  30. get :edit, :id => scenarios(:bob_weather).to_param
  31. assigns(:scenario).should eq(scenarios(:bob_weather))
  32. lambda {
  33. get :edit, :id => scenarios(:jane_weather).to_param
  34. }.should raise_error(ActiveRecord::RecordNotFound)
  35. end
  36. end
  37. describe "POST create" do
  38. it "creates Scenarios for the current user" do
  39. expect {
  40. post :create, :scenario => valid_attributes
  41. }.to change { users(:bob).scenarios.count }.by(1)
  42. end
  43. it "shows errors" do
  44. expect {
  45. post :create, :scenario => valid_attributes(:name => "")
  46. }.not_to change { users(:bob).scenarios.count }
  47. assigns(:scenario).should have(1).errors_on(:name)
  48. response.should render_template("new")
  49. end
  50. it "will not create Scenarios for other users" do
  51. expect {
  52. post :create, :scenario => valid_attributes(:user_id => users(:jane).id)
  53. }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
  54. end
  55. end
  56. describe "PUT update" do
  57. it "updates attributes on Scenarios for the current user" do
  58. post :update, :id => scenarios(:bob_weather).to_param, :scenario => { :name => "new_name" }
  59. response.should redirect_to(scenario_path(scenarios(:bob_weather)))
  60. scenarios(:bob_weather).reload.name.should == "new_name"
  61. lambda {
  62. post :update, :id => scenarios(:jane_weather).to_param, :scenario => { :name => "new_name" }
  63. }.should raise_error(ActiveRecord::RecordNotFound)
  64. scenarios(:jane_weather).reload.name.should_not == "new_name"
  65. end
  66. it "shows errors" do
  67. post :update, :id => scenarios(:bob_weather).to_param, :scenario => { :name => "" }
  68. assigns(:scenario).should have(1).errors_on(:name)
  69. response.should render_template("edit")
  70. end
  71. end
  72. describe "DELETE destroy" do
  73. it "destroys only Scenarios owned by the current user" do
  74. expect {
  75. delete :destroy, :id => scenarios(:bob_weather).to_param
  76. }.to change(Scenario, :count).by(-1)
  77. lambda {
  78. delete :destroy, :id => scenarios(:jane_weather).to_param
  79. }.should raise_error(ActiveRecord::RecordNotFound)
  80. end
  81. end
  82. end