forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.coffee
428 lines (343 loc) · 13.2 KB
/
middleware_test.coffee
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
# Assertions and Stubbing
chai = require 'chai'
sinon = require 'sinon'
chai.use require 'sinon-chai'
{ expect } = chai
mockery = require 'mockery'
# Hubot classes
Robot = require '../src/robot.coffee'
{ CatchAllMessage, EnterMessage, TextMessage } = require '../src/message'
Adapter = require '../src/adapter'
Response = require '../src/response'
Middleware = require '../src/middleware'
# Preload the Hubot mock adapter but substitute in the latest version of Adapter
mockery.enable()
mockery.registerAllowable 'hubot-mock-adapter'
mockery.registerAllowable 'lodash' # hubot-mock-adapter uses lodash
# Force hubot-mock-adapter to use the latest version of Adapter
mockery.registerMock 'hubot/src/adapter', Adapter
# Load the mock adapter into the cache
require 'hubot-mock-adapter'
# We're done with mockery
mockery.deregisterMock 'hubot/src/adapter'
mockery.disable()
describe 'Middleware', ->
describe 'Unit Tests', ->
beforeEach ->
@robot =
# Stub out event emitting
emit: sinon.spy()
@middleware = new Middleware(@robot)
describe '#execute', ->
it 'executes synchronous middleware', (testDone) ->
testMiddleware = sinon.spy (context, next, done) =>
next(done)
@middleware.register testMiddleware
middlewareFinished = ->
expect(testMiddleware).to.have.been.called
testDone()
@middleware.execute(
{}
(_, done) -> done()
middlewareFinished
)
it 'executes asynchronous middleware', (testDone) ->
testMiddleware = sinon.spy (context, next, done) ->
# Yield to the event loop
process.nextTick ->
next(done)
@middleware.register testMiddleware
middlewareFinished = (context, done) ->
expect(testMiddleware).to.have.been.called
testDone()
@middleware.execute(
{}
(_, done) -> done()
middlewareFinished
)
it 'passes the correct arguments to each middleware', (testDone) ->
testContext = {}
# Pull the Robot in scope for simpler callbacks
testRobot = @robot
testMiddleware = (context, next, done) ->
# Break out of middleware error handling so assertion errors are
# more visible
process.nextTick ->
# Check that variables were passed correctly
expect(context).to.equal(testContext)
next(done)
@middleware.register testMiddleware
@middleware.execute(
testContext
(_, done) -> done()
-> testDone()
)
it 'executes all registered middleware in definition order', (testDone) ->
middlewareExecution = []
testMiddlewareA = (context, next, done) =>
middlewareExecution.push('A')
next(done)
testMiddlewareB = (context, next, done) ->
middlewareExecution.push('B')
next(done)
@middleware.register testMiddlewareA
@middleware.register testMiddlewareB
middlewareFinished = ->
expect(middlewareExecution).to.deep.equal(['A','B'])
testDone()
@middleware.execute(
{}
(_, done) -> done()
middlewareFinished
)
it 'executes the next callback after the function returns when there is no middleware', (testDone) ->
finished = false
@middleware.execute(
{}
->
expect(finished).to.be.ok
testDone()
->
)
finished = true
it 'always executes middleware after the function returns', (testDone) ->
finished = false
@middleware.register (context, next, done) ->
expect(finished).to.be.ok
testDone()
@middleware.execute {}, (->), (->)
finished = true
it 'creates a default "done" function', (testDone) ->
finished = false
@middleware.register (context, next, done) ->
expect(finished).to.be.ok
testDone()
# we're testing the lack of a third argument here.
@middleware.execute {}, (->)
finished = true
it 'does the right thing with done callbacks', (testDone) ->
# we want to ensure that the 'done' callbacks are nested correctly
# (executed in reverse order of definition)
execution = []
testMiddlewareA = (context, next, done) ->
execution.push 'middlewareA'
next ->
execution.push 'doneA'
done()
testMiddlewareB = (context, next, done) ->
execution.push 'middlewareB'
next ->
execution.push 'doneB'
done()
@middleware.register testMiddlewareA
@middleware.register testMiddlewareB
allDone = ->
expect(execution).to.deep.equal(['middlewareA', 'middlewareB', 'doneB', 'doneA'])
testDone()
@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', (testDone) ->
# we want to ensure that the 'done' callbacks are nested correctly
# (executed in reverse order of definition)
execution = []
testMiddlewareA = (context, next, done) ->
execution.push 'middlewareA'
next ->
execution.push 'doneA'
done()
testMiddlewareB = (context, next, done) ->
execution.push 'middlewareB'
next()
@middleware.register testMiddlewareA
@middleware.register testMiddlewareB
allDone = ->
expect(execution).to.deep.equal(['middlewareA', 'middlewareB', 'doneA'])
testDone()
@middleware.execute(
{}
# Short circuit at the bottom of the middleware stack
(_, done) -> done()
allDone
)
describe 'error handling', ->
it 'does not execute subsequent middleware after the error is thrown', (testDone) ->
middlewareExecution = []
testMiddlewareA = (context, next, done) ->
middlewareExecution.push('A')
next(done)
testMiddlewareB = (context, next, done) ->
middlewareExecution.push('B')
throw new Error
testMiddlewareC = (context, next, done) ->
middlewareExecution.push('C')
next(done)
@middleware.register testMiddlewareA
@middleware.register testMiddlewareB
@middleware.register testMiddlewareC
middlewareFinished = sinon.spy()
middlewareFailed = () =>
expect(middlewareFinished).to.not.have.been.called
expect(middlewareExecution).to.deep.equal(['A','B'])
testDone()
@middleware.execute(
{}
middlewareFinished
middlewareFailed
)
it 'emits an error event', (testDone) ->
testResponse = {}
theError = new Error
testMiddleware = (context, next, done) ->
throw theError
@middleware.register testMiddleware
@robot.emit = sinon.spy (name, err, response) ->
expect(name).to.equal('error')
expect(err).to.equal(theError)
expect(response).to.equal(testResponse)
middlewareFinished = sinon.spy()
middlewareFailed = () =>
expect(@robot.emit).to.have.been.called
testDone()
@middleware.execute(
{response: testResponse},
middlewareFinished,
middlewareFailed
)
it 'unwinds the middleware stack (calling all done functions)', (testDone) ->
extraDoneFunc = null
testMiddlewareA = (context, next, done) ->
# Goal: make sure that the middleware stack is unwound correctly
extraDoneFunc = sinon.spy done
next extraDoneFunc
testMiddlewareB = (context, next, done) ->
throw new Error
@middleware.register testMiddlewareA
@middleware.register testMiddlewareB
middlewareFinished = sinon.spy()
middlewareFailed = ->
# Sanity check that the error was actually thrown
expect(middlewareFinished).to.not.have.been.called
expect(extraDoneFunc).to.have.been.called
testDone()
@middleware.execute(
{}
middlewareFinished
middlewareFailed
)
describe '#register', ->
it 'adds to the list of middleware', ->
testMiddleware = (context, next, done) ->
@middleware.register testMiddleware
expect(@middleware.stack).to.include(testMiddleware)
it 'validates the arity of middleware', ->
testMiddleware = (context, next, done, extra) ->
expect(=> @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', ->
beforeEach ->
@robot = new Robot null, 'mock-adapter', yes, 'TestHubot'
@robot.run
# Re-throw AssertionErrors for clearer test failures
@robot.on 'error', (name, err, response) ->
if err?.constructor?.name == "AssertionError"
process.nextTick ->
throw err
@user = @robot.brain.userForId '1', {
name: 'hubottester'
room: '#mocha'
}
# Dummy middleware
@middleware = sinon.spy (context, next, done) ->
next(done)
@testMessage = new TextMessage @user, 'message123'
@robot.hear /^message123$/, (response) ->
@testListener = @robot.listeners[0]
afterEach ->
@robot.server.close()
@robot.shutdown()
describe 'listener middleware context', ->
beforeEach ->
@robot.listenerMiddleware (context, next, done) =>
@middleware.call @, context, next, done
describe 'listener', ->
it 'is the listener object that matched', (testDone) ->
@robot.receive @testMessage, () =>
expect(@middleware).to.have.been.calledWithMatch(
sinon.match.has('listener',
sinon.match.same(@testListener)) # context
sinon.match.any # next
sinon.match.any # done
)
testDone()
it 'has options.id (metadata)', (testDone) ->
@robot.receive @testMessage, () =>
expect(@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', (testDone) ->
@robot.receive @testMessage, () =>
expect(@middleware).to.have.been.calledWithMatch(
sinon.match.has('response',
sinon.match.instanceOf(Response).and(
sinon.match.has('message',
sinon.match.same(@testMessage)))) # context
sinon.match.any # next
sinon.match.any # done
)
testDone()
describe 'receive middleware context', ->
beforeEach ->
@robot.receiveMiddleware (context, next, done) =>
@middleware.call @, context, next, done
describe 'response', ->
it 'is a match-less Response object', (testDone) ->
@robot.receive @testMessage, () =>
expect(@middleware).to.have.been.calledWithMatch(
sinon.match.has('response',
sinon.match.instanceOf(Response).and(
sinon.match.has('message',
sinon.match.same(@testMessage)))) # context
sinon.match.any # next
sinon.match.any # done
)
testDone()
describe 'next', ->
beforeEach ->
@robot.listenerMiddleware (context, next, done) =>
@middleware.call @, context, next, done
it 'is a function with arity one', (testDone) ->
@robot.receive @testMessage, () =>
expect(@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', ->
beforeEach ->
@robot.listenerMiddleware (context, next, done) =>
@middleware.call @, context, next, done
it 'is a function with arity zero', (testDone) ->
@robot.receive @testMessage, () =>
expect(@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()