forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware_test.js
507 lines (422 loc) · 15.1 KB
/
middleware_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
'use strict'
/* global describe, beforeEach, it, afterEach */
/* eslint-disable no-unused-expressions */
// Assertions and Stubbing
const chai = require('chai')
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const expect = chai.expect
// Hubot classes
const Robot = require('../src/robot')
const TextMessage = require('../src/message').TextMessage
const Response = require('../src/response')
const Middleware = require('../src/middleware')
// mock `hubot-mock-adapter` module from fixture
const mockery = require('mockery')
describe('Middleware', function () {
describe('Unit Tests', function () {
beforeEach(function () {
// Stub out event emitting
this.robot = { emit: sinon.spy() }
this.middleware = new Middleware(this.robot)
})
describe('#execute', function () {
it('executes synchronous middleware', function (testDone) {
const testMiddleware = sinon.spy((context, next, done) => {
next(done)
})
this.middleware.register(testMiddleware)
const middlewareFinished = function () {
expect(testMiddleware).to.have.been.called
testDone()
}
this.middleware.execute(
{},
(_, done) => done(),
middlewareFinished
)
})
it('executes asynchronous middleware', function (testDone) {
const testMiddleware = sinon.spy((context, next, done) =>
// Yield to the event loop
process.nextTick(() => next(done))
)
this.middleware.register(testMiddleware)
const middlewareFinished = function (context, done) {
expect(testMiddleware).to.have.been.called
testDone()
}
this.middleware.execute(
{},
(_, done) => done(),
middlewareFinished
)
})
it('passes the correct arguments to each middleware', function (testDone) {
const testContext = {}
const testMiddleware = (context, next, done) =>
// Break out of middleware error handling so assertion errors are
// more visible
process.nextTick(function () {
// Check that variables were passed correctly
expect(context).to.equal(testContext)
next(done)
})
this.middleware.register(testMiddleware)
this.middleware.execute(
testContext,
(_, done) => done(),
() => testDone())
})
it('executes all registered middleware in definition order', function (testDone) {
const middlewareExecution = []
const testMiddlewareA = (context, next, done) => {
middlewareExecution.push('A')
next(done)
}
const testMiddlewareB = function (context, next, done) {
middlewareExecution.push('B')
next(done)
}
this.middleware.register(testMiddlewareA)
this.middleware.register(testMiddlewareB)
const middlewareFinished = function () {
expect(middlewareExecution).to.deep.equal(['A', 'B'])
testDone()
}
this.middleware.execute(
{},
(_, done) => done(),
middlewareFinished
)
})
it('executes the next callback after the function returns when there is no middleware', function (testDone) {
let finished = false
this.middleware.execute(
{},
function () {
expect(finished).to.be.ok
testDone()
},
function () {}
)
finished = true
})
it('always executes middleware after the function returns', function (testDone) {
let finished = false
this.middleware.register(function (context, next, done) {
expect(finished).to.be.ok
testDone()
})
this.middleware.execute({}, function () {}, function () {})
finished = true
})
it('creates a default "done" function', function (testDone) {
let finished = false
this.middleware.register(function (context, next, done) {
expect(finished).to.be.ok
testDone()
})
// we're testing the lack of a third argument here.
this.middleware.execute({}, function () {})
finished = true
})
it('does the right thing with done callbacks', function (testDone) {
// we want to ensure that the 'done' callbacks are nested correctly
// (executed in reverse order of definition)
const execution = []
const testMiddlewareA = function (context, next, done) {
execution.push('middlewareA')
next(function () {
execution.push('doneA')
done()
})
}
const testMiddlewareB = function (context, next, done) {
execution.push('middlewareB')
next(function () {
execution.push('doneB')
done()
})
}
this.middleware.register(testMiddlewareA)
this.middleware.register(testMiddlewareB)
const allDone = function () {
expect(execution).to.deep.equal(['middlewareA', 'middlewareB', 'doneB', 'doneA'])
testDone()
}
this.middleware.execute(
{},
// Short circuit at the bottom of the middleware stack
(_, done) => done(),
allDone
)
})
it('defaults to the latest done callback if none is provided', function (testDone) {
// we want to ensure that the 'done' callbacks are nested correctly
// (executed in reverse order of definition)
const execution = []
const testMiddlewareA = function (context, next, done) {
execution.push('middlewareA')
next(function () {
execution.push('doneA')
done()
})
}
const testMiddlewareB = function (context, next, done) {
execution.push('middlewareB')
next()
}
this.middleware.register(testMiddlewareA)
this.middleware.register(testMiddlewareB)
const allDone = function () {
expect(execution).to.deep.equal(['middlewareA', 'middlewareB', 'doneA'])
testDone()
}
this.middleware.execute(
{},
// Short circuit at the bottom of the middleware stack
(_, done) => done(),
allDone
)
})
describe('error handling', function () {
it('does not execute subsequent middleware after the error is thrown', function (testDone) {
const middlewareExecution = []
const testMiddlewareA = function (context, next, done) {
middlewareExecution.push('A')
next(done)
}
const testMiddlewareB = function (context, next, done) {
middlewareExecution.push('B')
throw new Error()
}
const testMiddlewareC = function (context, next, done) {
middlewareExecution.push('C')
next(done)
}
this.middleware.register(testMiddlewareA)
this.middleware.register(testMiddlewareB)
this.middleware.register(testMiddlewareC)
const middlewareFinished = sinon.spy()
const middlewareFailed = () => {
expect(middlewareFinished).to.not.have.been.called
expect(middlewareExecution).to.deep.equal(['A', 'B'])
testDone()
}
this.middleware.execute(
{},
middlewareFinished,
middlewareFailed
)
})
it('emits an error event', function (testDone) {
const testResponse = {}
const theError = new Error()
const testMiddleware = function (context, next, done) {
throw theError
}
this.middleware.register(testMiddleware)
this.robot.emit = sinon.spy(function (name, err, response) {
expect(name).to.equal('error')
expect(err).to.equal(theError)
expect(response).to.equal(testResponse)
})
const middlewareFinished = sinon.spy()
const middlewareFailed = () => {
expect(this.robot.emit).to.have.been.called
testDone()
}
this.middleware.execute(
{ response: testResponse },
middlewareFinished,
middlewareFailed
)
})
it('unwinds the middleware stack (calling all done functions)', function (testDone) {
let extraDoneFunc = null
const testMiddlewareA = function (context, next, done) {
// Goal: make sure that the middleware stack is unwound correctly
extraDoneFunc = sinon.spy(done)
next(extraDoneFunc)
}
const testMiddlewareB = function (context, next, done) {
throw new Error()
}
this.middleware.register(testMiddlewareA)
this.middleware.register(testMiddlewareB)
const middlewareFinished = sinon.spy()
const middlewareFailed = function () {
// Sanity check that the error was actually thrown
expect(middlewareFinished).to.not.have.been.called
expect(extraDoneFunc).to.have.been.called
testDone()
}
this.middleware.execute(
{},
middlewareFinished,
middlewareFailed
)
})
})
})
describe('#register', function () {
it('adds to the list of middleware', function () {
const testMiddleware = function (context, next, done) {}
this.middleware.register(testMiddleware)
expect(this.middleware.stack).to.include(testMiddleware)
})
it('validates the arity of middleware', function () {
const testMiddleware = function (context, next, done, extra) {}
expect(() => this.middleware.register(testMiddleware)).to.throw(/Incorrect number of arguments/)
})
})
})
// Per the documentation in docs/scripting.md
// Any new fields that are exposed to middleware should be explicitly
// tested for.
describe('Public Middleware APIs', function () {
beforeEach(async function () {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false
})
mockery.registerMock('hubot-mock-adapter', require('./fixtures/mock-adapter.js'))
process.env.EXPRESS_PORT = 0
this.robot = new Robot('mock-adapter', true, 'TestHubot')
await this.robot.loadAdapter()
this.robot.run
// Re-throw AssertionErrors for clearer test failures
this.robot.on('error', function (name, err, response) {
if (__guard__(err != null ? err.constructor : undefined, x => x.name) === 'AssertionError') {
process.nextTick(function () {
throw err
})
}
})
this.user = this.robot.brain.userForId('1', {
name: 'hubottester',
room: '#mocha'
})
// Dummy middleware
this.middleware = sinon.spy((context, next, done) => next(done))
this.testMessage = new TextMessage(this.user, 'message123')
this.robot.hear(/^message123$/, function (response) {})
this.testListener = this.robot.listeners[0]
})
afterEach(function () {
mockery.disable()
this.robot.shutdown()
})
describe('listener middleware context', function () {
beforeEach(function () {
this.robot.listenerMiddleware((context, next, done) => {
this.middleware(context, next, done)
})
})
describe('listener', function () {
it('is the listener object that matched', function (testDone) {
this.robot.receive(this.testMessage, () => {
expect(this.middleware).to.have.been.calledWithMatch(
sinon.match.has('listener',
sinon.match.same(this.testListener)), // context
sinon.match.any, // next
sinon.match.any // done
)
testDone()
})
})
it('has options.id (metadata)', function (testDone) {
this.robot.receive(this.testMessage, () => {
expect(this.middleware).to.have.been.calledWithMatch(
sinon.match.has('listener',
sinon.match.has('options',
sinon.match.has('id'))), // context
sinon.match.any, // next
sinon.match.any // done
)
testDone()
})
})
})
describe('response', () =>
it('is a Response that wraps the message', function (testDone) {
this.robot.receive(this.testMessage, () => {
expect(this.middleware).to.have.been.calledWithMatch(
sinon.match.has('response',
sinon.match.instanceOf(Response).and(
sinon.match.has('message',
sinon.match.same(this.testMessage)))), // context
sinon.match.any, // next
sinon.match.any // done
)
testDone()
})
})
)
})
describe('receive middleware context', function () {
beforeEach(function () {
this.robot.receiveMiddleware((context, next, done) => {
this.middleware(context, next, done)
})
})
describe('response', () =>
it('is a match-less Response object', function (testDone) {
this.robot.receive(this.testMessage, () => {
expect(this.middleware).to.have.been.calledWithMatch(
sinon.match.has('response',
sinon.match.instanceOf(Response).and(
sinon.match.has('message',
sinon.match.same(this.testMessage)))), // context
sinon.match.any, // next
sinon.match.any // done
)
testDone()
})
})
)
})
describe('next', function () {
beforeEach(function () {
this.robot.listenerMiddleware((context, next, done) => {
this.middleware(context, next, done)
})
})
it('is a function with arity one', function (testDone) {
this.robot.receive(this.testMessage, () => {
expect(this.middleware).to.have.been.calledWithMatch(
sinon.match.any, // context
sinon.match.func.and(
sinon.match.has('length',
sinon.match(1))), // next
sinon.match.any // done
)
testDone()
})
})
})
describe('done', function () {
beforeEach(function () {
this.robot.listenerMiddleware((context, next, done) => {
this.middleware(context, next, done)
})
})
it('is a function with arity zero', function (testDone) {
this.robot.receive(this.testMessage, () => {
expect(this.middleware).to.have.been.calledWithMatch(
sinon.match.any, // context
sinon.match.any, // next
sinon.match.func.and(
sinon.match.has('length',
sinon.match(0))) // done
)
testDone()
})
})
})
})
})
function __guard__ (value, transform) {
(typeof value !== 'undefined' && value !== null) ? transform(value) : undefined
}