scenarios_controller_spec.rb 5.7 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. expect(assigns(:scenarios).all? {|i| expect(i.user).to eq(users(:bob)) }).to 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. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  19. expect {
  20. get :show, :id => scenarios(:jane_weather).to_param
  21. }.to 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. expect(assigns(:agents).pluck(:id)).to 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. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  32. expect {
  33. get :share, :id => scenarios(:jane_weather).to_param
  34. }.to 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. expect(assigns(:exporter).options[:name]).to eq(scenarios(:bob_weather).name)
  41. expect(assigns(:exporter).options[:description]).to eq(scenarios(:bob_weather).description)
  42. expect(assigns(:exporter).options[:agents]).to eq(scenarios(:bob_weather).agents)
  43. expect(assigns(:exporter).options[:guid]).to eq(scenarios(:bob_weather).guid)
  44. expect(assigns(:exporter).options[:tag_fg_color]).to eq(scenarios(:bob_weather).tag_fg_color)
  45. expect(assigns(:exporter).options[:tag_bg_color]).to eq(scenarios(:bob_weather).tag_bg_color)
  46. expect(assigns(:exporter).options[:source_url]).to be_falsey
  47. expect(response.headers['Content-Disposition']).to eq('attachment; filename="bob-s-weather-alert-scenario.json"')
  48. expect(response.headers['Content-Type']).to eq('application/json; charset=utf-8')
  49. expect(JSON.parse(response.body)["name"]).to eq(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. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  54. expect {
  55. get :export, :id => scenarios(:jane_weather).to_param
  56. }.to 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. expect(assigns(:scenario)).to eq(scenarios(:jane_weather))
  65. expect(assigns(:exporter).options[:source_url]).to eq(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. expect(assigns(:scenario)).to eq(scenarios(:jane_weather))
  71. expect(assigns(:exporter).options[:source_url]).to eq(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. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  79. expect {
  80. get :edit, :id => scenarios(:jane_weather).to_param
  81. }.to 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. expect(assigns(:scenario)).to have(1).errors_on(:name)
  95. expect(response).to 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. expect(response).to redirect_to(scenario_path(scenarios(:bob_weather)))
  107. expect(scenarios(:bob_weather).reload.name).to eq("new_name")
  108. expect(scenarios(:bob_weather)).to be_public
  109. expect {
  110. post :update, :id => scenarios(:jane_weather).to_param, :scenario => { :name => "new_name" }
  111. }.to raise_error(ActiveRecord::RecordNotFound)
  112. expect(scenarios(:jane_weather).reload.name).not_to eq("new_name")
  113. end
  114. it "shows errors" do
  115. post :update, :id => scenarios(:bob_weather).to_param, :scenario => { :name => "" }
  116. expect(assigns(:scenario)).to have(1).errors_on(:name)
  117. expect(response).to 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. expect {
  126. delete :destroy, :id => scenarios(:jane_weather).to_param
  127. }.to raise_error(ActiveRecord::RecordNotFound)
  128. end
  129. end
  130. end