flapjack 0.7.14 → 0.7.15

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.
Files changed (49) hide show
  1. data/CHANGELOG.md +10 -0
  2. data/etc/flapjack_config.yaml.example +1 -0
  3. data/features/events.feature +5 -0
  4. data/features/notification_rules.feature +1 -1
  5. data/features/steps/events_steps.rb +28 -13
  6. data/features/steps/notifications_steps.rb +1 -1
  7. data/lib/flapjack/coordinator.rb +3 -1
  8. data/lib/flapjack/data/contact.rb +8 -6
  9. data/lib/flapjack/data/entity_check.rb +78 -113
  10. data/lib/flapjack/data/event.rb +54 -65
  11. data/lib/flapjack/data/notification.rb +5 -1
  12. data/lib/flapjack/executive.rb +42 -38
  13. data/lib/flapjack/filters/acknowledgement.rb +5 -5
  14. data/lib/flapjack/filters/base.rb +2 -2
  15. data/lib/flapjack/filters/delays.rb +11 -11
  16. data/lib/flapjack/filters/detect_mass_client_failures.rb +8 -8
  17. data/lib/flapjack/filters/ok.rb +6 -6
  18. data/lib/flapjack/filters/scheduled_maintenance.rb +2 -2
  19. data/lib/flapjack/filters/unscheduled_maintenance.rb +3 -2
  20. data/lib/flapjack/gateways/api.rb +374 -277
  21. data/lib/flapjack/gateways/api/entity_check_presenter.rb +52 -21
  22. data/lib/flapjack/gateways/api/entity_presenter.rb +14 -9
  23. data/lib/flapjack/gateways/email.rb +7 -0
  24. data/lib/flapjack/gateways/email/alert.html.haml +13 -1
  25. data/lib/flapjack/gateways/email/alert.text.erb +5 -4
  26. data/lib/flapjack/gateways/jabber.rb +90 -34
  27. data/lib/flapjack/gateways/pagerduty.rb +6 -2
  28. data/lib/flapjack/gateways/web.rb +13 -8
  29. data/lib/flapjack/gateways/web/views/check.haml +70 -45
  30. data/lib/flapjack/gateways/web/views/checks.haml +1 -1
  31. data/lib/flapjack/gateways/web/views/entity.haml +1 -1
  32. data/lib/flapjack/patches.rb +9 -2
  33. data/lib/flapjack/pikelet.rb +14 -10
  34. data/lib/flapjack/utility.rb +10 -4
  35. data/lib/flapjack/version.rb +1 -1
  36. data/spec/lib/flapjack/coordinator_spec.rb +19 -5
  37. data/spec/lib/flapjack/data/entity_check_spec.rb +3 -30
  38. data/spec/lib/flapjack/data/event_spec.rb +96 -1
  39. data/spec/lib/flapjack/executive_spec.rb +5 -11
  40. data/spec/lib/flapjack/gateways/api/entity_check_presenter_spec.rb +22 -3
  41. data/spec/lib/flapjack/gateways/api/entity_presenter_spec.rb +30 -15
  42. data/spec/lib/flapjack/gateways/api_spec.rb +552 -186
  43. data/spec/lib/flapjack/gateways/email_spec.rb +2 -0
  44. data/spec/lib/flapjack/gateways/jabber_spec.rb +5 -4
  45. data/spec/lib/flapjack/gateways/pagerduty_spec.rb +3 -2
  46. data/spec/lib/flapjack/gateways/web_spec.rb +17 -12
  47. data/spec/lib/flapjack/pikelet_spec.rb +5 -2
  48. metadata +4 -5
  49. data/config.ru +0 -11
@@ -1,6 +1,101 @@
1
1
  require 'spec_helper'
2
2
  require 'flapjack/data/event'
3
3
 
4
- describe Flapjack::Data::Event, :redis => true do
4
+ describe Flapjack::Data::Event do
5
+
6
+ let(:entity_name) { 'xyz-example.com' }
7
+ let(:check) { 'ping' }
8
+ let(:mock_redis) { mock(::Redis) }
9
+
10
+ let!(:time) { Time.now}
11
+
12
+ let(:event_data) { {'type' => 'service',
13
+ 'state' => 'critical',
14
+ 'entity' => entity_name,
15
+ 'check' => check,
16
+ 'time' => time.to_i,
17
+ 'summary' => "timeout",
18
+ 'details' => "couldn't access",
19
+ 'acknowledgement_id' => '1234',
20
+ 'duration' => (60 * 60) }
21
+ }
22
+
23
+ context 'class' do
24
+
25
+ it "returns the next event (blocking, archiving)" do
26
+ mock_redis.should_receive(:brpoplpush).with('events', /^events_archive:/, 0).and_return(event_data.to_json)
27
+ mock_redis.should_receive(:expire)
28
+
29
+ result = Flapjack::Data::Event.next(:block => true, :archive_events => true, :redis => mock_redis)
30
+ result.should be_an_instance_of(Flapjack::Data::Event)
31
+ end
32
+
33
+ it "returns the next event (blocking, not archiving)" do
34
+ mock_redis.should_receive(:brpop).with('events', 0).and_return(['events', event_data.to_json])
35
+
36
+ result = Flapjack::Data::Event.next(:block => true, :archive_events => false, :redis => mock_redis)
37
+ result.should be_an_instance_of(Flapjack::Data::Event)
38
+ end
39
+
40
+ it "returns the next event (non-blocking, archiving)" do
41
+ mock_redis.should_receive(:rpoplpush).with('events', /^events_archive:/).and_return(event_data.to_json)
42
+ mock_redis.should_receive(:expire)
43
+
44
+ result = Flapjack::Data::Event.next(:block => false, :archive_events => true, :redis => mock_redis)
45
+ result.should be_an_instance_of(Flapjack::Data::Event)
46
+ end
47
+
48
+ it "returns the next event (non-blocking, not archiving)" do
49
+ mock_redis.should_receive(:rpop).with('events').and_return(event_data.to_json)
50
+
51
+ result = Flapjack::Data::Event.next(:block => false, :archive_events => false, :redis => mock_redis)
52
+ result.should be_an_instance_of(Flapjack::Data::Event)
53
+ end
54
+
55
+ it "handles invalid event JSON"
56
+
57
+ it "returns a count of pending events" do
58
+ events_len = 23
59
+ mock_redis.should_receive(:llen).with('events').and_return(events_len)
60
+
61
+ pc = Flapjack::Data::Event.pending_count(:redis => mock_redis)
62
+ pc.should == events_len
63
+ end
64
+
65
+ it "creates a notification testing event" do
66
+ Time.should_receive(:now).and_return(time)
67
+ mock_redis.should_receive(:rpush).with('events', /"testing"/ )
68
+
69
+ Flapjack::Data::Event.test_notifications(entity_name, check,
70
+ :summary => 'test', :details => 'testing', :redis => mock_redis)
71
+ end
72
+
73
+ it "creates an acknowledgement event" do
74
+ Time.should_receive(:now).and_return(time)
75
+ mock_redis.should_receive(:rpush).with('events', /"acking"/ )
76
+
77
+ Flapjack::Data::Event.create_acknowledgement(entity_name, check,
78
+ :summary => 'acking', :time => time.to_i, :redis => mock_redis)
79
+ end
80
+
81
+ end
82
+
83
+ context 'instance' do
84
+ subject { Flapjack::Data::Event.new(event_data) }
85
+
86
+ its(:entity) { should == event_data['entity'] }
87
+ its(:state) { should == event_data['state'] }
88
+ its(:duration) { should == event_data['duration'] }
89
+ its(:time) { should == event_data['time'] }
90
+ its(:id) { should == 'xyz-example.com:ping' }
91
+ its(:client) { should == 'xyz' }
92
+ its(:type) { should == 'service' }
93
+
94
+ it { should be_a_service }
95
+ it { should_not be_a_acknowledgement }
96
+ it { should_not be_a_test_notifications }
97
+ it { should_not be_ok }
98
+ it { should be_a_failure }
99
+ end
5
100
 
6
101
  end
@@ -19,7 +19,6 @@ describe Flapjack::Executive, :logger => true do
19
19
 
20
20
  redis = mock('redis')
21
21
 
22
- #redis.should_receive(:set).with('boot_time', a_kind_of(Integer))
23
22
  redis.should_receive(:hset).with(/^executive_instance:/, "boot_time", anything)
24
23
  redis.should_receive(:hget).with('event_counters', 'all').and_return(nil)
25
24
  redis.should_receive(:hset).with('event_counters', 'all', 0)
@@ -27,17 +26,16 @@ describe Flapjack::Executive, :logger => true do
27
26
  redis.should_receive(:hset).with('event_counters', 'failure', 0)
28
27
  redis.should_receive(:hset).with('event_counters', 'action', 0)
29
28
 
30
- #redis.should_receive(:zadd).with('executive_instances', a_kind_of(Integer), a_kind_of(String))
31
29
  redis.should_receive(:hset).with(/^event_counters:/, 'all', 0)
32
30
  redis.should_receive(:hset).with(/^event_counters:/, 'ok', 0)
33
31
  redis.should_receive(:hset).with(/^event_counters:/, 'failure', 0)
34
32
  redis.should_receive(:hset).with(/^event_counters:/, 'action', 0)
35
33
 
36
- redis.should_receive(:expire).with(/^executive_instance:/, anything).twice
37
- redis.should_receive(:expire).with(/^event_counters:/, anything).exactly(8).times
34
+ redis.should_receive(:expire).with(/^executive_instance:/, anything)
35
+ redis.should_receive(:expire).with(/^event_counters:/, anything).exactly(4).times
38
36
 
39
- redis.should_receive(:hincrby).with('event_counters', 'all', 1)
40
- redis.should_receive(:hincrby).with(/^event_counters:/, 'all', 1)
37
+ # redis.should_receive(:hincrby).with('event_counters', 'all', 1)
38
+ # redis.should_receive(:hincrby).with(/^event_counters:/, 'all', 1)
41
39
 
42
40
  Flapjack::Data::Event.should_receive(:pending_count).with(:redis => redis).and_return(0)
43
41
 
@@ -46,11 +44,7 @@ describe Flapjack::Executive, :logger => true do
46
44
 
47
45
  shutdown_evt = mock(Flapjack::Data::Event)
48
46
  shutdown_evt.should_receive(:inspect)
49
- shutdown_evt.should_receive(:id).and_return('-:-')
50
- shutdown_evt.should_receive(:type).exactly(3).times.and_return('shutdown')
51
- shutdown_evt.should_receive(:state).and_return(nil)
52
- shutdown_evt.should_receive(:summary).and_return(nil)
53
- shutdown_evt.should_receive(:time).and_return(Time.now)
47
+ shutdown_evt.should_receive(:type).and_return('shutdown')
54
48
  Flapjack::Data::Event.should_receive(:next) {
55
49
  executive.instance_variable_set('@should_quit', true)
56
50
  shutdown_evt
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
  require 'flapjack/gateways/api/entity_check_presenter'
3
3
 
4
+ require 'pp'
5
+
4
6
  describe 'Flapjack::Gateways::API::EntityCheck::Presenter' do
5
7
 
6
8
  let(:entity_check) { mock(Flapjack::Data::EntityCheck) }
@@ -53,7 +55,7 @@ describe 'Flapjack::Gateways::API::EntityCheck::Presenter' do
53
55
  # TODO check the data in those hashes
54
56
  end
55
57
 
56
- it "returns the same list of outage hashes with no start and end time set" do
58
+ it "returns a list of outage hashes with no start and end time set" do
57
59
  entity_check.should_receive(:historical_states).
58
60
  with(nil, nil).and_return(states)
59
61
 
@@ -69,6 +71,23 @@ describe 'Flapjack::Gateways::API::EntityCheck::Presenter' do
69
71
  # TODO check the data in those hashes
70
72
  end
71
73
 
74
+ it "returns a consolidated list of outage hashes with repeated state events" do
75
+ states[1][:state] = 'critical'
76
+ states[2][:state] = 'ok'
77
+
78
+ entity_check.should_receive(:historical_states).
79
+ with(nil, nil).and_return(states)
80
+
81
+ entity_check.should_receive(:historical_state_before).
82
+ with(time - (4 * 60 * 60)).and_return(nil)
83
+
84
+ ecp = Flapjack::Gateways::API::EntityCheckPresenter.new(entity_check)
85
+ outages = ecp.outages(nil, nil)
86
+ outages.should_not be_nil
87
+ outages.should be_an(Array)
88
+ outages.should have(3).time_ranges
89
+ end
90
+
72
91
  it "returns a (small) outage hash for a single state change" do
73
92
  entity_check.should_receive(:historical_states).
74
93
  with(nil, nil).and_return([{:state => 'critical', :timestamp => time - (4 * 60 * 60)}])
@@ -90,7 +109,7 @@ describe 'Flapjack::Gateways::API::EntityCheck::Presenter' do
90
109
  with(nil, time - (12 * 60 * 60), :scheduled => false).and_return([])
91
110
 
92
111
  ecp = Flapjack::Gateways::API::EntityCheckPresenter.new(entity_check)
93
- unsched_maint = ecp.unscheduled_maintenance(time - (12 * 60 * 60), time)
112
+ unsched_maint = ecp.unscheduled_maintenances(time - (12 * 60 * 60), time)
94
113
 
95
114
  unsched_maint.should be_an(Array)
96
115
  unsched_maint.should have(4).time_ranges
@@ -106,7 +125,7 @@ describe 'Flapjack::Gateways::API::EntityCheck::Presenter' do
106
125
  with(nil, time - (12 * 60 * 60), :scheduled => true).and_return([])
107
126
 
108
127
  ecp = Flapjack::Gateways::API::EntityCheckPresenter.new(entity_check)
109
- sched_maint = ecp.scheduled_maintenance(time - (12 * 60 * 60), time)
128
+ sched_maint = ecp.scheduled_maintenances(time - (12 * 60 * 60), time)
110
129
 
111
130
  sched_maint.should be_an(Array)
112
131
  sched_maint.should have(4).time_ranges
@@ -17,7 +17,7 @@ describe 'Flapjack::Gateways::API::Entity::Presenter' do
17
17
  let(:end_time) { time - (2 * 60 * 60) }
18
18
 
19
19
  def expect_check_presenters
20
- entity.should_receive(:check_list).and_return(['ssh', 'ping'])
20
+ entity.should_receive(:check_list).and_return(['ping', 'ssh'])
21
21
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
22
22
  with(entity, 'ssh', anything).and_return(check_a)
23
23
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
@@ -29,6 +29,21 @@ describe 'Flapjack::Gateways::API::Entity::Presenter' do
29
29
  with(check_b).and_return(checkpres_b)
30
30
  end
31
31
 
32
+ it 'returns a list of status hashes for each check on an entity' do
33
+ expect_check_presenters
34
+
35
+ status_a = mock('status_a')
36
+ status_b = mock('status_b')
37
+ checkpres_a.should_receive(:status).and_return(status_a)
38
+ checkpres_b.should_receive(:status).and_return(status_b)
39
+
40
+ ep = Flapjack::Gateways::API::EntityPresenter.new(entity)
41
+ status = ep.status
42
+ status.should == [{:entity => entity, :check => 'ping', :status => status_b},
43
+ {:entity => entity, :check => 'ssh', :status => status_a}]
44
+
45
+ end
46
+
32
47
  it "returns a list of outage hashes for each check on an entity" do
33
48
  expect_check_presenters
34
49
  outages_a = mock('outages_a')
@@ -40,38 +55,38 @@ describe 'Flapjack::Gateways::API::Entity::Presenter' do
40
55
 
41
56
  ep = Flapjack::Gateways::API::EntityPresenter.new(entity)
42
57
  outages = ep.outages(start_time, end_time)
43
- outages.should == [{:check => 'ssh', :outages => outages_a},
44
- {:check => 'ping', :outages => outages_b}]
58
+ outages.should == [{:entity => entity, :check => 'ping', :outages => outages_b},
59
+ {:entity => entity, :check => 'ssh', :outages => outages_a}]
45
60
  end
46
61
 
47
62
  it "returns a list of unscheduled maintenance periods for each check on an entity" do
48
63
  expect_check_presenters
49
64
  unsched_maint_a = mock('unsched_maint_a')
50
65
  unsched_maint_b = mock('unsched_maint_b')
51
- checkpres_a.should_receive(:unscheduled_maintenance).with(start_time, end_time).
66
+ checkpres_a.should_receive(:unscheduled_maintenances).with(start_time, end_time).
52
67
  and_return(unsched_maint_a)
53
- checkpres_b.should_receive(:unscheduled_maintenance).with(start_time, end_time).
68
+ checkpres_b.should_receive(:unscheduled_maintenances).with(start_time, end_time).
54
69
  and_return(unsched_maint_b)
55
70
 
56
71
  ep = Flapjack::Gateways::API::EntityPresenter.new(entity)
57
- unsched_maint = ep.unscheduled_maintenance(start_time, end_time)
58
- unsched_maint.should == [{:check => 'ssh', :unscheduled_maintenance => unsched_maint_a},
59
- {:check => 'ping', :unscheduled_maintenance => unsched_maint_b}]
72
+ unsched_maint = ep.unscheduled_maintenances(start_time, end_time)
73
+ unsched_maint.should == [{:entity => entity, :check => 'ping', :unscheduled_maintenances => unsched_maint_b},
74
+ {:entity => entity, :check => 'ssh', :unscheduled_maintenances => unsched_maint_a}]
60
75
  end
61
76
 
62
77
  it "returns a list of scheduled maintenance periods for each check on an entity" do
63
78
  expect_check_presenters
64
79
  sched_maint_a = mock('sched_maint_a')
65
80
  sched_maint_b = mock('sched_maint_b')
66
- checkpres_a.should_receive(:scheduled_maintenance).with(start_time, end_time).
81
+ checkpres_a.should_receive(:scheduled_maintenances).with(start_time, end_time).
67
82
  and_return(sched_maint_a)
68
- checkpres_b.should_receive(:scheduled_maintenance).with(start_time, end_time).
83
+ checkpres_b.should_receive(:scheduled_maintenances).with(start_time, end_time).
69
84
  and_return(sched_maint_b)
70
85
 
71
86
  ep = Flapjack::Gateways::API::EntityPresenter.new(entity)
72
- sched_maint = ep.scheduled_maintenance(start_time, end_time)
73
- sched_maint.should == [{:check => 'ssh', :scheduled_maintenance => sched_maint_a},
74
- {:check => 'ping', :scheduled_maintenance => sched_maint_b}]
87
+ sched_maint = ep.scheduled_maintenances(start_time, end_time)
88
+ sched_maint.should == [{:entity => entity, :check => 'ping', :scheduled_maintenances => sched_maint_b},
89
+ {:entity => entity, :check => 'ssh', :scheduled_maintenances => sched_maint_a}]
75
90
  end
76
91
 
77
92
  it "returns a list of downtime for each check on an entity" do
@@ -85,8 +100,8 @@ describe 'Flapjack::Gateways::API::Entity::Presenter' do
85
100
 
86
101
  ep = Flapjack::Gateways::API::EntityPresenter.new(entity)
87
102
  downtime = ep.downtime(start_time, end_time)
88
- downtime.should == [{:check => 'ssh', :downtime => downtime_a},
89
- {:check => 'ping', :downtime => downtime_b}]
103
+ downtime.should == [{:entity => entity, :check => 'ping', :downtime => downtime_b},
104
+ {:entity => entity, :check => 'ssh', :downtime => downtime_a}]
90
105
  end
91
106
 
92
107
  end
@@ -83,88 +83,304 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
83
83
  last_response.body.should == [check].to_json
84
84
  end
85
85
 
86
+ context 'old API calling format' do
87
+
88
+ it "returns the status for all checks on an entity" do
89
+ status = mock('status', :to_json => 'status!'.to_json)
90
+ result = {:entity => entity_name, :check => check, :status => status}
91
+ entity_presenter.should_receive(:status).and_return(result)
92
+
93
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
94
+ with(entity, :redis => redis).and_return(entity_presenter)
95
+
96
+ Flapjack::Data::Entity.should_receive(:find_by_name).
97
+ with(entity_name, :redis => redis).and_return(entity)
98
+
99
+ get "/status/#{entity_name_esc}"
100
+ last_response.should be_ok
101
+ last_response.body.should == ['status!'].to_json
102
+ end
103
+
104
+ it "should not show the status for an entity that's not found" do
105
+ Flapjack::Data::Entity.should_receive(:find_by_name).
106
+ with(entity_name, :redis => redis).and_return(nil)
107
+
108
+ get "/status/#{entity_name_esc}"
109
+ last_response.should be_forbidden
110
+ end
111
+
112
+ it "returns the status for a check on an entity" do
113
+ status = mock('status', :to_json => 'status!'.to_json)
114
+ entity_check_presenter.should_receive(:status).and_return(status)
115
+
116
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
117
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
118
+
119
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
120
+ with(entity, check, :redis => redis).and_return(entity_check)
121
+
122
+ Flapjack::Data::Entity.should_receive(:find_by_name).
123
+ with(entity_name, :redis => redis).and_return(entity)
124
+
125
+ get "/status/#{entity_name_esc}/#{check}"
126
+ last_response.should be_ok
127
+ last_response.body.should == 'status!'.to_json
128
+ end
129
+
130
+ it "should not show the status for a check on an entity that's not found" do
131
+ Flapjack::Data::Entity.should_receive(:find_by_name).
132
+ with(entity_name, :redis => redis).and_return(nil)
133
+
134
+ get "/status/#{entity_name_esc}/#{check}"
135
+ last_response.should be_forbidden
136
+ end
137
+
138
+ it "should not show the status for a check that's not found on an entity" do
139
+ Flapjack::Data::Entity.should_receive(:find_by_name).
140
+ with(entity_name, :redis => redis).and_return(entity)
141
+
142
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
143
+ with(entity, check, :redis => redis).and_return(nil)
144
+
145
+ get "/status/#{entity_name_esc}/#{check}"
146
+ last_response.should be_forbidden
147
+ end
148
+
149
+ it "returns a list of scheduled maintenance periods for an entity" do
150
+ sched = mock('sched', :to_json => 'sched!'.to_json)
151
+ result = {:entity => entity_name, :check => check, :scheduled_maintenances => sched}
152
+ entity_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(result)
153
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
154
+ with(entity, :redis => redis).and_return(entity_presenter)
155
+ Flapjack::Data::Entity.should_receive(:find_by_name).
156
+ with(entity_name, :redis => redis).and_return(entity)
157
+
158
+ get "/scheduled_maintenances/#{entity_name_esc}"
159
+ last_response.should be_ok
160
+ last_response.body.should == [{:check => check, :scheduled_maintenance => sched}].to_json
161
+ end
162
+
163
+ it "returns a list of scheduled maintenance periods within a time window for an entity" do
164
+ start = Time.parse('1 Jan 2012')
165
+ finish = Time.parse('6 Jan 2012')
166
+
167
+ sched = mock('sched', :to_json => 'sched!'.to_json)
168
+ result = {:entity => entity_name, :check => check, :scheduled_maintenances => sched}
169
+ entity_presenter.should_receive(:scheduled_maintenances).with(start.to_i, finish.to_i).and_return(result)
170
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
171
+ with(entity, :redis => redis).and_return(entity_presenter)
172
+ Flapjack::Data::Entity.should_receive(:find_by_name).
173
+ with(entity_name, :redis => redis).and_return(entity)
174
+
175
+ get "/scheduled_maintenances/#{entity_name_esc}?" +
176
+ "start_time=#{CGI.escape(start.iso8601)}&end_time=#{CGI.escape(finish.iso8601)}"
177
+ last_response.should be_ok
178
+ last_response.body.should == [{:check => check, :scheduled_maintenance => sched}].to_json
179
+ end
180
+
181
+ it "returns a list of scheduled maintenance periods for a check on an entity" do
182
+ sched = mock('sched', :to_json => 'sched!'.to_json)
183
+ entity_check_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(sched)
184
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
185
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
186
+ Flapjack::Data::Entity.should_receive(:find_by_name).
187
+ with(entity_name, :redis => redis).and_return(entity)
188
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
189
+ with(entity, check, :redis => redis).and_return(entity_check)
190
+
191
+ get "/scheduled_maintenances/#{entity_name_esc}/#{check}"
192
+ last_response.should be_ok
193
+ last_response.body.should == 'sched!'.to_json
194
+ end
195
+
196
+ it "creates an acknowledgement for an entity check" do
197
+ entity_check.should_receive(:entity_name).and_return(entity_name)
198
+ entity_check.should_receive(:check).and_return(check)
199
+
200
+ Flapjack::Data::Entity.should_receive(:find_by_name).
201
+ with(entity_name, :redis => redis).and_return(entity)
202
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
203
+ with(entity, check, :redis => redis).and_return(entity_check)
204
+ Flapjack::Data::Event.should_receive(:create_acknowledgement).
205
+ with(entity_name, check, :summary => nil, :duration => (4 * 60 * 60), :redis => redis)
206
+
207
+ post "/acknowledgements/#{entity_name_esc}/#{check}"
208
+ last_response.status.should == 204
209
+ end
210
+
211
+ it "returns a list of unscheduled maintenance periods for an entity" do
212
+ unsched = mock('unsched', :to_json => 'unsched!'.to_json)
213
+ result = {:entity => entity_name, :check => check, :unscheduled_maintenances => unsched}
214
+ entity_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(result)
215
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
216
+ with(entity, :redis => redis).and_return(entity_presenter)
217
+ Flapjack::Data::Entity.should_receive(:find_by_name).
218
+ with(entity_name, :redis => redis).and_return(entity)
219
+
220
+ get "/unscheduled_maintenances/#{entity_name_esc}"
221
+ last_response.should be_ok
222
+ last_response.body.should == [{:check => check, :unscheduled_maintenance => unsched}].to_json
223
+ end
224
+
225
+ it "returns a list of unscheduled maintenance periods for a check on an entity" do
226
+ unsched = mock('unsched', :to_json => 'unsched!'.to_json)
227
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(unsched)
228
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
229
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
230
+ Flapjack::Data::Entity.should_receive(:find_by_name).
231
+ with(entity_name, :redis => redis).and_return(entity)
232
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
233
+ with(entity, check, :redis => redis).and_return(entity_check)
234
+
235
+ get "/unscheduled_maintenances/#{entity_name_esc}/#{check}"
236
+ last_response.should be_ok
237
+ last_response.body.should == 'unsched!'.to_json
238
+ end
239
+
240
+ it "returns a list of unscheduled maintenance periods within a time window for a check an entity" do
241
+ start = Time.parse('1 Jan 2012')
242
+ finish = Time.parse('6 Jan 2012')
243
+
244
+ unsched = mock('unsched', :to_json => 'unsched!'.to_json)
245
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(start.to_i, finish.to_i).and_return(unsched)
246
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
247
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
248
+ Flapjack::Data::Entity.should_receive(:find_by_name).
249
+ with(entity_name, :redis => redis).and_return(entity)
250
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
251
+ with(entity, check, :redis => redis).and_return(entity_check)
252
+
253
+ get "/unscheduled_maintenances/#{entity_name_esc}/#{check}" +
254
+ "?start_time=#{CGI.escape(start.iso8601)}&end_time=#{CGI.escape(finish.iso8601)}"
255
+ last_response.should be_ok
256
+ last_response.body.should == 'unsched!'.to_json
257
+ end
258
+
259
+ it "returns a list of outages for an entity" do
260
+ out = mock('out', :to_json => 'out!'.to_json)
261
+ result = {:entity => entity_name, :check => check, :outages => out}
262
+ entity_presenter.should_receive(:outages).with(nil, nil).and_return(result)
263
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
264
+ with(entity, :redis => redis).and_return(entity_presenter)
265
+ Flapjack::Data::Entity.should_receive(:find_by_name).
266
+ with(entity_name, :redis => redis).and_return(entity)
267
+
268
+ get "/outages/#{entity_name_esc}"
269
+ last_response.should be_ok
270
+ last_response.body.should == [{:check => check, :outages => out}].to_json
271
+ end
272
+
273
+ it "returns a list of outages for a check on an entity" do
274
+ out = mock('out', :to_json => 'out!'.to_json)
275
+ entity_check_presenter.should_receive(:outages).with(nil, nil).and_return(out)
276
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
277
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
278
+ Flapjack::Data::Entity.should_receive(:find_by_name).
279
+ with(entity_name, :redis => redis).and_return(entity)
280
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
281
+ with(entity, check, :redis => redis).and_return(entity_check)
282
+
283
+ get "/outages/#{entity_name_esc}/#{check}"
284
+ last_response.should be_ok
285
+ last_response.body.should == 'out!'.to_json
286
+ end
287
+
288
+ it "returns a list of downtimes for an entity" do
289
+ down = mock('down', :to_json => 'down!'.to_json)
290
+ result = {:entity => entity_name, :check => check, :downtime => down}
291
+ entity_presenter.should_receive(:downtime).with(nil, nil).and_return(result)
292
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
293
+ with(entity, :redis => redis).and_return(entity_presenter)
294
+ Flapjack::Data::Entity.should_receive(:find_by_name).
295
+ with(entity_name, :redis => redis).and_return(entity)
296
+
297
+ get "/downtime/#{entity_name_esc}"
298
+ last_response.should be_ok
299
+ last_response.body.should == [{:check => check, :downtime => down}].to_json
300
+ end
301
+
302
+ it "returns a list of downtimes for a check on an entity" do
303
+ down = mock('down', :to_json => 'down!'.to_json)
304
+ entity_check_presenter.should_receive(:downtime).with(nil, nil).and_return(down)
305
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
306
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
307
+ Flapjack::Data::Entity.should_receive(:find_by_name).
308
+ with(entity_name, :redis => redis).and_return(entity)
309
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
310
+ with(entity, check, :redis => redis).and_return(entity_check)
311
+
312
+ get "/downtime/#{entity_name_esc}/#{check}"
313
+ last_response.should be_ok
314
+ last_response.body.should == 'down!'.to_json
315
+ end
316
+
317
+ it "creates a test notification event for check on an entity" do
318
+ Flapjack::Data::Entity.should_receive(:find_by_name).
319
+ with(entity_name, :redis => redis).and_return(entity)
320
+ entity.should_receive(:name).and_return(entity_name)
321
+ entity_check.should_receive(:entity).and_return(entity)
322
+ entity_check.should_receive(:entity_name).and_return(entity_name)
323
+ entity_check.should_receive(:check).and_return('foo')
324
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
325
+ with(entity, 'foo', :redis => redis).and_return(entity_check)
326
+
327
+ Flapjack::Data::Event.should_receive(:test_notifications).
328
+ with(entity_name, 'foo', hash_including(:redis => redis))
329
+
330
+ post "/test_notifications/#{entity_name_esc}/foo"
331
+ last_response.status.should == 204
332
+ end
333
+
334
+ end
335
+
86
336
  it "returns the status for all checks on an entity" do
87
- entity.should_receive(:check_list).and_return([check])
337
+ status = mock('status')
338
+ result = [{:entity => entity_name, :check => check, :status => status}]
339
+ entity_presenter.should_receive(:status).and_return(result)
340
+
341
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
342
+ with(entity, :redis => redis).and_return(entity_presenter)
343
+
88
344
  Flapjack::Data::Entity.should_receive(:find_by_name).
89
345
  with(entity_name, :redis => redis).and_return(entity)
90
346
 
91
- now = Time.now.to_i
92
-
93
- entity_check.should_receive(:state).and_return('OK')
94
- entity_check.should_receive(:summary).and_return('not bad')
95
- entity_check.should_receive(:details).and_return(nil)
96
- entity_check.should_receive(:in_unscheduled_maintenance?).and_return(false)
97
- entity_check.should_receive(:in_scheduled_maintenance?).and_return(false)
98
- entity_check.should_receive(:last_update).and_return(now - 30)
99
- entity_check.should_receive(:last_problem_notification).and_return(now - 60)
100
- entity_check.should_receive(:last_recovery_notification).and_return(now - 30)
101
- entity_check.should_receive(:last_acknowledgement_notification).and_return(now - 45)
102
- Flapjack::Data::EntityCheck.should_receive(:for_entity).
103
- with(entity, check, :redis => redis).and_return(entity_check)
104
-
105
- get "/status/#{entity_name_esc}"
106
- last_response.should be_ok
107
- last_response.body.should == [{'name' => check,
108
- 'state' => 'OK',
109
- 'summary' => 'not bad',
110
- 'details' => nil,
111
- 'in_unscheduled_maintenance' => false,
112
- 'in_scheduled_maintenance' => false,
113
- 'last_update' => (now - 30),
114
- 'last_problem_notification' => (now - 60),
115
- 'last_recovery_notification' => (now - 30),
116
- 'last_acknowledgement_notification' => (now - 45)
117
- }].to_json
347
+ get "/status", :entity => entity_name
348
+ last_response.body.should == result.to_json
118
349
  end
119
350
 
120
351
  it "should not show the status for an entity that's not found" do
121
352
  Flapjack::Data::Entity.should_receive(:find_by_name).
122
353
  with(entity_name, :redis => redis).and_return(nil)
123
354
 
124
- get "/status/#{entity_name_esc}"
125
- last_response.should be_not_found
355
+ get "/status", :entity => entity_name
356
+ last_response.should be_forbidden
126
357
  end
127
358
 
128
- it "returns the status for a check (with non-word characters) on an entity" do
129
- nw_check = "HTTP Port 443"
130
- Flapjack::Data::Entity.should_receive(:find_by_name).
131
- with(entity_name, :redis => redis).and_return(entity)
359
+ it "returns the status for a check on an entity" do
360
+ status = mock('status')
361
+ result = [{:entity => entity_name, :check => check, :status => status}]
362
+ entity_check_presenter.should_receive(:status).and_return(status)
363
+
364
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
365
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
132
366
 
133
- now = Time.now.to_i
134
-
135
- entity_check.should_receive(:state).and_return('OK')
136
- entity_check.should_receive(:summary).and_return('not bad')
137
- entity_check.should_receive(:details).and_return(nil)
138
- entity_check.should_receive(:in_unscheduled_maintenance?).and_return(false)
139
- entity_check.should_receive(:in_scheduled_maintenance?).and_return(false)
140
- entity_check.should_receive(:last_update).and_return(now - 30)
141
- entity_check.should_receive(:last_problem_notification).and_return(now - 60)
142
- entity_check.should_receive(:last_recovery_notification).and_return(now - 30)
143
- entity_check.should_receive(:last_acknowledgement_notification).and_return(now - 45)
144
367
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
145
- with(entity, nw_check, :redis => redis).and_return(entity_check)
368
+ with(entity, check, :redis => redis).and_return(entity_check)
369
+
370
+ Flapjack::Data::Entity.should_receive(:find_by_name).
371
+ with(entity_name, :redis => redis).and_return(entity)
146
372
 
147
- get "/status/#{entity_name_esc}/#{URI.escape(nw_check)}"
373
+ get "/status", :check => {entity_name => check}
148
374
  last_response.should be_ok
149
- last_response.body.should == {'name' => nw_check,
150
- 'state' => 'OK',
151
- 'summary' => 'not bad',
152
- 'details' => nil,
153
- 'in_unscheduled_maintenance' => false,
154
- 'in_scheduled_maintenance' => false,
155
- 'last_update' => (now - 30),
156
- 'last_problem_notification' => (now - 60),
157
- 'last_recovery_notification' => (now - 30),
158
- 'last_acknowledgement_notification' => (now - 45)
159
- }.to_json
375
+ last_response.body.should == result.to_json
160
376
  end
161
377
 
162
378
  it "should not show the status for a check on an entity that's not found" do
163
379
  Flapjack::Data::Entity.should_receive(:find_by_name).
164
380
  with(entity_name, :redis => redis).and_return(nil)
165
381
 
166
- get "/status/#{entity_name_esc}/#{check}"
167
- last_response.should be_not_found
382
+ get "/status", :check => {entity_name => check}
383
+ last_response.should be_forbidden
168
384
  end
169
385
 
170
386
  it "should not show the status for a check that's not found on an entity" do
@@ -174,37 +390,41 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
174
390
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
175
391
  with(entity, check, :redis => redis).and_return(nil)
176
392
 
177
- get "/status/#{entity_name_esc}/#{check}"
178
- last_response.should be_not_found
393
+ get "/status", :check => {entity_name => check}
394
+ last_response.should be_forbidden
179
395
  end
180
396
 
181
- it "returns a list of scheduled maintenance periods for an entity" do
182
- result = mock('result')
183
- result_json = %q{"result"}
184
- result.should_receive(:to_json).and_return(result_json)
185
- entity_presenter.should_receive(:scheduled_maintenance).with(nil, nil).and_return(result)
186
- Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
187
- with(entity, :redis => redis).and_return(entity_presenter)
397
+ it "creates an acknowledgement for an entity check" do
188
398
  Flapjack::Data::Entity.should_receive(:find_by_name).
189
399
  with(entity_name, :redis => redis).and_return(entity)
400
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
401
+ with(entity, check, :redis => redis).and_return(entity_check)
190
402
 
191
- get "/scheduled_maintenances/#{entity_name_esc}"
192
- last_response.should be_ok
193
- last_response.body.should == result_json
403
+ entity_check.should_receive(:entity_name).and_return(entity_name)
404
+ entity_check.should_receive(:check).and_return(check)
405
+
406
+ Flapjack::Data::Event.should_receive(:create_acknowledgement).
407
+ with(entity_name, check, :summary => nil, :duration => (4 * 60 * 60), :redis => redis)
408
+
409
+ post '/acknowledgements',:check => {entity_name => check}
410
+ last_response.status.should == 204
194
411
  end
195
412
 
196
- it "creates an acknowledgement for an entity check" do
197
- Flapjack::Data::Entity.should_receive(:find_by_name).
198
- with(entity_name, :redis => redis).and_return(entity)
413
+ it "deletes an unscheduled maintenance period for an entity check" do
414
+ end_time = Time.now + (60 * 60) # an hour from now
415
+ entity_check.should_receive(:end_unscheduled_maintenance).with(:end_time => end_time.to_i)
416
+
199
417
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
200
418
  with(entity, check, :redis => redis).and_return(entity_check)
201
- entity_check.should_receive(:create_acknowledgement).with('summary' => nil, 'duration' => (4 * 60 * 60))
202
419
 
203
- post "/acknowledgements/#{entity_name_esc}/#{check}"
420
+ Flapjack::Data::Entity.should_receive(:find_by_name).
421
+ with(entity_name, :redis => redis).and_return(entity)
422
+
423
+ delete "/unscheduled_maintenances", :check => {entity_name => check}, :end_time => end_time.iso8601
204
424
  last_response.status.should == 204
205
425
  end
206
426
 
207
- it "creates a scheduled maintenance for an entity check" do
427
+ it "creates a scheduled maintenance period for an entity check" do
208
428
  start = Time.now + (60 * 60) # an hour from now
209
429
  duration = (2 * 60 * 60) # two hours
210
430
  Flapjack::Data::Entity.should_receive(:find_by_name).
@@ -219,169 +439,308 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
219
439
  last_response.status.should == 204
220
440
  end
221
441
 
442
+ it "deletes a scheduled maintenance period for an entity check" do
443
+ start_time = Time.now + (60 * 60) # an hour from now
444
+ entity_check.should_receive(:delete_scheduled_maintenance).with(:start_time => start_time.to_i)
445
+
446
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
447
+ with(entity, check, :redis => redis).and_return(entity_check)
448
+
449
+ Flapjack::Data::Entity.should_receive(:find_by_name).
450
+ with(entity_name, :redis => redis).and_return(entity)
451
+
452
+ delete "/scheduled_maintenances", :check => {entity_name => check}, :start_time => start_time.iso8601
453
+ last_response.status.should == 204
454
+ end
455
+
456
+ it "deletes scheduled maintenance periods for multiple entity checks" do
457
+ start_time = Time.now + (60 * 60) # an hour from now
458
+
459
+ entity_check_2 = mock(Flapjack::Data::EntityCheck)
460
+
461
+ entity_check.should_receive(:delete_scheduled_maintenance).with(:start_time => start_time.to_i)
462
+ entity_check_2.should_receive(:delete_scheduled_maintenance).with(:start_time => start_time.to_i)
463
+
464
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
465
+ with(entity, check, :redis => redis).and_return(entity_check)
466
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
467
+ with(entity, 'foo', :redis => redis).and_return(entity_check_2)
468
+
469
+ Flapjack::Data::Entity.should_receive(:find_by_name).
470
+ with(entity_name, :redis => redis).and_return(entity)
471
+
472
+ delete "/scheduled_maintenances", :check => {entity_name => [check, 'foo']}, :start_time => start_time.iso8601
473
+ last_response.status.should == 204
474
+ end
475
+
476
+ it "returns a list of scheduled maintenance periods for an entity" do
477
+ sm = mock('sched_maint')
478
+ result = [{:entity => entity_name, :check => check, :scheduled_maintenances => sm}]
479
+
480
+ entity_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(result)
481
+
482
+ Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
483
+ with(entity, :redis => redis).and_return(entity_presenter)
484
+
485
+ Flapjack::Data::Entity.should_receive(:find_by_name).
486
+ with(entity_name, :redis => redis).and_return(entity)
487
+
488
+ get "/scheduled_maintenances", :entity => entity_name
489
+ last_response.should be_ok
490
+ last_response.body.should == result.to_json
491
+ end
492
+
222
493
  it "returns a list of scheduled maintenance periods within a time window for an entity" do
223
494
  start = Time.parse('1 Jan 2012')
224
495
  finish = Time.parse('6 Jan 2012')
225
496
 
226
- result = mock('result')
227
- result_json = %q{"result"}
228
- result.should_receive(:to_json).and_return(result_json)
229
- entity_presenter.should_receive(:scheduled_maintenance).with(start.to_i, finish.to_i).and_return(result)
497
+ sm = mock('sched_maint')
498
+ result = [{:entity => entity_name, :check => check, :scheduled_maintenances => sm}]
499
+
500
+ entity_presenter.should_receive(:scheduled_maintenances).with(start.to_i, finish.to_i).and_return(result)
501
+
230
502
  Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
231
503
  with(entity, :redis => redis).and_return(entity_presenter)
504
+
232
505
  Flapjack::Data::Entity.should_receive(:find_by_name).
233
506
  with(entity_name, :redis => redis).and_return(entity)
234
507
 
235
- get "/scheduled_maintenances/#{entity_name_esc}?" +
236
- "start_time=#{CGI.escape(start.iso8601)}&end_time=#{CGI.escape(finish.iso8601)}"
508
+ get "/scheduled_maintenances", :entity => entity_name,
509
+ :start_time => start.iso8601, :end_time => finish.iso8601
237
510
  last_response.should be_ok
238
- last_response.body.should == result_json
511
+ last_response.body.should == result.to_json
239
512
  end
240
513
 
241
514
  it "returns a list of scheduled maintenance periods for a check on an entity" do
242
- result = mock('result')
243
- result_json = %q{"result"}
244
- result.should_receive(:to_json).and_return(result_json)
245
- entity_check_presenter.should_receive(:scheduled_maintenance).with(nil, nil).and_return(result)
515
+ sm = mock('sched_maint')
516
+ result = [{:entity => entity_name, :check => check, :scheduled_maintenances => sm}]
517
+
518
+ entity_check_presenter.should_receive(:scheduled_maintenances).with(nil, nil).and_return(sm)
519
+
246
520
  Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
247
- with(entity_check).and_return(entity_check_presenter)
248
- Flapjack::Data::Entity.should_receive(:find_by_name).
249
- with(entity_name, :redis => redis).and_return(entity)
521
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
522
+
250
523
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
251
524
  with(entity, check, :redis => redis).and_return(entity_check)
252
525
 
253
- get "/scheduled_maintenances/#{entity_name_esc}/#{check}"
526
+ Flapjack::Data::Entity.should_receive(:find_by_name).
527
+ with(entity_name, :redis => redis).and_return(entity)
528
+
529
+ get "/scheduled_maintenances", :check => {entity_name => check}
254
530
  last_response.should be_ok
255
- last_response.body.should == result_json
531
+ last_response.body.should == result.to_json
256
532
  end
257
533
 
258
534
  it "returns a list of unscheduled maintenance periods for an entity" do
259
- result = mock('result')
260
- result_json = %q{"result"}
261
- result.should_receive(:to_json).and_return(result_json)
262
- entity_presenter.should_receive(:unscheduled_maintenance).with(nil, nil).and_return(result)
535
+ um = mock('unsched_maint')
536
+ result = [{:entity => entity_name, :check => check, :unscheduled_maintenances => um}]
537
+
538
+ entity_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(result)
539
+
263
540
  Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
264
541
  with(entity, :redis => redis).and_return(entity_presenter)
542
+
265
543
  Flapjack::Data::Entity.should_receive(:find_by_name).
266
544
  with(entity_name, :redis => redis).and_return(entity)
267
545
 
268
- get "/unscheduled_maintenances/#{entity_name_esc}"
546
+ get "/unscheduled_maintenances", :entity => entity_name
269
547
  last_response.should be_ok
270
- last_response.body.should == result_json
548
+ last_response.body.should == result.to_json
271
549
  end
272
550
 
273
551
  it "returns a list of unscheduled maintenance periods for a check on an entity" do
274
- result = mock('result')
275
- result_json = %q{"result"}
276
- result.should_receive(:to_json).and_return(result_json)
277
- entity_check_presenter.should_receive(:unscheduled_maintenance).with(nil, nil).and_return(result)
552
+ um = mock('unsched_maint')
553
+ result = [{:entity => entity_name, :check => check, :unscheduled_maintenances => um}]
554
+
555
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(nil, nil).and_return(um)
556
+
278
557
  Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
279
- with(entity_check).and_return(entity_check_presenter)
280
- Flapjack::Data::Entity.should_receive(:find_by_name).
281
- with(entity_name, :redis => redis).and_return(entity)
558
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
559
+
282
560
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
283
561
  with(entity, check, :redis => redis).and_return(entity_check)
284
562
 
285
- get "/unscheduled_maintenances/#{entity_name_esc}/#{check}"
563
+ Flapjack::Data::Entity.should_receive(:find_by_name).
564
+ with(entity_name, :redis => redis).and_return(entity)
565
+
566
+ get "/unscheduled_maintenances", :check => {entity_name => check}
286
567
  last_response.should be_ok
287
- last_response.body.should == result_json
568
+ last_response.body.should == result.to_json
288
569
  end
289
570
 
290
571
  it "returns a list of unscheduled maintenance periods within a time window for a check an entity" do
291
- start = Time.parse('1 Jan 2012')
292
- finish = Time.parse('6 Jan 2012')
572
+ start = Time.parse('1 Jan 2012')
573
+ finish = Time.parse('6 Jan 2012')
574
+
575
+ um = mock('unsched_maint')
576
+ result = [{:entity => entity_name, :check => check, :unscheduled_maintenances => um}]
577
+
578
+ entity_check_presenter.should_receive(:unscheduled_maintenances).with(start.to_i, finish.to_i).and_return(um)
293
579
 
294
- result = mock('result')
295
- result_json = %q{"result"}
296
- result.should_receive(:to_json).and_return(result_json)
297
- entity_check_presenter.should_receive(:unscheduled_maintenance).with(start.to_i, finish.to_i).and_return(result)
298
580
  Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
299
- with(entity_check).and_return(entity_check_presenter)
300
- Flapjack::Data::Entity.should_receive(:find_by_name).
301
- with(entity_name, :redis => redis).and_return(entity)
581
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
582
+
302
583
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
303
584
  with(entity, check, :redis => redis).and_return(entity_check)
304
585
 
305
- get "/unscheduled_maintenances/#{entity_name_esc}/#{check}" +
306
- "?start_time=#{CGI.escape(start.iso8601)}&end_time=#{CGI.escape(finish.iso8601)}"
586
+ Flapjack::Data::Entity.should_receive(:find_by_name).
587
+ with(entity_name, :redis => redis).and_return(entity)
588
+
589
+ get "/unscheduled_maintenances", :check => {entity_name => check},
590
+ :start_time => start.iso8601, :end_time => finish.iso8601
307
591
  last_response.should be_ok
308
- last_response.body.should == result_json
592
+ last_response.body.should == result.to_json
309
593
  end
310
594
 
311
- it "returns a list of outages for an entity" do
312
- result = mock('result')
313
- result_json = %q{"result"}
314
- result.should_receive(:to_json).and_return(result_json)
315
- entity_presenter.should_receive(:outages).with(nil, nil).and_return(result)
595
+ it "returns a list of outages, for one whole entity and two checks on another entity" do
596
+ outages_1 = mock('outages_1')
597
+ outages_2 = mock('outages_2')
598
+ outages_3 = mock('outages_3')
599
+
600
+ entity_2_name = 'entity_2'
601
+ entity_2 = mock(Flapjack::Data::Entity)
602
+
603
+ result = [{:entity => entity_name, :check => check, :outages => outages_1},
604
+ {:entity => entity_2_name, :check => 'foo', :outages => outages_2},
605
+ {:entity => entity_2_name, :check => 'bar', :outages => outages_3}]
606
+
607
+ foo_check = mock(Flapjack::Data::EntityCheck)
608
+ bar_check = mock(Flapjack::Data::EntityCheck)
609
+
610
+ foo_check_presenter = mock(Flapjack::Gateways::API::EntityCheckPresenter)
611
+ bar_check_presenter = mock(Flapjack::Gateways::API::EntityCheckPresenter)
612
+
613
+ entity_presenter.should_receive(:outages).with(nil, nil).and_return(result[0])
614
+ foo_check_presenter.should_receive(:outages).with(nil, nil).and_return(outages_2)
615
+ bar_check_presenter.should_receive(:outages).with(nil, nil).and_return(outages_3)
616
+
316
617
  Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
317
618
  with(entity, :redis => redis).and_return(entity_presenter)
619
+
620
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
621
+ with(foo_check, :redis => redis).and_return(foo_check_presenter)
622
+ Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
623
+ with(bar_check, :redis => redis).and_return(bar_check_presenter)
624
+
318
625
  Flapjack::Data::Entity.should_receive(:find_by_name).
319
626
  with(entity_name, :redis => redis).and_return(entity)
627
+ Flapjack::Data::Entity.should_receive(:find_by_name).
628
+ with(entity_2_name, :redis => redis).and_return(entity_2)
629
+
630
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
631
+ with(entity_2, 'foo', :redis => redis).and_return(foo_check)
632
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
633
+ with(entity_2, 'bar', :redis => redis).and_return(bar_check)
320
634
 
321
- get "/outages/#{entity_name_esc}"
635
+ get "/outages", :entity => entity_name, :check => {entity_2_name => ['foo', 'bar']}
322
636
  last_response.should be_ok
323
- last_response.body.should == result_json
637
+ last_response.body.should == result.to_json
324
638
  end
325
639
 
326
640
  it "returns a list of outages for a check on an entity" do
327
- result = mock('result')
328
- result_json = %q{"result"}
329
- result.should_receive(:to_json).and_return(result_json)
330
- entity_check_presenter.should_receive(:outages).with(nil, nil).and_return(result)
641
+ outages = mock('outages')
642
+ result = [{:entity => entity_name, :check => check, :outages => outages}]
643
+
644
+ entity_check_presenter.should_receive(:outages).with(nil, nil).and_return(outages)
645
+
331
646
  Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
332
- with(entity_check).and_return(entity_check_presenter)
333
- Flapjack::Data::Entity.should_receive(:find_by_name).
334
- with(entity_name, :redis => redis).and_return(entity)
647
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
648
+
335
649
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
336
650
  with(entity, check, :redis => redis).and_return(entity_check)
337
651
 
338
- get "/outages/#{entity_name_esc}/#{check}"
652
+ Flapjack::Data::Entity.should_receive(:find_by_name).
653
+ with(entity_name, :redis => redis).and_return(entity)
654
+
655
+ get "/outages", :check => {entity_name => check}
339
656
  last_response.should be_ok
340
- last_response.body.should == result_json
657
+ last_response.body.should == result.to_json
341
658
  end
342
659
 
343
660
  it "returns a list of downtimes for an entity" do
344
- result = mock('result')
345
- result_json = %q{"result"}
346
- result.should_receive(:to_json).and_return(result_json)
661
+ downtime = mock('downtime')
662
+ result = [{:entity => entity_name, :check => check, :downtime => downtime}]
663
+
347
664
  entity_presenter.should_receive(:downtime).with(nil, nil).and_return(result)
665
+
348
666
  Flapjack::Gateways::API::EntityPresenter.should_receive(:new).
349
667
  with(entity, :redis => redis).and_return(entity_presenter)
668
+
350
669
  Flapjack::Data::Entity.should_receive(:find_by_name).
351
670
  with(entity_name, :redis => redis).and_return(entity)
352
671
 
353
- get "/downtime/#{entity_name_esc}"
672
+ get "/downtime", :entity => entity_name
354
673
  last_response.should be_ok
355
- last_response.body.should == result_json
674
+ last_response.body.should == result.to_json
356
675
  end
357
676
 
358
677
  it "returns a list of downtimes for a check on an entity" do
359
- result = mock('result')
360
- result_json = %q{"result"}
361
- result.should_receive(:to_json).and_return(result_json)
362
- entity_check_presenter.should_receive(:downtime).with(nil, nil).and_return(result)
678
+ downtime = mock('downtime')
679
+ result = [{:entity => entity_name, :check => check, :downtime => downtime}]
680
+
681
+ entity_check_presenter.should_receive(:downtime).with(nil, nil).and_return(downtime)
682
+
363
683
  Flapjack::Gateways::API::EntityCheckPresenter.should_receive(:new).
364
- with(entity_check).and_return(entity_check_presenter)
684
+ with(entity_check, :redis => redis).and_return(entity_check_presenter)
685
+
686
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
687
+ with(entity, check, :redis => redis).and_return(entity_check)
688
+
689
+ Flapjack::Data::Entity.should_receive(:find_by_name).
690
+ with(entity_name, :redis => redis).and_return(entity)
691
+
692
+ get "/downtime", :check => {entity_name => check}
693
+ last_response.should be_ok
694
+ last_response.body.should == result.to_json
695
+ end
696
+
697
+ it "creates test notification events for all checks on an entity" do
698
+ entity.should_receive(:check_list).and_return([check, 'foo'])
699
+ entity.should_receive(:name).twice.and_return(entity_name)
365
700
  Flapjack::Data::Entity.should_receive(:find_by_name).
366
701
  with(entity_name, :redis => redis).and_return(entity)
702
+
703
+ entity_check.should_receive(:entity).and_return(entity)
704
+ entity_check.should_receive(:entity_name).and_return(entity_name)
705
+ entity_check.should_receive(:check).and_return(check)
706
+
367
707
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
368
708
  with(entity, check, :redis => redis).and_return(entity_check)
369
709
 
370
- get "/downtime/#{entity_name_esc}/#{check}"
371
- last_response.should be_ok
372
- last_response.body.should == result_json
710
+ entity_check_2 = mock(Flapjack::Data::EntityCheck)
711
+ entity_check_2.should_receive(:entity).and_return(entity)
712
+ entity_check_2.should_receive(:entity_name).and_return(entity_name)
713
+ entity_check_2.should_receive(:check).and_return('foo')
714
+
715
+ Flapjack::Data::EntityCheck.should_receive(:for_entity).
716
+ with(entity, 'foo', :redis => redis).and_return(entity_check_2)
717
+
718
+ Flapjack::Data::Event.should_receive(:test_notifications).
719
+ with(entity_name, check, hash_including(:redis => redis))
720
+
721
+ Flapjack::Data::Event.should_receive(:test_notifications).
722
+ with(entity_name, 'foo', hash_including(:redis => redis))
723
+
724
+
725
+ post '/test_notifications', :entity => entity_name
726
+ last_response.status.should == 204
373
727
  end
374
728
 
375
729
  it "creates a test notification event for check on an entity" do
376
-
377
730
  Flapjack::Data::Entity.should_receive(:find_by_name).
378
731
  with(entity_name, :redis => redis).and_return(entity)
379
732
  entity.should_receive(:name).and_return(entity_name)
733
+ entity_check.should_receive(:entity).and_return(entity)
734
+ entity_check.should_receive(:entity_name).and_return(entity_name)
735
+ entity_check.should_receive(:check).and_return(check)
380
736
  Flapjack::Data::EntityCheck.should_receive(:for_entity).
381
- with(entity, 'foo', :redis => redis).and_return(entity_check)
382
- entity_check.should_receive(:test_notifications)
737
+ with(entity, check, :redis => redis).and_return(entity_check)
738
+
739
+ Flapjack::Data::Event.should_receive(:test_notifications).
740
+ with(entity_name, check, hash_including(:redis => redis))
741
+
383
742
 
384
- post "/test_notifications/#{entity_name_esc}/foo"
743
+ post '/test_notifications', :check => {entity_name => check}
385
744
  last_response.status.should == 204
386
745
  end
387
746
 
@@ -556,7 +915,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
556
915
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
557
916
 
558
917
  get "/contacts/#{contact.id}"
559
- last_response.should be_not_found
918
+ last_response.should be_forbidden
560
919
  end
561
920
 
562
921
  it "lists a contact's notification rules" do
@@ -579,7 +938,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
579
938
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
580
939
 
581
940
  get "/contacts/#{contact.id}/notification_rules"
582
- last_response.should be_not_found
941
+ last_response.should be_forbidden
583
942
  end
584
943
 
585
944
  it "returns a specified notification rule" do
@@ -597,7 +956,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
597
956
  with(notification_rule.id, {:redis => redis, :logger => @logger}).and_return(nil)
598
957
 
599
958
  get "/notification_rules/#{notification_rule.id}"
600
- last_response.should be_not_found
959
+ last_response.should be_forbidden
601
960
  end
602
961
 
603
962
  # POST /notification_rules
@@ -627,7 +986,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
627
986
 
628
987
  post "/notification_rules", notification_rule_data.to_json,
629
988
  {'CONTENT_TYPE' => 'application/json'}
630
- last_response.should be_not_found
989
+ last_response.should be_forbidden
631
990
  end
632
991
 
633
992
  it "does not create a notification_rule if a rule id is provided" do
@@ -665,7 +1024,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
665
1024
  with(notification_rule.id, {:redis => redis, :logger => @logger}).and_return(nil)
666
1025
 
667
1026
  put "/notification_rules/#{notification_rule.id}", notification_rule_data
668
- last_response.should be_not_found
1027
+ last_response.should be_forbidden
669
1028
  end
670
1029
 
671
1030
  it "does not update a notification_rule for a contact that's not present" do
@@ -676,7 +1035,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
676
1035
 
677
1036
  put "/notification_rules/#{notification_rule.id}", notification_rule_data.to_json,
678
1037
  {'CONTENT_TYPE' => 'application/json'}
679
- last_response.should be_not_found
1038
+ last_response.should be_forbidden
680
1039
  end
681
1040
 
682
1041
  # DELETE /notification_rules/RULE_ID
@@ -697,7 +1056,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
697
1056
  with(notification_rule.id, {:redis => redis, :logger => @logger}).and_return(nil)
698
1057
 
699
1058
  delete "/notification_rules/#{notification_rule.id}"
700
- last_response.should be_not_found
1059
+ last_response.should be_forbidden
701
1060
  end
702
1061
 
703
1062
  it "does not delete a notification rule if the contact is not present" do
@@ -708,7 +1067,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
708
1067
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
709
1068
 
710
1069
  delete "/notification_rules/#{notification_rule.id}"
711
- last_response.should be_not_found
1070
+ last_response.should be_forbidden
712
1071
  end
713
1072
 
714
1073
  # GET /contacts/CONTACT_ID/media
@@ -732,12 +1091,12 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
732
1091
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
733
1092
 
734
1093
  get "/contacts/#{contact.id}/media"
735
- last_response.should be_not_found
1094
+ last_response.should be_forbidden
736
1095
  end
737
1096
 
738
1097
  # GET /contacts/CONTACT_ID/media/MEDIA
739
1098
  it "returns the specified media of a contact" do
740
- contact.should_receive(:media).twice.and_return(media)
1099
+ contact.should_receive(:media).and_return(media)
741
1100
  contact.should_receive(:media_intervals).and_return(media_intervals)
742
1101
  Flapjack::Data::Contact.should_receive(:find_by_id).
743
1102
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(contact)
@@ -754,7 +1113,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
754
1113
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
755
1114
 
756
1115
  get "/contacts/#{contact.id}/media/sms"
757
- last_response.should be_not_found
1116
+ last_response.should be_forbidden
758
1117
  end
759
1118
 
760
1119
  it "does not return the media of a contact if the media is not present" do
@@ -763,7 +1122,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
763
1122
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(contact)
764
1123
 
765
1124
  get "/contacts/#{contact.id}/media/telepathy"
766
- last_response.should be_not_found
1125
+ last_response.should be_forbidden
767
1126
  end
768
1127
 
769
1128
  # PUT, DELETE /contacts/CONTACT_ID/media/MEDIA
@@ -792,7 +1151,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
792
1151
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
793
1152
 
794
1153
  put "/contacts/#{contact.id}/media/sms", {:address => '04987654321', :interval => '200'}
795
- last_response.should be_not_found
1154
+ last_response.should be_forbidden
796
1155
  end
797
1156
 
798
1157
  it "does not create a media of a contact if no address is provided" do
@@ -803,12 +1162,19 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
803
1162
  last_response.should be_forbidden
804
1163
  end
805
1164
 
806
- it "does not create a media of a contact if no interval is provided" do
1165
+ it "creates a media of a contact even if no interval is provided" do
1166
+ alt_media = media.merge('sms' => '04987654321')
1167
+ alt_media_intervals = media_intervals.merge('sms' => nil)
1168
+
1169
+ contact.should_receive(:set_address_for_media).with('sms', '04987654321')
1170
+ contact.should_receive(:set_interval_for_media).with('sms', nil)
1171
+ contact.should_receive(:media).and_return(alt_media)
1172
+ contact.should_receive(:media_intervals).and_return(alt_media_intervals)
807
1173
  Flapjack::Data::Contact.should_receive(:find_by_id).
808
1174
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(contact)
809
1175
 
810
1176
  put "/contacts/#{contact.id}/media/sms", {:address => '04987654321'}
811
- last_response.should be_forbidden
1177
+ last_response.should be_ok
812
1178
  end
813
1179
 
814
1180
  it "deletes a media of a contact" do
@@ -825,7 +1191,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
825
1191
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
826
1192
 
827
1193
  delete "/contacts/#{contact.id}/media/sms"
828
- last_response.should be_not_found
1194
+ last_response.should be_forbidden
829
1195
  end
830
1196
 
831
1197
  # GET /contacts/CONTACT_ID/timezone
@@ -844,7 +1210,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
844
1210
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
845
1211
 
846
1212
  get "/contacts/#{contact.id}/timezone"
847
- last_response.should be_not_found
1213
+ last_response.should be_forbidden
848
1214
  end
849
1215
 
850
1216
  # PUT /contacts/CONTACT_ID/timezone
@@ -863,7 +1229,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
863
1229
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
864
1230
 
865
1231
  put "/contacts/#{contact.id}/timezone", {:timezone => 'Australia/Perth'}
866
- last_response.should be_not_found
1232
+ last_response.should be_forbidden
867
1233
  end
868
1234
 
869
1235
  # DELETE /contacts/CONTACT_ID/timezone
@@ -881,7 +1247,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
881
1247
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
882
1248
 
883
1249
  delete "/contacts/#{contact.id}/timezone"
884
- last_response.should be_not_found
1250
+ last_response.should be_forbidden
885
1251
  end
886
1252
 
887
1253
 
@@ -902,7 +1268,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
902
1268
  with(entity_name, :redis => redis).and_return(nil)
903
1269
 
904
1270
  post "entities/#{entity_name}/tags", :tag => 'web'
905
- last_response.should be_not_found
1271
+ last_response.should be_forbidden
906
1272
  end
907
1273
 
908
1274
  it "sets multiple tags on an entity and returns current tags" do
@@ -922,7 +1288,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
922
1288
  with(entity_name, :redis => redis).and_return(nil)
923
1289
 
924
1290
  post "entities/#{entity_name}/tags", :tag => ['web', 'app']
925
- last_response.should be_not_found
1291
+ last_response.should be_forbidden
926
1292
  end
927
1293
 
928
1294
  it "removes a single tag from an entity" do
@@ -939,7 +1305,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
939
1305
  with(entity_name, :redis => redis).and_return(nil)
940
1306
 
941
1307
  delete "entities/#{entity_name}/tags", :tag => 'web'
942
- last_response.should be_not_found
1308
+ last_response.should be_forbidden
943
1309
  end
944
1310
 
945
1311
  it "removes multiple tags from an entity" do
@@ -956,7 +1322,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
956
1322
  with(entity_name, :redis => redis).and_return(nil)
957
1323
 
958
1324
  delete "entities/#{entity_name}/tags", :tag => ['web', 'app']
959
- last_response.should be_not_found
1325
+ last_response.should be_forbidden
960
1326
  end
961
1327
 
962
1328
  it "gets all tags on an entity" do
@@ -974,7 +1340,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
974
1340
  with(entity_name, :redis => redis).and_return(nil)
975
1341
 
976
1342
  get "entities/#{entity_name}/tags"
977
- last_response.should be_not_found
1343
+ last_response.should be_forbidden
978
1344
  end
979
1345
 
980
1346
 
@@ -994,7 +1360,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
994
1360
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
995
1361
 
996
1362
  post "contacts/#{contact.id}/tags", :tag => 'web'
997
- last_response.should be_not_found
1363
+ last_response.should be_forbidden
998
1364
  end
999
1365
 
1000
1366
  it "sets multiple tags on a contact and returns current tags" do
@@ -1013,7 +1379,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1013
1379
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
1014
1380
 
1015
1381
  post "contacts/#{contact.id}/tags", :tag => ['web', 'app']
1016
- last_response.should be_not_found
1382
+ last_response.should be_forbidden
1017
1383
  end
1018
1384
 
1019
1385
  it "removes a single tag from a contact" do
@@ -1030,7 +1396,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1030
1396
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
1031
1397
 
1032
1398
  delete "contacts/#{contact.id}/tags", :tag => 'web'
1033
- last_response.should be_not_found
1399
+ last_response.should be_forbidden
1034
1400
  end
1035
1401
 
1036
1402
  it "removes multiple tags from a contact" do
@@ -1047,7 +1413,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1047
1413
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
1048
1414
 
1049
1415
  delete "contacts/#{contact.id}/tags", :tag => ['web', 'app']
1050
- last_response.should be_not_found
1416
+ last_response.should be_forbidden
1051
1417
  end
1052
1418
 
1053
1419
  it "gets all tags on a contact" do
@@ -1065,7 +1431,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1065
1431
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
1066
1432
 
1067
1433
  get "contacts/#{contact.id}/tags"
1068
- last_response.should be_not_found
1434
+ last_response.should be_forbidden
1069
1435
  end
1070
1436
 
1071
1437
  it "gets all entity tags for a contact" do
@@ -1093,7 +1459,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1093
1459
  with(contact.id, {:redis => redis, :logger => @logger}).and_return(nil)
1094
1460
 
1095
1461
  get "contacts/#{contact.id}/entity_tags"
1096
- last_response.should be_not_found
1462
+ last_response.should be_forbidden
1097
1463
  end
1098
1464
 
1099
1465
  it "adds tags to multiple entities for a contact" do
@@ -1127,7 +1493,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1127
1493
 
1128
1494
  post "contacts/#{contact.id}/entity_tags",
1129
1495
  :entity => {'entity_1' => ['web'], 'entity_2' => ['app']}
1130
- last_response.should be_not_found
1496
+ last_response.should be_forbidden
1131
1497
  end
1132
1498
 
1133
1499
  it "deletes tags from multiple entities for a contact" do
@@ -1155,7 +1521,7 @@ describe 'Flapjack::Gateways::API', :sinatra => true, :logger => true, :json =>
1155
1521
 
1156
1522
  delete "contacts/#{contact.id}/entity_tags",
1157
1523
  :entity => {'entity_1' => ['web'], 'entity_2' => ['app']}
1158
- last_response.should be_not_found
1524
+ last_response.should be_forbidden
1159
1525
  end
1160
1526
 
1161
1527
  end