lita-hcadmin 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ {
2
+ "users": [
3
+ {
4
+ "user_id": 11,
5
+ "email": "user11@company.com",
6
+ "is_deleted": 0,
7
+ "is_group_admin": 0,
8
+ "name": "User 11",
9
+ "mention_name": "User11",
10
+ "photo_url": "https://secure.gravatar.com/avatar/0fcefe0e34ef508390c2cd8ecd3db787?s=125&d=https%3A%2F%2Fwww.hipchat.com%2Fimg%2Fsilhouette_125.png&r=g",
11
+ "last_active": 88,
12
+ "created": 55,
13
+ "status": "offline",
14
+ "status_message": "",
15
+ "timezone": "America/Chicago",
16
+ "title": "Analyst 11"
17
+ },
18
+ {
19
+ "user_id": 12,
20
+ "email": "user11@company.com",
21
+ "is_deleted": 0,
22
+ "is_group_admin": 0,
23
+ "name": "User 11",
24
+ "mention_name": "User11",
25
+ "photo_url": "https://secure.gravatar.com/avatar/0fcefe0e34ef508390c2cd8ecd3db787?s=125&d=https%3A%2F%2Fwww.hipchat.com%2Fimg%2Fsilhouette_125.png&r=g",
26
+ "last_active": 88,
27
+ "created": 55,
28
+ "status": "offline",
29
+ "status_message": "",
30
+ "timezone": "America/Chicago",
31
+ "title": "Analyst 11"
32
+ },
33
+ {
34
+ "user_id": 13,
35
+ "email": "user11@company.com",
36
+ "is_deleted": 0,
37
+ "is_group_admin": 0,
38
+ "name": "User 11",
39
+ "mention_name": "User11",
40
+ "photo_url": "https://secure.gravatar.com/avatar/0fcefe0e34ef508390c2cd8ecd3db787?s=125&d=https%3A%2F%2Fwww.hipchat.com%2Fimg%2Fsilhouette_125.png&r=g",
41
+ "last_active": 88,
42
+ "created": 55,
43
+ "status": "offline",
44
+ "status_message": "",
45
+ "timezone": "America/Chicago",
46
+ "title": "Analyst 11"
47
+ }
48
+ ]
49
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "error": {
3
+ "code": 404,
4
+ "message": "Room 'foobar' not found",
5
+ "type": "Not Found"
6
+ }
7
+ }
@@ -0,0 +1,41 @@
1
+
2
+ {
3
+ "created": "2014-09-19T23:26:09+00:00",
4
+ "email": "dummy.account@target.com",
5
+ "group":
6
+ {
7
+ "id": 9876,
8
+ "links":
9
+ {
10
+ "self": "https://api.hipchat.com/v2/group/1234"
11
+ },
12
+ "name": "Tgtbullseye"
13
+ },
14
+ "id": 11,
15
+ "is_deleted": false,
16
+ "is_group_admin": true,
17
+ "is_guest": false,
18
+ "last_active": "2016-02-19T16:04:04+0000",
19
+ "links":
20
+ {
21
+ "self": "https://api.hipchat.com/v2/user/11"
22
+ },
23
+ "mention_name": "DummyAccount",
24
+ "name": "Dummy Account",
25
+ "photo_url": "https://s3.amazonaws.com/uploads.hipchat.com/photos/11/nope.jpg",
26
+ "presence":
27
+ {
28
+ "client":
29
+ {
30
+ "type": "http://hipchat.com/client/mac/macweb",
31
+ "version": "519"
32
+ },
33
+ "is_online": true,
34
+ "show": "chat"
35
+ },
36
+ "roles": ["owner", "admin", "user"],
37
+ "timezone": "America/Chicago",
38
+ "title": "Chairman of Dummy",
39
+ "version": "NMJP5APO",
40
+ "xmpp_jid": "1234_11@chat.hipchat.com"
41
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "error": {
3
+ "code": 404,
4
+ "message": "Target user testmember is not a valid user",
5
+ "type": "Not Found"
6
+ }
7
+ }
@@ -0,0 +1,561 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require 'net/smtp'
4
+
5
+ describe Lita::Handlers::Hcadmin, lita_handler: true do
6
+ subject { described_class.new(robot) }
7
+ let(:room_json) { File.read(File.join('spec', 'fixtures', 'room_data.json')) }
8
+ let(:room_v2_json) { File.read(File.join('spec', 'fixtures', 'room_v2_data.json')) }
9
+ let(:user_json) { File.read(File.join('spec', 'fixtures', 'user_data.json')) }
10
+ let(:all_user_json) { File.read(File.join('spec', 'fixtures', 'users_data.json')) }
11
+ let(:user_v2_json) { File.read(File.join('spec', 'fixtures', 'v2_user_data.json')) }
12
+ let(:user_v2_noaccount) { File.read(File.join('spec', 'fixtures', 'user_v2_notreal_email.json')) }
13
+ let(:user_v2_not_found) { File.read(File.join('spec', 'fixtures', 'v2_user_not_found.json')) }
14
+ let(:room_v2_not_found) { File.read(File.join('spec', 'fixtures', 'v2_room_not_found.json')) }
15
+ let(:group_stats) { File.read(File.join('spec', 'fixtures', 'stats.json')) }
16
+ let(:status_json) { File.read(File.join('spec', 'fixtures', 'status.json')) }
17
+ let(:bad_gateway) { File.read(File.join('spec', 'fixtures', 'bad_gateway.txt')) }
18
+ let(:http_proxy) { 'http://proxy-proxy.company.com:3128' }
19
+ let(:url) { 'https://api.hipchat.com/v1/rooms/list?auth_token=testtoken&format=json' }
20
+ let(:mock_smtp) { mock('smtp') }
21
+
22
+ let(:room_change_admin_member) { Lita::User.create(55, name: 'TestAdmin') }
23
+ let(:regular_member) { Lita::User.create(66, name: 'RegularMember') }
24
+
25
+ let(:fetch_api_data) { Hcadmin.fetch_data('v1', 'foo', 'bar') }
26
+
27
+ before do
28
+ Lita.configure do |config|
29
+ config.handlers.hcadmin.v1_admin_token = '123'
30
+ config.handlers.hcadmin.v1_notify_token = '321'
31
+ config.handlers.hcadmin.v2_admin_token = 'abc'
32
+ config.handlers.hcadmin.http_proxy = 'http://proxy-proxy.company.com:3128'
33
+ config.handlers.hcadmin.email_dist_list = 'blahblah@company.com'
34
+ config.handlers.hcadmin.smtp_server = 'smtp.mailserverstuffandthings.com'
35
+ config.handlers.hcadmin.room_change_admins = ['TestAdmin', 'AnotherAdmin'] # rubocop:disable Style/WordArray
36
+ config.handlers.hcadmin.account_name = 'TestHipChatAccount'
37
+ config.handlers.hcadmin.email_domain_name = ['@company.com', '@network.net']
38
+ config.handlers.hcadmin.http_logging = false
39
+ end
40
+
41
+ stub_request(:get, 'https://api.hipchat.com/v1/rooms/list?auth_token=123&format=json')
42
+ .with(
43
+ headers: {
44
+ 'Accept' => 'application/json',
45
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
46
+ 'Content-Type' => 'application/json',
47
+ 'User-Agent' => 'Faraday v0.9.2' })
48
+ .to_return(
49
+ status: 200,
50
+ body: room_json,
51
+ headers: {}
52
+ )
53
+
54
+ stub_request(:get, 'https://api.hipchat.com/v1/users/show?auth_token=123&format=json&user_id=2341755')
55
+ .with(
56
+ headers: {
57
+ 'Accept' => 'application/json',
58
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
59
+ 'Content-Type' => 'application/json',
60
+ 'User-Agent' => 'Faraday v0.9.2' })
61
+ .to_return(
62
+ status: 200,
63
+ body: user_json,
64
+ headers: {}
65
+ )
66
+
67
+ stub_request(:get, 'https://api.hipchat.com/v2/health-check')
68
+ .with(
69
+ headers: {
70
+ 'Accept' => 'application/json',
71
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
72
+ 'Content-Type' => 'application/json',
73
+ 'User-Agent' => 'Faraday v0.9.2' })
74
+ .to_return(
75
+ status: 200,
76
+ body: status_json,
77
+ headers: {}
78
+ )
79
+ stub_request(:get, 'https://api.hipchat.com/v2/user/1234?auth_token=abc&format=json')
80
+ .with(
81
+ headers: {
82
+ 'Accept' => 'application/json',
83
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
84
+ 'Content-Type' => 'application/json',
85
+ 'User-Agent' => 'Faraday v0.9.2' })
86
+ .to_return(
87
+ status: 200,
88
+ body: user_v2_json,
89
+ headers: {}
90
+ )
91
+ stub_request(:get, 'https://api.hipchat.com/v2/user/@testmember?auth_token=abc&format=json')
92
+ .with(
93
+ headers: {
94
+ 'Accept' => 'application/json',
95
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
96
+ 'Content-Type' => 'application/json',
97
+ 'User-Agent' => 'Faraday v0.9.2' })
98
+ .to_return(
99
+ status: 200,
100
+ body: user_v2_not_found,
101
+ headers: {}
102
+ )
103
+ stub_request(:get, 'https://api.hipchat.com/v2/user/@realmember?auth_token=abc&format=json')
104
+ .with(
105
+ headers: {
106
+ 'Accept' => 'application/json',
107
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
108
+ 'Content-Type' => 'application/json',
109
+ 'User-Agent' => 'Faraday v0.9.2' })
110
+ .to_return(
111
+ status: 200,
112
+ body: user_v2_json,
113
+ headers: {}
114
+ )
115
+
116
+ stub_request(:get, 'https://api.hipchat.com/v2/room/foobar?auth_token=abc&format=json')
117
+ .with(
118
+ headers: {
119
+ 'Accept' => 'application/json',
120
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
121
+ 'Content-Type' => 'application/json',
122
+ 'User-Agent' => 'Faraday v0.9.2' })
123
+ .to_return(
124
+ status: 200,
125
+ body: room_v2_not_found,
126
+ headers: {}
127
+ )
128
+
129
+ stub_request(:get, 'https://api.hipchat.com/v2/room/realroom?auth_token=abc&format=json')
130
+ .with(
131
+ headers: {
132
+ 'Accept' => 'application/json',
133
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
134
+ 'Content-Type' => 'application/json',
135
+ 'User-Agent' => 'Faraday v0.9.2' })
136
+ .to_return(
137
+ status: 200,
138
+ body: room_v2_json,
139
+ headers: {}
140
+ )
141
+ stub_request(:put, 'https://api.hipchat.com/v2/room/1460619?auth_token=abc&format=json')
142
+ .with(
143
+ body: "{\"name\":\"Test Room @ Company.com\",\"privacy\":\"public\",\"is_archived\":false,\"is_guest_accessible\":false,\"topic\":\"Room for testing things\",\"owner\":{\"id\":\"11\"}}", # rubocop:disable Style/StringLiterals
144
+ headers: {
145
+ 'Accept' => 'application/json',
146
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
147
+ 'Content-Type' => 'application/json',
148
+ 'User-Agent' => 'Faraday v0.9.2' })
149
+ .to_return(
150
+ status: 204,
151
+ body: '',
152
+ headers: {}
153
+ )
154
+
155
+ stub_request(:put, 'https://api.hipchat.com/v2/room/1460619?auth_token=abc&format=json')
156
+ .with(
157
+ body: "{\"name\":\"Test Room @ Company.com\",\"privacy\":\"public\",\"is_archived\":false,\"is_guest_accessible\":false,\"topic\":\"Room for testing things\",\"owner\":{\"id\":\"66\"}}", # rubocop:disable Style/StringLiterals
158
+ headers: {
159
+ 'Accept' => 'application/json',
160
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
161
+ 'Content-Type' => 'application/json',
162
+ 'User-Agent' => 'Faraday v0.9.2' })
163
+ .to_return(
164
+ status: 204,
165
+ body: '',
166
+ headers: {}
167
+ )
168
+ stub_request(:get, 'https://api.hipchat.com/v2/room/real%20room?auth_token=abc&format=json')
169
+ .with(
170
+ headers: {
171
+ 'Accept' => 'application/json',
172
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
173
+ 'Content-Type' => 'application/json',
174
+ 'User-Agent' => 'Faraday v0.9.2' })
175
+ .to_return(
176
+ status: 200,
177
+ body: room_v2_json,
178
+ headers: {}
179
+ )
180
+ end
181
+
182
+ it 'routes the identify room owner command room name has spaces' do
183
+ is_expected.to route_command('identify room owner thisis a test').to(:identify_room_owner)
184
+ end
185
+
186
+ it 'routes the identify room owner command room name in quotes' do
187
+ is_expected.to route_command('identify room owner "thisis a test"').to(:identify_room_owner)
188
+ end
189
+
190
+ it 'routes the identify room owner command room name has a @' do
191
+ is_expected.to route_command('identify room owner "thisis @ test"').to(:identify_room_owner)
192
+ end
193
+
194
+ it 'routes the identify admins command' do
195
+ is_expected.to route_command('identify hipchat admins').to(:identify_hipchat_admins)
196
+ end
197
+
198
+ it 'routes the identify room count' do
199
+ is_expected.to route_command('identify room count').to(:identify_room_count)
200
+ end
201
+
202
+ it 'routes the identify user count' do
203
+ is_expected.to route_command('identify user count').to(:identify_user_count)
204
+ end
205
+
206
+ it 'routes the identify status check' do
207
+ is_expected.to route_command('identify status').to(:identify_status)
208
+ end
209
+
210
+ it 'routes a sponsor a person request correctly' do
211
+ is_expected.to route_command('hipchat sponsor first.last@company.com').to(:hipchat_sponsor)
212
+ end
213
+
214
+ it 'routes room change admin requests' do
215
+ is_expected.to route_command('identify room change admins').to(:identify_room_change_admins)
216
+ end
217
+
218
+ it 'routes room owner change requests' do
219
+ is_expected.to route_command('room owner change mention_name room_name').to(:room_owner_change)
220
+ end
221
+
222
+ it 'routes room unarchive requests' do
223
+ is_expected.to route_command('room unarchive room_name').to(:room_unarchive)
224
+ end
225
+
226
+ describe '#hipchat_sponsor' do
227
+ context 'validates the new account request is for a Target.com email address' do
228
+ it 'rejects non-Target.com email addresses' do
229
+ send_command('hipchat sponsor dummy@gmail.com')
230
+ expect(replies.last).to eq('The email address has to be one of: ["@company.com", "@network.net"]')
231
+ end
232
+ end
233
+ context 'hipchat account exists' do
234
+ before(:each) do
235
+ stub_request(:get, 'https://api.hipchat.com/v2/user/dummy.account@company.com?auth_token=abc&format=json')
236
+ .with(headers: { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' })
237
+ .to_return(status: 200, body: user_v2_json, headers: {})
238
+ end
239
+
240
+ it 'responds that the user exists if an ID is found' do
241
+ send_command('hipchat sponsor dummy.account@company.com')
242
+ expect(replies.last).to eq('The email address of dummy.account@company.com already has an account on TestHipChatAccount of 11')
243
+ end
244
+ end
245
+
246
+ context 'hipchat account does not exist' do
247
+ before(:each) do
248
+ stub_request(:get, 'https://api.hipchat.com/v2/user/dummy.account@company.com?auth_token=abc&format=json')
249
+ .with(headers: { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' })
250
+ .to_return(status: 200, body: user_v2_noaccount, headers: {})
251
+ allow(described_class).to receive(:send_mail).with('dummy.account@company.com', 'blahblah@company.com', 'dummyHipChatUser', 'smtp.mailserverstuffandthings.com').and_return('221')
252
+ allow(Net::SMTP).to receive(:start).with('smtp.mailserverstuffandthings.com').and_return('221')
253
+ end
254
+ it 'responds that the request is being sent to the defined admin room' do
255
+ send_command('hipchat sponsor dummy.account@company.com')
256
+ expect(replies.last).to eq('Message to admins sent')
257
+ end
258
+ end
259
+ it 'replies correctly when not using an approved email domain' do
260
+ send_command('hipchat sponsor dummy.account@something.com')
261
+ expect(replies.last).to eq('The email address has to be one of: ["@company.com", "@network.net"]')
262
+ end
263
+ end # end hipchat_sponsor
264
+
265
+ describe '#identify_hipchat_admins' do
266
+ describe 'successful HTTP request against the API' do
267
+ before(:each) do
268
+ stub_request(:get, 'https://api.hipchat.com/v1/users/list?auth_token=123&format=json')
269
+ .with(
270
+ headers: {
271
+ 'Accept' => 'application/json',
272
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
273
+ 'Content-Type' => 'application/json',
274
+ 'User-Agent' => 'Faraday v0.9.2' })
275
+ .to_return(
276
+ status: 200,
277
+ body: all_user_json,
278
+ headers: {}
279
+ )
280
+ end
281
+ it 'receives a list of admins from a JSON payload' do
282
+ send_command('identify hipchat admins')
283
+ expect(replies.last).to eq('Admin list: complete ( 0 s. to respond )')
284
+ end
285
+ end
286
+
287
+ describe 'handles Bad Gateway errors' do
288
+ before(:each) do
289
+ stub_request(:get, 'https://api.hipchat.com/v1/users/list?auth_token=123&format=json')
290
+ .with(headers: { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' })
291
+ .to_return(status: 502, body: bad_gateway, headers: {})
292
+ end
293
+ it 'sends a request for admins that fails with a 502 and responds with exception details' do
294
+ send_command('identify hipchat admins')
295
+ expect(replies.last).to eq('Admin list: I ran into an HTTP error ( HTTP code: 502 ) ( 0 s. to respond )')
296
+ end
297
+ end
298
+
299
+ describe 'handles non-200 series' do
300
+ before(:each) do
301
+ stub_request(:get, 'https://api.hipchat.com/v1/users/list?auth_token=123&format=json')
302
+ .with(headers: { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' })
303
+ .to_return(status: 501, body: bad_gateway, headers: {})
304
+ end
305
+ it 'sends a request for admins that fails with a 501 and responds with exception details' do
306
+ send_command('identify hipchat admins')
307
+ expect(replies.last).to eq('Admin list: I ran into an HTTP error ( HTTP code: 501 ) ( 0 s. to respond )')
308
+ end
309
+ end
310
+ end # end identify_hipchat_admins
311
+
312
+ describe '#identify_status' do
313
+ context 'parses the status message' do
314
+ it 'sends status message and parses results' do
315
+ send_command('identify status')
316
+ expect(replies.last).to include('Status of hipchat: Code:')
317
+ end
318
+ end
319
+ end # end identify_status
320
+
321
+ describe '#email_domain_name' do
322
+ context 'valid email domains are validated' do
323
+ before(:each) do
324
+ allow(described_class).to receive(:email_domain_name).and_return(Lita.config.handlers.hcadmin.email_domain_name)
325
+ end
326
+ it 'returns the approved email domains' do
327
+ expect(described_class.email_domain_name).to include('@company.com') # individual strings
328
+ expect(described_class.email_domain_name).to include('@network.net')
329
+ expect(described_class.email_domain_name).to include('@company.com', '@network.net') # multiple strings
330
+ end
331
+
332
+ it 'does not include email domains outside of the config' do
333
+ expect(described_class.email_domain_name).to_not include('@dummycorp.com')
334
+ end
335
+ end
336
+ end # end email_domain_name
337
+
338
+ describe '#identify_room_owner' do
339
+ context 'getting the list of rooms' do
340
+ it 'replies with the room_id for a real room name' do
341
+ send_command('identify room owner floppy drive')
342
+ expect(replies.last).to eq('The owner of floppy drive is AwesomeSauce')
343
+ end
344
+
345
+ it 'handles room names with numbers' do
346
+ send_command('identify room owner f00_bar')
347
+ expect(replies.last).to eq 'Room f00_bar apparently does not have an owner or I could not find a room with that name ( 0 s. to respond )'
348
+ end
349
+
350
+ it 'handles UCASE NAMES' do
351
+ send_command('identify room owner FOO_BAR')
352
+ expect(replies.last).to_not eq 'Sorry, no room found with that name FOO_BAR'
353
+ end
354
+
355
+ it 'handles NAMES-WITH-HYPHENS' do
356
+ send_command('identify room owner NAMES-WITH-HYPHENS')
357
+ expect(replies.last).to_not eq 'Sorry, no room found with that name NAMES-WITH-HYPHENS'
358
+ end
359
+ end
360
+ end # end identify_room_owner
361
+
362
+ describe '#fetch' do
363
+ context 'fetches JSON payload of rooms from the HipChat API' do
364
+ before(:each) do
365
+ allow(described_class).to receive(:fetch_rooms).and_return(room_json)
366
+ end
367
+
368
+ it 'returns JSON payload on fetch commands' do
369
+ expect(described_class.fetch_rooms).to eq room_json
370
+ end
371
+ end
372
+
373
+ context 'fetches JSON payload of users from the HipChat API' do
374
+ before(:each) do
375
+ allow(described_class).to receive(:fetch_user).with(2_341_755).and_return(user_json)
376
+ end
377
+
378
+ it 'returns JSON payload on fetch user commands' do
379
+ expect(described_class.fetch_user(2_341_755)).to eq user_json
380
+ end
381
+ end
382
+
383
+ context 'fetches JSON payload of a user through the V2 api' do
384
+ before(:each) do
385
+ allow(described_class).to receive(:fetch_user_v2).with('valid_user@company.com').and_return(user_v2_json)
386
+ end
387
+ it 'returns JSON of the requested user' do
388
+ expect(described_class.fetch_user_v2('valid_user@company.com')).to eq user_v2_json
389
+ end
390
+ end
391
+ end # end fetch
392
+
393
+ describe '#parse' do
394
+ before(:each) do
395
+ allow(described_class).to receive(:parse).with(room_json, 'rooms').and_return(JSON.parse(room_json))
396
+ end
397
+ let(:test_room_data) { described_class.parse(room_json, 'rooms') }
398
+ let(:test_room_details) { test_room_data['rooms'] }
399
+
400
+ context 'JSON data from parse should return a JSON object' do
401
+ it 'gets a JSON object during parse' do
402
+ expect(test_room_details.first['room_id']).to eq(31)
403
+ end
404
+ end
405
+ end
406
+
407
+ describe '#owner_name_from_id' do
408
+ before(:each) do
409
+ allow(described_class).to receive(:owner_name_from_id).with(31, user_json).and_return('AwesomeSauce')
410
+ end
411
+ context 'an owner id can be translated to an owner name' do
412
+ it 'gets the name of the room when presented a room ID' do
413
+ expect(described_class.owner_name_from_id(31, user_json)).to eq 'AwesomeSauce'
414
+ end
415
+ end
416
+ end
417
+
418
+ describe '#id_from_name' do
419
+ before(:each) do
420
+ allow(described_class).to receive(:id_from_name).with('floppy drive', room_json).and_return(31)
421
+ end
422
+
423
+ context 'returns the room_id from the room_name' do
424
+ it 'does not error when getting the ID from name' do
425
+ expect do
426
+ described_class.id_from_name('floppy drive', room_json)
427
+ end.to_not raise_error
428
+ end
429
+ it 'returns the room_id when given a room name' do
430
+ expect(described_class.id_from_name('floppy drive', room_json)).to eq 31
431
+ end
432
+ end
433
+ end
434
+
435
+ describe '#identify_room_count' do
436
+ context 'room count is returned when called' do
437
+ it 'returns the number of rooms when requested' do
438
+ send_command('identify room count')
439
+ expect(replies.last).to include('Rooms in hipchat')
440
+ end
441
+ end
442
+ end
443
+ # end identify_room_count
444
+
445
+ describe '#identify_user_count' do
446
+ before(:each) do
447
+ stub_request(:get, 'https://api.hipchat.com/v2/group/171096/statistics?auth_token=abc&format=json')
448
+ .with(headers: { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' })
449
+ .to_return(status: 200, body: group_stats, headers: {})
450
+ end
451
+ context 'grabs the statistics from HipChats API' do
452
+ it 'replies with the user count from the statistics URL' do
453
+ send_command('identify user count')
454
+ expect(replies.last).to include('Users in hipchat:')
455
+ end
456
+ end
457
+ end
458
+ # end identify_user_count
459
+
460
+ describe '#identify_room_change_admins' do
461
+ context 'returns the room change admins' do
462
+ it 'names are returned' do
463
+ send_command('identify room change admins')
464
+ expect(replies.last).to include('room change admins')
465
+ end
466
+ end
467
+
468
+ context 'shows TestAdmin name from hcadmin.config' do
469
+ it 'shows the TestAdmin was returned' do
470
+ send_command('identify room change admins')
471
+ expect(replies).to include('TestAdmin')
472
+ end
473
+ end
474
+ end # end room change admins
475
+
476
+ describe '#room_owner_change' do
477
+ it 'replies with some feedback to the member' do
478
+ send_command('room owner change')
479
+ expect(replies).to_not eq '' || nil
480
+ end
481
+ it 'validates a regular member is identified' do
482
+ send_command('room owner change testmember testroom', as: regular_member)
483
+ expect(replies.last).to eq 'You are not represented in the room owner change admin group'
484
+ end
485
+ it 'tells you bad arguments passed' do
486
+ send_command('room owner change testroom', as: room_change_admin_member)
487
+ expect(replies.last).to include('Wrong number of arguments; expected format of owner_name & room_name received arguments')
488
+ end
489
+ it 'tells you bad arguments passed' do
490
+ send_command('room owner change testroom', as: regular_member)
491
+ expect(replies.last).to eq 'You are not represented in the room owner change admin group'
492
+ end
493
+
494
+ it 'tells you when a user is not found' do
495
+ send_command('room owner change testmember realroom', as: room_change_admin_member)
496
+ expect(replies.last).to eq 'Uh-oh, 404 returned! The user testmember not found'
497
+ end
498
+
499
+ it 'tells you when a room is not found' do
500
+ send_command('room owner change realmember foobar', as: room_change_admin_member)
501
+ expect(replies.last).to eq 'Uh-oh, 404 returned! The room foobar not found'
502
+ end
503
+
504
+ it 'does not cry when a real user and a real room are passed' do
505
+ send_command('room owner change realmember realroom', as: room_change_admin_member)
506
+ expect(replies.last).to include('Room update request sent. HTTP response code:')
507
+ end
508
+ end # end room owner change
509
+
510
+ describe '#fetch_data' do
511
+ context 'accepts API v2 requests' do
512
+ it 'accepts multiple parameters' do
513
+ expect(subject).to respond_to(:fetch_data)
514
+ end
515
+
516
+ it 'returns feedback when v2 is not expressed' do
517
+ expect(subject.fetch_data('v1', 'foo', 'bar')).to eq 'Wrong API version provided'
518
+ end
519
+ it 'only accepts room and user object types' do
520
+ expect(subject.fetch_data('v2', 'foo', 'bar')).to eq "Wrong HipChat object type (foo). Only accepts 'room' or 'user'"
521
+ end
522
+ it 'requires an object id to not be nil' do
523
+ expect(subject.fetch_data('v2', 'user', nil)).to eq 'Please provide the object id of the room or user you want information for'
524
+ end
525
+ it 'gets parseable JSON data from the api' do
526
+ expect do
527
+ JSON.parse(subject.fetch_data('v2', 'user', '1234').to_json)
528
+ end.to_not raise_error
529
+ end
530
+ end
531
+ end # end fetch_data
532
+
533
+ describe '#unarchive_room' do
534
+ context 'accepts room name' do
535
+ it 'replies with some feedback to the member' do
536
+ send_command('room unarchive foobar')
537
+ expect(replies).to_not eq '' || nil
538
+ end
539
+ it 'validates a regular member is identified' do
540
+ send_command('room unarchive foobar', as: regular_member)
541
+ expect(replies.last).to eq 'You are not represented in the room owner change admin group'
542
+ end
543
+ it 'tells you bad arguments passed' do
544
+ send_command('room unarchive', as: room_change_admin_member)
545
+ expect(replies.last).to include('Wrong number of arguments; expected format of room_name received arguments')
546
+ end
547
+ it 'tells you when a room is not found' do
548
+ send_command('room unarchive foobar', as: room_change_admin_member)
549
+ expect(replies.last).to include('Uh-oh, 404 returned! The room foobar not found')
550
+ end
551
+ it 'does not cry when a real user and a real room are passed' do
552
+ send_command('room unarchive realroom', as: room_change_admin_member)
553
+ expect(replies.last).to include('Room update request sent. HTTP response code:')
554
+ end
555
+ it 'handles multi-word names' do
556
+ send_command('room unarchive real room', as: room_change_admin_member)
557
+ expect(replies.last).to include('Room update request sent. HTTP response code:')
558
+ end
559
+ end
560
+ end
561
+ end