Skip to content

Commit

Permalink
Merge pull request #23 from vulncheck-oss/tests-and-refactor
Browse files Browse the repository at this point in the history
💚 table module and more test coverage
  • Loading branch information
acidjazz authored May 22, 2024
2 parents f57e890 + f1e52c5 commit 7469339
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 128 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

> Bring VulnCheck into your CI/CD pipeline.
This Github Action uses the VulnCheck CLI to integrte security-related tasks
This Github Action uses the VulnCheck
[CLI](https://github.com/vulncheck-oss/cli) to integrate security-related tasks
into your CI/CD pipeline.

![CI](https://github.com/vulncheck-oss/action/actions/workflows/ci.yml/badge.svg)
Expand Down
109 changes: 109 additions & 0 deletions __tests__/scan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import type { ScanThreshold, ScanResult } from '../src/types'
import { processThresholds, scanDiff } from '../src/scan'
import * as core from '@actions/core'

jest.mock('@actions/core')

describe('Scan', () => {
describe('processThresholds', () => {
it('should process thresholds correctly', () => {
const mockInput = jest.spyOn(core, 'getInput')
mockInput.mockImplementation(name => {
switch (name) {
case 'scan-cvss-base-threshold':
return '7.0'
case 'scan-cvss-temporal-threshold':
return '5.0'
default:
return ''
}
})

const result: ScanResult = {
vulnerabilities: [],
failed: '',
success: '',
}

const expected: ScanThreshold = {
base: '7.0',
temporal: '5.0',
baseMatches: [],
temporalMatches: [],
baseMatchesBelow: [],
temporalMatchesBelow: [],
total: 0,
totalBelow: 0,
}

expect(processThresholds(result)).toEqual(expected)
})
})

describe('scanDiff', () => {
it('should calculate differences correctly', () => {
const prevScan: ScanResult = {
vulnerabilities: [
{
name: 'vuln1',
version: '1.0.0',
cve: 'CVE-2021-1234',
in_kev: false,
cvss_base_score: '5.0',
cvss_temporal_score: '4.0',
fixed_versions: '1.0.1',
},
{
name: 'vuln2',
version: '2.0.0',
cve: 'CVE-2021-2345',
in_kev: false,
cvss_base_score: '7.0',
cvss_temporal_score: '6.0',
fixed_versions: '2.0.1',
},
],
failed: '',
success: '',
}

const currentScan: ScanResult = {
vulnerabilities: [
{
name: 'vuln2',
version: '2.0.0',
cve: 'CVE-2021-2345',
in_kev: false,
cvss_base_score: '7.0',
cvss_temporal_score: '6.0',
fixed_versions: '2.0.1',
},
{
name: 'vuln3',
version: '3.0.0',
cve: 'CVE-2021-3456',
in_kev: false,
cvss_base_score: '8.0',
cvss_temporal_score: '7.0',
fixed_versions: '3.0.1',
},
],
failed: '',
success: '',
}

const expectedDiff = [
{
cve: 'CVE-2021-1234',
added: true,
},
{
cve: 'CVE-2021-3456',
removed: true,
},
]

expect(scanDiff(prevScan, currentScan)).toEqual(expectedDiff)
})
})
})
55 changes: 55 additions & 0 deletions __tests__/table.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { table, rows } from '../src/table'
import type { ScanResultVuln, ScanResultVulnDiff, TableRow } from '../src/types'

describe('table.ts tests', () => {
test('table function should generate a markdown table', () => {
const headers = ['Header1', 'Header2']
const tableRows: TableRow[] = [
{
added: false,
removed: false,
cells: [{ value: 'Cell1' }, { value: 'Cell2' }],
},
]
const title = 'Test Title'

const result = table(headers, tableRows, title)

expect(result).toContain(title)
expect(result).toContain(headers.join(' | '))
expect(result).toContain(
tableRows[0].cells.map(cell => cell.value).join(' | '),
)
})

test('rows function should generate table rows', () => {
const vulns: ScanResultVuln[] = [
{
name: 'Test Vuln',
version: '1.0.0',
cve: 'CVE-2021-1234',
in_kev: true,
cvss_base_score: '5.0',
cvss_temporal_score: '4.0',
fixed_versions: '1.0.1',
},
]
const diff: ScanResultVulnDiff[] = [
{
cve: 'CVE-2021-1234',
added: true,
removed: false,
},
]

const result = rows(vulns, diff)

expect(result).toHaveLength(1)
expect(result[0].added).toBe(true)
expect(result[0].removed).toBe(false)
expect(result[0].cells[0].value).toBe(vulns[0].name)
expect(result[0].cells[2].link).toBe(
`https://vulncheck.com/browse/cve/${vulns[0].cve}`,
)
})
})
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'VulnCheck Actions'
name: 'VulnCheck Action'
description: 'Integrate VulnCheck into your GitHub Actions workflow'
author: 'Kevin Olson <[email protected]>'
branding:
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 51 additions & 35 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 7469339

Please sign in to comment.