scenarios_controller_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 share" do
  29. it "only displays Scenario share information for the current user" do
  30. get :share, :id => scenarios(:bob_weather).to_param
  31. assigns(:scenario).should eq(scenarios(:bob_weather))
  32. lambda {
  33. get :share, :id => scenarios(:jane_weather).to_param
  34. }.should raise_error(ActiveRecord::RecordNotFound)
  35. end
  36. end
  37. describe "GET export" do
  38. it "returns a JSON file download from an instantiated AgentsExporter" do
  39. get :export, :id => scenarios(:bob_weather).to_param
  40. assigns(:exporter).options[:name].should == scenarios(:bob_weather).name
  41. assigns(:exporter).options[:description].should == scenarios(:bob_weather).description
  42. assigns(:exporter).options[:agents].should == scenarios(:bob_weather).agents
  43. assigns(:exporter).options[:guid].should == scenarios(:bob_weather).guid
  44. assigns(:exporter).options[:source_url].should be_false
  45. response.headers['Content-Disposition'].should == 'attachment; filename="bob-s-weather-alert-scenario.json"'
  46. response.headers['Content-Type'].should == 'application/json; charset=utf-8'
  47. JSON.parse(response.body)["name"].should == scenarios(:bob_weather).name
  48. end
  49. it "only exports private Scenarios for the current user" do
  50. get :export, :id => scenarios(:bob_weather).to_param
  51. assigns(:scenario).should eq(scenarios(:bob_weather))
  52. lambda {
  53. get :export, :id => scenarios(:jane_weather).to_param
  54. }.should raise_error(ActiveRecord::RecordNotFound)
  55. end
  56. describe "public exports" do
  57. before do
  58. scenarios(:jane_weather).update_attribute :public, true
  59. end
  60. it "exports public scenarios for other users when logged in" do
  61. get :export, :id => scenarios(:jane_weather).to_param
  62. assigns(:scenario).should eq(scenarios(:jane_weather))
  63. assigns(:exporter).options[:source_url].should == export_scenario_url(scenarios(:jane_weather))
  64. end
  65. it "exports public scenarios for other users when logged out" do
  66. sign_out :user
  67. get :export, :id => scenarios(:jane_weather).to_param
  68. assigns(:scenario).should eq(scenarios(:jane_weather))
  69. assigns(:exporter).options[:source_url].should == export_scenario_url(scenarios(:jane_weather))
  70. end
  71. end
  72. end
  73. describe "GET edit" do
  74. it "only shows Scenarios for the current user" do
  75. get :edit, :id => scenarios(:bob_weather).to_param
  76. assigns(:scenario).should eq(scenarios(:bob_weather))
  77. lambda {
  78. get :edit, :id => scenarios(:jane_weather).to_param
  79. }.should raise_error(ActiveRecord::RecordNotFound)
  80. end
  81. end
  82. describe "POST create" do
  83. it "creates Scenarios for the current user" do
  84. expect {
  85. post :create, :scenario => valid_attributes
  86. }.to change { users(:bob).scenarios.count }.by(1)
  87. end
  88. it "shows errors" do
  89. expect {
  90. post :create, :scenario => valid_attributes(:name => "")
  91. }.not_to change { users(:bob).scenarios.count }
  92. assigns(:scenario).should have(1).errors_on(:name)
  93. response.should render_template("new")
  94. end
  95. it "will not create Scenarios for other users" do
  96. expect {
  97. post :create, :scenario => valid_attributes(:user_id => users(:jane).id)
  98. }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
  99. end
  100. end
  101. describe "PUT update" do
  102. it "updates attributes on Scenarios for the current user" do
  103. post :update, :id => scenarios(:bob_weather).to_param, :scenario => { :name => "new_name", :public => "1" }
  104. response.should redirect_to(scenario_path(scenarios(:bob_weather)))
  105. scenarios(:bob_weather).reload.name.should == "new_name"
  106. scenarios(:bob_weather).should be_public
  107. lambda {
  108. post :update, :id => scenarios(:jane_weather).to_param, :scenario => { :name => "new_name" }
  109. }.should raise_error(ActiveRecord::RecordNotFound)
  110. scenarios(:jane_weather).reload.name.should_not == "new_name"
  111. end
  112. it "shows errors" do
  113. post :update, :id => scenarios(:bob_weather).to_param, :scenario => { :name => "" }
  114. assigns(:scenario).should have(1).errors_on(:name)
  115. response.should render_template("edit")
  116. end
  117. end
  118. describe "DELETE destroy" do
  119. it "destroys only Scenarios owned by the current user" do
  120. expect {
  121. delete :destroy, :id => scenarios(:bob_weather).to_param
  122. }.to change(Scenario, :count).by(-1)
  123. lambda {
  124. delete :destroy, :id => scenarios(:jane_weather).to_param
  125. }.should raise_error(ActiveRecord::RecordNotFound)
  126. end
  127. end
  128. end