flapjack-diner 1.0.0 → 1.2.0

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 (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,171 @@
1
+ require 'spec_helper'
2
+ require 'flapjack-diner'
3
+
4
+ describe Flapjack::Diner::Resources::Checks, :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 a check" do
14
+ check_data = [{
15
+ :name => 'SSH',
16
+ :entity_id => '1234'
17
+ }]
18
+
19
+ flapjack.given("an entity 'www.example.com' with id '1234' exists").
20
+ upon_receiving("a POST request with one check").
21
+ with(:method => :post, :path => '/checks',
22
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
23
+ :body => {:checks => check_data}).
24
+ will_respond_with(
25
+ :status => 201,
26
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
27
+ :body => ['www.example.com:SSH'] )
28
+
29
+ result = Flapjack::Diner.create_checks(check_data)
30
+ expect(result).to be_truthy
31
+ end
32
+
33
+ it "submits a POST request for several checks" do
34
+ check_data = [{
35
+ :name => 'SSH',
36
+ :entity_id => '1234'
37
+ }, {
38
+ :name => 'PING',
39
+ :entity_id => '5678'
40
+ }]
41
+
42
+ flapjack.given("entities 'www.example.com', id '1234' and 'www2.example.com', id '5678' exist").
43
+ upon_receiving("a POST request with two checks").
44
+ with(:method => :post, :path => '/checks',
45
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
46
+ :body => {:checks => check_data}).
47
+ will_respond_with(
48
+ :status => 201,
49
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
50
+ :body => ['www.example.com:SSH', 'www2.example.com:PING'] )
51
+
52
+ result = Flapjack::Diner.create_checks(check_data)
53
+ expect(result).to be_truthy
54
+ end
55
+
56
+ end
57
+
58
+ context 'read' do
59
+
60
+ let(:check_data) { {:id => 'www.example.com:SSH',
61
+ :name => 'SSH',
62
+ :entity_name => 'www.example.com',
63
+ :links => {
64
+ :entities => ['1234']
65
+ }
66
+ }
67
+ }
68
+
69
+ context 'GET all checks' do
70
+
71
+ it "has no data" do
72
+ flapjack.given("no entity exists").
73
+ upon_receiving("a GET request for all checks").
74
+ with(:method => :get, :path => '/checks').
75
+ will_respond_with(
76
+ :status => 200,
77
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
78
+ :body => {:checks => []} )
79
+
80
+ result = Flapjack::Diner.checks
81
+ expect(result).to eq([])
82
+ end
83
+
84
+ it "has some data" do
85
+ flapjack.given("a check 'www.example.com:SSH' exists").
86
+ upon_receiving("a GET request for all checks").
87
+ with(:method => :get, :path => '/checks').
88
+ will_respond_with(
89
+ :status => 200,
90
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
91
+ :body => {:checks => [check_data]} )
92
+
93
+ result = Flapjack::Diner.checks
94
+ expect(result).to eq([check_data])
95
+ end
96
+
97
+ end
98
+
99
+ context 'GET a single check' do
100
+
101
+ it "has some data" do
102
+ flapjack.given("a check 'www.example.com:SSH' exists").
103
+ upon_receiving("a GET request for check 'www.example.com:SSH'").
104
+ with(:method => :get, :path => '/checks/www.example.com:SSH').
105
+ will_respond_with(
106
+ :status => 200,
107
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
108
+ :body => {:checks => [check_data]} )
109
+
110
+ result = Flapjack::Diner.checks('www.example.com:SSH')
111
+ expect(result).to eq([check_data])
112
+ end
113
+
114
+ it "can't find entity for a check" do
115
+ flapjack.given("no entity exists").
116
+ upon_receiving("a GET request for check 'www.example.com:SSH'").
117
+ with(:method => :get, :path => '/checks/www.example.com:SSH').
118
+ will_respond_with(
119
+ :status => 404,
120
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
121
+ :body => {:errors => ["could not find entity checks: 'www.example.com:SSH'"]} )
122
+
123
+ result = Flapjack::Diner.checks('www.example.com:SSH')
124
+ expect(result).to be_nil
125
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
126
+ :errors => ["could not find entity checks: 'www.example.com:SSH'"])
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+
133
+ context 'update' do
134
+
135
+ it "submits a PATCH request for a check" do
136
+ flapjack.given("a check 'www.example.com:SSH' exists").
137
+ upon_receiving("a PATCH request for a single check").
138
+ with(:method => :patch,
139
+ :path => '/checks/www.example.com:SSH',
140
+ :body => [{:op => 'replace', :path => '/checks/0/enabled', :value => false}],
141
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
142
+ will_respond_with(
143
+ :status => 204,
144
+ :body => '')
145
+
146
+ result = Flapjack::Diner.update_checks('www.example.com:SSH', :enabled => false)
147
+ expect(result).not_to be_nil
148
+ expect(result).to be_truthy
149
+ end
150
+
151
+ it "doesn't find the entity of the check to update" do
152
+ flapjack.given("no entity exists").
153
+ upon_receiving("a PATCH request for a single check").
154
+ with(:method => :patch,
155
+ :path => '/checks/www.example.com:SSH',
156
+ :body => [{:op => 'replace', :path => '/checks/0/enabled', :value => false}],
157
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
158
+ will_respond_with(
159
+ :status => 404,
160
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
161
+ :body => {:errors => ["could not find entity 'www.example.com'"]} )
162
+
163
+ result = Flapjack::Diner.update_checks('www.example.com:SSH', :enabled => false)
164
+ expect(result).to be_nil
165
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
166
+ :errors => ["could not find entity 'www.example.com'"])
167
+ end
168
+
169
+ end
170
+
171
+ end
@@ -0,0 +1,297 @@
1
+ require 'spec_helper'
2
+ require 'flapjack-diner'
3
+
4
+ describe Flapjack::Diner::Resources::Contacts, :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 a contact" do
14
+ contact_data = [{:id => 'abc',
15
+ :first_name => 'Jim',
16
+ :last_name => 'Smith',
17
+ :email => 'jims@example.com',
18
+ :timezone => 'UTC' }]
19
+
20
+ flapjack.given("no contact exists").
21
+ upon_receiving("a POST request with one contact").
22
+ with(:method => :post, :path => '/contacts',
23
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
24
+ :body => {:contacts => contact_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_contacts(contact_data)
31
+ expect(result).not_to be_nil
32
+ expect(result).to eq(['abc'])
33
+ end
34
+
35
+ it "submits a POST request for several contacts" do
36
+ contact_data = [{:id => 'abc',
37
+ :first_name => 'Jim',
38
+ :last_name => 'Smith',
39
+ :email => 'jims@example.com',
40
+ :timezone => 'UTC'},
41
+ {:id => 'def',
42
+ :first_name => 'Joan',
43
+ :last_name => 'Smith',
44
+ :email => 'joans@example.com'}]
45
+
46
+ flapjack.given("no contact exists").
47
+ upon_receiving("a POST request with two contacts").
48
+ with(:method => :post, :path => '/contacts',
49
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
50
+ :body => {:contacts => contact_data}).
51
+ will_respond_with(
52
+ :status => 201,
53
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
54
+ :body => ['abc', 'def'] )
55
+
56
+ result = Flapjack::Diner.create_contacts(contact_data)
57
+ expect(result).not_to be_nil
58
+ expect(result).to eq(['abc', 'def'])
59
+ end
60
+
61
+ it "submits a POST request but a contact with that id exists" do
62
+ contact_data = [{:id => 'abc',
63
+ :first_name => 'Jim',
64
+ :last_name => 'Smith',
65
+ :email => 'jims@example.com',
66
+ :timezone => 'UTC'}]
67
+
68
+ flapjack.given("a contact with id 'abc' exists").
69
+ upon_receiving("a POST request with one contact").
70
+ with(:method => :post, :path => '/contacts',
71
+ :headers => {'Content-Type' => 'application/vnd.api+json'},
72
+ :body => {:contacts => contact_data}).
73
+ will_respond_with(
74
+ :status => 409,
75
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
76
+ :body => {:errors => ["Contacts already exist with the following IDs: abc"]} )
77
+
78
+ result = Flapjack::Diner.create_contacts(contact_data)
79
+ expect(result).to be_nil
80
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 409,
81
+ :errors => ['Contacts already exist with the following IDs: abc'])
82
+ end
83
+
84
+ end
85
+
86
+ context 'read' do
87
+
88
+ context 'GET all contacts' do
89
+
90
+ it "has some data" do
91
+ contact_data = {:id => 'abc',
92
+ :first_name => 'Jim',
93
+ :last_name => 'Smith',
94
+ :email => 'jims@example.com',
95
+ :timezone => 'UTC'}
96
+
97
+ flapjack.given("a contact with id 'abc' exists").
98
+ upon_receiving("a GET request for all contacts").
99
+ with(:method => :get, :path => '/contacts').
100
+ will_respond_with(
101
+ :status => 200,
102
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
103
+ :body => {:contacts => [contact_data]} )
104
+
105
+ result = Flapjack::Diner.contacts
106
+ expect(result).not_to be_nil
107
+ expect(result).to eq([contact_data])
108
+ end
109
+
110
+ it "has no data" do
111
+ flapjack.given("no contact exists").
112
+ upon_receiving("a GET request for all contacts").
113
+ with(:method => :get, :path => '/contacts').
114
+ will_respond_with(
115
+ :status => 200,
116
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
117
+ :body => {:contacts => []} )
118
+
119
+ result = Flapjack::Diner.contacts
120
+ expect(result).not_to be_nil
121
+ expect(result).to be_an_instance_of(Array)
122
+ expect(result).to be_empty
123
+ end
124
+
125
+ end
126
+
127
+ context 'GET a single contact' do
128
+
129
+ it "finds the contact" do
130
+ contact_data = {:id => 'abc',
131
+ :first_name => 'Jim',
132
+ :last_name => 'Smith',
133
+ :email => 'jims@example.com',
134
+ :timezone => 'UTC'}
135
+
136
+ flapjack.given("a contact with id 'abc' exists").
137
+ upon_receiving("a GET request for a single contact").
138
+ with(:method => :get, :path => '/contacts/abc').
139
+ will_respond_with(
140
+ :status => 200,
141
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
142
+ :body => {:contacts => [contact_data]} )
143
+
144
+ result = Flapjack::Diner.contacts('abc')
145
+ expect(result).not_to be_nil
146
+ expect(result).to eq([contact_data])
147
+ end
148
+
149
+ it "can't find the contact" do
150
+ flapjack.given("no contact exists").
151
+ upon_receiving("a GET request for a single contact").
152
+ with(:method => :get, :path => '/contacts/abc').
153
+ will_respond_with(
154
+ :status => 404,
155
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
156
+ :body => {:errors => ["could not find contacts 'abc'"]} )
157
+
158
+ result = Flapjack::Diner.contacts('abc')
159
+ expect(result).to be_nil
160
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
161
+ :errors => ["could not find contacts 'abc'"])
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+
168
+ context 'update' do
169
+
170
+ it "submits a PATCH request for one contact" do
171
+ flapjack.given("a contact with id 'abc' exists").
172
+ upon_receiving("a PATCH request to change properties for a single contact").
173
+ with(:method => :patch,
174
+ :path => '/contacts/abc',
175
+ :body => [{:op => 'replace', :path => '/contacts/0/timezone', :value => 'UTC'}],
176
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
177
+ will_respond_with(:status => 204,
178
+ :body => '')
179
+
180
+ result = Flapjack::Diner.update_contacts('abc', :timezone => 'UTC')
181
+ expect(result).not_to be_nil
182
+ expect(result).to be_truthy
183
+ end
184
+
185
+ it "submits a PATCH request for several contacts" do
186
+ flapjack.given("contacts with ids 'abc' and '872' exist").
187
+ upon_receiving("a PATCH request to change properties for two contacts").
188
+ with(:method => :patch,
189
+ :path => '/contacts/abc,872',
190
+ :body => [{:op => 'replace', :path => '/contacts/0/timezone', :value => 'UTC'}],
191
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
192
+ will_respond_with(:status => 204,
193
+ :body => '')
194
+
195
+ result = Flapjack::Diner.update_contacts('abc', '872', :timezone => 'UTC')
196
+ expect(result).not_to be_nil
197
+ expect(result).to be_truthy
198
+ end
199
+
200
+ it "submits a PATCH request to change a link for one contact" do
201
+ flapjack.given("a contact with id '872' exists").
202
+ upon_receiving("a PATCH requestto change a link for a single contact").
203
+ with(:method => :patch,
204
+ :path => '/contacts/872',
205
+ :body => [{:op => 'add', :path => '/contacts/0/links/entities/-', :value => '1234'}],
206
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
207
+ will_respond_with(:status => 204,
208
+ :body => '')
209
+
210
+ result = Flapjack::Diner.update_contacts('872', :add_entity => '1234')
211
+ expect(result).not_to be_nil
212
+ expect(result).to be_truthy
213
+ end
214
+
215
+ it "submits a PATCH request to change links for several contacts" do
216
+ flapjack.given("contacts with ids 'abc' and '872' exist").
217
+ upon_receiving("a PATCH request to change links for two contacts").
218
+ with(:method => :patch,
219
+ :path => '/contacts/abc,872',
220
+ :body => [{:op => 'add', :path => '/contacts/0/links/entities/-', :value => '1234'}],
221
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
222
+ will_respond_with(:status => 204,
223
+ :body => '')
224
+
225
+ result = Flapjack::Diner.update_contacts('abc', '872', :add_entity => '1234')
226
+ expect(result).not_to be_nil
227
+ expect(result).to be_truthy
228
+ end
229
+
230
+ it "can't find a contact to update" do
231
+ flapjack.given("no contact exists").
232
+ upon_receiving("a PATCH request to change properties for a single contact").
233
+ with(:method => :patch,
234
+ :path => '/contacts/323',
235
+ :body => [{:op => 'replace', :path => '/contacts/0/timezone', :value => 'UTC'}],
236
+ :headers => {'Content-Type'=>'application/json-patch+json'}).
237
+ will_respond_with(
238
+ :status => 404,
239
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
240
+ :body => {:errors => ["could not find contacts '323'"]} )
241
+
242
+ result = Flapjack::Diner.update_contacts('323', :timezone => 'UTC')
243
+ expect(result).to be_nil
244
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
245
+ :errors => ["could not find contacts '323'"])
246
+ end
247
+
248
+ end
249
+
250
+ context 'delete' do
251
+ it "submits a DELETE request for one contact" do
252
+ flapjack.given("a contact with id 'abc' exists").
253
+ upon_receiving("a DELETE request for a single contact").
254
+ with(:method => :delete,
255
+ :path => '/contacts/abc',
256
+ :body => nil).
257
+ will_respond_with(:status => 204,
258
+ :body => '')
259
+
260
+ result = Flapjack::Diner.delete_contacts('abc')
261
+ expect(result).not_to be_nil
262
+ expect(result).to be_truthy
263
+ end
264
+
265
+ it "submits a DELETE request for several contacts" do
266
+ flapjack.given("contacts with ids 'abc' and '872' exist").
267
+ upon_receiving("a DELETE request for two contacts").
268
+ with(:method => :delete,
269
+ :path => '/contacts/abc,872',
270
+ :body => nil).
271
+ will_respond_with(:status => 204,
272
+ :body => '')
273
+
274
+ result = Flapjack::Diner.delete_contacts('abc', '872')
275
+ expect(result).not_to be_nil
276
+ expect(result).to be_truthy
277
+ end
278
+
279
+ it "can't find the contact to delete" do
280
+ flapjack.given("no contact exists").
281
+ upon_receiving("a DELETE request for a single contact").
282
+ with(:method => :delete,
283
+ :path => '/contacts/abc',
284
+ :body => nil).
285
+ will_respond_with(
286
+ :status => 404,
287
+ :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
288
+ :body => {:errors => ["could not find contacts 'abc'"]} )
289
+
290
+ result = Flapjack::Diner.delete_contacts('abc')
291
+ expect(result).to be_nil
292
+ expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
293
+ :errors => ["could not find contacts 'abc'"])
294
+ end
295
+ end
296
+
297
+ end