forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.test.js
More file actions
207 lines (182 loc) · 8.89 KB
/
javascript.test.js
File metadata and controls
207 lines (182 loc) · 8.89 KB
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
import { fail } from 'assert';
import fs from 'fs'
import { expect } from 'chai'
import esmock from 'esmock';
import { useFakeTimers } from "sinon";
import { availableProviders, match } from '../../src/provider.js';
import Manifest from '../../src/providers/manifest.js';
import { compareSboms } from '../utils/sbom_utils.js';
let clock
async function mockProvider(providerName, listingOutput, version) {
const mockInvokeCommand = (_cmd, args) => {
if (args.includes('--version')) {return version ? version : '0.0.0-mock';}
return listingOutput;
};
return esmock(`../../src/providers/javascript_${providerName}.js`, {
'../../src/providers/base_javascript.js': await esmock('../../src/providers/base_javascript.js', {
'../../src/tools.js': {
invokeCommand: mockInvokeCommand
}
})
});
}
async function createMockProvider(providerName, listingOutput) {
switch (providerName) {
case 'npm': {
const Javascript_npm = await mockProvider(providerName, listingOutput);
return new Javascript_npm();
}
case 'pnpm': {
const Javascript_pnpm = await mockProvider(providerName, listingOutput);
return new Javascript_pnpm();
}
case 'yarn-classic': {
const Javascript_yarn = await mockProvider('yarn', listingOutput, '1.22.22');
return new Javascript_yarn();
}
case 'yarn-berry': {
const Javascript_yarn = await mockProvider('yarn', listingOutput, '4.9.1');
return new Javascript_yarn();
}
default: { fail('Not implemented'); }
}
}
suite('testing the javascript-npm data provider', async () => {
[
{ name: 'npm/with_lock_file', validation: true },
{ name: 'npm/without_lock_file', validation: false },
{ name: 'npm/workspace_member_with_lock/packages/module-a', validation: true },
{ name: 'npm/workspace_member_without_lock/packages/module-a', validation: false },
{ name: 'pnpm/with_lock_file', validation: true },
{ name: 'pnpm/without_lock_file', validation: false },
{ name: 'yarn-classic/with_lock_file', validation: true },
{ name: 'yarn-classic/without_lock_file', validation: false },
{ name: 'yarn-berry/with_lock_file', validation: true },
{ name: 'yarn-berry/without_lock_file', validation: false }
].forEach(testCase => {
test(`verify isSupported returns ${testCase.expected} for ${testCase.name}`, () => {
let manifest = `test/providers/provider_manifests/${testCase.name}/package.json`;
try {
const provider = match(manifest, availableProviders);
expect(provider).not.to.be.null;
expect(testCase.validation).to.be.true;
} catch (e) {
expect(testCase.validation).to.be.false;
}
})
});
['npm', 'pnpm', 'yarn-classic', 'yarn-berry'].flatMap(providerName => [
"package_json_deps_without_exhortignore_object",
"package_json_deps_with_exhortignore_object"
].map(testCase => ({ providerName, testCase }))).forEach(({ providerName, testCase }) => {
let scenario = testCase.replace('package_json_deps_', '').replaceAll('_', ' ')
test(`verify package.json data provided for ${providerName} - stack analysis - ${scenario}`, async () => {
// load the expected graph for the scenario
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/stack_expected_sbom.json`,).toString();
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_stack.json`,).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/package.json`;
let providedDataForStack = provider.provideStack(manifestPath);
compareSboms(providedDataForStack.content, expectedSbom);
}).timeout(process.env.GITHUB_ACTIONS ? 30000 : 10000);
test(`verify package.json data provided for ${providerName} - component analysis - ${scenario}`, async () => {
// load the expected list for the scenario
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/js-common/${testCase}/component_expected_sbom.json`,).toString().trim()
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_component.json`,).toString()
// verify returned data matches expectation
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/package.json`;
let providedDataForComponent = provider.provideComponent(manifestPath);
compareSboms(providedDataForComponent.content, expectedSbom);
}).timeout(process.env.GITHUB_ACTIONS ? 15000 : 10000)
});
test('loads a valid manifest with ignored dependencies', () => {
const testCase = 'package_json_deps_with_exhortignore_object';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
const m = new Manifest(manifestPath);
expect(m.name).to.be.equals('backend');
expect(m.version).to.be.equals('1.0.0');
expect(m.manifestPath).to.be.equals(manifestPath);
expect(m.dependencies).to.have.all.members([
"@hapi/joi",
"backend",
"bcryptjs",
"dotenv",
"express",
"jsonwebtoken",
"mongoose",
"nodemon",
"axios",
"jsdom"]);
const ignoredNames = m.ignored.map(dep => dep.name);
expect(ignoredNames).to.have.all.members(['jsonwebtoken']);
});
test('loads a valid manifest without ignored dependencies', () => {
const testCase = 'package_json_deps_without_exhortignore_object';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
const m = new Manifest(manifestPath);
expect(m.name).to.be.equals('backend');
expect(m.version).to.be.equals('1.0.0');
expect(m.manifestPath).to.be.equals(manifestPath);
expect(m.dependencies).to.have.all.members([
"@hapi/joi",
"backend",
"bcryptjs",
"dotenv",
"express",
"jsdom",
"jsonwebtoken",
"mongoose",
"nodemon",
"axios"]);
expect(m.ignored).to.be.empty;
});
test('fails when the manifest does not exist', () => {
const testCase = 'wrong_folder';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
expect(() => new Manifest(manifestPath)).to.throw(Error);
});
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR finds npm provider when lock is at workspace root', () => {
const manifest = 'test/providers/provider_manifests/npm/with_lock_file/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/npm/with_lock_file' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR finds pnpm provider when lock is at workspace root', () => {
const manifest = 'test/providers/provider_manifests/pnpm/with_lock_file/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/pnpm/with_lock_file' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify workspace member walks up and finds lock file at workspace root', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_with_lock/packages/module-a/package.json'
const provider = match(manifest, availableProviders)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify workspace member throws when workspace root has no lock file', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_without_lock/packages/module-a/package.json'
expect(() => match(manifest, availableProviders))
.to.throw('package.json requires a lock file')
})
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR overrides walk-up for workspace member', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_with_lock/packages/module-a/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/npm/workspace_member_with_lock' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify pnpm workspace member stops at pnpm-workspace.yaml boundary', () => {
const manifest = 'test/providers/provider_manifests/pnpm/workspace_member_without_lock/packages/module-a/package.json'
expect(() => match(manifest, availableProviders))
.to.throw('package.json requires a lock file')
})
test('verify match with wrong TRUSTIFY_DA_WORKSPACE_DIR fails even when walk-up would succeed', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_with_lock/packages/module-a/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/npm/workspace_member_without_lock' }
expect(() => match(manifest, availableProviders, opts))
.to.throw('package.json requires a lock file')
})
}).beforeAll(() => clock = useFakeTimers(new Date('2023-08-07T00:00:00.000Z'))).afterAll(() => clock.restore());