|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import assert from 'assert' |
| 7 | +import { SecurityIssueProvider } from '../../codewhisperer/service/securityIssueProvider' |
| 8 | +import { createCodeScanIssue } from './testUtil' |
| 9 | +import { displayFindingsDetectorName } from '../../codewhisperer/models/constants' |
| 10 | +import { AggregatedCodeScanIssue } from '../../codewhisperer/models/model' |
| 11 | + |
| 12 | +describe('mergeIssuesDisplayFindings', () => { |
| 13 | + let provider: SecurityIssueProvider |
| 14 | + const testFilePath = '/test/file.py' |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + provider = Object.create(SecurityIssueProvider.prototype) |
| 18 | + provider.issues = [] |
| 19 | + }) |
| 20 | + |
| 21 | + it('should add new issues when no existing group', () => { |
| 22 | + const newIssues: AggregatedCodeScanIssue = { |
| 23 | + filePath: testFilePath, |
| 24 | + issues: [createCodeScanIssue({ findingId: 'new-1' })], |
| 25 | + } |
| 26 | + |
| 27 | + provider.mergeIssuesDisplayFindings(newIssues, true) |
| 28 | + |
| 29 | + assert.strictEqual(provider.issues.length, 1) |
| 30 | + assert.strictEqual(provider.issues[0].filePath, testFilePath) |
| 31 | + assert.strictEqual(provider.issues[0].issues.length, 1) |
| 32 | + assert.strictEqual(provider.issues[0].issues[0].findingId, 'new-1') |
| 33 | + }) |
| 34 | + |
| 35 | + it('should keep displayFindings when fromQCA is true', () => { |
| 36 | + provider.issues = [ |
| 37 | + { |
| 38 | + filePath: testFilePath, |
| 39 | + issues: [ |
| 40 | + createCodeScanIssue({ findingId: 'qca-1', detectorName: 'QCA-detector' }), |
| 41 | + createCodeScanIssue({ findingId: 'display-1', detectorName: displayFindingsDetectorName }), |
| 42 | + ], |
| 43 | + }, |
| 44 | + ] |
| 45 | + |
| 46 | + const newIssues: AggregatedCodeScanIssue = { |
| 47 | + filePath: testFilePath, |
| 48 | + issues: [createCodeScanIssue({ findingId: 'new-qca-1', detectorName: 'QCA-detector' })], |
| 49 | + } |
| 50 | + |
| 51 | + provider.mergeIssuesDisplayFindings(newIssues, true) |
| 52 | + |
| 53 | + assert.strictEqual(provider.issues.length, 1) |
| 54 | + assert.strictEqual(provider.issues[0].issues.length, 2) |
| 55 | + |
| 56 | + const findingIds = provider.issues[0].issues.map((issue) => issue.findingId) |
| 57 | + assert.ok(findingIds.includes('display-1')) |
| 58 | + assert.ok(findingIds.includes('new-qca-1')) |
| 59 | + assert.ok(!findingIds.includes('qca-1')) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should keep QCA findings when fromQCA is false', () => { |
| 63 | + provider.issues = [ |
| 64 | + { |
| 65 | + filePath: testFilePath, |
| 66 | + issues: [ |
| 67 | + createCodeScanIssue({ findingId: 'qca-1', detectorName: 'QCA-detector' }), |
| 68 | + createCodeScanIssue({ findingId: 'display-1', detectorName: displayFindingsDetectorName }), |
| 69 | + ], |
| 70 | + }, |
| 71 | + ] |
| 72 | + |
| 73 | + const newIssues: AggregatedCodeScanIssue = { |
| 74 | + filePath: testFilePath, |
| 75 | + issues: [createCodeScanIssue({ findingId: 'new-display-1', detectorName: displayFindingsDetectorName })], |
| 76 | + } |
| 77 | + |
| 78 | + provider.mergeIssuesDisplayFindings(newIssues, false) |
| 79 | + |
| 80 | + assert.strictEqual(provider.issues.length, 1) |
| 81 | + assert.strictEqual(provider.issues[0].issues.length, 2) |
| 82 | + |
| 83 | + const findingIds = provider.issues[0].issues.map((issue) => issue.findingId) |
| 84 | + assert.ok(findingIds.includes('qca-1')) |
| 85 | + assert.ok(findingIds.includes('new-display-1')) |
| 86 | + assert.ok(!findingIds.includes('display-1')) |
| 87 | + }) |
| 88 | +}) |
0 commit comments