Skip to content

Commit 0323e7e

Browse files
committed
test: add unit test for projectName in JSON output
- Added new fixture container-deb-scan-result.json matching deb/container scenario - Added new fixture monitor-dependencies-response-with-project-name.json with real API response structure - Added test case to validate projectName comes from registry response, not id
1 parent 9dde760 commit 0323e7e

3 files changed

Lines changed: 50 additions & 22 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"identity": {
3+
"type": "deb"
4+
},
5+
"facts": [
6+
{
7+
"type": "depGraph",
8+
"data": {
9+
"schemaVersion": "1.2.0",
10+
"pkgManager": {
11+
"name": "deb",
12+
"repositories": [{"alias": "debian:11"}]
13+
},
14+
"pkgs": [{"id": "alpine@3.18", "info": {"name": "alpine", "version": "3.18"}}],
15+
"graph": {
16+
"rootNodeId": "root-node",
17+
"nodes": [{"nodeId": "root-node", "pkgId": "alpine@3.18", "deps": []}]
18+
}
19+
}
20+
}
21+
],
22+
"target": {
23+
"image": "alpine:latest"
24+
},
25+
"name": "my-custom-project-name"
26+
}
27+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"ok": true,
3+
"org": "test-org",
4+
"id": "7c7305e2-fbcb-44d7-8fbf-8367371c509f",
5+
"isMonitored": true,
6+
"licensesPolicy": null,
7+
"uri": "https://app.snyk.io/org/test-org/project/3dda9b21-ca42-4de6-be7a-85696fa6e866/history/f60dce17-8a72-4cca-8a76-e9c88df546aa",
8+
"trialStarted": false,
9+
"path": "/srv",
10+
"projectName": "my-custom-project-name"
11+
}
12+

test/jest/unit/ecosystems-monitor-docker.spec.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,29 +128,20 @@ describe('monitorEcosystem docker/container', () => {
128128
);
129129
});
130130

131-
it('should return projectName from registry response, not id, in JSON output', async () => {
132-
const mavenScanResult = readJsonFixture(
133-
'maven-project-0-dependencies-scan-result.json',
131+
it('should return projectName from registry response in JSON output', async () => {
132+
const containerScanResult = readJsonFixture(
133+
'container-deb-scan-result.json',
134134
) as ScanResult;
135-
136-
// Mock response with both id and projectName to verify correct field is used
137-
const monitorDependenciesResponse: ecosystemsTypes.MonitorDependenciesResponse =
138-
{
139-
ok: true,
140-
org: 'test-org',
141-
id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', // This should NOT appear as projectName
142-
isMonitored: true,
143-
licensesPolicy: null,
144-
uri: 'https://app.snyk.io/org/test-org/project/12345678/history/87654321',
145-
trialStarted: false,
146-
path: '/srv',
147-
projectName: 'my-expected-project-name', // This SHOULD appear as projectName
148-
};
135+
const monitorDependenciesResponse = readJsonFixture(
136+
'monitor-dependencies-response-with-project-name.json',
137+
) as ecosystemsTypes.MonitorDependenciesResponse;
149138

150139
jest
151140
.spyOn(dockerPlugin, 'scan')
152-
.mockResolvedValue({ scanResults: [mavenScanResult] });
153-
jest.spyOn(request, 'makeRequest').mockResolvedValue(monitorDependenciesResponse);
141+
.mockResolvedValue({ scanResults: [containerScanResult] });
142+
jest
143+
.spyOn(request, 'makeRequest')
144+
.mockResolvedValue(monitorDependenciesResponse);
154145

155146
const results: Array<GoodResult | BadResult> = [];
156147

@@ -179,9 +170,7 @@ describe('monitorEcosystem docker/container', () => {
179170
const parsedOutput = JSON.parse(jsonOutput);
180171

181172
// projectName should be the actual project name from the registry, not the id (UUID)
182-
expect(parsedOutput.projectName).toBe('my-expected-project-name');
183-
expect(parsedOutput.projectName).not.toBe(
184-
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
173+
expect(parsedOutput.projectName).toBe('my-custom-project-name');
185174
);
186175
});
187176
});

0 commit comments

Comments
 (0)