Skip to content

Commit 52112f2

Browse files
committed
Add unit tests for token selection behavior
Created dedicated test file src/utils/alerts-map.test.mts to verify the token selection fix in getAlertsMapFromPurls(). Tests verify: - setupSdk receives undefined when no apiToken provided, allowing proper token resolution through getDefaultApiToken() - setupSdk receives custom token when explicitly provided - Empty purls array returns empty map without calling setupSdk - PURLs are processed and alerts map is returned - Filter options are handled correctly These tests ensure the public token is never incorrectly used when a user-provided token is available in the environment.
1 parent 85647dd commit 52112f2

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

src/utils/alerts-map.test.mts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
3+
import { getAlertsMapFromPurls } from './alerts-map.mts'
4+
5+
// Mock all dependencies with vi.hoisted for better type safety.
6+
const mockSetupSdk = vi.hoisted(() => vi.fn())
7+
const mockFindSocketYmlSync = vi.hoisted(() => vi.fn())
8+
const mockAddArtifactToAlertsMap = vi.hoisted(() => vi.fn())
9+
const mockBatchPackageStream = vi.hoisted(() => vi.fn())
10+
11+
vi.mock('./sdk.mts', () => ({
12+
setupSdk: mockSetupSdk,
13+
}))
14+
15+
vi.mock('./config.mts', () => ({
16+
findSocketYmlSync: mockFindSocketYmlSync,
17+
}))
18+
19+
vi.mock('./socket-package-alert.mts', () => ({
20+
addArtifactToAlertsMap: mockAddArtifactToAlertsMap,
21+
}))
22+
23+
vi.mock('./filter-config.mts', () => ({
24+
toFilterConfig: vi.fn(filter => filter || {}),
25+
}))
26+
27+
describe('Alerts Map', () => {
28+
beforeEach(() => {
29+
vi.clearAllMocks()
30+
31+
// Setup default mock implementations.
32+
mockFindSocketYmlSync.mockReturnValue({ ok: false, data: undefined })
33+
mockAddArtifactToAlertsMap.mockResolvedValue(undefined)
34+
35+
mockBatchPackageStream.mockImplementation(async function* () {
36+
yield {
37+
success: true,
38+
data: {
39+
alerts: [],
40+
name: 'lodash',
41+
purl: 'pkg:npm/lodash@4.17.21',
42+
version: '4.17.21',
43+
},
44+
}
45+
})
46+
47+
mockSetupSdk.mockResolvedValue({
48+
ok: true,
49+
data: {
50+
batchPackageStream: mockBatchPackageStream,
51+
},
52+
})
53+
})
54+
55+
describe('getAlertsMapFromPurls', () => {
56+
it('should pass undefined apiToken to setupSdk when not provided', async () => {
57+
const purls = ['pkg:npm/lodash@4.17.21']
58+
59+
await getAlertsMapFromPurls(purls, {
60+
nothrow: true,
61+
})
62+
63+
// setupSdk should be called with undefined apiToken to let it handle token resolution.
64+
expect(mockSetupSdk).toHaveBeenCalledWith({ apiToken: undefined })
65+
})
66+
67+
it('should pass provided apiToken to setupSdk when explicitly set', async () => {
68+
const purls = ['pkg:npm/lodash@4.17.21']
69+
const customToken = 'sktsec_test_custom_token'
70+
71+
await getAlertsMapFromPurls(purls, {
72+
apiToken: customToken,
73+
nothrow: true,
74+
})
75+
76+
// setupSdk should be called with the custom token.
77+
expect(mockSetupSdk).toHaveBeenCalledWith({ apiToken: customToken })
78+
})
79+
80+
it('should return empty map when no purls provided', async () => {
81+
const alertsMap = await getAlertsMapFromPurls([], {
82+
nothrow: true,
83+
})
84+
85+
expect(alertsMap).toBeInstanceOf(Map)
86+
expect(alertsMap.size).toBe(0)
87+
// setupSdk should not be called if there are no purls.
88+
expect(mockSetupSdk).not.toHaveBeenCalled()
89+
})
90+
91+
it('should process purls and return alerts map', async () => {
92+
const purls = ['pkg:npm/lodash@4.17.21', 'pkg:npm/express@4.18.2']
93+
94+
const alertsMap = await getAlertsMapFromPurls(purls, {
95+
nothrow: true,
96+
})
97+
98+
expect(alertsMap).toBeInstanceOf(Map)
99+
expect(mockSetupSdk).toHaveBeenCalledWith({ apiToken: undefined })
100+
expect(mockBatchPackageStream).toHaveBeenCalled()
101+
})
102+
103+
it('should handle filter options correctly', async () => {
104+
const purls = ['pkg:npm/lodash@4.17.21']
105+
106+
await getAlertsMapFromPurls(purls, {
107+
filter: { actions: ['error', 'warn'] },
108+
nothrow: true,
109+
})
110+
111+
expect(mockSetupSdk).toHaveBeenCalled()
112+
expect(mockBatchPackageStream).toHaveBeenCalled()
113+
})
114+
})
115+
})

src/utils/pnpm-scanning.test.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
33
import { getAlertsMapFromPnpmLockfile } from './alerts-map.mts'
44
import { extractPurlsFromPnpmLockfile, parsePnpmLockfile } from './pnpm.mts'
55

6-
// Mock all dependencies with vi.hoisted for better type safety
6+
// Mock all dependencies with vi.hoisted for better type safety.
77
const mockGetPublicApiToken = vi.hoisted(() => vi.fn())
88
const mockSetupSdk = vi.hoisted(() => vi.fn())
99
const mockFindSocketYmlSync = vi.hoisted(() => vi.fn())
@@ -31,7 +31,7 @@ describe('PNPM Lockfile PURL Scanning', () => {
3131
beforeEach(() => {
3232
vi.clearAllMocks()
3333

34-
// Setup default mock implementations
34+
// Setup default mock implementations.
3535
mockGetPublicApiToken.mockReturnValue('test-token')
3636
mockFindSocketYmlSync.mockReturnValue({ ok: false, data: undefined })
3737
mockAddArtifactToAlertsMap.mockResolvedValue(undefined)

0 commit comments

Comments
 (0)