flapjack-diner 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.rubocop.yml +21 -0
  4. data/.rubocop_todo.yml +135 -0
  5. data/Gemfile +7 -0
  6. data/README.md +86 -20
  7. data/flapjack-diner.gemspec +11 -15
  8. data/lib/flapjack-diner.rb +23 -548
  9. data/lib/flapjack-diner/argument_validator.rb +31 -22
  10. data/lib/flapjack-diner/resources/checks.rb +58 -0
  11. data/lib/flapjack-diner/resources/contacts.rb +70 -0
  12. data/lib/flapjack-diner/resources/entities.rb +68 -0
  13. data/lib/flapjack-diner/resources/maintenance_periods.rb +82 -0
  14. data/lib/flapjack-diner/resources/media.rb +61 -0
  15. data/lib/flapjack-diner/resources/notification_rules.rb +66 -0
  16. data/lib/flapjack-diner/resources/notifications.rb +27 -0
  17. data/lib/flapjack-diner/resources/pagerduty_credentials.rb +60 -0
  18. data/lib/flapjack-diner/resources/reports.rb +33 -0
  19. data/lib/flapjack-diner/tools.rb +277 -0
  20. data/lib/flapjack-diner/version.rb +1 -1
  21. data/spec/argument_validator_spec.rb +15 -15
  22. data/spec/flapjack-diner_spec.rb +58 -1275
  23. data/spec/pacts/flapjack-diner-flapjack.json +4522 -0
  24. data/spec/resources/checks_spec.rb +171 -0
  25. data/spec/resources/contacts_spec.rb +297 -0
  26. data/spec/resources/entities_spec.rb +181 -0
  27. data/spec/resources/maintenance_periods_spec.rb +603 -0
  28. data/spec/resources/media_spec.rb +277 -0
  29. data/spec/resources/notification_rules_spec.rb +341 -0
  30. data/spec/resources/notifications_spec.rb +210 -0
  31. data/spec/resources/pagerduty_credentials_spec.rb +243 -0
  32. data/spec/resources/reports_spec.rb +255 -0
  33. data/spec/spec_helper.rb +14 -2
  34. metadata +35 -72
@@ -0,0 +1,210 @@
1
+ require 'spec_helper'
2
+ require 'flapjack-diner'
3
+
4
+ describe Flapjack::Diner::Resources::Notifications, :pact => true do
5
+
6
+ before(:each) do
7
+ Flapjack::Diner.base_uri('localhost:19081')
8
+ Flapjack::Diner.logger = nil
9
+ end
10
+
11
+ context 'entities' do
12
+
13
+ context 'create' do
14
+
15
+ context 'test notifications' do
16
+
17
+ it "submits a POST request for an entity" do
18
+ data = [{:summary => 'testing'}]
19
+
20
+ flapjack.given("an entity 'www.example.com' with id '1234' exists").
21
+ upon_receiving("a POST request with one test notification").
22
+ with(:method => :post, :path => '/test_notifications/entities/1234',
23
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
24
+ :body => {:test_notifications => data}).
25
+ will_respond_with(
26
+ :status => 204,
27
+ :body => '')
28
+
29
+ result = Flapjack::Diner.create_test_notifications_entities('1234', data)
30
+ expect(result).not_to be_nil
31
+ expect(result).to be_truthy
32
+ end
33
+
34
+ it "submits a POST request for several entities" do
35
+ data = [{:summary => 'testing'}]
36
+
37
+ flapjack.given("entities 'www.example.com', id '1234' and 'www2.example.com', id '5678' exist").
38
+ upon_receiving("a POST request with one test notification").
39
+ with(:method => :post, :path => '/test_notifications/entities/1234,5678',
40
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
41
+ :body => {:test_notifications => data}).
42
+ will_respond_with(
43
+ :status => 204,
44
+ :body => '')
45
+
46
+ result = Flapjack::Diner.create_test_notifications_entities('1234', '5678', data)
47
+ expect(result).not_to be_nil
48
+ expect(result).to be_truthy
49
+ end
50
+
51
+ it "submits a POST request for multiple notifications on an entity" do
52
+ data = [{:summary => 'testing'},
53
+ {:summary => 'more tests'}]
54
+
55
+ flapjack.given("an entity 'www.example.com' with id '1234' exists").
56
+ upon_receiving("a POST request with two test notifications").
57
+ with(:method => :post, :path => '/test_notifications/entities/1234',
58
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
59
+ :body => {:test_notifications => data}).
60
+ will_respond_with(
61
+ :status => 204,
62
+ :body => '')
63
+
64
+ result = Flapjack::Diner.create_test_notifications_entities('1234', data)
65
+ expect(result).not_to be_nil
66
+ expect(result).to be_truthy
67
+ end
68
+
69
+ it "submits a POST request for multiple notifications on several entities" do
70
+ data = [{:summary => 'testing'},
71
+ {:summary => 'more tests'}]
72
+
73
+ flapjack.given("entities 'www.example.com', id '1234' and 'www2.example.com', id '5678' exist").
74
+ upon_receiving("a POST request with two test notifications").
75
+ with(:method => :post, :path => '/test_notifications/entities/1234,5678',
76
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
77
+ :body => {:test_notifications => data}).
78
+ will_respond_with(
79
+ :status => 204,
80
+ :body => '')
81
+
82
+ result = Flapjack::Diner.create_test_notifications_entities('1234', '5678', data)
83
+ expect(result).not_to be_nil
84
+ expect(result).to be_truthy
85
+ end
86
+
87
+ it "can't find the entity to create notifications for" do
88
+ data = [{:summary => 'testing'}]
89
+
90
+ flapjack.given("no entity exists").
91
+ upon_receiving("a POST request with one test notification").
92
+ with(:method => :post, :path => '/test_notifications/entities/1234',
93
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
94
+ :body => {:test_notifications => data}).
95
+ will_respond_with(
96
+ :status => 404,
97
+ :body => {:errors => ["could not find entity '1234'"]})
98
+
99
+ result = Flapjack::Diner.create_test_notifications_entities('1234', data)
100
+ expect(result).to be_nil
101
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
102
+ :errors => ["could not find entity '1234'"])
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ context 'checks' do
112
+ context 'create' do
113
+
114
+ context 'test notifications' do
115
+
116
+ it "submits a POST request for a check" do
117
+ data = [{:summary => 'testing'}]
118
+
119
+ flapjack.given("a check 'www.example.com:SSH' exists").
120
+ upon_receiving("a POST request with one test notification").
121
+ with(:method => :post, :path => '/test_notifications/checks/www.example.com:SSH',
122
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
123
+ :body => {:test_notifications => data}).
124
+ will_respond_with(
125
+ :status => 204,
126
+ :body => '')
127
+
128
+ result = Flapjack::Diner.create_test_notifications_checks('www.example.com:SSH', data)
129
+ expect(result).not_to be_nil
130
+ expect(result).to be_truthy
131
+ end
132
+
133
+ it "submits a POST request for several checks" do
134
+ data = [{:summary => 'testing'}]
135
+
136
+ flapjack.given("checks 'www.example.com:SSH' and 'www2.example.com:PING' exist").
137
+ upon_receiving("a POST request with one test notification").
138
+ with(:method => :post, :path => '/test_notifications/checks/www.example.com:SSH,www2.example.com:PING',
139
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
140
+ :body => {:test_notifications => data}).
141
+ will_respond_with(
142
+ :status => 204,
143
+ :body => '')
144
+
145
+ result = Flapjack::Diner.create_test_notifications_checks('www.example.com:SSH', 'www2.example.com:PING', data)
146
+ expect(result).not_to be_nil
147
+ expect(result).to be_truthy
148
+ end
149
+
150
+ it "submits a POST request for multiple notifications on a check" do
151
+ data = [{:summary => 'testing'},
152
+ {:summary => 'more tests'}]
153
+
154
+ flapjack.given("a check 'www.example.com:SSH' exists").
155
+ upon_receiving("a POST request with two test notifications").
156
+ with(:method => :post, :path => '/test_notifications/checks/www.example.com:SSH',
157
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
158
+ :body => {:test_notifications => data}).
159
+ will_respond_with(
160
+ :status => 204,
161
+ :body => '')
162
+
163
+ result = Flapjack::Diner.create_test_notifications_checks('www.example.com:SSH', data)
164
+ expect(result).not_to be_nil
165
+ expect(result).to be_truthy
166
+ end
167
+
168
+ it "submits a POST request for multiple notifications on several checks" do
169
+ data = [{:summary => 'testing'},
170
+ {:summary => 'more tests'}]
171
+
172
+ flapjack.given("checks 'www.example.com:SSH' and 'www2.example.com:PING' exist").
173
+ upon_receiving("a POST request with two test notifications").
174
+ with(:method => :post, :path => '/test_notifications/checks/www.example.com:SSH,www2.example.com:PING',
175
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
176
+ :body => {:test_notifications => data}).
177
+ will_respond_with(
178
+ :status => 204,
179
+ :body => '')
180
+
181
+ result = Flapjack::Diner.create_test_notifications_checks('www.example.com:SSH', 'www2.example.com:PING', data)
182
+ expect(result).not_to be_nil
183
+ expect(result).to be_truthy
184
+ end
185
+
186
+ it "can't find the check to create notifications for" do
187
+ data = [{:summary => 'testing'}]
188
+
189
+ flapjack.given("no check exists").
190
+ upon_receiving("a POST request with one test notification").
191
+ with(:method => :post, :path => '/test_notifications/checks/www.example.com:SSH',
192
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
193
+ :body => {:test_notifications => data}).
194
+ will_respond_with(
195
+ :status => 404,
196
+ :body => {:errors => ["could not find entity 'www.example.com'"]})
197
+
198
+ result = Flapjack::Diner.create_test_notifications_checks('www.example.com:SSH', data)
199
+ expect(result).to be_nil
200
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
201
+ :errors => ["could not find entity 'www.example.com'"])
202
+ end
203
+
204
+ end
205
+
206
+ end
207
+
208
+ end
209
+
210
+ end
@@ -0,0 +1,243 @@
1
+ require 'spec_helper'
2
+ require 'flapjack-diner'
3
+
4
+ describe Flapjack::Diner::Resources::PagerdutyCredentials, :pact => true do
5
+
6
+ before(:each) do
7
+ Flapjack::Diner.base_uri('localhost:19081')
8
+ Flapjack::Diner.logger = nil
9
+ end
10
+
11
+ context 'create' do
12
+
13
+ it "submits a POST request for pagerduty credentials" do
14
+ data = [{:service_key => 'abc',
15
+ :subdomain => 'def',
16
+ :username => 'ghi',
17
+ :password => 'jkl',
18
+ }]
19
+
20
+ flapjack.given("a contact with id 'abc' exists").
21
+ upon_receiving("a POST request with one set of pagerduty credentials").
22
+ with(:method => :post, :path => '/contacts/abc/pagerduty_credentials',
23
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
24
+ :body => {:pagerduty_credentials => data}).
25
+ will_respond_with(
26
+ :status => 201,
27
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
28
+ :body => ['abc'] )
29
+
30
+ result = Flapjack::Diner.create_contact_pagerduty_credentials('abc', data)
31
+ expect(result).to eq(['abc'])
32
+ end
33
+
34
+ it "can't find the contact to create pagerduty credentials for" do
35
+ data = [{:service_key => 'abc',
36
+ :subdomain => 'def',
37
+ :username => 'ghi',
38
+ :password => 'jkl',
39
+ }]
40
+
41
+ flapjack.given("no contact exists").
42
+ upon_receiving("a POST request with one set of pagerduty credentials").
43
+ with(:method => :post, :path => '/contacts/abc/pagerduty_credentials',
44
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
45
+ :body => {:pagerduty_credentials => data}).
46
+ will_respond_with(
47
+ :status => 422,
48
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
49
+ :body => {:errors => ["Contact id: 'abc' could not be loaded"]} )
50
+
51
+ result = Flapjack::Diner.create_contact_pagerduty_credentials('abc', data)
52
+ expect(result).to be_nil
53
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 422,
54
+ :errors => ["Contact id: 'abc' could not be loaded"])
55
+ end
56
+
57
+ end
58
+
59
+ context 'read' do
60
+ it "submits a GET request for all pagerduty credentials" do
61
+ pdc_data = [{
62
+ :service_key => 'abc',
63
+ :subdomain => 'def',
64
+ :username => 'ghi',
65
+ :password => 'jkl',
66
+ }]
67
+
68
+ flapjack.given("a contact with id 'abc' has pagerduty credentials").
69
+ upon_receiving("a GET request for all pagerduty credentials").
70
+ with(:method => :get, :path => '/pagerduty_credentials').
71
+ will_respond_with(
72
+ :status => 200,
73
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
74
+ :body => {:pagerduty_credentials => pdc_data} )
75
+
76
+ result = Flapjack::Diner.pagerduty_credentials
77
+ expect(result).to eq(pdc_data)
78
+ end
79
+
80
+ it "submits a GET request for one set of pagerduty credentials" do
81
+ pdc_data = [{
82
+ :service_key => 'abc',
83
+ :subdomain => 'def',
84
+ :username => 'ghi',
85
+ :password => 'jkl',
86
+ }]
87
+
88
+ flapjack.given("a contact with id 'abc' has pagerduty credentials").
89
+ upon_receiving("a GET request for one set of pagerduty credentials").
90
+ with(:method => :get, :path => '/pagerduty_credentials/abc').
91
+ will_respond_with(
92
+ :status => 200,
93
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
94
+ :body => {:pagerduty_credentials => pdc_data} )
95
+
96
+ result = Flapjack::Diner.pagerduty_credentials('abc')
97
+ expect(result).to eq(pdc_data)
98
+ end
99
+
100
+ it "submits a GET request for several sets of pagerduty credentials" do
101
+ pdc_data = [{
102
+ :service_key => 'abc',
103
+ :subdomain => 'def',
104
+ :username => 'ghi',
105
+ :password => 'jkl',
106
+ }, {
107
+ :service_key => 'mno',
108
+ :subdomain => 'pqr',
109
+ :username => 'stu',
110
+ :password => 'vwx',
111
+ }]
112
+
113
+ flapjack.given("contacts with ids 'abc' and '872' have pagerduty credentials").
114
+ upon_receiving("a GET request for two sets of pagerduty credentials").
115
+ with(:method => :get, :path => '/pagerduty_credentials/abc,872').
116
+ will_respond_with(
117
+ :status => 200,
118
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
119
+ :body => {:pagerduty_credentials => pdc_data} )
120
+
121
+ result = Flapjack::Diner.pagerduty_credentials('abc', '872')
122
+ expect(result).to eq(pdc_data)
123
+ end
124
+
125
+ it "can't find the contact with pagerduty credentials to read" do
126
+ flapjack.given("no contact exists").
127
+ upon_receiving("a GET request for one set of pagerduty credentials").
128
+ with(:method => :get, :path => '/pagerduty_credentials/abc').
129
+ will_respond_with(
130
+ :status => 404,
131
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
132
+ :body => {:errors => ["could not find contact 'abc'"]} )
133
+
134
+ result = Flapjack::Diner.pagerduty_credentials('abc')
135
+ expect(result).to be_nil
136
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
137
+ :errors => ["could not find contact 'abc'"])
138
+ end
139
+
140
+ end
141
+
142
+ context 'update' do
143
+
144
+ it "submits a PATCH request for one set of pagerduty credentials" do
145
+ flapjack.given("a contact with id 'abc' has pagerduty credentials").
146
+ upon_receiving("a PATCH request for pagerduty credentials").
147
+ with(:method => :patch,
148
+ :path => '/pagerduty_credentials/abc',
149
+ :headers => {'Content-Type'=>'application/json-patch+json'},
150
+ :body => [{:op => 'replace', :path => '/pagerduty_credentials/0/password', :value => 'pswrd'}]).
151
+ will_respond_with(
152
+ :status => 204,
153
+ :body => '' )
154
+
155
+ result = Flapjack::Diner.update_pagerduty_credentials('abc', :password => 'pswrd')
156
+ expect(result).not_to be_nil
157
+ expect(result).to be_truthy
158
+ end
159
+
160
+ it "submits a PATCH request for several sets of pagerduty credentials" do
161
+ flapjack.given("contacts with ids 'abc' and '872' have pagerduty credentials").
162
+ upon_receiving("a PATCH request for pagerduty credentials").
163
+ with(:method => :patch,
164
+ :path => '/pagerduty_credentials/abc,872',
165
+ :headers => {'Content-Type'=>'application/json-patch+json'},
166
+ :body => [{:op => 'replace', :path => '/pagerduty_credentials/0/password', :value => 'pswrd'}]).
167
+ will_respond_with(
168
+ :status => 204,
169
+ :body => '' )
170
+
171
+ result = Flapjack::Diner.update_pagerduty_credentials('abc', '872', :password => 'pswrd')
172
+ expect(result).not_to be_nil
173
+ expect(result).to be_truthy
174
+ end
175
+
176
+ it "can't find the contact with pagerduty credentials to update" do
177
+ flapjack.given("no contact exists").
178
+ upon_receiving("a PATCH request for pagerduty credentials").
179
+ with(:method => :patch,
180
+ :path => '/pagerduty_credentials/abc',
181
+ :headers => {'Content-Type'=>'application/json-patch+json'},
182
+ :body => [{:op => 'replace', :path => '/pagerduty_credentials/0/password', :value => 'pswrd'}]).
183
+ will_respond_with(:status => 404,
184
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
185
+ :body => {:errors => ["could not find contact 'abc'"]} )
186
+
187
+ result = Flapjack::Diner.update_pagerduty_credentials('abc', :password => 'pswrd')
188
+ expect(result).to be_nil
189
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
190
+ :errors => ["could not find contact 'abc'"])
191
+ end
192
+
193
+ end
194
+
195
+ context 'delete' do
196
+ it "submits a DELETE request for one set of pagerduty credentials" do
197
+
198
+ flapjack.given("a contact with id 'abc' has pagerduty credentials").
199
+ upon_receiving("a DELETE request for one set of pagerduty credentials").
200
+ with(:method => :delete,
201
+ :path => '/pagerduty_credentials/abc',
202
+ :body => nil).
203
+ will_respond_with(:status => 204,
204
+ :body => '')
205
+
206
+ result = Flapjack::Diner.delete_pagerduty_credentials('abc')
207
+ expect(result).not_to be_nil
208
+ expect(result).to be_truthy
209
+ end
210
+
211
+ it "submits a DELETE request for several sets of pagerduty credentials" do
212
+ flapjack.given("contacts with ids 'abc' and '872' have pagerduty credentials").
213
+ upon_receiving("a DELETE request for two sets of pagerduty credentials").
214
+ with(:method => :delete,
215
+ :path => '/pagerduty_credentials/abc,872',
216
+ :body => nil).
217
+ will_respond_with(:status => 204,
218
+ :body => '')
219
+
220
+ result = Flapjack::Diner.delete_pagerduty_credentials('abc', '872')
221
+ expect(result).not_to be_nil
222
+ expect(result).to be_truthy
223
+ end
224
+
225
+ it "can't find the contact with pagerduty credentials to delete" do
226
+ flapjack.given("no contact exists").
227
+ upon_receiving("a DELETE request for one set of pagerduty credentials").
228
+ with(:method => :delete,
229
+ :path => '/pagerduty_credentials/abc',
230
+ :body => nil).
231
+ will_respond_with(:status => 404,
232
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
233
+ :body => {:errors => ["could not find contact 'abc'"]} )
234
+
235
+ result = Flapjack::Diner.delete_pagerduty_credentials('abc')
236
+ expect(result).to be_nil
237
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
238
+ :errors => ["could not find contact 'abc'"])
239
+ end
240
+
241
+ end
242
+
243
+ end