agents_controller_spec.rb 17 KB

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