-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.test.ts
254 lines (233 loc) · 5.94 KB
/
index.test.ts
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
import untypedTest, { TestFn } from 'ava'
import { Redis } from 'ioredis'
import { expose, getTLSOptions, start } from '../src'
import { Adapter, AdapterDependencies, AdapterEndpoint } from '../src/adapter'
import { RedisCache } from '../src/cache'
import { AdapterConfig, buildAdapterSettings } from '../src/config'
import { NopTransport, RedisMock, TestAdapter } from '../src/util/testing-utils'
const test = untypedTest as TestFn<{
testAdapter: TestAdapter
}>
test.beforeEach(async (t) => {
const adapter = new Adapter({
name: 'TEST',
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
t.context.testAdapter = await TestAdapter.start(adapter, t.context)
})
test('health endpoint returns health OK', async (t) => {
const response = await t.context.testAdapter.getHealth()
t.deepEqual(response.json(), {
message: 'OK',
version: process.env['npm_package_version'],
})
})
test('MTLS_ENABLED with no TLS params should error', async (t) => {
const config = new AdapterConfig(
{},
{
envDefaultOverrides: {
MTLS_ENABLED: true,
},
},
)
const adapter = new Adapter({
name: 'TEST',
config,
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
try {
await start(adapter)
} catch (e: unknown) {
t.is(
(e as Error).message,
'TLS_PRIVATE_KEY, TLS_PUBLIC_KEY, and TLS_CA environment variables are required when MTLS_ENABLED is set to true.',
)
}
})
test('Having both TLS_ENABLED and MTLS_ENABLED should throw an error', async (t) => {
const config = new AdapterConfig(
{},
{
envDefaultOverrides: {
MTLS_ENABLED: true,
TLS_ENABLED: true,
},
},
)
const adapter = new Adapter({
name: 'TEST',
config,
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
try {
await start(adapter)
} catch (e: unknown) {
t.is((e as Error).message, 'TLS_ENABLED and MTLS_ENABLED cannot both be set to true.')
}
})
test('MTLS_ENABLED connection with incorrect params should error', async (t) => {
const config = new AdapterConfig(
{},
{
envDefaultOverrides: {
MTLS_ENABLED: true,
TLS_PRIVATE_KEY: 'dGVzdA==',
TLS_PUBLIC_KEY: 'dGVzdA==',
TLS_CA: 'dGVzdA==',
},
},
)
const adapter = new Adapter({
name: 'TEST',
config,
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
try {
await start(adapter)
} catch (_) {
t.pass()
}
})
test('getTLSOptions should return an empty object if TLS and mTLS are not enabled', async (t) => {
const adapterSettings = buildAdapterSettings({})
t.deepEqual(getTLSOptions(adapterSettings), {})
})
test('requestCert should be false when TLS_ENABLED is set to true', async (t) => {
const adapterSettings = buildAdapterSettings({})
adapterSettings.TLS_ENABLED = true
adapterSettings.TLS_PRIVATE_KEY = 'dGVzdA=='
adapterSettings.TLS_PUBLIC_KEY = 'dGVzdA=='
adapterSettings.TLS_CA = 'dGVzdA=='
t.deepEqual(getTLSOptions(adapterSettings), {
https: {
key: 'dGVzdA==',
cert: 'dGVzdA==',
ca: 'dGVzdA==',
passphrase: '',
requestCert: false,
},
})
})
test('requestCert should be true when MTLS_ENABLED is set to true', async (t) => {
const adapterSettings = buildAdapterSettings({})
adapterSettings.MTLS_ENABLED = true
adapterSettings.TLS_PRIVATE_KEY = 'dGVzdA=='
adapterSettings.TLS_PUBLIC_KEY = 'dGVzdA=='
adapterSettings.TLS_CA = 'dGVzdA=='
t.deepEqual(getTLSOptions(adapterSettings), {
https: {
key: 'dGVzdA==',
cert: 'dGVzdA==',
ca: 'dGVzdA==',
passphrase: '',
requestCert: true,
},
})
})
test('Adapter writer mode api disabled', async (t) => {
process.env['CACHE_TYPE'] = 'redis'
const config = new AdapterConfig(
{},
{
envDefaultOverrides: {
EA_MODE: 'writer',
CACHE_LOCK_DEFERRAL_MS: 0,
},
},
)
const adapter = new Adapter({
name: 'TEST',
config,
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
const cache = new RedisCache(new RedisMock() as unknown as Redis, 10000) // Fake redis
const dependencies: Partial<AdapterDependencies> = {
cache,
}
const api = await expose(adapter, dependencies)
t.is(api, undefined)
})
test('Initialize adapter (error)', async (t) => {
try {
await start({} as Adapter)
} catch (e: unknown) {
t.is(
(e as Error).message,
'The adapter has not been initialized as an instance of the Adapter class, exiting.',
)
}
})
test('Initialize adapter twice (error)', async (t) => {
const adapter = new Adapter({
name: 'TEST',
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
const cache = new RedisCache(new RedisMock() as unknown as Redis, 10000) // Fake redis
const dependencies: Partial<AdapterDependencies> = {
cache,
}
try {
await start(adapter, dependencies)
await start(adapter, dependencies)
t.fail()
} catch (e: unknown) {
t.is((e as Error).message, 'This adapter has already been initialized!')
}
})
test('Throw error if EA mode is not RW and cache type is local', async (t) => {
process.env['CACHE_TYPE'] = 'local'
const config = new AdapterConfig(
{},
{
envDefaultOverrides: {
EA_MODE: 'writer',
},
},
)
const adapter = new Adapter({
name: 'TEST',
config,
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
try {
await expose(adapter)
} catch (e: unknown) {
t.is((e as Error).message, 'EA mode cannot be writer while cache type is local')
}
})