3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
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'
9
8
import { AWSError , HttpResponse } from 'aws-sdk'
10
9
import {
11
10
CodeAnalysisScope ,
@@ -16,16 +15,18 @@ import {
16
15
ListCodeScanFindingsResponse ,
17
16
pollScanJobStatus ,
18
17
SecurityScanTimedOutError ,
19
- } from '../.. /codewhisperer'
20
- import { timeoutUtils } from '../.. /shared'
18
+ } from 'aws-core-vscode /codewhisperer'
19
+ import { timeoutUtils } from 'aws-core-vscode /shared'
21
20
import assert from 'assert'
22
- import * as sinon from 'sinon'
21
+ import sinon from 'sinon'
23
22
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 '
26
25
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' ) ,
29
30
startLine : 1 ,
30
31
endLine : 1 ,
31
32
title : 'title' ,
@@ -67,19 +68,15 @@ const buildMockListCodeScanFindingsResponse = (
67
68
nextToken : nextToken ? 'nextToken' : undefined ,
68
69
} )
69
70
71
+ function getWorkspaceFolder ( ) : string {
72
+ return path . join ( __dirname , '../../../../../../core/dist/src/testFixtures/workspaceFolder' )
73
+ }
74
+
70
75
describe ( 'securityScanHandler' , function ( ) {
71
76
describe ( 'listScanResults' , function ( ) {
72
77
let mockClient : Stub < DefaultCodeWhispererClient >
73
78
beforeEach ( function ( ) {
74
79
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 ( )
83
80
} )
84
81
85
82
it ( 'should make ListCodeScanFindings request and aggregate findings by file path' , async function ( ) {
@@ -89,36 +86,35 @@ describe('securityScanHandler', function () {
89
86
mockClient ,
90
87
'jobId' ,
91
88
'codeScanFindingsSchema' ,
92
- [ 'projectPath' ] ,
89
+ [ getWorkspaceFolder ( ) ] ,
93
90
CodeAnalysisScope . PROJECT ,
94
91
undefined
95
92
)
96
93
97
- assert . equal ( aggregatedCodeScanIssueList . length , 2 )
94
+ assert . equal ( aggregatedCodeScanIssueList . length , 1 )
98
95
assert . equal ( aggregatedCodeScanIssueList [ 0 ] . issues . length , 1 )
99
- assert . equal ( aggregatedCodeScanIssueList [ 1 ] . issues . length , 1 )
100
96
} )
101
97
102
98
it ( 'should handle ListCodeScanFindings request with paginated response' , async function ( ) {
103
99
mockClient . listCodeScanFindings
104
100
. onFirstCall ( )
105
101
. resolves (
106
102
buildMockListCodeScanFindingsResponse (
107
- JSON . stringify ( [ buildRawCodeScanIssue ( { title : 'title1' } ) ] ) ,
103
+ JSON . stringify ( [ buildRawCodeScanIssue ( true , { title : 'title1' } ) ] ) ,
108
104
true
109
105
)
110
106
)
111
107
. onSecondCall ( )
112
108
. resolves (
113
109
buildMockListCodeScanFindingsResponse (
114
- JSON . stringify ( [ buildRawCodeScanIssue ( { title : 'title2' } ) ] ) ,
110
+ JSON . stringify ( [ buildRawCodeScanIssue ( true , { title : 'title2' } ) ] ) ,
115
111
true
116
112
)
117
113
)
118
114
. onThirdCall ( )
119
115
. resolves (
120
116
buildMockListCodeScanFindingsResponse (
121
- JSON . stringify ( [ buildRawCodeScanIssue ( { title : 'title3' } ) ] ) ,
117
+ JSON . stringify ( [ buildRawCodeScanIssue ( true , { title : 'title3' } ) ] ) ,
122
118
false
123
119
)
124
120
)
@@ -127,12 +123,12 @@ describe('securityScanHandler', function () {
127
123
mockClient ,
128
124
'jobId' ,
129
125
'codeScanFindingsSchema' ,
130
- [ 'projectPath' ] ,
126
+ [ getWorkspaceFolder ( ) ] ,
131
127
CodeAnalysisScope . PROJECT ,
132
128
undefined
133
129
)
134
130
135
- assert . equal ( aggregatedCodeScanIssueList . length , 2 )
131
+ assert . equal ( aggregatedCodeScanIssueList . length , 1 )
136
132
assert . equal ( aggregatedCodeScanIssueList [ 0 ] . issues . length , 3 )
137
133
} )
138
134
@@ -149,7 +145,7 @@ describe('securityScanHandler', function () {
149
145
mockClient ,
150
146
'jobId' ,
151
147
'codeScanFindingsSchema' ,
152
- [ 'projectPath' ] ,
148
+ [ getWorkspaceFolder ( ) ] ,
153
149
scope ,
154
150
undefined
155
151
)
@@ -160,6 +156,22 @@ describe('securityScanHandler', function () {
160
156
)
161
157
}
162
158
} )
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
+ } )
163
175
} )
164
176
165
177
describe ( 'mapToAggregatedList' , ( ) => {
@@ -294,64 +306,16 @@ describe('securityScanHandler', function () {
294
306
it ( 'should return status when scan completes successfully' , async function ( ) {
295
307
mockClient . getCodeScan
296
308
. 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' } } )
314
310
. 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' } } )
332
312
333
313
const result = await pollScanJobStatus ( mockClient , mockJobId , CodeAnalysisScope . FILE_AUTO , mockStartTime )
334
314
assert . strictEqual ( result , 'Completed' )
335
315
} )
336
316
337
317
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' } } )
355
319
356
320
const pollPromise = pollScanJobStatus ( mockClient , mockJobId , CodeAnalysisScope . FILE_AUTO , mockStartTime )
357
321
@@ -362,23 +326,7 @@ describe('securityScanHandler', function () {
362
326
} )
363
327
364
328
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' } } )
382
330
383
331
const pollPromise = pollScanJobStatus ( mockClient , mockJobId , CodeAnalysisScope . PROJECT , mockStartTime )
384
332
0 commit comments