@@ -14,7 +14,10 @@ import (
1414 "github.com/stretchr/testify/assert"
1515)
1616
17- var blurredCreditCard = "49*****************"
17+ var (
18+ blurredCreditCard = "49*****************"
19+ blurredAPIKey = "yr**************************************"
20+ )
1821
1922type mockNightfall struct {
2023 scanFn func (context.Context , * nf.ScanTextRequest ) (* nf.ScanTextResponse , error )
@@ -215,6 +218,99 @@ func TestReviewDiffDetectionRuleUUID(t *testing.T) {
215218 assert .Equal (t , expectedComments , comments , "Received incorrect response from ReviewDiff" )
216219}
217220
221+ func TestReviewDiffHasFindingMetadata (t * testing.T ) {
222+ mockAPIClient := & mockNightfall {}
223+ client := Client {
224+ APIClient : mockAPIClient ,
225+ DetectionRules : testDetectionRules ,
226+ MaxNumberRoutines : 1 ,
227+ }
228+
229+ numLines := 20
230+ numFiles := 50
231+ numScanReq := ((numLines * numFiles ) + maxItemsForAPIReq - 1 ) / maxItemsForAPIReq
232+ filePath := "test/data"
233+ lineNum := 0
234+ content := fmt .Sprintf ("this has a api key %s" , exampleAPIKey )
235+
236+ lines := make ([]* diffreviewer.Line , numLines )
237+ for i := range lines {
238+ lines [i ] = & diffreviewer.Line {
239+ LnumNew : lineNum ,
240+ Content : content ,
241+ }
242+ }
243+
244+ input := make ([]* diffreviewer.FileDiff , numFiles )
245+ for i := range input {
246+ h := & diffreviewer.Hunk {
247+ Lines : lines ,
248+ }
249+ input [i ] = & diffreviewer.FileDiff {
250+ Hunks : []* diffreviewer.Hunk {h },
251+ PathNew : filePath ,
252+ }
253+ }
254+
255+ c := diffreviewer.Comment {
256+ FilePath : filePath ,
257+ LineNumber : lineNum ,
258+ Body : fmt .Sprintf ("Suspicious content detected (%q, type %q (%s %s key))" , blurredAPIKey , "API_KEY" , "Active" , "Stripe" ),
259+ Title : fmt .Sprintf ("Detected API_KEY" ),
260+ }
261+ expectedComments := []* diffreviewer.Comment {& c , & c , & c }
262+
263+ scanResp := & nf.ScanTextResponse {
264+ Findings : [][]* nf.Finding {
265+ {},
266+ {
267+ {
268+ Finding : exampleAPIKey ,
269+ RedactedFinding : blurredAPIKey ,
270+ Detector : nf.DetectorMetadata {
271+ DisplayName : "API_KEY" ,
272+ DetectorUUID : "2136e3c9-feb0-4aea-8d3e-a767afabf501" ,
273+ },
274+ Confidence : string (nf .ConfidencePossible ),
275+ FindingMetadata : & nf.FindingMetadata {APIKeyMetadata : & nf.APIKeyMetadata {
276+ Status : "ACTIVE" ,
277+ Kind : "Stripe" ,
278+ }},
279+ },
280+ },
281+ },
282+ }
283+
284+ totalItems := make ([]string , numLines * numFiles )
285+ for i := 0 ; i < numLines * numFiles ; i ++ {
286+ totalItems [i ] = content
287+ }
288+
289+ var callCount int
290+ expectedRequests := make ([]* nf.ScanTextRequest , 0 , numScanReq )
291+ for i := 0 ; i < numScanReq ; i ++ {
292+ startIndex := i * maxItemsForAPIReq
293+ var endIndex int
294+ if len (totalItems ) < startIndex + maxItemsForAPIReq {
295+ endIndex = len (totalItems )
296+ } else {
297+ endIndex = startIndex + maxItemsForAPIReq
298+ }
299+
300+ expectedScanReq := client .buildScanRequest (totalItems [startIndex :endIndex ])
301+ expectedRequests = append (expectedRequests , expectedScanReq )
302+ mockAPIClient .scanFn = func (ctx context.Context , request * nf.ScanTextRequest ) (* nf.ScanTextResponse , error ) {
303+ assert .Equal (t , expectedRequests [callCount ], request , "request object did not match" )
304+ callCount ++
305+ return scanResp , nil
306+ }
307+ }
308+
309+ comments , err := client .ReviewDiff (context .Background (), githublogger .NewDefaultGithubLogger (), input )
310+ assert .NoError (t , err , "Received error from ReviewDiff" )
311+ assert .Equal (t , expectedComments , comments , "Received incorrect response from ReviewDiff" )
312+ }
313+
218314func TestScanPaths (t * testing.T ) {
219315 client := Client {
220316 DetectionRules : testDetectionRules ,
0 commit comments