-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcmd-scan-view.test.mts
More file actions
271 lines (221 loc) · 7.37 KB
/
cmd-scan-view.test.mts
File metadata and controls
271 lines (221 loc) · 7.37 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Unit tests for scan view command.
*
* Tests the command that views raw scan results.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
// Mock the logger.
const mockLogger = vi.hoisted(() => ({
error: vi.fn(),
fail: vi.fn(),
info: vi.fn(),
log: vi.fn(),
success: vi.fn(),
warn: vi.fn(),
}))
vi.mock('@socketsecurity/lib/logger', async importOriginal => {
const actual =
await importOriginal<typeof import('@socketsecurity/lib/logger')>()
return {
...actual,
getDefaultLogger: () => mockLogger,
}
})
// Mock dependencies.
const mockHandleScanView = vi.hoisted(() => vi.fn())
const mockStreamScan = vi.hoisted(() => vi.fn())
const mockDetermineOrgSlug = vi.hoisted(() =>
vi.fn().mockResolvedValue(['test-org', 'test-org']),
)
const mockHasDefaultApiToken = vi.hoisted(() => vi.fn().mockReturnValue(true))
vi.mock('../../../../src/commands/scan/handle-scan-view.mts', () => ({
handleScanView: mockHandleScanView,
}))
vi.mock('../../../../src/commands/scan/stream-scan.mts', () => ({
streamScan: mockStreamScan,
}))
vi.mock('../../../../src/utils/socket/org-slug.mjs', () => ({
determineOrgSlug: mockDetermineOrgSlug,
}))
vi.mock('../../../../src/utils/socket/sdk.mjs', async importOriginal => {
const actual =
await importOriginal<
typeof import('../../../../src/utils/socket/sdk.mjs')
>()
return {
...actual,
hasDefaultApiToken: mockHasDefaultApiToken,
}
})
// Import after mocks.
const { cmdScanView } =
await import('../../../../src/commands/scan/cmd-scan-view.mts')
describe('cmd-scan-view', () => {
beforeEach(() => {
vi.clearAllMocks()
process.exitCode = undefined
})
describe('command metadata', () => {
it('should have correct description', () => {
expect(cmdScanView.description).toBe('View the raw results of a scan')
})
it('should not be hidden', () => {
expect(cmdScanView.hidden).toBe(false)
})
})
describe('run', () => {
const importMeta = { url: 'file:///test/cmd-scan-view.mts' }
const context = { parentName: 'socket scan' }
const testScanId = '000aaaa1-0000-0a0a-00a0-00a0000000a0'
it('should support --dry-run flag', async () => {
await cmdScanView.run(['--dry-run', testScanId], importMeta, context)
expect(mockHandleScanView).not.toHaveBeenCalled()
expect(mockStreamScan).not.toHaveBeenCalled()
expect(mockLogger.log).toHaveBeenCalledWith(
expect.stringContaining('DryRun'),
)
})
it('should fail without Socket API token', async () => {
mockHasDefaultApiToken.mockReturnValueOnce(false)
await cmdScanView.run([testScanId], importMeta, context)
expect(process.exitCode).toBe(2)
expect(mockHandleScanView).not.toHaveBeenCalled()
})
it('should fail without scan ID', async () => {
await cmdScanView.run([], importMeta, context)
expect(process.exitCode).toBe(2)
expect(mockHandleScanView).not.toHaveBeenCalled()
})
it('should call handleScanView with scan ID', async () => {
await cmdScanView.run([testScanId], importMeta, context)
expect(mockHandleScanView).toHaveBeenCalledWith(
'test-org',
testScanId,
'',
'text',
)
})
it('should pass output file path to handleScanView', async () => {
await cmdScanView.run([testScanId, './output.json'], importMeta, context)
expect(mockHandleScanView).toHaveBeenCalledWith(
'test-org',
testScanId,
'./output.json',
'text',
)
})
it('should pass --org flag to determineOrgSlug', async () => {
mockDetermineOrgSlug.mockResolvedValueOnce(['custom-org', 'custom-org'])
await cmdScanView.run(
[testScanId, '--org', 'custom-org'],
importMeta,
context,
)
expect(mockDetermineOrgSlug).toHaveBeenCalledWith(
'custom-org',
true,
false,
)
expect(mockHandleScanView).toHaveBeenCalledWith(
'custom-org',
testScanId,
'',
'text',
)
})
it('should support --json output mode', async () => {
await cmdScanView.run([testScanId, '--json'], importMeta, context)
expect(mockHandleScanView).toHaveBeenCalledWith(
'test-org',
testScanId,
'',
'json',
)
})
it('should support --markdown output mode', async () => {
await cmdScanView.run([testScanId, '--markdown'], importMeta, context)
expect(mockHandleScanView).toHaveBeenCalledWith(
'test-org',
testScanId,
'',
'markdown',
)
})
it('should fail if both --json and --markdown are provided', async () => {
await cmdScanView.run(
[testScanId, '--json', '--markdown'],
importMeta,
context,
)
expect(process.exitCode).toBe(2)
expect(mockHandleScanView).not.toHaveBeenCalled()
})
it('should call streamScan with --json --stream flags', async () => {
await cmdScanView.run(
[testScanId, '--json', '--stream'],
importMeta,
context,
)
expect(mockStreamScan).toHaveBeenCalledWith('test-org', testScanId, {
commandPath: 'socket scan view',
file: '',
})
expect(mockHandleScanView).not.toHaveBeenCalled()
})
it('should pass file to streamScan with --json --stream', async () => {
await cmdScanView.run(
[testScanId, './stream.txt', '--json', '--stream'],
importMeta,
context,
)
expect(mockStreamScan).toHaveBeenCalledWith('test-org', testScanId, {
commandPath: 'socket scan view',
file: './stream.txt',
})
})
it('should fail if --stream is used without --json', async () => {
await cmdScanView.run([testScanId, '--stream'], importMeta, context)
expect(process.exitCode).toBe(2)
expect(mockHandleScanView).not.toHaveBeenCalled()
expect(mockStreamScan).not.toHaveBeenCalled()
})
it('should pass --no-interactive to determineOrgSlug', async () => {
await cmdScanView.run(
[testScanId, '--no-interactive'],
importMeta,
context,
)
expect(mockDetermineOrgSlug).toHaveBeenCalledWith('', false, false)
})
it('should fail if org slug cannot be determined', async () => {
mockDetermineOrgSlug.mockResolvedValueOnce(['', ''])
await cmdScanView.run([testScanId], importMeta, context)
expect(process.exitCode).toBe(2)
expect(mockHandleScanView).not.toHaveBeenCalled()
})
it('should show scan ID in dry-run', async () => {
await cmdScanView.run(['--dry-run', testScanId], importMeta, context)
expect(mockLogger.log).toHaveBeenCalledWith(
expect.stringContaining(testScanId),
)
})
it('should show organization in dry-run', async () => {
await cmdScanView.run(['--dry-run', testScanId], importMeta, context)
expect(mockLogger.log).toHaveBeenCalledWith(
expect.stringContaining('test-org'),
)
})
it('should show stream mode in dry-run with --json --stream', async () => {
await cmdScanView.run(
['--dry-run', testScanId, '--json', '--stream'],
importMeta,
context,
)
// Dry-run output routes to stderr when --json is set so the
// primary payload stays pipe-safe on stdout.
expect(mockLogger.error).toHaveBeenCalledWith(
expect.stringContaining('stream'),
)
})
})
})