-
Notifications
You must be signed in to change notification settings - Fork 533
/
cli-json-output.spec.ts
105 lines (83 loc) · 3.24 KB
/
cli-json-output.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
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
import { fakeServer } from '../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
jest.setTimeout(1000 * 60);
describe('test --json', () => {
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_DISABLE_ANALYTICS: '1',
};
server = fakeServer(apiPath, env.SNYK_TOKEN);
server.listen(apiPort, () => done());
});
afterEach(() => {
server.restore();
});
afterAll((done) => {
server.close(() => done());
});
it('test with --json returns without error and with JSON return type when no vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setCustomResponse(await project.readJSON('vulns-result.json'));
const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});
expect(code).toEqual(0);
expect(server.getRequests().length).toBeGreaterThanOrEqual(1);
const outputObj = JSON.parse(stdout);
expect(outputObj).not.toBe('');
});
it('test without --json returns without error and with a string return type when no vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setCustomResponse(await project.readJSON('vulns-result.json'));
const { code, stdout } = await runSnykCLI(`test`, {
cwd: project.path(),
env,
});
expect(code).toEqual(0);
expect(server.getRequests().length).toBeGreaterThanOrEqual(1);
expect(stdout).not.toBe('');
expect(typeof stdout).toBe('string');
});
it('test with --json throws error and error contains json output with vulnerabilities when vulns found', async () => {
const project = await createProjectFromWorkspace('fail-on/no-fixable');
server.setCustomResponse(await project.readJSON('vulns-result.json'));
const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});
const returnedJson = JSON.parse(stdout);
expect(returnedJson.vulnerabilities.length > 0).toBeTruthy();
expect(code).toEqual(1);
expect(stdout).not.toBe('');
});
it('test --json handles responses larger than 512Mb string size limit in v8', async () => {
const project = await createProjectFromWorkspace(
'fail-on/no-fixable',
);
const response = await project.readJSON('vulns-result.json');
const remediation = response.result.remediation.unresolved[0];
response.result.remediation.unresolved = new Array(420000).fill(remediation);
server.setCustomResponse(response);
const { code, stdout, stderr } = await runSnykCLI(
`test --json`,
{
cwd: project.path(),
env,
},
);
if (stderr) console.debug({ stdout, stderr, code });
expect(code).toEqual(1);
expect(stdout).not.toBe('');
const returnedJson = JSON.parse(stdout);
expect(returnedJson.vulnerabilities.length > 0).toBeTruthy();
}, 120000);
});