agents_controller_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. expect(assigns(:agents).all? {|i| expect(i.user).to eq(users(:bob)) }).to 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. expect(JSON.parse(response.body)).to eq({ "success" => true })
  23. expect(agents(:bob_manual_event_agent).events.last.payload).to eq({ 'foo' => "bar" })
  24. end
  25. it "can only be accessed by the Agent's owner" do
  26. sign_in users(:jane)
  27. expect {
  28. post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => :bar }
  29. }.to 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. expect {
  41. post :run, :id => agents(:bob_manual_event_agent).to_param
  42. }.to 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. expect(Event.where(:id => agent_event).count).to eq(0)
  52. expect(Event.where(:id => other_event).count).to eq(1)
  53. end
  54. it "can only be accessed by the Agent's owner" do
  55. sign_in users(:jane)
  56. expect {
  57. post :remove_events, :id => agents(:bob_website_agent).to_param
  58. }.to 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. expect(assigns(:agent)).to eq(agents(:bob_website_agent))
  73. expect {
  74. get :show, :id => agents(:jane_website_agent).to_param
  75. }.to 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. expect(assigns(:agent).attributes).to 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. expect {
  87. get :new, :id => agents(:jane_website_agent).to_param
  88. }.to 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. expect(assigns(:agent)).to eq(agents(:bob_website_agent))
  96. expect {
  97. get :edit, :id => agents(:jane_website_agent).to_param
  98. }.to 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. expect(assigns(:agent)).to be_a(Agent)
  108. expect(assigns(:agent)).to 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. expect(assigns(:agent)).to be_a(Agent)
  114. expect(assigns(:agent)).to 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. expect(assigns(:agent)).to be_a(Agent)
  120. expect(assigns(:agent)).to have(1).error_on(:type)
  121. expect {
  122. post :create, :agent => valid_attributes(:type => "User")
  123. }.not_to change { users(:bob).agents.count }
  124. expect(assigns(:agent)).to be_a(Agent)
  125. expect(assigns(:agent)).to 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. expect(assigns(:agent)).to 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. expect(assigns(:agent)).to have(1).errors_on(:name)
  142. expect(response).to 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. expect(assigns(:agent)).to have(1).errors_on(:type)
  158. expect(response).to 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. expect(response).to redirect_to(agents_path)
  164. expect(agents(:bob_website_agent).reload.name).to eq("New name")
  165. expect {
  166. post :update, :id => agents(:jane_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  167. }.to 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. expect(agents(:bob_website_agent).reload.name).to eq("New name")
  173. expect(JSON.parse(response.body)['name']).to eq("New name")
  174. expect(response).to 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. expect(assigns(:agent)).to 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. expect(assigns(:agent)).to 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. expect(assigns(:agent)).to have(1).errors_on(:name)
  190. expect(response).to 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. expect(response).to 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. expect(response).to 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. expect(response).to 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. expect(response).to 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. expect(response).to 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. expect(response).to 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. expect(agent.disabled).to eq(false)
  226. expect(agent.last_checked_event_id).to eq(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. expect(agents(:bob_weather_agent).scenarios).to include(scenarios(:bob_weather))
  233. put :leave_scenario, :id => agents(:bob_weather_agent).to_param, :scenario_id => scenarios(:bob_weather).to_param
  234. expect(agents(:bob_weather_agent).scenarios).not_to include(scenarios(:bob_weather))
  235. expect(Scenario.where(:id => scenarios(:bob_weather).id)).to exist
  236. expect {
  237. put :leave_scenario, :id => agents(:jane_weather_agent).to_param, :scenario_id => scenarios(:jane_weather).to_param
  238. }.to 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. expect {
  248. delete :destroy, :id => agents(:jane_website_agent).to_param
  249. }.to raise_error(ActiveRecord::RecordNotFound)
  250. end
  251. it "redirects correctly when the Agent is deleted from the Agent itself" do
  252. sign_in users(:bob)
  253. delete :destroy, :id => agents(:bob_website_agent).to_param
  254. expect(response).to redirect_to agents_path
  255. end
  256. it "redirects correctly when the Agent is deleted from a Scenario" do
  257. sign_in users(:bob)
  258. delete :destroy, :id => agents(:bob_weather_agent).to_param, :return => scenario_path(scenarios(:bob_weather)).to_param
  259. expect(response).to redirect_to scenario_path(scenarios(:bob_weather))
  260. end
  261. end
  262. describe "#form_configurable actions" do
  263. before(:each) do
  264. @params = {attribute: 'auth_token', agent: valid_attributes(:type => "Agents::HipchatAgent", options: {auth_token: '12345'})}
  265. sign_in users(:bob)
  266. end
  267. describe "POST validate" do
  268. it "returns with status 200 when called with a valid option" do
  269. any_instance_of(Agents::HipchatAgent) do |klass|
  270. stub(klass).validate_option { true }
  271. end
  272. post :validate, @params
  273. expect(response.status).to eq 200
  274. end
  275. it "returns with status 403 when called with an invalid option" do
  276. any_instance_of(Agents::HipchatAgent) do |klass|
  277. stub(klass).validate_option { false }
  278. end
  279. post :validate, @params
  280. expect(response.status).to eq 403
  281. end
  282. end
  283. describe "POST complete" do
  284. it "callsAgent#complete_option and renders json" do
  285. any_instance_of(Agents::HipchatAgent) do |klass|
  286. stub(klass).complete_option { [{name: 'test', value: 1}] }
  287. end
  288. post :complete, @params
  289. expect(response.status).to eq 200
  290. expect(response.header['Content-Type']).to include('application/json')
  291. end
  292. end
  293. end
  294. describe "POST dry_run" do
  295. it "does not actually create any agent, event or log" do
  296. sign_in users(:bob)
  297. expect {
  298. post :dry_run, agent: valid_attributes()
  299. }.not_to change {
  300. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count]
  301. }
  302. json = JSON.parse(response.body)
  303. expect(json['log']).to be_a(String)
  304. expect(json['events']).to be_a(String)
  305. expect(JSON.parse(json['events']).map(&:class)).to eq([Hash])
  306. expect(json['memory']).to be_a(String)
  307. expect(JSON.parse(json['memory'])).to be_a(Hash)
  308. end
  309. it "does not actually update an agent" do
  310. sign_in users(:bob)
  311. agent = agents(:bob_weather_agent)
  312. expect {
  313. post :dry_run, id: agents(:bob_website_agent), agent: valid_attributes(name: 'New Name')
  314. }.not_to change {
  315. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count, agent.name, agent.updated_at]
  316. }
  317. end
  318. end
  319. describe "DELETE memory" do
  320. it "clears memory of the agent" do
  321. agent = agents(:bob_website_agent)
  322. agent.update!(memory: { "test" => 42 })
  323. sign_in users(:bob)
  324. delete :destroy_memory, id: agent.to_param
  325. expect(agent.reload.memory).to eq({})
  326. end
  327. it "does not clear memory of an agent not owned by the current user" do
  328. agent = agents(:jane_website_agent)
  329. agent.update!(memory: { "test" => 42 })
  330. sign_in users(:bob)
  331. expect {
  332. delete :destroy_memory, id: agent.to_param
  333. }.to raise_error(ActiveRecord::RecordNotFound)
  334. expect(agent.reload.memory).to eq({ "test" => 42})
  335. end
  336. end
  337. end