1
0

agents_controller_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. require 'spec_helper'
  2. describe AgentsController do
  3. def valid_attributes(options = {})
  4. {
  5. :type => "Agents::WebsiteAgent",
  6. :name => "Something",
  7. :options => agents(:bob_website_agent).options,
  8. :source_ids => [agents(:bob_weather_agent).id, ""]
  9. }.merge(options)
  10. end
  11. describe "GET index" do
  12. it "only returns Agents for the current user" do
  13. sign_in users(:bob)
  14. get :index
  15. assigns(:agents).all? {|i| i.user.should == users(:bob) }.should be_truthy
  16. end
  17. end
  18. describe "POST handle_details_post" do
  19. it "passes control to handle_details_post on the agent" do
  20. sign_in users(:bob)
  21. post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => "bar" }
  22. JSON.parse(response.body).should == { "success" => true }
  23. agents(:bob_manual_event_agent).events.last.payload.should == { 'foo' => "bar" }
  24. end
  25. it "can only be accessed by the Agent's owner" do
  26. sign_in users(:jane)
  27. lambda {
  28. post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => :bar }
  29. }.should raise_error(ActiveRecord::RecordNotFound)
  30. end
  31. end
  32. describe "POST run" do
  33. it "triggers Agent.async_check with the Agent's ID" do
  34. sign_in users(:bob)
  35. mock(Agent).async_check(agents(:bob_manual_event_agent).id)
  36. post :run, :id => agents(:bob_manual_event_agent).to_param
  37. end
  38. it "can only be accessed by the Agent's owner" do
  39. sign_in users(:jane)
  40. lambda {
  41. post :run, :id => agents(:bob_manual_event_agent).to_param
  42. }.should raise_error(ActiveRecord::RecordNotFound)
  43. end
  44. end
  45. describe "POST remove_events" do
  46. it "deletes all events created by the given Agent" do
  47. sign_in users(:bob)
  48. agent_event = events(:bob_website_agent_event).id
  49. other_event = events(:jane_website_agent_event).id
  50. post :remove_events, :id => agents(:bob_website_agent).to_param
  51. Event.where(:id => agent_event).count.should == 0
  52. Event.where(:id => other_event).count.should == 1
  53. end
  54. it "can only be accessed by the Agent's owner" do
  55. sign_in users(:jane)
  56. lambda {
  57. post :remove_events, :id => agents(:bob_website_agent).to_param
  58. }.should raise_error(ActiveRecord::RecordNotFound)
  59. end
  60. end
  61. describe "POST propagate" do
  62. it "runs event propagation for all Agents" do
  63. sign_in users(:bob)
  64. mock.proxy(Agent).receive!
  65. post :propagate
  66. end
  67. end
  68. describe "GET show" do
  69. it "only shows Agents for the current user" do
  70. sign_in users(:bob)
  71. get :show, :id => agents(:bob_website_agent).to_param
  72. assigns(:agent).should eq(agents(:bob_website_agent))
  73. lambda {
  74. get :show, :id => agents(:jane_website_agent).to_param
  75. }.should raise_error(ActiveRecord::RecordNotFound)
  76. end
  77. end
  78. describe "GET new with :id" do
  79. it "opens a clone of a given Agent" do
  80. sign_in users(:bob)
  81. get :new, :id => agents(:bob_website_agent).to_param
  82. assigns(:agent).attributes.should eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
  83. end
  84. it "only allows the current user to clone his own Agent" do
  85. sign_in users(:bob)
  86. lambda {
  87. get :new, :id => agents(:jane_website_agent).to_param
  88. }.should raise_error(ActiveRecord::RecordNotFound)
  89. end
  90. end
  91. describe "GET edit" do
  92. it "only shows Agents for the current user" do
  93. sign_in users(:bob)
  94. get :edit, :id => agents(:bob_website_agent).to_param
  95. assigns(:agent).should eq(agents(:bob_website_agent))
  96. lambda {
  97. get :edit, :id => agents(:jane_website_agent).to_param
  98. }.should raise_error(ActiveRecord::RecordNotFound)
  99. end
  100. end
  101. describe "POST create" do
  102. it "errors on bad types" do
  103. sign_in users(:bob)
  104. expect {
  105. post :create, :agent => valid_attributes(:type => "Agents::ThisIsFake")
  106. }.not_to change { users(:bob).agents.count }
  107. assigns(:agent).should be_a(Agent)
  108. assigns(:agent).should have(1).error_on(:type)
  109. sign_in users(:bob)
  110. expect {
  111. post :create, :agent => valid_attributes(:type => "Object")
  112. }.not_to change { users(:bob).agents.count }
  113. assigns(:agent).should be_a(Agent)
  114. assigns(:agent).should have(1).error_on(:type)
  115. sign_in users(:bob)
  116. expect {
  117. post :create, :agent => valid_attributes(:type => "Agent")
  118. }.not_to change { users(:bob).agents.count }
  119. assigns(:agent).should be_a(Agent)
  120. assigns(:agent).should have(1).error_on(:type)
  121. expect {
  122. post :create, :agent => valid_attributes(:type => "User")
  123. }.not_to change { users(:bob).agents.count }
  124. assigns(:agent).should be_a(Agent)
  125. assigns(:agent).should have(1).error_on(:type)
  126. end
  127. it "creates Agents for the current user" do
  128. sign_in users(:bob)
  129. expect {
  130. expect {
  131. post :create, :agent => valid_attributes
  132. }.to change { users(:bob).agents.count }.by(1)
  133. }.to change { Link.count }.by(1)
  134. assigns(:agent).should be_a(Agents::WebsiteAgent)
  135. end
  136. it "shows errors" do
  137. sign_in users(:bob)
  138. expect {
  139. post :create, :agent => valid_attributes(:name => "")
  140. }.not_to change { users(:bob).agents.count }
  141. assigns(:agent).should have(1).errors_on(:name)
  142. response.should render_template("new")
  143. end
  144. it "will not accept Agent sources owned by other users" do
  145. sign_in users(:bob)
  146. expect {
  147. expect {
  148. post :create, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  149. }.not_to change { users(:bob).agents.count }
  150. }.not_to change { Link.count }
  151. end
  152. end
  153. describe "PUT update" do
  154. it "does not allow changing types" do
  155. sign_in users(:bob)
  156. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:type => "Agents::WeatherAgent")
  157. assigns(:agent).should have(1).errors_on(:type)
  158. response.should render_template("edit")
  159. end
  160. it "updates attributes on Agents for the current user" do
  161. sign_in users(:bob)
  162. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  163. response.should redirect_to(agents_path)
  164. agents(:bob_website_agent).reload.name.should == "New name"
  165. lambda {
  166. post :update, :id => agents(:jane_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  167. }.should raise_error(ActiveRecord::RecordNotFound)
  168. end
  169. it "accepts JSON requests" do
  170. sign_in users(:bob)
  171. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :format => :json
  172. agents(:bob_website_agent).reload.name.should == "New name"
  173. JSON.parse(response.body)['name'].should == "New name"
  174. response.should be_success
  175. end
  176. it "will not accept Agent sources owned by other users" do
  177. sign_in users(:bob)
  178. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  179. assigns(:agent).should have(1).errors_on(:sources)
  180. end
  181. it "will not accept Scenarios owned by other users" do
  182. sign_in users(:bob)
  183. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:scenario_ids => [scenarios(:jane_weather).id])
  184. assigns(:agent).should have(1).errors_on(:scenarios)
  185. end
  186. it "shows errors" do
  187. sign_in users(:bob)
  188. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "")
  189. assigns(:agent).should have(1).errors_on(:name)
  190. response.should render_template("edit")
  191. end
  192. describe "redirecting back" do
  193. before do
  194. sign_in users(:bob)
  195. end
  196. it "can redirect back to the show path" do
  197. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "show"
  198. response.should redirect_to(agent_path(agents(:bob_website_agent)))
  199. end
  200. it "redirect back to the index path by default" do
  201. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  202. response.should redirect_to(agents_path)
  203. end
  204. it "accepts return paths to scenarios" do
  205. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "/scenarios/2"
  206. response.should redirect_to("/scenarios/2")
  207. end
  208. it "sanitizes return paths" do
  209. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "/scenar"
  210. response.should redirect_to(agents_path)
  211. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "http://google.com"
  212. response.should redirect_to(agents_path)
  213. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "javascript:alert(1)"
  214. response.should redirect_to(agents_path)
  215. end
  216. end
  217. it "updates last_checked_event_id when drop_pending_events is given" do
  218. sign_in users(:bob)
  219. agent = agents(:bob_website_agent)
  220. agent.disabled = true
  221. agent.last_checked_event_id = nil
  222. agent.save!
  223. post :update, id: agents(:bob_website_agent).to_param, agent: { disabled: 'false', drop_pending_events: 'true' }
  224. agent.reload
  225. agent.disabled.should == false
  226. agent.last_checked_event_id.should == Event.maximum(:id)
  227. end
  228. end
  229. describe "PUT leave_scenario" do
  230. it "removes an Agent from the given Scenario for the current user" do
  231. sign_in users(:bob)
  232. agents(:bob_weather_agent).scenarios.should include(scenarios(:bob_weather))
  233. put :leave_scenario, :id => agents(:bob_weather_agent).to_param, :scenario_id => scenarios(:bob_weather).to_param
  234. agents(:bob_weather_agent).scenarios.should_not include(scenarios(:bob_weather))
  235. Scenario.where(:id => scenarios(:bob_weather).id).should exist
  236. lambda {
  237. put :leave_scenario, :id => agents(:jane_weather_agent).to_param, :scenario_id => scenarios(:jane_weather).to_param
  238. }.should raise_error(ActiveRecord::RecordNotFound)
  239. end
  240. end
  241. describe "DELETE destroy" do
  242. it "destroys only Agents owned by the current user" do
  243. sign_in users(:bob)
  244. expect {
  245. delete :destroy, :id => agents(:bob_website_agent).to_param
  246. }.to change(Agent, :count).by(-1)
  247. lambda {
  248. delete :destroy, :id => agents(:jane_website_agent).to_param
  249. }.should raise_error(ActiveRecord::RecordNotFound)
  250. end
  251. end
  252. end