Skip to content

Commit 303c83d

Browse files
committed
test: fix request counts for new FF
1 parent 80179e4 commit 303c83d

File tree

3 files changed

+61
-91
lines changed

3 files changed

+61
-91
lines changed

test/jest/unit/pnpm/snyk-test-pnpm-project.spec.ts

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,29 @@ describe('snyk test for pnpm project', () => {
2626
jest.restoreAllMocks();
2727
});
2828

29+
// If the featureFlag does not match, this returns `ok: false`
30+
function createFeatureFlagResponse(featureFlag, enabled) {
31+
return (payload) => {
32+
const url = payload?.url || '';
33+
const ok = url.includes(featureFlag) ? enabled : false;
34+
35+
return Promise.resolve({
36+
res: { statusCode: 200 } as NeedleResponse,
37+
body: {
38+
code: 200,
39+
ok,
40+
},
41+
});
42+
}
43+
}
44+
2945
describe('no local flag is used', () => {
3046
describe('project contains pnpm-lock.yaml file', () => {
3147
it('should scan pnpm vulnerabilities when enablePnpmCli feature flag is enabled', async () => {
3248
const fixturePath = getFixturePath('pnpm-app');
3349

34-
// this is for 'enablePnpmCli' feature flag
35-
mockedMakeRequest.mockImplementationOnce(() => {
36-
return Promise.resolve({
37-
res: { statusCode: 200 } as NeedleResponse,
38-
body: {
39-
code: 200,
40-
ok: true,
41-
},
42-
});
43-
});
50+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enablePnpmCli', true));
51+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enableAdvancedPackageManagerDetection', false));
4452

4553
mockedMakeRequest.mockImplementationOnce(() => {
4654
return Promise.resolve({
@@ -59,7 +67,7 @@ describe('snyk test for pnpm project', () => {
5967
_doubleDashArgs: [],
6068
});
6169

62-
expect(mockedMakeRequest).toHaveBeenCalledTimes(2);
70+
expect(mockedMakeRequest).toHaveBeenCalledTimes(3);
6371
expect(mockedMakeRequest).toHaveBeenCalledWith(
6472
expect.objectContaining({
6573
body: expect.objectContaining({
@@ -100,16 +108,8 @@ describe('snyk test for pnpm project', () => {
100108
it('should scan pnpm vulnerabilities as npm project when enablePnpmCli feature flag is not enabled', async () => {
101109
const fixturePath = getFixturePath('pnpm-app');
102110

103-
// this is for 'enablePnpmCli' feature flag
104-
mockedMakeRequest.mockImplementationOnce(() => {
105-
return Promise.resolve({
106-
res: { statusCode: 200 } as NeedleResponse,
107-
body: {
108-
code: 200,
109-
ok: false,
110-
},
111-
});
112-
});
111+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enablePnpmCli', false));
112+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enableAdvancedPackageManagerDetection', false));
113113

114114
mockedMakeRequest.mockImplementationOnce(() => {
115115
return Promise.resolve({
@@ -128,7 +128,7 @@ describe('snyk test for pnpm project', () => {
128128
_doubleDashArgs: [],
129129
});
130130

131-
expect(mockedMakeRequest).toHaveBeenCalledTimes(2);
131+
expect(mockedMakeRequest).toHaveBeenCalledTimes(3);
132132

133133
const expectedResultObject = {
134134
vulnerabilities: [],
@@ -166,16 +166,8 @@ describe('snyk test for pnpm project', () => {
166166
it('should scan pnpm workspace vulnerabilities', async () => {
167167
const fixturePath = getFixturePath('workspace-multi-type');
168168

169-
// this is for 'enablePnpmCli' feature flag
170-
mockedMakeRequest.mockImplementationOnce(() => {
171-
return Promise.resolve({
172-
res: { statusCode: 200 } as NeedleResponse,
173-
body: {
174-
code: 200,
175-
ok: true,
176-
},
177-
});
178-
});
169+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enablePnpmCli', true));
170+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enableAdvancedPackageManagerDetection', false));
179171

180172
mockedMakeRequest.mockImplementation(() => {
181173
return Promise.resolve({
@@ -195,7 +187,7 @@ describe('snyk test for pnpm project', () => {
195187
_doubleDashArgs: [],
196188
});
197189

198-
expect(mockedMakeRequest).toHaveBeenCalledTimes(11);
190+
expect(mockedMakeRequest).toHaveBeenCalledTimes(12);
199191

200192
const parsedResult = JSON.parse(result.getDisplayResults());
201193
const pnpmResult = parsedResult.filter(
@@ -209,16 +201,8 @@ describe('snyk test for pnpm project', () => {
209201
it('should not scan pnpm workspace vulnerabilities, only npm and yarn', async () => {
210202
const fixturePath = getFixturePath('workspace-multi-type');
211203

212-
// this is for 'enablePnpmCli' feature flag
213-
mockedMakeRequest.mockImplementationOnce(() => {
214-
return Promise.resolve({
215-
res: { statusCode: 200 } as NeedleResponse,
216-
body: {
217-
code: 200,
218-
ok: false,
219-
},
220-
});
221-
});
204+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enablePnpmCli', false));
205+
mockedMakeRequest.mockImplementationOnce(createFeatureFlagResponse('enableAdvancedPackageManagerDetection', false));
222206

223207
mockedMakeRequest.mockImplementation(() => {
224208
return Promise.resolve({
@@ -238,7 +222,7 @@ describe('snyk test for pnpm project', () => {
238222
_doubleDashArgs: [],
239223
});
240224

241-
expect(mockedMakeRequest).toHaveBeenCalledTimes(7);
225+
expect(mockedMakeRequest).toHaveBeenCalledTimes(8);
242226

243227
const parsedResult = JSON.parse(result.getDisplayResults());
244228
const pnpmResult = parsedResult.filter(

test/jest/unit/python/snyk-test-pyproject.spec.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ describe('snyk test for python project', () => {
3131
jest.restoreAllMocks();
3232
});
3333

34+
const mockFeatureFlagResponseFn = () => {
35+
return Promise.resolve({
36+
res: { statusCode: 200 } as NeedleResponse,
37+
body: {
38+
code: 200,
39+
ok: true,
40+
},
41+
});
42+
}
43+
3444
describe('no flag is used', () => {
3545
describe('project contains pyproject.toml file', () => {
3646
it('should scan poetry vulnerabilities', async () => {
@@ -49,16 +59,10 @@ describe('snyk test for python project', () => {
4959
},
5060
};
5161

52-
// this is for 'enablePnpmCli' feature flag
53-
mockedMakeRequest.mockImplementationOnce(() => {
54-
return Promise.resolve({
55-
res: { statusCode: 200 } as NeedleResponse,
56-
body: {
57-
code: 200,
58-
ok: false,
59-
},
60-
});
61-
});
62+
// this is for 'enablePnpmCli' and 'enableAdvancedPackageManagerDetection' feature flags
63+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
64+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
65+
6266
mockedLoadPlugin.mockImplementationOnce(() => {
6367
return plugin;
6468
});
@@ -82,7 +86,7 @@ describe('snyk test for python project', () => {
8286
expect(mockedLoadPlugin).toHaveBeenCalledTimes(1);
8387
expect(mockedLoadPlugin).toHaveBeenCalledWith('poetry');
8488

85-
expect(mockedMakeRequest).toHaveBeenCalledTimes(2);
89+
expect(mockedMakeRequest).toHaveBeenCalledTimes(3);
8690
expect(mockedMakeRequest).toHaveBeenCalledWith(
8791
expect.objectContaining({
8892
body: expect.objectContaining({
@@ -136,16 +140,10 @@ describe('snyk test for python project', () => {
136140
},
137141
};
138142

139-
// this is for 'enablePnpmCli' feature flag
140-
mockedMakeRequest.mockImplementationOnce(() => {
141-
return Promise.resolve({
142-
res: { statusCode: 200 } as NeedleResponse,
143-
body: {
144-
code: 200,
145-
ok: false,
146-
},
147-
});
148-
});
143+
// this is for 'enablePnpmCli' and 'enableAdvancedPackageManagerDetection' feature flags
144+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
145+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
146+
149147
mockedLoadPlugin.mockImplementationOnce(() => {
150148
return plugin;
151149
});
@@ -169,7 +167,7 @@ describe('snyk test for python project', () => {
169167
expect(mockedLoadPlugin).toHaveBeenCalledTimes(1);
170168
expect(mockedLoadPlugin).toHaveBeenCalledWith('poetry');
171169

172-
expect(mockedMakeRequest).toHaveBeenCalledTimes(2);
170+
expect(mockedMakeRequest).toHaveBeenCalledTimes(3);
173171
expect(mockedMakeRequest).toHaveBeenCalledWith(
174172
expect.objectContaining({
175173
body: expect.objectContaining({
@@ -227,16 +225,10 @@ describe('snyk test for python project', () => {
227225
},
228226
};
229227

230-
// this is for 'enablePnpmCli' feature flag
231-
mockedMakeRequest.mockImplementationOnce(() => {
232-
return Promise.resolve({
233-
res: { statusCode: 200 } as NeedleResponse,
234-
body: {
235-
code: 200,
236-
ok: false,
237-
},
238-
});
239-
});
228+
// this is for 'enablePnpmCli' and 'enableAdvancedPackageManagerDetection' feature flags
229+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
230+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
231+
240232
mockedLoadPlugin.mockImplementationOnce(() => {
241233
return plugin;
242234
});
@@ -261,7 +253,7 @@ describe('snyk test for python project', () => {
261253
expect(mockedLoadPlugin).toHaveBeenCalledTimes(1);
262254
expect(mockedLoadPlugin).toHaveBeenCalledWith('pip');
263255

264-
expect(mockedMakeRequest).toHaveBeenCalledTimes(2);
256+
expect(mockedMakeRequest).toHaveBeenCalledTimes(3);
265257
expect(mockedMakeRequest).toHaveBeenCalledWith(
266258
expect.objectContaining({
267259
body: expect.objectContaining({
@@ -329,16 +321,10 @@ describe('snyk test for python project', () => {
329321
},
330322
};
331323

332-
// this is for 'enablePnpmCli' feature flag
333-
mockedMakeRequest.mockImplementationOnce(() => {
334-
return Promise.resolve({
335-
res: { statusCode: 200 } as NeedleResponse,
336-
body: {
337-
code: 200,
338-
ok: false,
339-
},
340-
});
341-
});
324+
// this is for 'enablePnpmCli' and 'enableAdvancedPackageManagerDetection' feature flags
325+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
326+
mockedMakeRequest.mockImplementationOnce(mockFeatureFlagResponseFn);
327+
342328
mockedLoadPlugin
343329
.mockImplementationOnce(() => pipfilePythonPluginResponse)
344330
.mockImplementationOnce(() => pyprojectPythonPluginResponse);
@@ -364,7 +350,7 @@ describe('snyk test for python project', () => {
364350
expect(mockedLoadPlugin).toHaveBeenCalledWith('pip');
365351
expect(mockedLoadPlugin).toHaveBeenCalledWith('poetry');
366352

367-
expect(mockedMakeRequest).toHaveBeenCalledTimes(3);
353+
expect(mockedMakeRequest).toHaveBeenCalledTimes(4);
368354
expect(mockedMakeRequest).toHaveBeenCalledWith(
369355
expect.objectContaining({
370356
body: expect.objectContaining({

test/tap/monitor-target.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test('Make sure that target is sent correctly', async (t) => {
4848
.resolves('master');
4949

5050
const { data } = await getFakeServerRequestBody();
51-
t.ok(requestSpy.calledTwice, 'needle.request was not called twice');
51+
t.ok(requestSpy.calledThrice, 'needle.request was not called thrice');
5252
t.ok(!isEmpty(data.target), 'target passed to request');
5353
t.ok(
5454
!isEmpty(data.targetFileRelativePath),
@@ -75,7 +75,7 @@ test("Make sure it's not failing monitor for non git projects", async (t) => {
7575
const requestSpy = sinon.spy(requestLib, 'request');
7676
const { data } = await getFakeServerRequestBody();
7777

78-
t.ok(requestSpy.calledTwice, 'needle.request was not called twice');
78+
t.ok(requestSpy.calledThrice, 'needle.request was not called thrice');
7979
t.ok(isEmpty(data.target), 'empty target passed to request');
8080
t.match(
8181
data.targetFileRelativePath,
@@ -92,7 +92,7 @@ test("Make sure it's not failing if there is no remote configured", async (t) =>
9292
const requestSpy = sinon.spy(requestLib, 'request');
9393
const { data } = await getFakeServerRequestBody();
9494

95-
t.ok(requestSpy.calledTwice, 'needle.request was not called twice');
95+
t.ok(requestSpy.calledThrice, 'needle.request was not called thrice');
9696
t.ok(isEmpty(data.target), 'empty target passed to request');
9797
t.match(
9898
data.targetFileRelativePath,

0 commit comments

Comments
 (0)