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