-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.test.js
160 lines (138 loc) · 4.22 KB
/
connections.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
/* eslint-disable no-console */
const redisClient = require('redis-mock').createClient()
const { IS_ACCESSIBLE } = require('./test-utils')
const connections = require('./connections')
const mockApiConfig = {
testApi: {
https: false,
port: 3001,
host: 'localhost',
proxyBasePath: '/api/test',
required: true,
},
}
const mockApiKeyConfig = {
testApi: '1234',
}
const logConsole = false
const mockLogger = {
debug: logConsole ? console.log : () => {},
error: logConsole ? console.log : () => {},
warn: logConsole ? console.log : () => {},
info: logConsole ? console.log : () => {},
}
const opts = {
log: mockLogger,
redis() {
return Promise.resolve(redisClient)
},
timeout: 100,
cache: {
testApi: mockApiConfig,
},
checkAPIs: true, // performs api-key checks against the apis, if a 'required' check fails, the app will exit. Required apis are specified in the config
}
const paths = {}
const apicall = uri => {
const myUri = uri.uri ? uri.uri : uri
return new Promise((resolve, reject) => {
if (paths[myUri]) {
resolve(paths[myUri].shift())
} else {
reject(new Error('Path ' + myUri + ' not found.'))
}
})
}
jest.mock('./basic', () =>
jest.fn().mockImplementation(() => ({
getAsync: jest.fn(apicall),
}))
)
describe('Testing connection', () => {
const originalProcessExit = process.exit
beforeAll(() => {
process.exit = jest.fn()
})
it(IS_ACCESSIBLE, () => expect(connections.setup).toBeFunction())
it('should throw exception if no api config is provided or not object', () => {
expect(() => connections.setup('INVALID', mockApiKeyConfig, opts)).toThrowError(/Apis config is required/)
})
it('should shut down on bad API key', done => {
paths['/api/test/_paths'] = [{ statusCode: 200, body: {} }]
paths['/api/test/_checkAPIkey'] = [{ statusCode: 401, body: {} }]
connections.setup(mockApiConfig, mockApiKeyConfig, opts)
setTimeout(() => {
expect(process.exit).toBeCalledWith(1)
done()
}, 500) // wait for setup to finish
})
it('should set up connections', done => {
paths['/api/test/_paths'] = [
{
statusCode: 200,
body: {
path1: {
uri: '/api/test/v1/path1/:param1',
method: 'GET',
apikey: {
scope_required: true,
scopes: ['read'],
type: 'api_key',
},
},
},
},
]
paths['/api/test/_checkAPIkey'] = [{ statusCode: 200, body: {} }]
const output = connections.setup(mockApiConfig, mockApiKeyConfig, opts)
setTimeout(() => {
expect(process.exit).not.toBeCalled()
expect(output.testApi.client.getAsync).toBeCalled()
done()
}, 500) // wait for setup to finish
})
it('should not test connection if doNotCallPathsEndpoint is set', done => {
paths['/api/test/_checkAPIkey'] = [{ statusCode: 200, body: {} }]
const output = connections.setup(
{ testApi: { ...mockApiConfig.testApi, doNotCallPathsEndpoint: true } },
mockApiKeyConfig,
{ ...opts }
)
setTimeout(() => {
expect(output.testApi.connected).toBeTrue()
expect(process.exit).not.toBeCalled()
// Should only be called once to check API key
expect(output.testApi.client.getAsync).toBeCalledTimes(1)
done()
}, 500) // wait for setup to finish
})
it("should retry the api connection if it doesn't gets a bad status code response", done => {
paths['/api/test/_paths'] = [
{ statusCode: 503, body: { message: 'Service Unavailable' } },
{
statusCode: 200,
body: {
path1: {
uri: '/api/test/v1/path1/:param1',
method: 'GET',
apikey: {
scope_required: true,
scopes: ['read'],
type: 'api_key',
},
},
},
},
]
paths['/api/test/_checkAPIkey'] = [{ statusCode: 200, body: {} }]
const output = connections.setup(mockApiConfig, mockApiKeyConfig, opts)
setTimeout(() => {
expect(output.testApi.connected).toBeTrue()
expect(process.exit).not.toBeCalled()
done()
}, 500) // wait for setup to finish
})
afterAll(() => {
process.exit = originalProcessExit
})
})