UNPKG

13 kBJavaScriptView Raw
1var assert = require('assert');
2var mockHttpClient = require('ac-node/test/mock-http-client');
3var fixtures = require('./fixtures');
4
5module.exports = function (tokenExpiresIn) {
6 var apiUrl = 'https://mock.hipchat.com/v2';
7 var mockClient = mockHttpClient();
8 var capabilities = fixtures.load('tenant-capabilities.json');
9 var token1 = '1a2b3c4d5e6f';
10 var token2 = '2b3c4d5e6f7g';
11 var token;
12 var tokenType = 'client_credentials';
13 var groupId = 1;
14 var groupName = 'Test Group';
15
16 mockClient.mount('GET', apiUrl + '/capabilities', function (url, options, callback) {
17 assert.ok(!options.body);
18 assert.ok(options.json);
19 // TODO: request assertions
20 var body = capabilities;
21 var response = {
22 statusCode: 200,
23 body: capabilities
24 };
25 callback(null, response, body);
26 });
27
28 mockClient.mount('GET', apiUrl + '/emoticon', function (url, options, callback) {
29 assert.ok(options.json);
30 assert.ok(options.auth);
31 assert.equal(options.auth.bearer, token1);
32 var body = {/* no need for a mock response body in this test */};
33 var response = {
34 statusCode: 200,
35 body: body
36 };
37 callback(null, response);
38 });
39
40 mockClient.mount('GET', apiUrl + '/emoticon/*', function (url, options, callback) {
41 assert.ok(options.json);
42 assert.ok(options.auth);
43 assert.equal(options.auth.bearer, token1);
44 var body = {/* no need for a mock response body in this test */};
45 var response = {
46 statusCode: 200,
47 body: body
48 };
49 callback(null, response);
50 });
51
52 mockClient.mount('DELETE', apiUrl + '/oauth/token/*', function (url, options, callback) {
53 assert.ok(options.json);
54 assert.equal(options.auth.bearer, token1);
55 var response = {
56 statusCode: 204
57 };
58 callback(null, response);
59 });
60
61 mockClient.mount('POST', apiUrl + '/oauth/token', function (url, options, callback) {
62 token = token === token1 ? token2 : token1;
63 assert.ok(options.json);
64 assert.ok(options.body);
65 assert.equal(options.body.grant_type, tokenType);
66 assert.equal(typeof options.body.scope, 'string');
67 assert.ok(options.auth);
68 assert.equal(typeof options.auth.username, 'string');
69 assert.equal(typeof options.auth.password, 'string');
70 var body = {
71 access_token: token,
72 expires_in: tokenExpiresIn,
73 token_type: tokenType,
74 group_id: groupId,
75 group_name: groupName,
76 scope: options.body.scope
77 };
78 var response = {
79 statusCode: 200,
80 body: body
81 };
82 callback(null, response, body);
83 });
84
85 mockClient.mount('GET', apiUrl + '/oauth/token/*', function (url, options, callback) {
86 assert.ok(options.json);
87 assert.equal(options.auth.bearer, token1);
88 var body = {/* no need for a mock response body in this test */};
89 var response = {
90 statusCode: 200,
91 body: body
92 };
93 callback(null, response);
94 });
95
96 mockClient.mount('GET', apiUrl + '/room', function (url, options, callback) {
97 assert.ok(options.json);
98 assert.ok(options.auth);
99 assert.equal(options.auth.bearer, token1);
100 var body = {/* no need for a mock response body in this test */};
101 var response = {
102 statusCode: 200,
103 body: body
104 };
105 callback(null, response);
106 });
107
108 mockClient.mount('GET', apiUrl + '/room/*/history/*', function (url, options, callback) {
109 assert.ok(options.json);
110 assert.ok(options.auth);
111 assert.equal(options.auth.bearer, token1);
112 var body = {/* no need for a mock response body in this test */};
113 var response = {
114 statusCode: 200,
115 body: body
116 };
117 callback(null, response);
118 });
119
120 mockClient.mount('POST', apiUrl + '/room', function (url, options, callback) {
121 assert.ok(options.json);
122 assert.ok(options.auth);
123 assert.equal(options.auth.bearer, token1);
124 var response = {
125 statusCode: 201,
126 body: options.body
127 };
128 callback(null, response);
129 });
130
131 mockClient.mount('GET', apiUrl + '/room/*/history/latest', function (url, options, callback) {
132 assert.ok(options.json);
133 assert.ok(options.auth);
134 assert.equal(options.auth.bearer, token1);
135 var body = {/* no need for a mock response body in this test */};
136 var response = {
137 statusCode: 200,
138 body: body
139 };
140 callback(null, response);
141 });
142
143 mockClient.mount('POST', apiUrl + '/room/*/notification', function (url, options, callback) {
144 assert.ok(options.json);
145 assert.ok(options.body);
146 assert.ok(options.body.message);
147 assert.ok(options.auth);
148 assert.equal(options.auth.bearer, token1);
149 var response = {
150 statusCode: 204
151 };
152 callback(null, response);
153 });
154
155 mockClient.mount('PUT', apiUrl + '/room/*', function (url, options, callback) {
156 assert.ok(options.json);
157 assert.ok(options.auth);
158 assert.equal(options.auth.bearer, token1);
159 var response = {
160 statusCode: 204
161 };
162 callback(null, response);
163 });
164
165 mockClient.mount('GET', apiUrl + '/room/*', function (url, options, callback) {
166 assert.ok(options.json);
167 assert.ok(options.auth);
168 assert.equal(options.auth.bearer, token1);
169 var body = {/* no need for a mock response body in this test */};
170 var response = {
171 statusCode: 200,
172 body: body
173 };
174 callback(null, response);
175 });
176
177 mockClient.mount('DELETE', apiUrl + '/room/*', function (url, options, callback) {
178 assert.ok(options.json);
179 assert.ok(options.auth);
180 assert.equal(options.auth.bearer, token1);
181 var response = {
182 statusCode: 204
183 };
184 callback(null, response);
185 });
186
187 mockClient.mount('POST', apiUrl + '/room/*/webhook', function (url, options, callback) {
188 assert.ok(options.json);
189 assert.ok(options.body);
190 assert.ok(options.body.event);
191 assert.ok(options.body.name);
192 assert.ok(options.body.url);
193 assert.ok(options.auth);
194 assert.equal(options.auth.bearer, token1);
195 var body = {
196 id: 123,
197 links: {
198 self: 'options.body.url'
199 }
200 };
201 var response = {
202 statusCode: 201,
203 body: body
204 };
205 callback(null, response, body);
206 });
207
208 mockClient.mount('GET', apiUrl + '/room/*/webhook', function (url, options, callback) {
209 assert.ok(options.json);
210 assert.ok(options.auth);
211 assert.equal(options.auth.bearer, token1);
212 var body = {/* no need for a mock response body in this test */};
213 var response = {
214 statusCode: 200,
215 body: body
216 };
217 callback(null, response, body);
218 });
219
220 mockClient.mount('GET', apiUrl + '/room/*/statistics', function (url, options, callback) {
221 assert.ok(options.json);
222 assert.ok(options.auth);
223 assert.equal(options.auth.bearer, token1);
224 var body = {/* no need for a mock response body in this test */};
225 var response = {
226 statusCode: 200,
227 body: body
228 };
229 callback(null, response);
230 });
231
232 mockClient.mount('POST', apiUrl + '/room/*/reply', function (url, options, callback) {
233 assert.ok(options.json);
234 assert.ok(options.auth);
235 assert.equal(options.auth.bearer, token1);
236 var response = {
237 statusCode: 204
238 };
239 callback(null, response);
240 });
241
242 mockClient.mount('GET', apiUrl + '/room/*/member', function (url, options, callback) {
243 assert.ok(options.json);
244 assert.ok(options.auth);
245 assert.equal(options.auth.bearer, token1);
246 var body = {/* no need for a mock response body in this test */};
247 var response = {
248 statusCode: 200,
249 body: body
250 };
251 callback(null, response);
252 });
253
254 mockClient.mount('PUT', apiUrl + '/room/*/topic', function (url, options, callback) {
255 assert.ok(options.json);
256 assert.ok(options.auth);
257 assert.equal(options.auth.bearer, token1);
258 var response = {
259 statusCode: 204
260 };
261 callback(null, response);
262 });
263
264 mockClient.mount('PUT', apiUrl + '/room/*/share/link', function (url, options, callback) {
265 assert.ok(options.json);
266 assert.ok(options.auth);
267 assert.equal(options.auth.bearer, token1);
268 var response = {
269 statusCode: 204
270 };
271 callback(null, response);
272 });
273
274 mockClient.mount('PUT', apiUrl + '/room/*/member/*', function (url, options, callback) {
275 assert.ok(options.json);
276 assert.ok(options.auth);
277 assert.equal(options.auth.bearer, token1);
278 var body = {/* no need for a mock response body in this test */};
279 var response = {
280 statusCode: 204,
281 body: body
282 };
283 callback(null, response);
284 });
285
286 mockClient.mount('DELETE', apiUrl + '/room/*/member/*', function (url, options, callback) {
287 assert.ok(options.json);
288 assert.ok(options.auth);
289 assert.equal(options.auth.bearer, token1);
290 var response = {
291 statusCode: 204
292 };
293 callback(null, response);
294 });
295
296 mockClient.mount('DELETE', apiUrl + '/room/*/webhook/*', function (url, options, callback) {
297 assert.ok(options.json);
298 assert.ok(options.auth);
299 assert.equal(options.auth.bearer, token1);
300 var response = {
301 statusCode: 204
302 };
303 callback(null, response);
304 });
305
306 mockClient.mount('GET', apiUrl + '/room/*/webhook/*', function (url, options, callback) {
307 assert.ok(options.json);
308 assert.ok(options.auth);
309 assert.equal(options.auth.bearer, token1);
310 var body = {/* no need for a mock response body in this test */};
311 var response = {
312 statusCode: 200,
313 body: body
314 };
315 callback(null, response);
316 });
317
318 mockClient.mount('GET', apiUrl + '/room/*/history', function (url, options, callback) {
319 assert.ok(options.json);
320 assert.ok(options.auth);
321 assert.equal(options.auth.bearer, token1);
322 var body = {/* no need for a mock response body in this test */};
323 var response = {
324 statusCode: 200,
325 body: body
326 };
327 callback(null, response);
328 });
329
330 mockClient.mount('GET', apiUrl + '/user/*/history/*', function (url, options, callback) {
331 assert.ok(options.json);
332 assert.ok(options.auth);
333 assert.equal(options.auth.bearer, token1);
334 var body = {/* no need for a mock response body in this test */};
335 var response = {
336 statusCode: 200,
337 body: body
338 };
339 callback(null, response);
340 });
341
342 mockClient.mount('PUT', apiUrl + '/user/*/photo', function (url, options, callback) {
343 assert.ok(options.json);
344 assert.ok(options.auth);
345 assert.equal(options.auth.bearer, token1);
346 var response = {
347 statusCode: 204
348 };
349 callback(null, response);
350 });
351
352 mockClient.mount('DELETE', apiUrl + '/user/*/photo', function (url, options, callback) {
353 assert.ok(options.json);
354 assert.ok(options.auth);
355 assert.equal(options.auth.bearer, token1);
356 var response = {
357 statusCode: 204
358 };
359 callback(null, response);
360 });
361
362 mockClient.mount('PUT', apiUrl + '/user/*', function (url, options, callback) {
363 assert.ok(options.json);
364 assert.ok(options.auth);
365 assert.equal(options.auth.bearer, token1);
366 var response = {
367 statusCode: 204
368 };
369 callback(null, response);
370 });
371
372 mockClient.mount('DELETE', apiUrl + '/user/*', function (url, options, callback) {
373 assert.ok(options.json);
374 assert.ok(options.auth);
375 assert.equal(options.auth.bearer, token1);
376 var response = {
377 statusCode: 204
378 };
379 callback(null, response);
380 });
381
382 mockClient.mount('GET', apiUrl + '/user/*', function (url, options, callback) {
383 assert.ok(options.json);
384 assert.ok(options.auth);
385 assert.equal(options.auth.bearer, token1);
386 var body = {/* no need for a mock response body in this test */};
387 var response = {
388 statusCode: 200,
389 body: body
390 };
391 callback(null, response);
392 });
393
394 mockClient.mount('POST', apiUrl + '/user', function (url, options, callback) {
395 assert.ok(options.json);
396 assert.ok(options.auth);
397 assert.equal(options.auth.bearer, token1);
398 var body = {/* no need for a mock response body in this test */};
399 var response = {
400 statusCode: 201,
401 body: body
402 };
403 callback(null, response);
404 });
405
406 mockClient.mount('GET', apiUrl + '/user', function (url, options, callback) {
407 assert.ok(options.json);
408 assert.ok(options.auth);
409 assert.equal(options.auth.bearer, token1);
410 var body = {/* no need for a mock response body in this test */};
411 var response = {
412 statusCode: 200,
413 body: body
414 };
415 callback(null, response);
416 });
417
418 mockClient.mount('PUT', apiUrl + '/user/*/share/link', function (url, options, callback) {
419 assert.ok(options.json);
420 assert.ok(options.auth);
421 assert.equal(options.auth.bearer, token1);
422 var response = {
423 statusCode: 204
424 };
425 callback(null, response);
426 });
427
428 mockClient.notFound(function (url, options, callback) {
429 var body = {error: {code: '404', message: 'No handler found for mock request route ' + this.route}};
430 var response = {
431 statusCode: 404,
432 headers: {'Content-Type': 'application/json'},
433 body: body
434 };
435 callback(null, response, body);
436 });
437
438 return mockClient;
439};