hipchat 1.5.2 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,26 +19,19 @@ module HipChat
19
19
  #
20
20
  # Send a private message to user.
21
21
  #
22
- def send(message, message_format='text')
22
+ def send(message, message_format='text', notify=false)
23
23
  response = self.class.post(@api.send_config[:url],
24
24
  :query => { :auth_token => @token },
25
25
  :body => {
26
26
  :message => message,
27
- :message_format => message_format
27
+ :message_format => message_format,
28
+ :notify => notify
28
29
  }.send(@api.send_config[:body_format]),
29
30
  :headers => @api.headers
30
31
  )
31
32
 
32
- case response.code
33
- when 200, 204;
34
- true
35
- when 404
36
- raise UnknownUser, "Unknown user: `#{user_id}'"
37
- when 401
38
- raise Unauthorized, "Access denied to user `#{user_id}'"
39
- else
40
- raise UnknownResponseCode, "Unexpected #{response.code} for private message to `#{user_id}'"
41
- end
33
+ ErrorHandler.response_code_to_exception_for :user, user_id, response
34
+ true
42
35
  end
43
36
 
44
37
  #
@@ -51,15 +44,8 @@ module HipChat
51
44
  :headers => file_body_headers(@api.headers)
52
45
  )
53
46
 
54
- case response.code
55
- when 200, 204; true
56
- when 404
57
- raise UnknownUser, "Unknown user: `#{user_id}'"
58
- when 401
59
- raise Unauthorized, "Access denied to user `#{user_id}'"
60
- else
61
- raise UnknownResponseCode, "Unexpected #{response.code} for private message to `#{user_id}'"
62
- end
47
+ ErrorHandler.response_code_to_exception_for :user, user_id, response
48
+ true
63
49
  end
64
50
 
65
51
  #
@@ -71,12 +57,8 @@ module HipChat
71
57
  :headers => @api.headers
72
58
  )
73
59
 
74
- case response.code
75
- when 200
76
- User.new(@token, response.merge(:api_version => @api.version))
77
- else
78
- raise UnknownResponseCode, "Unexpected #{response.code} for view message to `#{user_id}'"
79
- end
60
+ ErrorHandler.response_code_to_exception_for :user, user_id, response
61
+ User.new(@token, response.merge(:api_version => @api.version))
80
62
  end
81
63
 
82
64
  #
@@ -90,12 +72,30 @@ module HipChat
90
72
  :headers => @api.headers
91
73
  )
92
74
 
93
- case response.code
94
- when 200
95
- response.body
96
- else
97
- raise UnknownResponseCode, "Unexpected #{response.code} for view private message history for `#{user_id}'"
75
+ ErrorHandler.response_code_to_exception_for :user, user_id, response
76
+ response.body
77
+ end
78
+
79
+ #
80
+ # Get private message history
81
+ #
82
+ def delete(params = {})
83
+ case @api.version
84
+ when 'v1'
85
+ response = self.class.post(@api.delete_config[:url],
86
+ :query => { :auth_token => @token }.merge(params),
87
+ :headers => @api.headers
88
+ )
89
+ when 'v2'
90
+ response = self.class.delete(@api.delete_config[:url],
91
+ :query => { :auth_token => @token },
92
+ :headers => @api.headers
93
+ )
98
94
  end
95
+
96
+ ErrorHandler.response_code_to_exception_for :user, user_id, response
97
+ true
99
98
  end
99
+
100
100
  end
101
101
  end
@@ -1,3 +1,3 @@
1
1
  module HipChat
2
- VERSION = '1.5.2'
2
+ VERSION = '1.5.3'
3
3
  end
@@ -15,8 +15,8 @@ describe "HipChat (API V1)" do
15
15
  end
16
16
 
17
17
  it "is successful with custom options" do
18
- mock_successful_history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10)
19
- expect(room.history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10)).to be_truthy
18
+ mock_successful_history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10, :'end-date' => '2010-11-19')
19
+ expect(room.history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10, :'end-date' => '2010-11-19')).to be_truthy
20
20
  end
21
21
 
22
22
  it "is successful from fetched room" do
@@ -48,7 +48,7 @@ describe "HipChat (API V1)" do
48
48
  OpenStruct.new(:code => 403)
49
49
  }
50
50
 
51
- expect { room.history }.to raise_error(HipChat::UnknownResponseCode)
51
+ expect { room.history }.to raise_error(HipChat::Unauthorized)
52
52
  end
53
53
  end
54
54
 
@@ -87,7 +87,23 @@ describe "HipChat (API V1)" do
87
87
  OpenStruct.new(:code => 403)
88
88
  }
89
89
 
90
- expect { room.topic "" }.to raise_error(HipChat::UnknownResponseCode)
90
+ expect { room.topic "" }.to raise_error(HipChat::Unauthorized)
91
+ end
92
+ end
93
+
94
+ describe "#delete_room" do
95
+ include_context "HipChatV1"
96
+
97
+ it "successfully" do
98
+ mock_successful_delete_room("Hipchat")
99
+ expect(room.delete_room).to be_truthy
100
+ end
101
+
102
+ it "missing room" do
103
+ mock_delete_missing_room("Hipchat")
104
+ expect do
105
+ room.delete_room
106
+ end.to raise_exception(HipChat::UnknownRoom)
91
107
  end
92
108
  end
93
109
 
@@ -142,7 +158,7 @@ describe "HipChat (API V1)" do
142
158
  OpenStruct.new(:code => 403)
143
159
  }
144
160
 
145
- expect { room.send "", "" }.to raise_error(HipChat::UnknownResponseCode)
161
+ expect { room.send "", "" }.to raise_error(HipChat::Unauthorized)
146
162
  end
147
163
  end
148
164
 
@@ -166,6 +182,27 @@ describe "HipChat (API V1)" do
166
182
  end
167
183
  end
168
184
 
185
+ describe "#create_user" do
186
+ include_context "HipChatV1"
187
+
188
+ it "successfully with user name" do
189
+ mock_successful_user_creation("A User", "email@example.com")
190
+
191
+ expect(subject.create_user("A User", "email@example.com")).to be_truthy
192
+ end
193
+
194
+ it "successfully with custom parameters" do
195
+ mock_successful_user_creation("A User", "email@example.com", {:title => "Super user", :password => "password", :is_group_admin => "true"})
196
+
197
+ expect(subject.create_user("A User", "email@example.com", {:title => "Super user", :password => "password", :is_group_admin =>true})).to be_truthy
198
+ end
199
+
200
+ it "but fail is name is longer then 50 char" do
201
+ expect { subject.create_user("A User that is too long that I should fail right now", "email@example.com") }.
202
+ to raise_error(HipChat::UsernameTooLong)
203
+ end
204
+ end
205
+
169
206
  describe "#send user message" do
170
207
  it "fails because API V1 doesn't support user operations" do
171
208
 
@@ -173,4 +210,11 @@ describe "HipChat (API V1)" do
173
210
  to raise_error(HipChat::InvalidApiVersion)
174
211
  end
175
212
  end
213
+
214
+ describe "#create_webhook"
215
+ it "fails because API V1 doesn't support webhooks" do
216
+
217
+ expect { room.create_webhook('https://example.org/hooks/awesome', 'room_enter') }.
218
+ to raise_error(HipChat::InvalidApiVersion)
219
+ end
176
220
  end
@@ -16,8 +16,8 @@ describe "HipChat (API V2)" do
16
16
  end
17
17
 
18
18
  it "is successful with custom options" do
19
- mock_successful_history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10)
20
- expect(room.history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10)).to be_truthy
19
+ mock_successful_history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10, :'end-date' => '2010-11-19')
20
+ expect(room.history(:timezone => 'America/Los_Angeles', :date => '2010-11-19', :'max-results' => 10, :'start-index' => 10, :'end-date' => '2010-11-19')).to be_truthy
21
21
  end
22
22
 
23
23
  it "is successful from fetched room" do
@@ -49,7 +49,7 @@ describe "HipChat (API V2)" do
49
49
  OpenStruct.new(:code => 403)
50
50
  }
51
51
 
52
- expect { room.history }.to raise_error(HipChat::UnknownResponseCode)
52
+ expect { room.history }.to raise_error(HipChat::Unauthorized)
53
53
  end
54
54
  end
55
55
 
@@ -90,7 +90,7 @@ describe "HipChat (API V2)" do
90
90
  OpenStruct.new(:code => 403)
91
91
  }
92
92
 
93
- expect { room.statistics }.to raise_error(HipChat::UnknownResponseCode)
93
+ expect { room.statistics }.to raise_error(HipChat::Unauthorized)
94
94
  end
95
95
  end
96
96
 
@@ -129,7 +129,40 @@ describe "HipChat (API V2)" do
129
129
  OpenStruct.new(:code => 403)
130
130
  }
131
131
 
132
- expect { room.topic "" }.to raise_error(HipChat::UnknownResponseCode)
132
+ expect { room.topic "" }.to raise_error(HipChat::Unauthorized)
133
+ end
134
+ end
135
+
136
+ describe "#send_message" do
137
+ include_context "HipChatV2"
138
+ it "successfully without custom options" do
139
+ mock_successful_send_message 'Hello world'
140
+
141
+ expect(room.send_message("Hello world")).to be_truthy
142
+ end
143
+
144
+ it "but fails when the room doesn't exist" do
145
+ mock(HipChat::Room).post(anything, anything) {
146
+ OpenStruct.new(:code => 404)
147
+ }
148
+
149
+ expect { room.send_message "" }.to raise_error(HipChat::UnknownRoom)
150
+ end
151
+
152
+ it "but fails when we're not allowed to do so" do
153
+ mock(HipChat::Room).post(anything, anything) {
154
+ OpenStruct.new(:code => 401)
155
+ }
156
+
157
+ expect { room.send_message "" }.to raise_error(HipChat::Unauthorized)
158
+ end
159
+
160
+ it "but fails if we get an unknown response code" do
161
+ mock(HipChat::Room).post(anything, anything) {
162
+ OpenStruct.new(:code => 403)
163
+ }
164
+
165
+ expect { room.send_message "" }.to raise_error(HipChat::Unauthorized)
133
166
  end
134
167
  end
135
168
 
@@ -153,6 +186,17 @@ describe "HipChat (API V2)" do
153
186
  expect(room.send("Dude", "Hello world", :color => 'red')).to be_truthy
154
187
  end
155
188
 
189
+ it "successfully creates a card in the room" do
190
+ card = {
191
+ :style => 'application',
192
+ :title => 'My Awesome Card',
193
+ :id => 12345
194
+ }
195
+ mock_successful_send_card 'Dude', 'Hello world', card
196
+
197
+ expect(room.send("Dude", "Hello world", :card => card)).to be_truthy
198
+ end
199
+
156
200
  it "successfully with text message_format" do
157
201
  mock_successful_send 'Dude', 'Hello world', :message_format => 'text'
158
202
 
@@ -184,7 +228,46 @@ describe "HipChat (API V2)" do
184
228
  OpenStruct.new(:code => 403)
185
229
  }
186
230
 
187
- expect { room.send "", "" }.to raise_error(HipChat::UnknownResponseCode)
231
+ expect { room.send "", "" }.to raise_error(HipChat::Unauthorized)
232
+ end
233
+ end
234
+
235
+ describe '#share_link' do
236
+ let(:link) { "http://i.imgur.com/cZ6GDFY.jpg" }
237
+ include_context "HipChatV2"
238
+ it "successfully" do
239
+ mock_successful_link_share 'Dude', 'Sloth love Chunk!', link
240
+
241
+ room.share_link("Dude", "Sloth love Chunk!", link).should be_truthy
242
+ end
243
+
244
+ it "but fails when the room doesn't exist" do
245
+ mock(HipChat::Room).post(anything, anything) {
246
+ OpenStruct.new(:code => 404)
247
+ }
248
+
249
+ lambda { room.share_link "", "", link }.should raise_error(HipChat::UnknownRoom)
250
+ end
251
+
252
+ it "but fails when we're not allowed to do so" do
253
+ mock(HipChat::Room).post(anything, anything) {
254
+ OpenStruct.new(:code => 401)
255
+ }
256
+
257
+ lambda { room.share_link "", "", link }.should raise_error(HipChat::Unauthorized)
258
+ end
259
+
260
+ it "but fails if the username is more than 15 chars" do
261
+ lambda { room.share_link "a very long username here", "a message", link }.should raise_error(HipChat::UsernameTooLong)
262
+ end
263
+
264
+ it "but fails if we get an unknown response code" do
265
+ mock(HipChat::Room).post(anything, anything) {
266
+ OpenStruct.new(:code => 403)
267
+ }
268
+
269
+ lambda { room.share_link "", "", link }.
270
+ should raise_error(HipChat::Unauthorized)
188
271
  end
189
272
  end
190
273
 
@@ -232,7 +315,7 @@ describe "HipChat (API V2)" do
232
315
  }
233
316
 
234
317
  lambda { room.send_file "", "", file }.
235
- should raise_error(HipChat::UnknownResponseCode)
318
+ should raise_error(HipChat::Unauthorized)
236
319
  end
237
320
  end
238
321
 
@@ -257,6 +340,27 @@ describe "HipChat (API V2)" do
257
340
  end
258
341
  end
259
342
 
343
+ describe "#create_user" do
344
+ include_context "HipChatV2"
345
+
346
+ it "successfully with user name" do
347
+ mock_successful_user_creation("A User", "email@example.com")
348
+
349
+ expect(subject.create_user("A User", "email@example.com")).to be_truthy
350
+ end
351
+
352
+ it "successfully with custom parameters" do
353
+ mock_successful_user_creation("A User", "email@example.com", {:title => "Super user", :password => "password", :is_group_admin => true})
354
+
355
+ expect(subject.create_user("A User", "email@example.com", {:title => "Super user", :password => "password", :is_group_admin =>true})).to be_truthy
356
+ end
357
+
358
+ it "but fail is name is longer then 50 char" do
359
+ expect { subject.create_user("A User that is too long that I should fail right now", "email@example.com") }.
360
+ to raise_error(HipChat::UsernameTooLong)
361
+ end
362
+ end
363
+
260
364
  describe "#get_room" do
261
365
  include_context "HipChatV2"
262
366
 
@@ -286,6 +390,22 @@ describe "HipChat (API V2)" do
286
390
  end
287
391
  end
288
392
 
393
+ describe "#delete_room" do
394
+ include_context "HipChatV2"
395
+
396
+ it "successfully" do
397
+ mock_successful_delete_room("Hipchat",)
398
+ expect(room.delete_room).to be_truthy
399
+ end
400
+
401
+ it "missing room" do
402
+ mock_delete_missing_room("Hipchat")
403
+ expect do
404
+ room.delete_room
405
+ end.to raise_exception(HipChat::UnknownRoom)
406
+ end
407
+ end
408
+
289
409
  describe "#invite" do
290
410
  include_context "HipChatV2"
291
411
 
@@ -302,6 +422,22 @@ describe "HipChat (API V2)" do
302
422
  end
303
423
  end
304
424
 
425
+ describe "#add_member" do
426
+ include_context "HipChatV2"
427
+
428
+ it "successfully with user_id" do
429
+ mock_successful_add_member()
430
+
431
+ expect(room.add_member("1234")).to be_truthy
432
+ end
433
+
434
+ it "successfully with custom parameters" do
435
+ mock_successful_add_member({:user_id => "321", :room_roles => ["room_admin","room_member"]})
436
+
437
+ expect(room.add_member("321", ["room_admin","room_member"])).to be_truthy
438
+ end
439
+ end
440
+
305
441
  describe "#send user message" do
306
442
  include_context "HipChatV2"
307
443
  it "successfully with a standard message" do
@@ -372,4 +508,152 @@ describe "HipChat (API V2)" do
372
508
  lambda { user.send_file "", file }.should raise_error(HipChat::Unauthorized)
373
509
  end
374
510
  end
511
+
512
+ describe '#create_webhook' do
513
+ include_context "HipChatV2"
514
+
515
+ it "successfully with a valid room, url and event" do
516
+ mock_successful_create_webhook('Hipchat', 'https://example.org/hooks/awesome', 'room_enter')
517
+
518
+ expect(room.create_webhook('https://example.org/hooks/awesome', 'room_enter')).to be_truthy
519
+ end
520
+
521
+ it "but fails when the room doesn't exist" do
522
+ mock(HipChat::Room).post(anything, anything) {
523
+ OpenStruct.new(:code => 404)
524
+ }
525
+
526
+ lambda { room.create_webhook('https://example.org/hooks/awesome', 'room_enter') }.should raise_error(HipChat::UnknownRoom)
527
+ end
528
+
529
+ it "but fails when we're not allowed to do so" do
530
+ mock(HipChat::Room).post(anything, anything) {
531
+ OpenStruct.new(:code => 401)
532
+ }
533
+
534
+ lambda { room.create_webhook('https://example.org/hooks/awesome', 'room_enter') }.should raise_error(HipChat::Unauthorized)
535
+ end
536
+
537
+ it "but fails if the url is invalid" do
538
+ lambda { room.create_webhook('foo://bar.baz/', 'room_enter') }.should raise_error(HipChat::InvalidUrl)
539
+ end
540
+
541
+ it "but fails if the event is invalid" do
542
+ lambda { room.create_webhook('https://example.org/hooks/awesome', 'room_vandalize') }.should raise_error(HipChat::InvalidEvent)
543
+ end
544
+
545
+ it "but fails if we get an unknown response code" do
546
+ mock(HipChat::Room).post(anything, anything) {
547
+ OpenStruct.new(:code => 403)
548
+ }
549
+
550
+ lambda { room.create_webhook('https://example.org/hooks/awesome', 'room_enter') }.
551
+ should raise_error(HipChat::Unauthorized)
552
+ end
553
+ end
554
+
555
+ describe '#delete_webhook' do
556
+ include_context "HipChatV2"
557
+
558
+ it "successfully deletes a webhook with a valid webhook id" do
559
+ mock_successful_delete_webhook('Hipchat', 'my_awesome_webhook')
560
+
561
+ expect(room.delete_webhook('my_awesome_webhook')).to be_truthy
562
+ end
563
+
564
+ it "but fails when the webhook doesn't exist" do
565
+ mock(HipChat::Room).delete(anything, anything) {
566
+ OpenStruct.new(:code => 404)
567
+ }
568
+
569
+ lambda { room.delete_webhook('my_awesome_webhook') }.should raise_error(HipChat::UnknownWebhook)
570
+ end
571
+
572
+ it "but fails when we're not allowed to do so" do
573
+ mock(HipChat::Room).delete(anything, anything) {
574
+ OpenStruct.new(:code => 401)
575
+ }
576
+
577
+ lambda { room.delete_webhook('my_awesome_webhook') }.should raise_error(HipChat::Unauthorized)
578
+ end
579
+
580
+ it "but fails if we get an unknown response code" do
581
+ mock(HipChat::Room).delete(anything, anything) {
582
+ OpenStruct.new(:code => 403)
583
+ }
584
+
585
+ lambda { room.delete_webhook('my_awesome_webhook') }.
586
+ should raise_error(HipChat::Unauthorized)
587
+ end
588
+ end
589
+
590
+ describe '#get_all_webhooks' do
591
+ include_context "HipChatV2"
592
+
593
+ it "successfully lists webhooks with a valid room id" do
594
+ mock_successful_get_all_webhooks('Hipchat')
595
+
596
+ expect(room.get_all_webhooks).to be_truthy
597
+ end
598
+
599
+ it "but fails when the room doesn't exist" do
600
+ mock(HipChat::Room).get(anything, anything) {
601
+ OpenStruct.new(:code => 404)
602
+ }
603
+
604
+ lambda { room.get_all_webhooks }.should raise_error(HipChat::UnknownRoom)
605
+ end
606
+
607
+ it "but fails when we're not allowed to do so" do
608
+ mock(HipChat::Room).get(anything, anything) {
609
+ OpenStruct.new(:code => 401)
610
+ }
611
+
612
+ lambda { room.get_all_webhooks }.should raise_error(HipChat::Unauthorized)
613
+ end
614
+
615
+ it "but fails if we get an unknown response code" do
616
+ mock(HipChat::Room).get(anything, anything) {
617
+ OpenStruct.new(:code => 403)
618
+ }
619
+
620
+ lambda { room.get_all_webhooks }.
621
+ should raise_error(HipChat::Unauthorized)
622
+ end
623
+ end
624
+
625
+ describe '#get_webhook' do
626
+ include_context "HipChatV2"
627
+
628
+ it "successfully gets webhook info with valid room and webhook ids" do
629
+ mock_successful_get_webhook('Hipchat', '5678')
630
+
631
+ expect(room.get_webhook('5678')).to be_truthy
632
+ end
633
+
634
+ it "but fails when the webhook doesn't exist" do
635
+ mock(HipChat::Room).get(anything, anything) {
636
+ OpenStruct.new(:code => 404)
637
+ }
638
+
639
+ lambda { room.get_webhook('5678') }.should raise_error(HipChat::UnknownWebhook)
640
+ end
641
+
642
+ it "but fails when we're not allowed to do so" do
643
+ mock(HipChat::Room).get(anything, anything) {
644
+ OpenStruct.new(:code => 401)
645
+ }
646
+
647
+ lambda { room.get_webhook('5678') }.should raise_error(HipChat::Unauthorized)
648
+ end
649
+
650
+ it "but fails if we get an unknown response code" do
651
+ mock(HipChat::Room).get(anything, anything) {
652
+ OpenStruct.new(:code => 403)
653
+ }
654
+
655
+ lambda { room.get_webhook('5678') }.
656
+ should raise_error(HipChat::Unauthorized)
657
+ end
658
+ end
375
659
  end