forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.test.js
More file actions
273 lines (248 loc) · 9.48 KB
/
analysis.test.js
File metadata and controls
273 lines (248 loc) · 9.48 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
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
import { afterEach } from 'mocha'
import analysis from '../src/analysis.js'
import { expect } from 'chai'
import { rest } from 'msw'
import { setupServer } from 'msw/node'
import sinon from 'sinon'
// utility function creating a dummy server, intercepting a handler,
// running a test, and shutting the server down
function interceptAndRun(handler, test) {
return async () => {
let server = setupServer(handler)
server.listen()
return Promise.resolve(test(server))
.finally(() => {
server.resetHandlers()
server.close()
});
};
}
function determineResponse(req, res, ctx) {
let response
if (req.headers.get("ex-snyk-token") === null) {
response = res(ctx.status(400));
} else if (req.headers.get("ex-snyk-token") === "good-dummy-token") {
response = res(ctx.status(200));
} else {
response = res(ctx.status(401));
}
return response
}
suite('testing the analysis module for sending api requests', () => {
let backendUrl = 'http://url.lru' // dummy backend url will be used for fake server
// fake provided data, in prod will be provided by the provider and used for creating requests
let fakeProvided = {
ecosystem: 'dummy-ecosystem',
content: 'dummy-content',
contentType: 'dummy-content-type'
};
test('invoking the requestComponent should return a json report', interceptAndRun(
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
// interception route, will return ok response for our fake content type
if (fakeProvided.contentType === req.headers.get('content-type')) {
return res(ctx.json({dummy: 'response'}))
}
return res(ctx.status(400))
}),
async () => {
let fakeContent = 'i-am-manifest-content'
// stub the provideComponent function to return the fake provided data for our fake manifest
let componentProvideStub = sinon.stub()
componentProvideStub.withArgs(fakeContent).returns(fakeProvided)
// fake providers hosts our stubbed provideStack function
let fakeProvider = {
provideComponent: componentProvideStub,
provideStack: () => {}, // not required for this test
isSupported: () => {} // not required for this test
}
// verify response as expected
let res = await analysis.requestComponent(fakeProvider, fakeContent, backendUrl)
expect(res).to.deep.equal({dummy: 'response'})
}
))
suite('testing the requestStack function', () => {
let fakeManifest = 'fake-file.typ'
// stub the provideStack function to return the fake provided data for our fake manifest
let stackProviderStub = sinon.stub()
stackProviderStub.withArgs(fakeManifest).returns(fakeProvided)
// fake providers hosts our stubbed provideStack function
let fakeProvider = {
provideComponent: () => {}, // not required for this test
provideStack: stackProviderStub,
isSupported: () => {} // not required for this test
}
test('invoking the requestStack for html should return a string report', interceptAndRun(
// interception route, will return ok response for our fake content type
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
if (fakeProvided.contentType === req.headers.get('content-type')) {
return res(ctx.text('<html lang="en">html-content</html>'))
}
return res(ctx.status(400))
}),
async () => {
// verify response as expected
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl, true)
expect(res).to.equal('<html lang="en">html-content</html>')
}
))
test('invoking the requestStack for non-html should return a json report', interceptAndRun(
// interception route, will return ok response for our fake content type
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
if (fakeProvided.contentType === req.headers.get('content-type')) {
return res(ctx.json({dummy: 'response'}))
}
return res(ctx.status(400))
}),
async () => {
// verify response as expected
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
expect(res).to.deep.equal({dummy: 'response'})
}
))
})
suite('testing the validateToken function', () => {
test('invoking validateToken function with good token', interceptAndRun(
// interception route, will return ok response for our fake content type
rest.get(`${backendUrl}/api/v3/token`, (req, res, ctx) => {
return determineResponse(req, res, ctx);
}),
async () => {
let options = {
'EXHORT_SNYK_TOKEN': 'good-dummy-token'
}
// verify response as expected
let res = await analysis.validateToken(backendUrl, options)
expect(res).to.equal(200)
}
))
test('invoking validateToken function with bad token', interceptAndRun(
// interception route, will return ok response for our fake content type
rest.get(`${backendUrl}/api/v3/token`, (req, res, ctx) => {
return determineResponse(req, res, ctx);
}),
async () => {
let options = {
'EXHORT_SNYK_TOKEN': 'bad-dummy-token'
}
// verify response as expected
let res = await analysis.validateToken(backendUrl, options)
expect(res).to.equal(401)
}
))
test('invoking validateToken function without token', interceptAndRun(
// interception route, will return ok response for our fake content type
rest.get(`${backendUrl}/api/v3/token`, (req, res, ctx) => {
return determineResponse(req, res, ctx);
}),
async () => {
let options = {
}
// verify response as expected
let res = await analysis.validateToken(backendUrl, options)
expect(res).to.equal(400)
}
))
})
suite('verify environment variables to token headers mechanism', () => {
let fakeManifest = 'fake-file.typ'
// stub the provideStack function to return the fake provided data for our fake manifest
let stackProviderStub = sinon.stub()
stackProviderStub.withArgs(fakeManifest).returns(fakeProvided)
// fake providers hosts our stubbed provideStack function
let fakeProvider = {
provideComponent: () => {}, // not required for this test
provideStack: stackProviderStub,
isSupported: () => {} // not required for this test
};
afterEach(() => delete process.env['EXHORT_SNYK_TOKEN'])
test('when the relevant token environment variables are set, verify corresponding headers are included', interceptAndRun(
// interception route, will return ok response if found the expected token
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
if ('dummy-snyk-token' === req.headers.get('ex-snyk-token')) {
return res(ctx.json({ok: 'ok'}))
}
return res(ctx.status(400))
}),
async () => {
process.env['EXHORT_SNYK_TOKEN'] = 'dummy-snyk-token'
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
expect(res).to.deep.equal({ok: 'ok'})
}
))
test('when the relevant token environment variables are not set, verify no corresponding headers are included', interceptAndRun(
// interception route, will return ok response if found the expected token
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
if (!req.headers.get('ex-snyk-token')) {
return res(ctx.json({ok: 'ok'}))
}
return res(ctx.status(400))
}),
async () => {
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
expect(res).to.deep.equal({ok: 'ok'})
}
))
})
suite('verify proxy configuration', () => {
let fakeManifest = 'fake-file.typ'
let stackProviderStub = sinon.stub()
stackProviderStub.withArgs(fakeManifest).returns(fakeProvided)
let fakeProvider = {
provideComponent: () => {},
provideStack: stackProviderStub,
isSupported: () => {}
};
afterEach(() => {
delete process.env['EXHORT_PROXY_URL']
})
test('when HTTP proxy is configured, verify agent is set correctly', interceptAndRun(
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
// The request should go through the proxy
return res(ctx.json({ok: 'ok'}))
}),
async () => {
const httpProxyUrl = 'http://proxy.example.com:8080'
const options = {
'EXHORT_PROXY_URL': httpProxyUrl
}
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl, false, options)
expect(res).to.deep.equal({ok: 'ok'})
}
))
test('when HTTPS proxy is configured, verify agent is set correctly', interceptAndRun(
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
// The request should go through the proxy
return res(ctx.json({ok: 'ok'}))
}),
async () => {
const httpsProxyUrl = 'https://proxy.example.com:8080'
const options = {
'EXHORT_PROXY_URL': httpsProxyUrl
}
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl, false, options)
expect(res).to.deep.equal({ok: 'ok'})
}
))
test('when proxy is configured via environment variable, verify agent is set correctly', interceptAndRun(
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
// The request should go through the proxy
return res(ctx.json({ok: 'ok'}))
}),
async () => {
process.env['EXHORT_PROXY_URL'] = 'http://proxy.example.com:8080'
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
expect(res).to.deep.equal({ok: 'ok'})
}
))
test('when no proxy is configured, verify no agent is set', interceptAndRun(
rest.post(`${backendUrl}/api/v3/analysis`, (req, res, ctx) => {
// The request should go directly without proxy
return res(ctx.json({ok: 'ok'}))
}),
async () => {
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
expect(res).to.deep.equal({ok: 'ok'})
}
))
})
})