Skip to content

Commit 1ad7ff2

Browse files
author
Günter Schafranek
committed
test(cli): Started unit tests for SBOM reporting
Signed-off-by: Günter Schafranek <[email protected]>
1 parent 3c90c79 commit 1ad7ff2

File tree

3 files changed

+192
-40
lines changed

3 files changed

+192
-40
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
@@ -516,6 +516,8 @@
516516
"devDependencies": {
517517
"@biomejs/biome": "2.2.6",
518518
"poku": "3.0.2",
519+
"quibble": "^0.9.2",
520+
"sinon": "^21.0.0",
519521
"typescript": "5.9.3"
520522
},
521523
"optionalDependencies": {

0 commit comments

Comments
 (0)