scenarios_controller_spec.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. require 'rails_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, params: {:id => scenarios(:bob_weather).to_param}
  18. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  19. expect {
  20. get :show, params: {: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, params: {:id => scenarios(:bob_weather).to_param}
  25. expect(assigns(:agents).pluck(:id).sort).to eq(scenarios(:bob_weather).agents.pluck(:id).sort)
  26. end
  27. end
  28. describe "GET share" do
  29. it "only displays Scenario share information for the current user" do
  30. get :share, params: {:id => scenarios(:bob_weather).to_param}
  31. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  32. expect {
  33. get :share, params: {: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, params: {: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, params: {:id => scenarios(:bob_weather).to_param}
  53. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  54. expect {
  55. get :export, params: {: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, params: {: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, params: {: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, params: {:id => scenarios(:bob_weather).to_param}
  78. expect(assigns(:scenario)).to eq(scenarios(:bob_weather))
  79. expect {
  80. get :edit, params: {: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, params: {:scenario => valid_attributes}
  88. }.to change { users(:bob).scenarios.count }.by(1)
  89. end
  90. it "shows errors" do
  91. expect {
  92. post :create, params: {: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, params: {:scenario => valid_attributes(:user_id => users(:jane).id)}
  100. }.to raise_error(ActionController::UnpermittedParameters)
  101. end
  102. end
  103. describe "PUT update" do
  104. it "updates attributes on Scenarios for the current user" do
  105. post :update, params: {: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, params: {: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, params: {: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. it 'adds an agent to the scenario' do
  120. expect {
  121. post :update, params: {:id => scenarios(:bob_weather).to_param, :scenario => { :name => "new_name", :public => "1", agent_ids: scenarios(:bob_weather).agent_ids + [agents(:bob_website_agent).id] }}
  122. }.to change { scenarios(:bob_weather).reload.agent_ids.length }.by(1)
  123. end
  124. end
  125. describe 'PUT enable_or_disable_all_agents' do
  126. it 'updates disabled on all agents in a scenario for the current user' do
  127. @params = {"scenario"=>{"disabled"=>"true"}, "commit"=>"Yes", "id"=> scenarios(:bob_weather).id}
  128. put :enable_or_disable_all_agents, params: @params
  129. expect(agents(:bob_rain_notifier_agent).disabled).to eq(true)
  130. expect(response).to redirect_to(scenario_path(scenarios(:bob_weather)))
  131. end
  132. end
  133. describe "DELETE destroy" do
  134. it "destroys only Scenarios owned by the current user" do
  135. expect {
  136. delete :destroy, params: {:id => scenarios(:bob_weather).to_param}
  137. }.to change(Scenario, :count).by(-1)
  138. expect {
  139. delete :destroy, params: {:id => scenarios(:jane_weather).to_param}
  140. }.to raise_error(ActiveRecord::RecordNotFound)
  141. end
  142. it "passes the mode to the model" do
  143. expect {
  144. delete :destroy, params: {id: scenarios(:bob_weather).to_param, mode: 'all_agents'}
  145. }.to change(Agent, :count).by(-2)
  146. end
  147. end
  148. end