/
oauth-token.spec.ts
58 lines (48 loc) · 1.87 KB
/
oauth-token.spec.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
import { fakeServer } from '../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
describe('test using OAuth token', () => {
let server: ReturnType<typeof fakeServer>;
let env: Record<string, string>;
beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
SNYK_TOKEN: '123456789',
SNYK_OAUTH_TOKEN: 'oauth-jwt-token',
};
server = fakeServer(apiPath, env.SNYK_TOKEN);
server.listen(apiPort, () => done());
});
afterAll(async () => {
await server.close();
});
it('successfully tests a project with an OAuth env variable set', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
const jsonObj = JSON.parse(await project.read('vulns-result.json'));
server.setNextResponse(jsonObj);
const { code } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});
expect(code).toEqual(0);
const requests = server.popRequests(2);
expect(requests[0].headers.authorization).toBe('Bearer oauth-jwt-token');
expect(requests[0].method).toBe('POST');
});
it('successfully monitors a project with an OAuth env variable set', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
const jsonObj = JSON.parse(await project.read('vulns-result.json'));
server.setNextResponse(jsonObj);
const { code } = await runSnykCLI(`monitor --json`, {
cwd: project.path(),
env,
});
expect(code).toEqual(0);
const requests = server.popRequests(2);
expect(requests[0].headers.authorization).toBe('Bearer oauth-jwt-token');
expect(requests[0].method).toBe('PUT');
});
});