1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/whitesource/scanReports_test.go

112 lines
4.1 KiB
Go
Raw Normal View History

package whitesource
import (
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDownloadReports(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
// init
options := ReportOptions{
ReportDirectory: "report-dir",
VulnerabilityReportFormat: "txt",
}
utils := &mock.FilesMock{}
system := NewSystemMock("2010-05-30 00:15:00 +0100")
scan := &Scan{ProductVersion: "1"}
_ = scan.AppendScannedProject("mock-project")
_ = scan.UpdateProjects("mock-product-token", system)
// test
paths, err := scan.DownloadReports(options, utils, system)
// assert
if assert.NoError(t, err) && assert.Len(t, paths, 2) {
vPath := filepath.Join("report-dir", "mock-project - 1-vulnerability-report.txt")
assert.True(t, utils.HasWrittenFile(vPath))
vContent, _ := utils.FileRead(vPath)
assert.Equal(t, []byte("mock-vulnerability-report"), vContent)
rPath := filepath.Join("report-dir", "mock-project - 1-risk-report.pdf")
assert.True(t, utils.HasWrittenFile(rPath))
rContent, _ := utils.FileRead(rPath)
assert.Equal(t, []byte("mock-risk-report"), rContent)
}
})
t.Run("success - projects with /", func(t *testing.T) {
// init
options := ReportOptions{
ReportDirectory: "report-dir",
VulnerabilityReportFormat: "txt",
}
utils := &mock.FilesMock{}
system := NewSystemMockWithProjectName("2010-05-30 00:15:00 +0100", "@test/mock-project - 1")
scan := &Scan{ProductVersion: "1", scannedProjects: map[string]Project{"@test/mock-project - 1": system.Projects[0]}}
//scan := &Scan{ProductVersion: "1", scannedProjects: map[string]Project{"mock-product-token": {Name:"@test/mock-project"}}}
//_ = scan.AppendScannedProject("@test/mock-project")
//_ = scan.UpdateProjects("mock-product-token", system)
// test
paths, err := scan.DownloadReports(options, utils, system)
// assert
if assert.NoError(t, err) && assert.Len(t, paths, 2) {
vPath := filepath.Join("report-dir", "@test_mock-project - 1-vulnerability-report.txt")
assert.True(t, utils.HasWrittenFile(vPath))
vContent, _ := utils.FileRead(vPath)
assert.Equal(t, []byte("mock-vulnerability-report"), vContent)
rPath := filepath.Join("report-dir", "@test_mock-project - 1-risk-report.pdf")
assert.True(t, utils.HasWrittenFile(rPath))
rContent, _ := utils.FileRead(rPath)
assert.Equal(t, []byte("mock-risk-report"), rContent)
}
})
t.Run("invalid project token", func(t *testing.T) {
// init
options := ReportOptions{
ReportDirectory: "report-dir",
VulnerabilityReportFormat: "txt",
}
utils := &mock.FilesMock{}
system := NewSystemMock("2010-05-30 00:15:00 +0100")
scan := &Scan{ProductVersion: "1"}
_ = scan.AppendScannedProject("no-such-project")
_ = scan.UpdateProjects("mock-product-token", system)
// test
paths, err := scan.DownloadReports(options, utils, system)
// assert
assert.EqualError(t, err, "no project with token '' found in Whitesource")
assert.Nil(t, paths)
})
t.Run("multiple scanned projects", func(t *testing.T) {
// init
options := ReportOptions{
ReportDirectory: "report-dir",
VulnerabilityReportFormat: "txt",
}
utils := &mock.FilesMock{}
system := NewSystemMock("2010-05-30 00:15:00 +0100")
scan := &Scan{ProductVersion: "1"}
err := scan.AppendScannedProjectVersion("mock-project - 1")
require.NoError(t, err)
_ = scan.UpdateProjects("mock-product-token", system)
// test
paths, err := scan.DownloadReports(options, utils, system)
// assert
if assert.NoError(t, err) && assert.Len(t, paths, 2) {
vPath := filepath.Join("report-dir", "mock-project - 1-vulnerability-report.txt")
assert.True(t, utils.HasWrittenFile(vPath))
vContent, _ := utils.FileRead(vPath)
assert.Equal(t, []byte("mock-vulnerability-report"), vContent)
rPath := filepath.Join("report-dir", "mock-project - 1-risk-report.pdf")
assert.True(t, utils.HasWrittenFile(rPath))
rContent, _ := utils.FileRead(rPath)
assert.Equal(t, []byte("mock-risk-report"), rContent)
}
})
}