Skip to content

Commit cbee835

Browse files
committed
revamp test to not stub files to better simulate real environment
1 parent 87346e0 commit cbee835

File tree

1 file changed

+43
-95
lines changed

1 file changed

+43
-95
lines changed

packages/core/src/test/codewhisperer/securityScanHandler.test.ts renamed to packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts

Lines changed: 43 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { PromiseResult, Request } from 'aws-sdk/lib/request'
7-
import { createMockDocument } from './testUtil'
8-
import { Stub, stub } from '../utilities/stubber'
6+
import { PromiseResult } from 'aws-sdk/lib/request'
7+
import { Stub, stub } from 'aws-core-vscode/test'
98
import { AWSError, HttpResponse } from 'aws-sdk'
109
import {
1110
CodeAnalysisScope,
@@ -16,16 +15,18 @@ import {
1615
ListCodeScanFindingsResponse,
1716
pollScanJobStatus,
1817
SecurityScanTimedOutError,
19-
} from '../../codewhisperer'
20-
import { timeoutUtils } from '../../shared'
18+
} from 'aws-core-vscode/codewhisperer'
19+
import { timeoutUtils } from 'aws-core-vscode/shared'
2120
import assert from 'assert'
22-
import * as sinon from 'sinon'
21+
import sinon from 'sinon'
2322
import * as vscode from 'vscode'
24-
import fs from 'fs' // eslint-disable-line no-restricted-imports
25-
import { GetCodeScanResponse } from '../../codewhisperer/client/codewhispererclient'
23+
// import fs from 'fs' // eslint-disable-line no-restricted-imports
24+
import path from 'path'
2625

27-
const buildRawCodeScanIssue = (params?: Partial<RawCodeScanIssue>): RawCodeScanIssue => ({
28-
filePath: 'workspaceFolder/python3.7-plain-sam-app/hello_world/app.py',
26+
const buildRawCodeScanIssue = (fromProject: boolean = true, params?: Partial<RawCodeScanIssue>): RawCodeScanIssue => ({
27+
filePath: fromProject
28+
? 'workspaceFolder/python3.7-plain-sam-app/hello_world/app.py'
29+
: path.join(getWorkspaceFolder().substring(1), '/python3.7-plain-sam-app/hello_world/app.py'),
2930
startLine: 1,
3031
endLine: 1,
3132
title: 'title',
@@ -67,19 +68,15 @@ const buildMockListCodeScanFindingsResponse = (
6768
nextToken: nextToken ? 'nextToken' : undefined,
6869
})
6970

71+
function getWorkspaceFolder(): string {
72+
return path.join(__dirname, '../../../../../../core/dist/src/testFixtures/workspaceFolder')
73+
}
74+
7075
describe('securityScanHandler', function () {
7176
describe('listScanResults', function () {
7277
let mockClient: Stub<DefaultCodeWhispererClient>
7378
beforeEach(function () {
7479
mockClient = stub(DefaultCodeWhispererClient)
75-
sinon.stub(fs, 'existsSync').returns(true)
76-
sinon.stub(fs, 'statSync').returns({ isFile: () => true } as fs.Stats)
77-
const textDocumentMock = createMockDocument('first line\n second line\n fourth line')
78-
sinon.stub(vscode.workspace, 'openTextDocument').resolves(textDocumentMock)
79-
})
80-
81-
afterEach(function () {
82-
sinon.restore()
8380
})
8481

8582
it('should make ListCodeScanFindings request and aggregate findings by file path', async function () {
@@ -89,36 +86,35 @@ describe('securityScanHandler', function () {
8986
mockClient,
9087
'jobId',
9188
'codeScanFindingsSchema',
92-
['projectPath'],
89+
[getWorkspaceFolder()],
9390
CodeAnalysisScope.PROJECT,
9491
undefined
9592
)
9693

97-
assert.equal(aggregatedCodeScanIssueList.length, 2)
94+
assert.equal(aggregatedCodeScanIssueList.length, 1)
9895
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 1)
99-
assert.equal(aggregatedCodeScanIssueList[1].issues.length, 1)
10096
})
10197

10298
it('should handle ListCodeScanFindings request with paginated response', async function () {
10399
mockClient.listCodeScanFindings
104100
.onFirstCall()
105101
.resolves(
106102
buildMockListCodeScanFindingsResponse(
107-
JSON.stringify([buildRawCodeScanIssue({ title: 'title1' })]),
103+
JSON.stringify([buildRawCodeScanIssue(true, { title: 'title1' })]),
108104
true
109105
)
110106
)
111107
.onSecondCall()
112108
.resolves(
113109
buildMockListCodeScanFindingsResponse(
114-
JSON.stringify([buildRawCodeScanIssue({ title: 'title2' })]),
110+
JSON.stringify([buildRawCodeScanIssue(true, { title: 'title2' })]),
115111
true
116112
)
117113
)
118114
.onThirdCall()
119115
.resolves(
120116
buildMockListCodeScanFindingsResponse(
121-
JSON.stringify([buildRawCodeScanIssue({ title: 'title3' })]),
117+
JSON.stringify([buildRawCodeScanIssue(true, { title: 'title3' })]),
122118
false
123119
)
124120
)
@@ -127,12 +123,12 @@ describe('securityScanHandler', function () {
127123
mockClient,
128124
'jobId',
129125
'codeScanFindingsSchema',
130-
['projectPath'],
126+
[getWorkspaceFolder()],
131127
CodeAnalysisScope.PROJECT,
132128
undefined
133129
)
134130

135-
assert.equal(aggregatedCodeScanIssueList.length, 2)
131+
assert.equal(aggregatedCodeScanIssueList.length, 1)
136132
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 3)
137133
})
138134

@@ -149,7 +145,7 @@ describe('securityScanHandler', function () {
149145
mockClient,
150146
'jobId',
151147
'codeScanFindingsSchema',
152-
['projectPath'],
148+
[getWorkspaceFolder()],
153149
scope,
154150
undefined
155151
)
@@ -160,6 +156,22 @@ describe('securityScanHandler', function () {
160156
)
161157
}
162158
})
159+
it('should include ListCodeScanFindings from opened file that is not from project', async function () {
160+
mockClient.listCodeScanFindings.resolves(
161+
buildMockListCodeScanFindingsResponse(JSON.stringify([buildRawCodeScanIssue(false)]))
162+
)
163+
164+
const aggregatedCodeScanIssueList = await listScanResults(
165+
mockClient,
166+
'jobId',
167+
'codeScanFindingsSchema',
168+
[],
169+
CodeAnalysisScope.PROJECT,
170+
undefined
171+
)
172+
assert.equal(aggregatedCodeScanIssueList.length, 1)
173+
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 1)
174+
})
163175
})
164176

165177
describe('mapToAggregatedList', () => {
@@ -294,64 +306,16 @@ describe('securityScanHandler', function () {
294306
it('should return status when scan completes successfully', async function () {
295307
mockClient.getCodeScan
296308
.onFirstCall()
297-
.resolves({
298-
status: 'Pending',
299-
$response: {
300-
requestId: 'req1',
301-
hasNextPage: function (): boolean {
302-
throw new Error('Function not implemented.')
303-
},
304-
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
305-
throw new Error('Function not implemented.')
306-
},
307-
data: undefined,
308-
error: undefined,
309-
redirectCount: 0,
310-
retryCount: 0,
311-
httpResponse: new HttpResponse(),
312-
},
313-
})
309+
.resolves({ status: 'Pending', $response: { requestId: 'req1' } })
314310
.onSecondCall()
315-
.resolves({
316-
status: 'Completed',
317-
$response: {
318-
requestId: 'req2',
319-
hasNextPage: function (): boolean {
320-
throw new Error('Function not implemented.')
321-
},
322-
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
323-
throw new Error('Function not implemented.')
324-
},
325-
data: undefined,
326-
error: undefined,
327-
redirectCount: 0,
328-
retryCount: 0,
329-
httpResponse: new HttpResponse(),
330-
},
331-
})
311+
.resolves({ status: 'Completed', $response: { requestId: 'req2' } })
332312

333313
const result = await pollScanJobStatus(mockClient, mockJobId, CodeAnalysisScope.FILE_AUTO, mockStartTime)
334314
assert.strictEqual(result, 'Completed')
335315
})
336316

337317
it('should throw SecurityScanTimedOutError when polling exceeds timeout for express scans', async function () {
338-
mockClient.getCodeScan.resolves({
339-
status: 'Pending',
340-
$response: {
341-
requestId: 'req1',
342-
hasNextPage: function (): boolean {
343-
throw new Error('Function not implemented.')
344-
},
345-
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
346-
throw new Error('Function not implemented.')
347-
},
348-
data: undefined,
349-
error: undefined,
350-
redirectCount: 0,
351-
retryCount: 0,
352-
httpResponse: new HttpResponse(),
353-
},
354-
})
318+
mockClient.getCodeScan.resolves({ status: 'Pending', $response: { requestId: 'req1' } })
355319

356320
const pollPromise = pollScanJobStatus(mockClient, mockJobId, CodeAnalysisScope.FILE_AUTO, mockStartTime)
357321

@@ -362,23 +326,7 @@ describe('securityScanHandler', function () {
362326
})
363327

364328
it('should throw SecurityScanTimedOutError when polling exceeds timeout for standard scans', async function () {
365-
mockClient.getCodeScan.resolves({
366-
status: 'Pending',
367-
$response: {
368-
requestId: 'req1',
369-
hasNextPage: function (): boolean {
370-
throw new Error('Function not implemented.')
371-
},
372-
nextPage: function (): Request<GetCodeScanResponse, AWSError> | null {
373-
throw new Error('Function not implemented.')
374-
},
375-
data: undefined,
376-
error: undefined,
377-
redirectCount: 0,
378-
retryCount: 0,
379-
httpResponse: new HttpResponse(),
380-
},
381-
})
329+
mockClient.getCodeScan.resolves({ status: 'Pending', $response: { requestId: 'req1' } })
382330

383331
const pollPromise = pollScanJobStatus(mockClient, mockJobId, CodeAnalysisScope.PROJECT, mockStartTime)
384332

0 commit comments

Comments
 (0)