-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdebug-endpoints.test.ts
More file actions
181 lines (162 loc) · 4.8 KB
/
debug-endpoints.test.ts
File metadata and controls
181 lines (162 loc) · 4.8 KB
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
import test from 'ava'
import { start } from '../src'
import { Adapter } from '../src/adapter'
import { AdapterConfig } from '../src/config'
import { DebugPageSetting } from '../src/util/settings'
test.serial('debug endpoints return 404 if env var is omitted', async (t) => {
process.env['DEBUG_ENDPOINTS'] = undefined
const adapter = new Adapter({
name: 'TEST',
endpoints: [],
})
const { api } = await start(adapter)
const expect404ForPath = async (path: string) => {
const error = await api?.inject({ path })
t.is(error?.statusCode, 404)
}
await expect404ForPath('/debug/settings')
await expect404ForPath('/debug/settings/raw')
})
test.serial('debug endpoints return 404 if env var is false', async (t) => {
process.env['DEBUG_ENDPOINTS'] = 'false'
const adapter = new Adapter({
name: 'TEST',
endpoints: [],
})
const { api } = await start(adapter)
const expect404ForPath = async (path: string) => {
const error = await api?.inject({ path })
t.is(error?.statusCode, 404)
}
await expect404ForPath('/debug/settings')
await expect404ForPath('/debug/settings/raw')
})
test.serial('/debug/settings/raw endpoint returns expected values', async (t) => {
process.env['DEBUG_ENDPOINTS'] = 'true'
process.env['API_KEY'] = '12312341234'
const config = new AdapterConfig(
{
API_KEY: {
description: 'Api key',
type: 'string',
sensitive: true,
required: true,
},
},
{
envDefaultOverrides: {
REQUESTER_SLEEP_BEFORE_REQUEUEING_MS: 9999,
},
},
)
const adapter = new Adapter({
name: 'TEST',
endpoints: [],
config,
})
const { api } = await start(adapter)
const settingsResponse = await api?.inject({ path: '/debug/settings/raw' })
if (!settingsResponse?.body) {
t.fail()
return
}
const parsedResponse = settingsResponse?.json() as DebugPageSetting[]
// Test that framework setting is correctly set
t.deepEqual(
parsedResponse.find((s) => s.name === 'DEBUG_ENDPOINTS'),
{
type: 'boolean',
description:
'Whether to enable debug enpoints (/debug/*) for this adapter. Enabling them might consume more resources.',
name: 'DEBUG_ENDPOINTS',
required: false,
default: false,
customSetting: false,
value: true,
},
)
// Test that env override is correctly accounted for
t.deepEqual(
parsedResponse.find((s) => s.name === 'REQUESTER_SLEEP_BEFORE_REQUEUEING_MS'),
{
type: 'number',
description:
'Time to sleep after a failed HTTP request before re-queueing the request (in ms)',
name: 'REQUESTER_SLEEP_BEFORE_REQUEUEING_MS',
required: false,
default: 0,
customSetting: false,
envDefaultOverride: 9999,
value: 9999,
},
)
// Test that custom adapter setting is correctly set and censored
t.deepEqual(
parsedResponse.find((s) => s.name === 'API_KEY'),
{
type: 'string',
description: 'Api key',
name: 'API_KEY',
required: true,
sensitive: true,
customSetting: true,
value: '[API_KEY REDACTED]',
},
)
// Test that setting with default is assigned that value if not set in env vars
t.deepEqual(
parsedResponse.find((s) => s.name === 'API_TIMEOUT'),
{
type: 'number',
default: 30000,
description:
'The number of milliseconds a request can be pending before returning a timeout error for data provider request',
name: 'API_TIMEOUT',
required: false,
customSetting: false,
value: 30000,
},
)
// Test that setting with no default is correctly processed
t.deepEqual(
parsedResponse.find((s) => s.name === 'RATE_LIMIT_API_TIER'),
{
type: 'string',
description:
'Rate limiting tier to use from the available options for the adapter. If not present, the adapter will run using the first tier on the list.',
name: 'RATE_LIMIT_API_TIER',
required: false,
sensitive: false,
customSetting: false,
},
)
})
test.serial('/debug/settings returns html response', async (t) => {
process.env['DEBUG_ENDPOINTS'] = 'true'
process.env['API_KEY'] = '12312341234'
const config = new AdapterConfig(
{
API_KEY: {
description: 'Api key',
type: 'string',
sensitive: true,
required: true,
},
},
{
envDefaultOverrides: {
REQUESTER_SLEEP_BEFORE_REQUEUEING_MS: 9999,
},
},
)
const adapter = new Adapter({
name: 'TEST',
endpoints: [],
config,
})
const { api } = await start(adapter)
const settingsResponse = await api?.inject({ path: '/debug/settings' })
const text = settingsResponse?.payload.trim()
t.is(settingsResponse?.headers['content-type'], 'text/html')
t.true(text?.startsWith('<html>') && text?.endsWith('</html>'))
})