flapjack 0.7.16 → 0.7.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'flapjack/gateways/api/entity_check_presenter'
3
3
 
4
- require 'pp'
5
-
6
- describe 'Flapjack::Gateways::API::EntityCheck::Presenter' do
4
+ describe 'Flapjack::Gateways::API::EntityCheckPresenter' do
7
5
 
8
6
  let(:entity_check) { mock(Flapjack::Data::EntityCheck) }
9
7
 
@@ -0,0 +1,868 @@
1
+ require 'spec_helper'
2
+ require 'flapjack/gateways/api'
3
+
4
+ describe 'Flapjack::Gateways::API::EntityMethods', :sinatra => true, :logger => true, :json => true do
5
+
6
+ def app
7
+ Flapjack::Gateways::API
8
+ end
9
+
10
+ let(:entity) { mock(Flapjack::Data::Entity) }
11
+ let(:entity_check) { mock(Flapjack::Data::EntityCheck) }
12
+
13
+ let(:entity_name) { 'www.example.net'}
14
+ let(:entity_name_esc) { URI.escape(entity_name) }
15
+ let(:check) { 'ping' }
16
+
17
+ let(:entity_presenter) { mock(Flapjack::Gateways::API::EntityPresenter) }
18
+ let(:entity_check_presenter) { mock(Flapjack::Gateways::API::EntityCheckPresenter) }
19
+
20
+ let(:redis) { mock(::Redis) }
21
+
22
+ before(:all) do
23
+ Flapjack::Gateways::API.class_eval {
24
+ set :raise_errors, true
25
+ }
26
+ Flapjack::Gateways::API.instance_variable_get('@middleware').delete_if {|m|
27
+ m[0] == Rack::FiberPool
28
+ }
29
+ end
30
+
31
+ before(:each) do
32
+ Flapjack::RedisPool.should_receive(:new).and_return(redis)
33
+ Flapjack::Gateways::API.instance_variable_set('@config', {})
34
+ Flapjack::Gateways::API.instance_variable_set('@logger', @logger)
35
+ Flapjack::Gateways::API.start
36
+ end
37
+
38
+ it "returns a list of checks for an entity" do
39
+ entity.should_receive(:check_list).and_return([check])
40
+ Flapjack::Data::Entity.should_receive(:find_by_name).
41
+ with(entity_name, :redis => redis).and_return(entity)
42
+
43
+ get "/checks/#{entity_name_esc}"
44
+ last_response.should be_ok
45
+ last_response.body.should == [check].to_json
46
+ end
47
+
48
+ context 'non-bulk API calls' do
49
+
50
+ it "returns the status for all checks on an entity" do
51
+ status = mock('status', :to_json => 'status!'.to_json)
52
+ result = {:entity => entity_name, :check => check, :status => status}
53
+ entity_presenter.should_receive(:status).and_return(result)
54
+
55
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
56
+ with(entity, :redis => redis).and_return(entity_presenter)
57
+
58
+ Flapjack::Data::Entity.should_receive(:find_by_name).
59
+ with(entity_name, :redis => redis).and_return(entity)
60
+
61
+ get "/status/#{entity_name_esc}"
62
+ last_response.should be_ok
63
+ last_response.body.should == ['status!'].to_json
64
+ end
65
+
66
+ it "should not show the status for an entity that's not found" do
67
+ Flapjack::Data::Entity.should_receive(:find_by_name).
68
+ with(entity_name, :redis => redis).and_return(nil)
69
+
70
+ get "/status/#{entity_name_esc}"
71
+ last_response.should be_forbidden
72
+ end
73
+
74
+ it "returns the status for a check on an entity" do
75
+ status = mock('status', :to_json => 'status!'.to_json)
76
+ entity_check_presenter.should_receive(:status).and_return(status)
77
+
78
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
79
+ with(entity_check).and_return(entity_check_presenter)
80
+
81
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
82
+ with(entity, check, :redis => redis).and_return(entity_check)
83
+
84
+ Flapjack::Data::Entity.should_receive(:find_by_name).
85
+ with(entity_name, :redis => redis).and_return(entity)
86
+
87
+ get "/status/#{entity_name_esc}/#{check}"
88
+ last_response.should be_ok
89
+ last_response.body.should == 'status!'.to_json
90
+ end
91
+
92
+ it "should not show the status for a check on an entity that's not found" do
93
+ Flapjack::Data::Entity.should_receive(:find_by_name).
94
+ with(entity_name, :redis => redis).and_return(nil)
95
+
96
+ get "/status/#{entity_name_esc}/#{check}"
97
+ last_response.should be_forbidden
98
+ end
99
+
100
+ it "should not show the status for a check that's not found on an entity" do
101
+ Flapjack::Data::Entity.should_receive(:find_by_name).
102
+ with(entity_name, :redis => redis).and_return(entity)
103
+
104
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
105
+ with(entity, check, :redis => redis).and_return(nil)
106
+
107
+ get "/status/#{entity_name_esc}/#{check}"
108
+ last_response.should be_forbidden
109
+ end
110
+
111
+ it "returns a list of scheduled maintenance periods for an entity" do
112
+ sched = mock('sched', :to_json => 'sched!'.to_json)
113
+ result = {:entity => entity_name, :check => check, :scheduled_maintenances => sched}
114
+ entity_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(result)
115
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
116
+ with(entity, :redis => redis).and_return(entity_presenter)
117
+ Flapjack::Data::Entity.should_receive(:find_by_name).
118
+ with(entity_name, :redis => redis).and_return(entity)
119
+
120
+ get "/scheduled_maintenances/#{entity_name_esc}"
121
+ last_response.should be_ok
122
+ last_response.body.should == [{:check => check, :scheduled_maintenance => sched}].to_json
123
+ end
124
+
125
+ it "returns a list of scheduled maintenance periods within a time window for an entity" do
126
+ start = Time.parse('1 Jan 2012')
127
+ finish = Time.parse('6 Jan 2012')
128
+
129
+ sched = mock('sched', :to_json => 'sched!'.to_json)
130
+ result = {:entity => entity_name, :check => check, :scheduled_maintenances => sched}
131
+ entity_presenter.should_receive(:scheduled_maintenances).with(start.to_i, finish.to_i).and_return(result)
132
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
133
+ with(entity, :redis => redis).and_return(entity_presenter)
134
+ Flapjack::Data::Entity.should_receive(:find_by_name).
135
+ with(entity_name, :redis => redis).and_return(entity)
136
+
137
+ get "/scheduled_maintenances/#{entity_name_esc}?" +
138
+ "start_time=#{CGI.escape(start.iso8601)}&end_time=#{CGI.escape(finish.iso8601)}"
139
+ last_response.should be_ok
140
+ last_response.body.should == [{:check => check, :scheduled_maintenance => sched}].to_json
141
+ end
142
+
143
+ it "returns a list of scheduled maintenance periods for a check on an entity" do
144
+ sched = mock('sched', :to_json => 'sched!'.to_json)
145
+ entity_check_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(sched)
146
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
147
+ with(entity_check).and_return(entity_check_presenter)
148
+ Flapjack::Data::Entity.should_receive(:find_by_name).
149
+ with(entity_name, :redis => redis).and_return(entity)
150
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
151
+ with(entity, check, :redis => redis).and_return(entity_check)
152
+
153
+ get "/scheduled_maintenances/#{entity_name_esc}/#{check}"
154
+ last_response.should be_ok
155
+ last_response.body.should == 'sched!'.to_json
156
+ end
157
+
158
+ it "creates an acknowledgement for an entity check" do
159
+ entity_check.should_receive(:entity_name).and_return(entity_name)
160
+ entity_check.should_receive(:check).and_return(check)
161
+
162
+ Flapjack::Data::Entity.should_receive(:find_by_name).
163
+ with(entity_name, :redis => redis).and_return(entity)
164
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
165
+ with(entity, check, :redis => redis).and_return(entity_check)
166
+ Flapjack::Data::Event.should_receive(:create_acknowledgement).
167
+ with(entity_name, check, :summary => nil, :duration => (4 * 60 * 60), :redis => redis)
168
+
169
+ post "/acknowledgements/#{entity_name_esc}/#{check}"
170
+ last_response.status.should == 204
171
+ end
172
+
173
+ it "returns a list of unscheduled maintenance periods for an entity" do
174
+ unsched = mock('unsched', :to_json => 'unsched!'.to_json)
175
+ result = {:entity => entity_name, :check => check, :unscheduled_maintenances => unsched}
176
+ entity_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(result)
177
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
178
+ with(entity, :redis => redis).and_return(entity_presenter)
179
+ Flapjack::Data::Entity.should_receive(:find_by_name).
180
+ with(entity_name, :redis => redis).and_return(entity)
181
+
182
+ get "/unscheduled_maintenances/#{entity_name_esc}"
183
+ last_response.should be_ok
184
+ last_response.body.should == [{:check => check, :unscheduled_maintenance => unsched}].to_json
185
+ end
186
+
187
+ it "returns a list of unscheduled maintenance periods for a check on an entity" do
188
+ unsched = mock('unsched', :to_json => 'unsched!'.to_json)
189
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(unsched)
190
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
191
+ with(entity_check).and_return(entity_check_presenter)
192
+ Flapjack::Data::Entity.should_receive(:find_by_name).
193
+ with(entity_name, :redis => redis).and_return(entity)
194
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
195
+ with(entity, check, :redis => redis).and_return(entity_check)
196
+
197
+ get "/unscheduled_maintenances/#{entity_name_esc}/#{check}"
198
+ last_response.should be_ok
199
+ last_response.body.should == 'unsched!'.to_json
200
+ end
201
+
202
+ it "returns a list of unscheduled maintenance periods within a time window for a check an entity" do
203
+ start = Time.parse('1 Jan 2012')
204
+ finish = Time.parse('6 Jan 2012')
205
+
206
+ unsched = mock('unsched', :to_json => 'unsched!'.to_json)
207
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(start.to_i, finish.to_i).and_return(unsched)
208
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
209
+ with(entity_check).and_return(entity_check_presenter)
210
+ Flapjack::Data::Entity.should_receive(:find_by_name).
211
+ with(entity_name, :redis => redis).and_return(entity)
212
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
213
+ with(entity, check, :redis => redis).and_return(entity_check)
214
+
215
+ get "/unscheduled_maintenances/#{entity_name_esc}/#{check}" +
216
+ "?start_time=#{CGI.escape(start.iso8601)}&end_time=#{CGI.escape(finish.iso8601)}"
217
+ last_response.should be_ok
218
+ last_response.body.should == 'unsched!'.to_json
219
+ end
220
+
221
+ it "returns a list of outages for an entity" do
222
+ out = mock('out', :to_json => 'out!'.to_json)
223
+ result = {:entity => entity_name, :check => check, :outages => out}
224
+ entity_presenter.should_receive(:outages).with(nil, nil).and_return(result)
225
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
226
+ with(entity, :redis => redis).and_return(entity_presenter)
227
+ Flapjack::Data::Entity.should_receive(:find_by_name).
228
+ with(entity_name, :redis => redis).and_return(entity)
229
+
230
+ get "/outages/#{entity_name_esc}"
231
+ last_response.should be_ok
232
+ last_response.body.should == [{:check => check, :outages => out}].to_json
233
+ end
234
+
235
+ it "returns a list of outages for a check on an entity" do
236
+ out = mock('out', :to_json => 'out!'.to_json)
237
+ entity_check_presenter.should_receive(:outages).with(nil, nil).and_return(out)
238
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
239
+ with(entity_check).and_return(entity_check_presenter)
240
+ Flapjack::Data::Entity.should_receive(:find_by_name).
241
+ with(entity_name, :redis => redis).and_return(entity)
242
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
243
+ with(entity, check, :redis => redis).and_return(entity_check)
244
+
245
+ get "/outages/#{entity_name_esc}/#{check}"
246
+ last_response.should be_ok
247
+ last_response.body.should == 'out!'.to_json
248
+ end
249
+
250
+ it "returns a list of downtimes for an entity" do
251
+ down = mock('down', :to_json => 'down!'.to_json)
252
+ result = {:entity => entity_name, :check => check, :downtime => down}
253
+ entity_presenter.should_receive(:downtime).with(nil, nil).and_return(result)
254
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
255
+ with(entity, :redis => redis).and_return(entity_presenter)
256
+ Flapjack::Data::Entity.should_receive(:find_by_name).
257
+ with(entity_name, :redis => redis).and_return(entity)
258
+
259
+ get "/downtime/#{entity_name_esc}"
260
+ last_response.should be_ok
261
+ last_response.body.should == [{:check => check, :downtime => down}].to_json
262
+ end
263
+
264
+ it "returns a list of downtimes for a check on an entity" do
265
+ down = mock('down', :to_json => 'down!'.to_json)
266
+ entity_check_presenter.should_receive(:downtime).with(nil, nil).and_return(down)
267
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
268
+ with(entity_check).and_return(entity_check_presenter)
269
+ Flapjack::Data::Entity.should_receive(:find_by_name).
270
+ with(entity_name, :redis => redis).and_return(entity)
271
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
272
+ with(entity, check, :redis => redis).and_return(entity_check)
273
+
274
+ get "/downtime/#{entity_name_esc}/#{check}"
275
+ last_response.should be_ok
276
+ last_response.body.should == 'down!'.to_json
277
+ end
278
+
279
+ it "creates a test notification event for check on an entity" do
280
+ Flapjack::Data::Entity.should_receive(:find_by_name).
281
+ with(entity_name, :redis => redis).and_return(entity)
282
+ entity.should_receive(:name).and_return(entity_name)
283
+ entity_check.should_receive(:entity).and_return(entity)
284
+ entity_check.should_receive(:entity_name).and_return(entity_name)
285
+ entity_check.should_receive(:check).and_return('foo')
286
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
287
+ with(entity, 'foo', :redis => redis).and_return(entity_check)
288
+
289
+ Flapjack::Data::Event.should_receive(:test_notifications).
290
+ with(entity_name, 'foo', hash_including(:redis => redis))
291
+
292
+ post "/test_notifications/#{entity_name_esc}/foo"
293
+ last_response.status.should == 204
294
+ end
295
+
296
+ end
297
+
298
+ context 'bulk API calls' do
299
+
300
+ it "returns the status for all checks on an entity" do
301
+ status = mock('status')
302
+ result = [{:entity => entity_name, :check => check, :status => status}]
303
+ entity_presenter.should_receive(:status).and_return(result)
304
+
305
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
306
+ with(entity, :redis => redis).and_return(entity_presenter)
307
+
308
+ Flapjack::Data::Entity.should_receive(:find_by_name).
309
+ with(entity_name, :redis => redis).and_return(entity)
310
+
311
+ get "/status", :entity => entity_name
312
+ last_response.body.should == result.to_json
313
+ end
314
+
315
+ it "should not show the status for an entity that's not found" do
316
+ Flapjack::Data::Entity.should_receive(:find_by_name).
317
+ with(entity_name, :redis => redis).and_return(nil)
318
+
319
+ get "/status", :entity => entity_name
320
+ last_response.should be_forbidden
321
+ end
322
+
323
+ it "returns the status for a check on an entity" do
324
+ status = mock('status')
325
+ result = [{:entity => entity_name, :check => check, :status => status}]
326
+ entity_check_presenter.should_receive(:status).and_return(status)
327
+
328
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
329
+ with(entity_check).and_return(entity_check_presenter)
330
+
331
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
332
+ with(entity, check, :redis => redis).and_return(entity_check)
333
+
334
+ Flapjack::Data::Entity.should_receive(:find_by_name).
335
+ with(entity_name, :redis => redis).and_return(entity)
336
+
337
+ get "/status", :check => {entity_name => check}
338
+ last_response.should be_ok
339
+ last_response.body.should == result.to_json
340
+ end
341
+
342
+ it "should not show the status for a check on an entity that's not found" do
343
+ Flapjack::Data::Entity.should_receive(:find_by_name).
344
+ with(entity_name, :redis => redis).and_return(nil)
345
+
346
+ get "/status", :check => {entity_name => check}
347
+ last_response.should be_forbidden
348
+ end
349
+
350
+ it "should not show the status for a check that's not found on an entity" do
351
+ Flapjack::Data::Entity.should_receive(:find_by_name).
352
+ with(entity_name, :redis => redis).and_return(entity)
353
+
354
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
355
+ with(entity, check, :redis => redis).and_return(nil)
356
+
357
+ get "/status", :check => {entity_name => check}
358
+ last_response.should be_forbidden
359
+ end
360
+
361
+ it "creates an acknowledgement for an entity check" do
362
+ Flapjack::Data::Entity.should_receive(:find_by_name).
363
+ with(entity_name, :redis => redis).and_return(entity)
364
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
365
+ with(entity, check, :redis => redis).and_return(entity_check)
366
+
367
+ entity_check.should_receive(:entity_name).and_return(entity_name)
368
+ entity_check.should_receive(:check).and_return(check)
369
+
370
+ Flapjack::Data::Event.should_receive(:create_acknowledgement).
371
+ with(entity_name, check, :summary => nil, :duration => (4 * 60 * 60), :redis => redis)
372
+
373
+ post '/acknowledgements',:check => {entity_name => check}
374
+ last_response.status.should == 204
375
+ end
376
+
377
+ it "deletes an unscheduled maintenance period for an entity check" do
378
+ end_time = Time.now + (60 * 60) # an hour from now
379
+ entity_check.should_receive(:end_unscheduled_maintenance).with(:end_time => end_time.to_i)
380
+
381
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
382
+ with(entity, check, :redis => redis).and_return(entity_check)
383
+
384
+ Flapjack::Data::Entity.should_receive(:find_by_name).
385
+ with(entity_name, :redis => redis).and_return(entity)
386
+
387
+ delete "/unscheduled_maintenances", :check => {entity_name => check}, :end_time => end_time.iso8601
388
+ last_response.status.should == 204
389
+ end
390
+
391
+ it "creates a scheduled maintenance period for an entity check" do
392
+ start = Time.now + (60 * 60) # an hour from now
393
+ duration = (2 * 60 * 60) # two hours
394
+ Flapjack::Data::Entity.should_receive(:find_by_name).
395
+ with(entity_name, :redis => redis).and_return(entity)
396
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
397
+ with(entity, check, :redis => redis).and_return(entity_check)
398
+ entity_check.should_receive(:create_scheduled_maintenance).
399
+ with(:summary => 'test', :duration => duration, :start_time => start.getutc.to_i)
400
+
401
+ post "/scheduled_maintenances/#{entity_name_esc}/#{check}?" +
402
+ "start_time=#{CGI.escape(start.iso8601)}&summary=test&duration=#{duration}"
403
+ last_response.status.should == 204
404
+ end
405
+
406
+ it "doesn't create a scheduled maintenance period if the start time isn't passed" do
407
+ duration = (2 * 60 * 60) # two hours
408
+
409
+ post "/scheduled_maintenances/#{entity_name_esc}/#{check}?" +
410
+ "summary=test&duration=#{duration}"
411
+ last_response.status.should == 403
412
+ end
413
+
414
+ it "deletes a scheduled maintenance period for an entity check" do
415
+ start_time = Time.now + (60 * 60) # an hour from now
416
+ entity_check.should_receive(:delete_scheduled_maintenance).with(:start_time => start_time.to_i)
417
+
418
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
419
+ with(entity, check, :redis => redis).and_return(entity_check)
420
+
421
+ Flapjack::Data::Entity.should_receive(:find_by_name).
422
+ with(entity_name, :redis => redis).and_return(entity)
423
+
424
+ delete "/scheduled_maintenances", :check => {entity_name => check}, :start_time => start_time.iso8601
425
+ last_response.status.should == 204
426
+ end
427
+
428
+ it "doesn't delete a scheduled maintenance period if the start time isn't passed" do
429
+ entity_check.should_not_receive(:delete_scheduled_maintenance)
430
+
431
+ delete "/scheduled_maintenances", :check => {entity_name => check}
432
+ last_response.status.should == 403
433
+ end
434
+
435
+ it "deletes scheduled maintenance periods for multiple entity checks" do
436
+ start_time = Time.now + (60 * 60) # an hour from now
437
+
438
+ entity_check_2 = mock(Flapjack::Data::EntityCheck)
439
+
440
+ entity_check.should_receive(:delete_scheduled_maintenance).with(:start_time => start_time.to_i)
441
+ entity_check_2.should_receive(:delete_scheduled_maintenance).with(:start_time => start_time.to_i)
442
+
443
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
444
+ with(entity, check, :redis => redis).and_return(entity_check)
445
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
446
+ with(entity, 'foo', :redis => redis).and_return(entity_check_2)
447
+
448
+ Flapjack::Data::Entity.should_receive(:find_by_name).
449
+ with(entity_name, :redis => redis).and_return(entity)
450
+
451
+ delete "/scheduled_maintenances", :check => {entity_name => [check, 'foo']}, :start_time => start_time.iso8601
452
+ last_response.status.should == 204
453
+ end
454
+
455
+ it "returns a list of scheduled maintenance periods for an entity" do
456
+ sm = mock('sched_maint')
457
+ result = [{:entity => entity_name, :check => check, :scheduled_maintenances => sm}]
458
+
459
+ entity_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(result)
460
+
461
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
462
+ with(entity, :redis => redis).and_return(entity_presenter)
463
+
464
+ Flapjack::Data::Entity.should_receive(:find_by_name).
465
+ with(entity_name, :redis => redis).and_return(entity)
466
+
467
+ get "/scheduled_maintenances", :entity => entity_name
468
+ last_response.should be_ok
469
+ last_response.body.should == result.to_json
470
+ end
471
+
472
+ it "returns a list of scheduled maintenance periods within a time window for an entity" do
473
+ start = Time.parse('1 Jan 2012')
474
+ finish = Time.parse('6 Jan 2012')
475
+
476
+ sm = mock('sched_maint')
477
+ result = [{:entity => entity_name, :check => check, :scheduled_maintenances => sm}]
478
+
479
+ entity_presenter.should_receive(:scheduled_maintenances).with(start.to_i, finish.to_i).and_return(result)
480
+
481
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
482
+ with(entity, :redis => redis).and_return(entity_presenter)
483
+
484
+ Flapjack::Data::Entity.should_receive(:find_by_name).
485
+ with(entity_name, :redis => redis).and_return(entity)
486
+
487
+ get "/scheduled_maintenances", :entity => entity_name,
488
+ :start_time => start.iso8601, :end_time => finish.iso8601
489
+ last_response.should be_ok
490
+ last_response.body.should == result.to_json
491
+ end
492
+
493
+ it "returns a list of scheduled maintenance periods for a check on an entity" do
494
+ sm = mock('sched_maint')
495
+ result = [{:entity => entity_name, :check => check, :scheduled_maintenances => sm}]
496
+
497
+ entity_check_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(sm)
498
+
499
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
500
+ with(entity_check).and_return(entity_check_presenter)
501
+
502
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
503
+ with(entity, check, :redis => redis).and_return(entity_check)
504
+
505
+ Flapjack::Data::Entity.should_receive(:find_by_name).
506
+ with(entity_name, :redis => redis).and_return(entity)
507
+
508
+ get "/scheduled_maintenances", :check => {entity_name => check}
509
+ last_response.should be_ok
510
+ last_response.body.should == result.to_json
511
+ end
512
+
513
+ it "returns a list of unscheduled maintenance periods for an entity" do
514
+ um = mock('unsched_maint')
515
+ result = [{:entity => entity_name, :check => check, :unscheduled_maintenances => um}]
516
+
517
+ entity_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(result)
518
+
519
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
520
+ with(entity, :redis => redis).and_return(entity_presenter)
521
+
522
+ Flapjack::Data::Entity.should_receive(:find_by_name).
523
+ with(entity_name, :redis => redis).and_return(entity)
524
+
525
+ get "/unscheduled_maintenances", :entity => entity_name
526
+ last_response.should be_ok
527
+ last_response.body.should == result.to_json
528
+ end
529
+
530
+ it "returns a list of unscheduled maintenance periods for a check on an entity" do
531
+ um = mock('unsched_maint')
532
+ result = [{:entity => entity_name, :check => check, :unscheduled_maintenances => um}]
533
+
534
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(um)
535
+
536
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
537
+ with(entity_check).and_return(entity_check_presenter)
538
+
539
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
540
+ with(entity, check, :redis => redis).and_return(entity_check)
541
+
542
+ Flapjack::Data::Entity.should_receive(:find_by_name).
543
+ with(entity_name, :redis => redis).and_return(entity)
544
+
545
+ get "/unscheduled_maintenances", :check => {entity_name => check}
546
+ last_response.should be_ok
547
+ last_response.body.should == result.to_json
548
+ end
549
+
550
+ it "returns a list of unscheduled maintenance periods within a time window for a check an entity" do
551
+ start = Time.parse('1 Jan 2012')
552
+ finish = Time.parse('6 Jan 2012')
553
+
554
+ um = mock('unsched_maint')
555
+ result = [{:entity => entity_name, :check => check, :unscheduled_maintenances => um}]
556
+
557
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(start.to_i, finish.to_i).and_return(um)
558
+
559
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
560
+ with(entity_check).and_return(entity_check_presenter)
561
+
562
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
563
+ with(entity, check, :redis => redis).and_return(entity_check)
564
+
565
+ Flapjack::Data::Entity.should_receive(:find_by_name).
566
+ with(entity_name, :redis => redis).and_return(entity)
567
+
568
+ get "/unscheduled_maintenances", :check => {entity_name => check},
569
+ :start_time => start.iso8601, :end_time => finish.iso8601
570
+ last_response.should be_ok
571
+ last_response.body.should == result.to_json
572
+ end
573
+
574
+ it "returns a list of outages, for one whole entity and two checks on another entity" do
575
+ outages_1 = mock('outages_1')
576
+ outages_2 = mock('outages_2')
577
+ outages_3 = mock('outages_3')
578
+
579
+ entity_2_name = 'entity_2'
580
+ entity_2 = mock(Flapjack::Data::Entity)
581
+
582
+ result = [{:entity => entity_name, :check => check, :outages => outages_1},
583
+ {:entity => entity_2_name, :check => 'foo', :outages => outages_2},
584
+ {:entity => entity_2_name, :check => 'bar', :outages => outages_3}]
585
+
586
+ foo_check = mock(Flapjack::Data::EntityCheck)
587
+ bar_check = mock(Flapjack::Data::EntityCheck)
588
+
589
+ foo_check_presenter = mock(Flapjack::Gateways::API::EntityCheckPresenter)
590
+ bar_check_presenter = mock(Flapjack::Gateways::API::EntityCheckPresenter)
591
+
592
+ entity_presenter.should_receive(:outages).with(nil, nil).and_return(result[0])
593
+ foo_check_presenter.should_receive(:outages).with(nil, nil).and_return(outages_2)
594
+ bar_check_presenter.should_receive(:outages).with(nil, nil).and_return(outages_3)
595
+
596
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
597
+ with(entity, :redis => redis).and_return(entity_presenter)
598
+
599
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
600
+ with(foo_check).and_return(foo_check_presenter)
601
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
602
+ with(bar_check).and_return(bar_check_presenter)
603
+
604
+ Flapjack::Data::Entity.should_receive(:find_by_name).
605
+ with(entity_name, :redis => redis).and_return(entity)
606
+ Flapjack::Data::Entity.should_receive(:find_by_name).
607
+ with(entity_2_name, :redis => redis).and_return(entity_2)
608
+
609
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
610
+ with(entity_2, 'foo', :redis => redis).and_return(foo_check)
611
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
612
+ with(entity_2, 'bar', :redis => redis).and_return(bar_check)
613
+
614
+ get "/outages", :entity => entity_name, :check => {entity_2_name => ['foo', 'bar']}
615
+ last_response.should be_ok
616
+ last_response.body.should == result.to_json
617
+ end
618
+
619
+ it "returns a list of outages for a check on an entity" do
620
+ outages = mock('outages')
621
+ result = [{:entity => entity_name, :check => check, :outages => outages}]
622
+
623
+ entity_check_presenter.should_receive(:outages).with(nil, nil).and_return(outages)
624
+
625
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
626
+ with(entity_check).and_return(entity_check_presenter)
627
+
628
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
629
+ with(entity, check, :redis => redis).and_return(entity_check)
630
+
631
+ Flapjack::Data::Entity.should_receive(:find_by_name).
632
+ with(entity_name, :redis => redis).and_return(entity)
633
+
634
+ get "/outages", :check => {entity_name => check}
635
+ last_response.should be_ok
636
+ last_response.body.should == result.to_json
637
+ end
638
+
639
+ it "returns a list of downtimes for an entity" do
640
+ downtime = mock('downtime')
641
+ result = [{:entity => entity_name, :check => check, :downtime => downtime}]
642
+
643
+ entity_presenter.should_receive(:downtime).with(nil, nil).and_return(result)
644
+
645
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
646
+ with(entity, :redis => redis).and_return(entity_presenter)
647
+
648
+ Flapjack::Data::Entity.should_receive(:find_by_name).
649
+ with(entity_name, :redis => redis).and_return(entity)
650
+
651
+ get "/downtime", :entity => entity_name
652
+ last_response.should be_ok
653
+ last_response.body.should == result.to_json
654
+ end
655
+
656
+ it "returns a list of downtimes for a check on an entity" do
657
+ downtime = mock('downtime')
658
+ result = [{:entity => entity_name, :check => check, :downtime => downtime}]
659
+
660
+ entity_check_presenter.should_receive(:downtime).with(nil, nil).and_return(downtime)
661
+
662
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
663
+ with(entity_check).and_return(entity_check_presenter)
664
+
665
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
666
+ with(entity, check, :redis => redis).and_return(entity_check)
667
+
668
+ Flapjack::Data::Entity.should_receive(:find_by_name).
669
+ with(entity_name, :redis => redis).and_return(entity)
670
+
671
+ get "/downtime", :check => {entity_name => check}
672
+ last_response.should be_ok
673
+ last_response.body.should == result.to_json
674
+ end
675
+
676
+ it "creates test notification events for all checks on an entity" do
677
+ entity.should_receive(:check_list).and_return([check, 'foo'])
678
+ entity.should_receive(:name).twice.and_return(entity_name)
679
+ Flapjack::Data::Entity.should_receive(:find_by_name).
680
+ with(entity_name, :redis => redis).and_return(entity)
681
+
682
+ entity_check.should_receive(:entity).and_return(entity)
683
+ entity_check.should_receive(:entity_name).and_return(entity_name)
684
+ entity_check.should_receive(:check).and_return(check)
685
+
686
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
687
+ with(entity, check, :redis => redis).and_return(entity_check)
688
+
689
+ entity_check_2 = mock(Flapjack::Data::EntityCheck)
690
+ entity_check_2.should_receive(:entity).and_return(entity)
691
+ entity_check_2.should_receive(:entity_name).and_return(entity_name)
692
+ entity_check_2.should_receive(:check).and_return('foo')
693
+
694
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
695
+ with(entity, 'foo', :redis => redis).and_return(entity_check_2)
696
+
697
+ Flapjack::Data::Event.should_receive(:test_notifications).
698
+ with(entity_name, check, hash_including(:redis => redis))
699
+
700
+ Flapjack::Data::Event.should_receive(:test_notifications).
701
+ with(entity_name, 'foo', hash_including(:redis => redis))
702
+
703
+
704
+ post '/test_notifications', :entity => entity_name
705
+ last_response.status.should == 204
706
+ end
707
+
708
+ it "creates a test notification event for check on an entity" do
709
+ Flapjack::Data::Entity.should_receive(:find_by_name).
710
+ with(entity_name, :redis => redis).and_return(entity)
711
+ entity.should_receive(:name).and_return(entity_name)
712
+ entity_check.should_receive(:entity).and_return(entity)
713
+ entity_check.should_receive(:entity_name).and_return(entity_name)
714
+ entity_check.should_receive(:check).and_return(check)
715
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
716
+ with(entity, check, :redis => redis).and_return(entity_check)
717
+
718
+ Flapjack::Data::Event.should_receive(:test_notifications).
719
+ with(entity_name, check, hash_including(:redis => redis))
720
+
721
+
722
+ post '/test_notifications', :check => {entity_name => check}
723
+ last_response.status.should == 204
724
+ end
725
+
726
+ it "creates entities from a submitted list" do
727
+ entities = {'entities' =>
728
+ [
729
+ {"id" => "10001",
730
+ "name" => "clientx-app-01",
731
+ "contacts" => ["0362","0363","0364"]
732
+ },
733
+ {"id" => "10002",
734
+ "name" => "clientx-app-02",
735
+ "contacts" => ["0362"]
736
+ }
737
+ ]
738
+ }
739
+ Flapjack::Data::Entity.should_receive(:add).twice
740
+
741
+ post "/entities", entities.to_json, {'CONTENT_TYPE' => 'application/json'}
742
+ last_response.status.should == 204
743
+ end
744
+
745
+ it "does not create entities if the data is improperly formatted" do
746
+ Flapjack::Data::Entity.should_not_receive(:add)
747
+
748
+ post "/entities", {'entities' => ["Hello", "there"]}.to_json,
749
+ {'CONTENT_TYPE' => 'application/json'}
750
+ last_response.status.should == 403
751
+ end
752
+
753
+ it "does not create entities if they don't contain an id" do
754
+ entities = {'entities' =>
755
+ [
756
+ {"id" => "10001",
757
+ "name" => "clientx-app-01",
758
+ "contacts" => ["0362","0363","0364"]
759
+ },
760
+ {"name" => "clientx-app-02",
761
+ "contacts" => ["0362"]
762
+ }
763
+ ]
764
+ }
765
+ Flapjack::Data::Entity.should_receive(:add)
766
+
767
+ post "/entities", entities.to_json, {'CONTENT_TYPE' => 'application/json'}
768
+ last_response.status.should == 403
769
+ end
770
+
771
+ end
772
+
773
+ context "tags" do
774
+
775
+ it "sets a single tag on an entity and returns current tags" do
776
+ entity.should_receive(:add_tags).with('web')
777
+ entity.should_receive(:tags).and_return(['web'])
778
+ Flapjack::Data::Entity.should_receive(:find_by_name).
779
+ with(entity_name, :redis => redis).and_return(entity)
780
+
781
+ post "entities/#{entity_name}/tags", :tag => 'web'
782
+ last_response.should be_ok
783
+ last_response.body.should be_json_eql( ['web'].to_json )
784
+ end
785
+
786
+ it "does not set a single tag on an entity that's not found" do
787
+ Flapjack::Data::Entity.should_receive(:find_by_name).
788
+ with(entity_name, :redis => redis).and_return(nil)
789
+
790
+ post "entities/#{entity_name}/tags", :tag => 'web'
791
+ last_response.should be_forbidden
792
+ end
793
+
794
+ it "sets multiple tags on an entity and returns current tags" do
795
+ entity.should_receive(:add_tags).with('web', 'app')
796
+ entity.should_receive(:tags).and_return(['web', 'app'])
797
+ Flapjack::Data::Entity.should_receive(:find_by_name).
798
+ with(entity_name, :redis => redis).and_return(entity)
799
+
800
+ # NB submitted at a lower level as tag[]=web&tag[]=app
801
+ post "entities/#{entity_name}/tags", :tag => ['web', 'app']
802
+ last_response.should be_ok
803
+ last_response.body.should be_json_eql( ['web', 'app'].to_json )
804
+ end
805
+
806
+ it "does not set multiple tags on an entity that's not found" do
807
+ Flapjack::Data::Entity.should_receive(:find_by_name).
808
+ with(entity_name, :redis => redis).and_return(nil)
809
+
810
+ post "entities/#{entity_name}/tags", :tag => ['web', 'app']
811
+ last_response.should be_forbidden
812
+ end
813
+
814
+ it "removes a single tag from an entity" do
815
+ entity.should_receive(:delete_tags).with('web')
816
+ Flapjack::Data::Entity.should_receive(:find_by_name).
817
+ with(entity_name, :redis => redis).and_return(entity)
818
+
819
+ delete "entities/#{entity_name}/tags", :tag => 'web'
820
+ last_response.status.should == 204
821
+ end
822
+
823
+ it "does not remove a single tag from an entity that's not found" do
824
+ Flapjack::Data::Entity.should_receive(:find_by_name).
825
+ with(entity_name, :redis => redis).and_return(nil)
826
+
827
+ delete "entities/#{entity_name}/tags", :tag => 'web'
828
+ last_response.should be_forbidden
829
+ end
830
+
831
+ it "removes multiple tags from an entity" do
832
+ entity.should_receive(:delete_tags).with('web', 'app')
833
+ Flapjack::Data::Entity.should_receive(:find_by_name).
834
+ with(entity_name, :redis => redis).and_return(entity)
835
+
836
+ delete "entities/#{entity_name}/tags", :tag => ['web', 'app']
837
+ last_response.status.should == 204
838
+ end
839
+
840
+ it "does not remove multiple tags from an entity that's not found" do
841
+ Flapjack::Data::Entity.should_receive(:find_by_name).
842
+ with(entity_name, :redis => redis).and_return(nil)
843
+
844
+ delete "entities/#{entity_name}/tags", :tag => ['web', 'app']
845
+ last_response.should be_forbidden
846
+ end
847
+
848
+ it "gets all tags on an entity" do
849
+ entity.should_receive(:tags).and_return(['web', 'app'])
850
+ Flapjack::Data::Entity.should_receive(:find_by_name).
851
+ with(entity_name, :redis => redis).and_return(entity)
852
+
853
+ get "entities/#{entity_name}/tags"
854
+ last_response.should be_ok
855
+ last_response.body.should be_json_eql( ['web', 'app'].to_json )
856
+ end
857
+
858
+ it "does not get all tags on an entity that's not found" do
859
+ Flapjack::Data::Entity.should_receive(:find_by_name).
860
+ with(entity_name, :redis => redis).and_return(nil)
861
+
862
+ get "entities/#{entity_name}/tags"
863
+ last_response.should be_forbidden
864
+ end
865
+
866
+ end
867
+
868
+ end