Skip to content

Commit 89c2627

Browse files
committed
test(cli): Started implementing unit test for SBOM reporting params
Signed-off-by: Guenter Schafranek <[email protected]>
1 parent d3afc75 commit 89c2627

File tree

3 files changed

+157
-5
lines changed

3 files changed

+157
-5
lines changed

lib/cli/index.poku.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import quibble from "quibble";
2+
import sinon from "sinon";
3+
import { assert, beforeEach, afterEach, describe, it } from "poku";
4+
5+
describe("CLI tests", () => {
6+
let gotStub;
7+
let submitBom;
8+
9+
beforeEach(async () => {
10+
// Create a sinon stub that mimics got()
11+
const fakeGotResponse = {
12+
json: sinon.stub().resolves({ success: true }),
13+
};
14+
15+
gotStub = sinon.stub().returns(fakeGotResponse);
16+
17+
// Attach extend to the function itself
18+
gotStub.extend = sinon.stub().returns(gotStub);
19+
20+
// Replace the real 'got' module with our stub
21+
await quibble.esm("got", {
22+
default: gotStub,
23+
});
24+
25+
// Import the module under test AFTER quibble
26+
({ submitBom } = await import("./index.js"));
27+
});
28+
29+
afterEach(() => {
30+
quibble.reset(); // Restore real modules
31+
});
32+
33+
it("should report the SBOM with given project tag", async () => {
34+
const serverUrl = "https://api.example.com/upload";
35+
const projectId = "1111";
36+
const projectName = "test";
37+
const projectVersion = "1.0.0";
38+
const bomPayload = { bom: "test" };
39+
40+
await submitBom(
41+
{ serverUrl, projectId, projectName, projectVersion },
42+
bomPayload,
43+
);
44+
45+
// Verify got was called exactly once
46+
sinon.assert.calledOnce(gotStub);
47+
48+
// Grab call arguments
49+
const [calledUrl, options] = gotStub.firstCall.args;
50+
51+
assert.equal(calledUrl, serverUrl);
52+
assert.equal(options.method, "PUT");
53+
assert.equal(options.https.rejectUnauthorized, true);
54+
assert.equal(options.headers["X-Api-Key"], "MY_API_KEY");
55+
assert.match(options.headers["user-agent"], /@CycloneDX\/cdxgen/);
56+
assert.deepEqual(options.json, bomPayload);
57+
});
58+
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@
271271
"devDependencies": {
272272
"@biomejs/biome": "2.2.5",
273273
"poku": "3.0.2",
274+
"quibble": "^0.9.2",
275+
"sinon": "^21.0.0",
274276
"typescript": "5.9.3"
275277
},
276278
"optionalDependencies": {

pnpm-lock.yaml

Lines changed: 97 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)