-
Notifications
You must be signed in to change notification settings - Fork 532
/
cli-test.npm.spec.ts
251 lines (235 loc) · 7.68 KB
/
cli-test.npm.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
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
import * as path from 'path';
import { AcceptanceTests } from './cli-test.acceptance.test';
export const NpmTests: AcceptanceTests = {
language: 'NPM',
tests: {
'`test npm-package with custom --project-name`': (params, utils) => async (
t,
) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package', {
'project-name': 'custom-project-name',
});
const req = params.server.popRequest();
t.match(
req.body.projectNameOverride,
'custom-project-name',
'custom project name is passed',
);
t.match(req.body.targetFile, undefined, 'target is undefined');
},
'test npm-package remoteUrl': (params, utils) => async (t) => {
utils.chdirWorkspaces();
process.env.GIT_DIR = 'npm-package/gitdir';
await params.cli.test('npm-package');
const req = params.server.popRequest();
t.equal(
req.body.target.remoteUrl,
'http://github.com/snyk/npm-package',
'git remoteUrl is passed',
);
t.equals(
req.body.target.branch,
'master',
'correct branch passed to request',
);
delete process.env.GIT_DIR;
},
'test npm-package remoteUrl with --remote-repo-url': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
process.env.GIT_DIR = 'npm-package/gitdir';
await params.cli.test('npm-package', {
'remote-repo-url': 'foo',
});
const req = params.server.popRequest();
t.equal(
req.body.target.remoteUrl,
'foo',
'specified remoteUrl is passed',
);
t.equals(
req.body.target.branch,
'master',
'correct branch passed to request',
);
delete process.env.GIT_DIR;
},
'`test --file=fixtures/protect/package.json`': (params) => async (t) => {
const res = await params.cli.test(path.resolve(__dirname, '../..'), {
file: 'fixtures/protect/package.json',
});
t.match(
res,
/Tested 1 dependencies for known vulnerabilities/,
'should succeed in a folder',
);
},
'`test npm-package-policy` returns correct meta': (params, utils) => async (
t,
) => {
utils.chdirWorkspaces();
const res = await params.cli.test('npm-package-policy');
const meta = res.slice(res.indexOf('Organization:')).split('\n');
t.match(meta[0], /Organization:\s+test-org/, 'organization displayed');
t.match(meta[1], /Package manager:\s+npm/, 'package manager displayed');
t.match(meta[2], /Target file:\s+package.json/, 'target file displayed');
t.match(
meta[3],
/Project name:\s+custom-policy-location-package/,
'project name displayed',
);
t.match(meta[4], /Open source:\s+no/, 'open source displayed');
t.match(meta[5], /Project path:\s+npm-package-policy/, 'path displayed');
t.match(meta[6], /Local Snyk policy:\s+found/, 'local policy displayed');
},
'`test npm-package` sends pkg info': (params, utils) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package');
const req = params.server.popRequest();
t.match(req.url, '/test-dep-graph', 'posts to correct url');
t.match(req.body.targetFile, undefined, 'target is undefined');
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
['npm-package@1.0.0', 'ms@0.7.1', 'debug@2.2.0'].sort(),
'depGraph looks fine',
);
},
'`test npm-package --file=package-lock.json ` sends pkg info': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package', { file: 'package-lock.json' });
const req = params.server.popRequest();
t.match(req.url, '/test-dep-graph', 'posts to correct url');
t.match(req.body.targetFile, undefined, 'target is undefined');
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
['npm-package@1.0.0', 'ms@0.7.1', 'debug@2.2.0'].sort(),
'depGraph looks fine',
);
},
'`test npm-package --file=package-lock.json --dev` sends pkg info': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package', {
file: 'package-lock.json',
dev: true,
});
const req = params.server.popRequest();
t.match(req.url, '/test-dep-graph', 'posts to correct url');
t.match(req.body.targetFile, undefined, 'target is undefined');
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
[
'npm-package@1.0.0',
'ms@0.7.1',
'debug@2.2.0',
'object-assign@4.1.1',
].sort(),
'depGraph looks fine',
);
},
'`test npm-out-of-sync` out of sync fails': (params, utils) => async (
t,
) => {
utils.chdirWorkspaces();
try {
await params.cli.test('npm-out-of-sync', { dev: true });
t.fail('Should fail');
} catch (e) {
t.equal(
e.message,
'\nTesting npm-out-of-sync...\n\n' +
'Dependency snyk was not found in package-lock.json.' +
' Your package.json and package-lock.json are probably out of sync.' +
' Please run "npm install" and try again.',
'Contains enough info about err',
);
}
},
'`test npm-out-of-sync --strict-out-of-sync=false` passes': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-out-of-sync', {
dev: true,
strictOutOfSync: false,
});
const req = params.server.popRequest();
t.match(req.url, '/test-dep-graph', 'posts to correct url');
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
[
'npm-package@1.0.0',
'object-assign@4.1.1',
'rewire@^4.0.1',
'snyk@*',
'to-array@0.1.4',
].sort(),
'depGraph looks fine',
);
},
'`test npm-package-shrinkwrap --file=package-lock.json` with npm-shrinkwrap errors': (
params,
utils,
) => async (t) => {
t.plan(1);
utils.chdirWorkspaces();
try {
await params.cli.test('npm-package-shrinkwrap', {
file: 'package-lock.json',
});
t.fail('Should fail');
} catch (e) {
t.includes(
e.message,
'--file=package.json',
'Contains enough info about err',
);
}
},
'`test npm-package-with-subfolder --file=package-lock.json ` picks top-level files': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package-with-subfolder', {
file: 'package-lock.json',
});
const req = params.server.popRequest();
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
['npm-package-top-level@1.0.0', 'to-array@0.1.4'].sort(),
'depGraph looks fine',
);
},
'`test npm-package-with-subfolder --file=subfolder/package-lock.json ` picks subfolder files': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('npm-package-with-subfolder', {
file: 'subfolder/package-lock.json',
});
const req = params.server.popRequest();
const depGraph = req.body.depGraph;
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
['npm-package-subfolder@1.0.0', 'to-array@0.1.4'].sort(),
'depGraph looks fine',
);
},
},
};