-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jahed Ahmed
committed
Nov 9, 2021
1 parent
d918397
commit 8b4c2d8
Showing
10 changed files
with
319 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities. | ||
version: v1.22.1 | ||
# ignores vulnerabilities until expiry date; change duration by modifying expiry date | ||
ignore: | ||
'npm:marked:20170907': | ||
- '*': | ||
reason: Default policy location test | ||
expires: 2027-11-19T14:12:53.987Z | ||
patch: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { load as loadPolicy } from 'snyk-policy'; | ||
import { fakeServer } from '../../../acceptance/fake-server'; | ||
import { createProjectFromWorkspace } from '../../util/createProject'; | ||
import { runSnykCLI } from '../../util/runSnykCLI'; | ||
|
||
jest.setTimeout(1000 * 60); | ||
|
||
describe('snyk ignore', () => { | ||
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', // replace token from process.env | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
}; | ||
|
||
server = fakeServer(apiPath, env.SNYK_TOKEN); | ||
server.listen(apiPort, () => done()); | ||
}); | ||
|
||
afterEach(() => { | ||
server.restore(); | ||
}); | ||
|
||
afterAll((done) => { | ||
server.close(() => done()); | ||
}); | ||
|
||
it('creates a policy file using minimal options', async () => { | ||
const project = await createProjectFromWorkspace('empty'); | ||
const { code } = await runSnykCLI(`ignore --id=ID`, { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(0); | ||
|
||
const policy = await loadPolicy(project.path()); | ||
expect(policy).toMatchObject({ | ||
ignore: { | ||
ID: [ | ||
{ | ||
'*': { | ||
reason: 'None Given', | ||
expires: expect.any(Date), | ||
created: expect.any(Date), | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('creates a policy file using provided options', async () => { | ||
const project = await createProjectFromWorkspace('empty'); | ||
const { code } = await runSnykCLI( | ||
`ignore --id=ID --reason=REASON --expiry=2017-10-07 --policy-path=${project.path()}`, | ||
{ | ||
cwd: project.path(), | ||
env: env, | ||
}, | ||
); | ||
|
||
expect(code).toEqual(0); | ||
const policy = await loadPolicy(project.path()); | ||
expect(policy).toMatchObject({ | ||
ignore: { | ||
ID: [ | ||
{ | ||
'*': { | ||
reason: 'REASON', | ||
expires: new Date('2017-10-07'), | ||
created: expect.any(Date), | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('fails on missing ID', async () => { | ||
const project = await createProjectFromWorkspace('empty'); | ||
const { code, stdout } = await runSnykCLI(`ignore --reason=REASON`, { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(2); | ||
expect(stdout).toMatch('id is a required field'); | ||
}); | ||
|
||
it('errors when user is not authorized to ignore', async () => { | ||
const project = await createProjectFromWorkspace('empty'); | ||
server.unauthorizeAction('cliIgnore', 'not allowed'); | ||
|
||
const { code, stdout } = await runSnykCLI(`ignore --id=ID`, { | ||
cwd: project.path(), | ||
env, | ||
}); | ||
|
||
expect(code).toEqual(0); | ||
expect(stdout).toMatch('not allowed'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { fakeServer } from '../../../acceptance/fake-server'; | ||
import { createProjectFromWorkspace } from '../../util/createProject'; | ||
import { runSnykCLI } from '../../util/runSnykCLI'; | ||
|
||
jest.setTimeout(1000 * 60); | ||
|
||
describe('snyk monitor --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', // replace token from process.env | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
}; | ||
|
||
server = fakeServer(apiPath, env.SNYK_TOKEN); | ||
server.listen(apiPort, () => done()); | ||
}); | ||
|
||
afterEach(() => { | ||
server.restore(); | ||
}); | ||
|
||
afterAll((done) => { | ||
server.close(() => done()); | ||
}); | ||
|
||
it('includes result details', async () => { | ||
const project = await createProjectFromWorkspace('no-vulns'); | ||
const { code, stdout } = await runSnykCLI(`monitor --json`, { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(0); | ||
expect(JSON.parse(stdout)).toMatchObject({ | ||
packageManager: 'npm', | ||
manageUrl: 'http://localhost:12345/manage', | ||
}); | ||
}); | ||
|
||
it('includes path errors', async () => { | ||
const project = await createProjectFromWorkspace( | ||
'no-supported-target-files', | ||
); | ||
const { code, stdout } = await runSnykCLI(`monitor --json`, { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(3); | ||
expect(JSON.parse(stdout)).toMatchObject({ | ||
path: project.path(), | ||
error: expect.stringMatching( | ||
`Could not detect supported target files in ${project.path()}.`, | ||
), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { fakeServer } from '../../../acceptance/fake-server'; | ||
import { createProjectFromWorkspace } from '../../util/createProject'; | ||
import { runSnykCLI } from '../../util/runSnykCLI'; | ||
|
||
jest.setTimeout(1000 * 60); | ||
|
||
describe('snyk policy', () => { | ||
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', // replace token from process.env | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
}; | ||
|
||
server = fakeServer(apiPath, env.SNYK_TOKEN); | ||
server.listen(apiPort, () => done()); | ||
}); | ||
|
||
afterEach(() => { | ||
server.restore(); | ||
}); | ||
|
||
afterAll((done) => { | ||
server.close(() => done()); | ||
}); | ||
|
||
it('loads policy file', async () => { | ||
const project = await createProjectFromWorkspace('policy'); | ||
const { code, stdout } = await runSnykCLI('policy', { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(0); | ||
expect(stdout).toMatch('Current Snyk policy, read from .snyk file'); | ||
}); | ||
|
||
it('fails when policy not found', async () => { | ||
const project = await createProjectFromWorkspace('empty'); | ||
const { code, stdout } = await runSnykCLI('policy', { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(2); | ||
expect(stdout).toMatch('Could not load policy.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { fakeServer } from '../../../acceptance/fake-server'; | ||
import { createProjectFromWorkspace } from '../../util/createProject'; | ||
import { runSnykCLI } from '../../util/runSnykCLI'; | ||
|
||
jest.setTimeout(1000 * 60); | ||
|
||
describe('snyk 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', // replace token from process.env | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
}; | ||
|
||
server = fakeServer(apiPath, env.SNYK_TOKEN); | ||
server.listen(apiPort, () => done()); | ||
}); | ||
|
||
afterEach(() => { | ||
server.restore(); | ||
}); | ||
|
||
afterAll((done) => { | ||
server.close(() => done()); | ||
}); | ||
|
||
it('includes path errors', async () => { | ||
const project = await createProjectFromWorkspace( | ||
'no-supported-target-files', | ||
); | ||
const { code, stdout } = await runSnykCLI(`test --json`, { | ||
cwd: project.path(), | ||
env: env, | ||
}); | ||
|
||
expect(code).toEqual(3); | ||
expect(JSON.parse(stdout)).toMatchObject({ | ||
path: project.path(), | ||
error: expect.stringMatching( | ||
`Could not detect supported target files in ${project.path()}.`, | ||
), | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.