agents_controller_spec.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. require 'rails_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. it "should not show disabled agents if the cookie is set" do
  18. @request.cookies["huginn_view_only_enabled_agents"] = "true"
  19. sign_in users(:bob)
  20. get :index
  21. expect(assigns(:agents).map(&:disabled).uniq).to eq([false])
  22. end
  23. end
  24. describe "POST handle_details_post" do
  25. it "passes control to handle_details_post on the agent" do
  26. sign_in users(:bob)
  27. post :handle_details_post, params: {:id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => "bar" }.to_json}
  28. expect(JSON.parse(response.body)).to eq({ "success" => true })
  29. expect(agents(:bob_manual_event_agent).events.last.payload).to eq({ 'foo' => "bar" })
  30. end
  31. it "can only be accessed by the Agent's owner" do
  32. sign_in users(:jane)
  33. expect {
  34. post :handle_details_post, params: {:id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => :bar }.to_json}
  35. }.to raise_error(ActiveRecord::RecordNotFound)
  36. end
  37. end
  38. describe "POST run" do
  39. it "triggers Agent.async_check with the Agent's ID" do
  40. sign_in users(:bob)
  41. expect(Agent).to receive(:async_check).with(agents(:bob_manual_event_agent).id)
  42. post :run, params: {:id => agents(:bob_manual_event_agent).to_param}
  43. end
  44. it "can only be accessed by the Agent's owner" do
  45. sign_in users(:jane)
  46. expect {
  47. post :run, params: {:id => agents(:bob_manual_event_agent).to_param}
  48. }.to raise_error(ActiveRecord::RecordNotFound)
  49. end
  50. end
  51. describe "POST reemit_events" do
  52. let(:agent) { agents(:bob_website_agent) }
  53. let(:params) { { :id => agent.to_param } }
  54. it "enqueues an AgentReemitJob" do
  55. expect(AgentReemitJob).to receive(:perform_later).with(agent, agent.most_recent_event.id, false)
  56. sign_in users(:bob)
  57. post :reemit_events, params: params
  58. end
  59. context "when agent has no events" do
  60. let(:agent) { agents(:bob_weather_agent) }
  61. it "does not enqueue an AgentReemitJob" do
  62. expect(AgentReemitJob).not_to receive(:perform_later)
  63. sign_in users(:bob)
  64. post :reemit_events, params: params
  65. end
  66. end
  67. context "when delete_old_events passed" do
  68. it "enqueues an AgentReemitJob with delete_old_events set to true" do
  69. expect(AgentReemitJob).to receive(:perform_later).with(agent, agent.most_recent_event.id, true)
  70. sign_in users(:bob)
  71. post :reemit_events, params: params.merge('delete_old_events' => '1')
  72. end
  73. end
  74. it "can only be accessed by the Agent's owner" do
  75. sign_in users(:jane)
  76. expect {
  77. post :reemit_events, params: {:id => agents(:bob_website_agent).to_param}
  78. }.to raise_error(ActiveRecord::RecordNotFound)
  79. end
  80. end
  81. describe "POST remove_events" do
  82. it "deletes all events created by the given Agent" do
  83. sign_in users(:bob)
  84. agent_event = events(:bob_website_agent_event).id
  85. other_event = events(:jane_website_agent_event).id
  86. post :remove_events, params: {:id => agents(:bob_website_agent).to_param}
  87. expect(Event.where(:id => agent_event).count).to eq(0)
  88. expect(Event.where(:id => other_event).count).to eq(1)
  89. end
  90. it "can only be accessed by the Agent's owner" do
  91. sign_in users(:jane)
  92. expect {
  93. post :remove_events, params: {:id => agents(:bob_website_agent).to_param}
  94. }.to raise_error(ActiveRecord::RecordNotFound)
  95. end
  96. end
  97. describe "PUT toggle_visibility" do
  98. it "should set the cookie" do
  99. sign_in users(:jane)
  100. put :toggle_visibility
  101. expect(response.cookies["huginn_view_only_enabled_agents"]).to eq("true")
  102. end
  103. it "should delete the cookie" do
  104. @request.cookies["huginn_view_only_enabled_agents"] = "true"
  105. sign_in users(:jane)
  106. put :toggle_visibility
  107. expect(response.cookies["huginn_view_only_enabled_agents"]).to be_nil
  108. end
  109. end
  110. describe "POST propagate" do
  111. before(:each) do
  112. sign_in users(:bob)
  113. end
  114. it "runs event propagation for all Agents" do
  115. expect(Agent).to receive(:receive!).and_call_original
  116. post :propagate
  117. end
  118. it "does not run the propagation when a job is already enqueued" do
  119. expect(AgentPropagateJob).to receive(:can_enqueue?) { false }
  120. post :propagate
  121. expect(flash[:notice]).to eq('Event propagation is already scheduled to run.')
  122. end
  123. end
  124. describe "GET show" do
  125. it "only shows Agents for the current user" do
  126. sign_in users(:bob)
  127. get :show, params: {:id => agents(:bob_website_agent).to_param}
  128. expect(assigns(:agent)).to eq(agents(:bob_website_agent))
  129. expect {
  130. get :show, params: {:id => agents(:jane_website_agent).to_param}
  131. }.to raise_error(ActiveRecord::RecordNotFound)
  132. end
  133. end
  134. describe "GET new" do
  135. describe "with :id" do
  136. it "opens a clone of a given Agent" do
  137. sign_in users(:bob)
  138. get :new, params: {:id => agents(:bob_website_agent).to_param}
  139. expect(assigns(:agent).attributes).to eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
  140. end
  141. it "only allows the current user to clone his own Agent" do
  142. sign_in users(:bob)
  143. expect {
  144. get :new, params: {:id => agents(:jane_website_agent).to_param}
  145. }.to raise_error(ActiveRecord::RecordNotFound)
  146. end
  147. end
  148. describe "with a scenario_id" do
  149. it 'populates the assigned agent with the scenario' do
  150. sign_in users(:bob)
  151. get :new, params: {:scenario_id => scenarios(:bob_weather).id}
  152. expect(assigns(:agent).scenario_ids).to eq([scenarios(:bob_weather).id])
  153. end
  154. it "does not see other user's scenarios" do
  155. sign_in users(:bob)
  156. get :new, params: {:scenario_id => scenarios(:jane_weather).id}
  157. expect(assigns(:agent).scenario_ids).to eq([])
  158. end
  159. end
  160. end
  161. describe "GET edit" do
  162. it "only shows Agents for the current user" do
  163. sign_in users(:bob)
  164. get :edit, params: {:id => agents(:bob_website_agent).to_param}
  165. expect(assigns(:agent)).to eq(agents(:bob_website_agent))
  166. expect {
  167. get :edit, params: {:id => agents(:jane_website_agent).to_param}
  168. }.to raise_error(ActiveRecord::RecordNotFound)
  169. end
  170. end
  171. describe "POST create" do
  172. it "errors on bad types" do
  173. sign_in users(:bob)
  174. expect {
  175. post :create, params: {:agent => valid_attributes(:type => "Agents::ThisIsFake")}
  176. }.not_to change { users(:bob).agents.count }
  177. expect(assigns(:agent)).to be_a(Agent)
  178. expect(assigns(:agent)).to have(1).error_on(:type)
  179. sign_in users(:bob)
  180. expect {
  181. post :create, params: {:agent => valid_attributes(:type => "Object")}
  182. }.not_to change { users(:bob).agents.count }
  183. expect(assigns(:agent)).to be_a(Agent)
  184. expect(assigns(:agent)).to have(1).error_on(:type)
  185. sign_in users(:bob)
  186. expect {
  187. post :create, params: {:agent => valid_attributes(:type => "Agent")}
  188. }.not_to change { users(:bob).agents.count }
  189. expect(assigns(:agent)).to be_a(Agent)
  190. expect(assigns(:agent)).to have(1).error_on(:type)
  191. expect {
  192. post :create, params: {:agent => valid_attributes(:type => "User")}
  193. }.not_to change { users(:bob).agents.count }
  194. expect(assigns(:agent)).to be_a(Agent)
  195. expect(assigns(:agent)).to have(1).error_on(:type)
  196. end
  197. it "creates Agents for the current user" do
  198. sign_in users(:bob)
  199. expect {
  200. expect {
  201. post :create, params: {:agent => valid_attributes}
  202. }.to change { users(:bob).agents.count }.by(1)
  203. }.to change { Link.count }.by(1)
  204. expect(assigns(:agent)).to be_a(Agents::WebsiteAgent)
  205. end
  206. it "creates Agents and accepts specifing a target agent" do
  207. sign_in users(:bob)
  208. attributes = valid_attributes(service_id: 1)
  209. attributes[:receiver_ids] = attributes[:source_ids]
  210. expect {
  211. expect {
  212. post :create, params: {:agent => attributes}
  213. }.to change { users(:bob).agents.count }.by(1)
  214. }.to change { Link.count }.by(2)
  215. expect(assigns(:agent)).to be_a(Agents::WebsiteAgent)
  216. end
  217. it "shows errors" do
  218. sign_in users(:bob)
  219. expect {
  220. post :create, params: {:agent => valid_attributes(:name => "")}
  221. }.not_to change { users(:bob).agents.count }
  222. expect(assigns(:agent)).to have(1).errors_on(:name)
  223. expect(response).to render_template("new")
  224. end
  225. it "will not accept Agent sources owned by other users" do
  226. sign_in users(:bob)
  227. expect {
  228. expect {
  229. post :create, params: {:agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])}
  230. }.not_to change { users(:bob).agents.count }
  231. }.not_to change { Link.count }
  232. end
  233. end
  234. describe "PUT update" do
  235. it "does not allow changing types" do
  236. sign_in users(:bob)
  237. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:type => "Agents::WeatherAgent")}
  238. expect(assigns(:agent)).to have(1).errors_on(:type)
  239. expect(response).to render_template("edit")
  240. end
  241. it "updates attributes on Agents for the current user" do
  242. sign_in users(:bob)
  243. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")}
  244. expect(response).to redirect_to(agents_path)
  245. expect(agents(:bob_website_agent).reload.name).to eq("New name")
  246. expect {
  247. post :update, params: {:id => agents(:jane_website_agent).to_param, :agent => valid_attributes(:name => "New name")}
  248. }.to raise_error(ActiveRecord::RecordNotFound)
  249. end
  250. it "accepts JSON requests" do
  251. sign_in users(:bob)
  252. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")}, :format => :json
  253. expect(agents(:bob_website_agent).reload.name).to eq("New name")
  254. expect(JSON.parse(response.body)['name']).to eq("New name")
  255. expect(response).to be_successful
  256. end
  257. it "will not accept Agent sources owned by other users" do
  258. sign_in users(:bob)
  259. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])}
  260. expect(assigns(:agent)).to have(1).errors_on(:sources)
  261. end
  262. it "will not accept Scenarios owned by other users" do
  263. sign_in users(:bob)
  264. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:scenario_ids => [scenarios(:jane_weather).id])}
  265. expect(assigns(:agent)).to have(1).errors_on(:scenarios)
  266. end
  267. it "shows errors" do
  268. sign_in users(:bob)
  269. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "")}
  270. expect(assigns(:agent)).to have(1).errors_on(:name)
  271. expect(response).to render_template("edit")
  272. end
  273. it 'does not allow to modify the agents user_id' do
  274. sign_in users(:bob)
  275. expect {
  276. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:user_id => users(:jane).id)}
  277. }.to raise_error(ActionController::UnpermittedParameters)
  278. end
  279. describe "redirecting back" do
  280. before do
  281. sign_in users(:bob)
  282. end
  283. it "can redirect back to the show path" do
  284. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "show"}
  285. expect(response).to redirect_to(agent_path(agents(:bob_website_agent)))
  286. end
  287. it "redirect back to the index path by default" do
  288. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")}
  289. expect(response).to redirect_to(agents_path)
  290. end
  291. it "accepts return paths to scenarios" do
  292. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "/scenarios/2"}
  293. expect(response).to redirect_to("/scenarios/2")
  294. end
  295. it "sanitizes return paths" do
  296. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "/scenar"}
  297. expect(response).to redirect_to(agents_path)
  298. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "http://google.com"}
  299. expect(response).to redirect_to(agents_path)
  300. post :update, params: {:id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name"), :return => "javascript:alert(1)"}
  301. expect(response).to redirect_to(agents_path)
  302. end
  303. end
  304. it "updates last_checked_event_id when drop_pending_events is given" do
  305. sign_in users(:bob)
  306. agent = agents(:bob_website_agent)
  307. agent.disabled = true
  308. agent.last_checked_event_id = nil
  309. agent.save!
  310. post :update, params: {id: agents(:bob_website_agent).to_param, agent: { disabled: 'false', drop_pending_events: 'true' }}
  311. agent.reload
  312. expect(agent.disabled).to eq(false)
  313. expect(agent.last_checked_event_id).to eq(Event.maximum(:id))
  314. end
  315. end
  316. describe "PUT leave_scenario" do
  317. it "removes an Agent from the given Scenario for the current user" do
  318. sign_in users(:bob)
  319. expect(agents(:bob_weather_agent).scenarios).to include(scenarios(:bob_weather))
  320. put :leave_scenario, params: {:id => agents(:bob_weather_agent).to_param, :scenario_id => scenarios(:bob_weather).to_param}
  321. expect(agents(:bob_weather_agent).scenarios).not_to include(scenarios(:bob_weather))
  322. expect(Scenario.where(:id => scenarios(:bob_weather).id)).to exist
  323. expect {
  324. put :leave_scenario, params: {:id => agents(:jane_weather_agent).to_param, :scenario_id => scenarios(:jane_weather).to_param}
  325. }.to raise_error(ActiveRecord::RecordNotFound)
  326. end
  327. end
  328. describe "DELETE destroy" do
  329. it "destroys only Agents owned by the current user" do
  330. sign_in users(:bob)
  331. expect {
  332. delete :destroy, params: {:id => agents(:bob_website_agent).to_param}
  333. }.to change(Agent, :count).by(-1)
  334. expect {
  335. delete :destroy, params: {:id => agents(:jane_website_agent).to_param}
  336. }.to raise_error(ActiveRecord::RecordNotFound)
  337. end
  338. it "redirects correctly when the Agent is deleted from the Agent itself" do
  339. sign_in users(:bob)
  340. delete :destroy, params: {:id => agents(:bob_website_agent).to_param}
  341. expect(response).to redirect_to agents_path
  342. end
  343. it "redirects correctly when the Agent is deleted from a Scenario" do
  344. sign_in users(:bob)
  345. delete :destroy, params: {:id => agents(:bob_weather_agent).to_param, :return => scenario_path(scenarios(:bob_weather)).to_param}
  346. expect(response).to redirect_to scenario_path(scenarios(:bob_weather))
  347. end
  348. end
  349. describe "#form_configurable actions" do
  350. before(:each) do
  351. @params = {attribute: 'auth_token', agent: valid_attributes(:type => "Agents::HipchatAgent", options: {auth_token: '12345'})}
  352. sign_in users(:bob)
  353. end
  354. describe "POST validate" do
  355. it "returns with status 200 when called with a valid option" do
  356. allow_any_instance_of(Agents::HipchatAgent).to receive(:validate_option) { true }
  357. post :validate, params: @params
  358. expect(response.status).to eq 200
  359. end
  360. it "returns with status 403 when called with an invalid option" do
  361. allow_any_instance_of(Agents::HipchatAgent).to receive(:validate_option) { false }
  362. post :validate, params: @params
  363. expect(response.status).to eq 403
  364. end
  365. end
  366. describe "POST complete" do
  367. it "callsAgent#complete_option and renders json" do
  368. allow_any_instance_of(Agents::HipchatAgent).to receive(:complete_option) { [{name: 'test', value: 1}] }
  369. post :complete, params: @params
  370. expect(response.status).to eq 200
  371. expect(response.header['Content-Type']).to include('application/json')
  372. end
  373. end
  374. end
  375. describe "DELETE memory" do
  376. it "clears memory of the agent" do
  377. agent = agents(:bob_website_agent)
  378. agent.update!(memory: { "test" => 42 })
  379. sign_in users(:bob)
  380. delete :destroy_memory, params: {id: agent.to_param}
  381. expect(agent.reload.memory).to eq({})
  382. end
  383. it "does not clear memory of an agent not owned by the current user" do
  384. agent = agents(:jane_website_agent)
  385. agent.update!(memory: { "test" => 42 })
  386. sign_in users(:bob)
  387. expect {
  388. delete :destroy_memory, params: {id: agent.to_param}
  389. }.to raise_error(ActiveRecord::RecordNotFound)
  390. expect(agent.reload.memory).to eq({ "test" => 42})
  391. end
  392. end
  393. describe 'DELETE undefined' do
  394. it 'removes an undefined agent from the database' do
  395. sign_in users(:bob)
  396. agent = agents(:bob_website_agent)
  397. agent.update_attribute(:type, 'Agents::UndefinedAgent')
  398. agent2 = agents(:jane_website_agent)
  399. agent2.update_attribute(:type, 'Agents::UndefinedAgent')
  400. expect {
  401. delete :destroy_undefined
  402. }.to change { Agent.count }.by(-1)
  403. end
  404. end
  405. end