scenarios_controller_spec.rb 5.6 KB

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