-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
connector.test.js
130 lines (103 loc) · 3.25 KB
/
connector.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
const { expect } = require('chai')
const { stub, spy, resetHistory } = require('sinon')
const connector = require('src')
const ERRORS = require('src/errors')
const exampleV2 = require('test/fixtures/exampleV2.json')
const exampleV3 = require('test/fixtures/exampleV3.json')
describe('src/connector', () => {
const mockApi = {
versions: () => {}
}
const fakeSecurity = {
'admin,identity.basic,identity.email': () => {}
}
const mockApp = {
get: stub()
}
const onCreateRoute = spy()
context('given an invalid document', () => {
after(resetHistory)
it('throws an error', () =>
expect(() => {
connector(mockApi, {})
}).to.throw(ERRORS.INVALID_VERSION))
})
context('given a valid document', () => {
const doTest = doc => {
context('without options', () => {
let connect
before(() => {
connect = connector(mockApi, doc)
connect(mockApp)
})
after(resetHistory)
it('returned a function', () => {
expect(connect).to.be.a('function')
})
it('called app.get for each route', () => {
expect(mockApp.get.callCount).to.equal(4)
})
})
context('with options', () => {
context('with a security option', () => {
before(() => {
const connect = connector(mockApi, doc, {
security: fakeSecurity
})
connect(mockApp)
})
after(resetHistory)
it('called app.get for each route', () => {
expect(mockApp.get.callCount).to.equal(4)
})
})
context('with onCreateRoute callback', () => {
before(() => {
const connect = connector(mockApi, doc, {
security: fakeSecurity,
onCreateRoute
})
connect(mockApp)
})
after(resetHistory)
it('called app.get for each route', () => {
expect(mockApp.get.callCount).to.equal(4)
})
it('called onCreateRoute for each route', () => {
expect(onCreateRoute.callCount).to.equal(4)
})
})
context('with middleware', () => {
const middleTest = () => {}
before(() => {
const connect = connector(mockApi, doc, {
security: fakeSecurity,
middleware: { middleTest }
})
connect(mockApp)
})
after(resetHistory)
it('called app.get for each route', () => {
expect(mockApp.get.callCount).to.equal(4)
})
it("called get('/') with the versions handler", () => {
expect(mockApp.get).to.have.been.calledWith('/', mockApi.versions)
})
it("passed the middleware into the call to get('/api/v1/test') after the security middleware", () => {
expect(mockApp.get).to.have.been.calledWith(
'/api/v1/test',
fakeSecurity['admin,identity.basic,identity.email'],
middleTest
)
})
})
})
}
context('given a Swagger version 2 doc', () => {
doTest(exampleV2)
})
context('given an OpenAPI version 3 doc', () => {
doTest(exampleV3)
})
})
})