github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/reporting/reporting_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package reporting
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestVulToMarkdown(t *testing.T) {
    16  	t.Parallel()
    17  	t.Run("success - empty", func(t *testing.T) {
    18  		t.Parallel()
    19  		vulReport := VulnerabilityReport{}
    20  		_, err := vulReport.ToMarkdown()
    21  		assert.NoError(t, err)
    22  	})
    23  
    24  	t.Run("success - filled", func(t *testing.T) {
    25  		t.Parallel()
    26  		vulReport := VulnerabilityReport{
    27  			ProjectName:          "theProjectName",
    28  			ProjectVersion:       "theProjectVersion",
    29  			BlackDuckProjectLink: "https://the.link.to.the.project.version",
    30  			ArtifactID:           "theArtifact",
    31  			Branch:               "main",
    32  			CommitID:             "acb123",
    33  			Description:          "This is the test description.",
    34  			Origin:               "Origin",
    35  			DependencyType:       "direct",
    36  			Footer:               "This is the test footer",
    37  			Group:                "the.group",
    38  			PipelineName:         "thePipelineName",
    39  			PipelineLink:         "https://the.link.to.the.pipeline",
    40  			PublishDate:          "2022-06-30",
    41  			Resolution:           "This is the test resolution.",
    42  			Score:                7.8,
    43  			Severity:             "high",
    44  			Version:              "1.2.3",
    45  			PackageURL:           "pkg:generic/the.group/theArtifact@1.2.3",
    46  			VulnerabilityLink:    "https://the.link/to/the/vulnerability",
    47  			VulnerabilityName:    "CVE-Test-001",
    48  		}
    49  		goldenFilePath := filepath.Join("testdata", "markdownVulnerability.golden")
    50  		expected, err := os.ReadFile(goldenFilePath)
    51  		assert.NoError(t, err)
    52  
    53  		res, err := vulReport.ToMarkdown()
    54  		assert.NoError(t, err)
    55  		assert.Equal(t, string(expected), string(res))
    56  	})
    57  }
    58  
    59  func TestToHTML(t *testing.T) {
    60  	t.Run("empty table", func(t *testing.T) {
    61  		report := ScanReport{
    62  			ReportTitle: "Report Test Title",
    63  			Subheaders:  []Subheader{{Description: "sub 1", Details: "1"}, {Description: "sub 2", Details: "2"}},
    64  			Overview: []OverviewRow{
    65  				{"overview 1", "1", Green},
    66  				{"overview 2", "2", Green},
    67  			},
    68  			FurtherInfo: "this is further information",
    69  			ReportTime:  time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
    70  			DetailTable: ScanDetailTable{
    71  				Headers:       []string{"column 1", "column 2"},
    72  				Rows:          []ScanRow{},
    73  				WithCounter:   true,
    74  				CounterHeader: "Entry #",
    75  				NoRowsMessage: "no rows available",
    76  			},
    77  		}
    78  		expectedSub := `<span>
    79  		sub 1: 1<br />
    80  		sub 2: 2<br />
    81  		</span>
    82  	</h2>`
    83  		expectedOverview := `<h3>
    84  		overview 1: 1<br />
    85  		overview 2: 2<br />
    86  		</h3>`
    87  
    88  		res, err := report.ToHTML()
    89  		result := string(res)
    90  		assert.NoError(t, err)
    91  		assert.Contains(t, result, "<h1>Report Test Title</h1>")
    92  		assert.Contains(t, result, expectedSub)
    93  		assert.Contains(t, result, expectedOverview)
    94  		assert.Contains(t, result, `<span>this is further information</span>`)
    95  		assert.Contains(t, result, `<th>Entry #</th>`)
    96  		assert.Contains(t, result, `<th>column 1</th>`)
    97  		assert.Contains(t, result, `<th>column 2</th>`)
    98  		assert.Contains(t, result, "Snapshot taken: Jan 01, 2021 - 00:00:00 UTC")
    99  		assert.Contains(t, result, `<td colspan="3">no rows available</td>`)
   100  	})
   101  
   102  	t.Run("table with content", func(t *testing.T) {
   103  		report := ScanReport{
   104  			ReportTitle: "Report Test Title",
   105  			ReportTime:  time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
   106  			DetailTable: ScanDetailTable{
   107  				Headers: []string{"column 1", "column 2"},
   108  				Rows: []ScanRow{
   109  					{Columns: []ScanCell{{Content: "c1 r1"}, {Content: "c2 r1"}}},
   110  					{Columns: []ScanCell{{Content: "c1 r2"}, {Content: "c2 r2"}}},
   111  					{Columns: []ScanCell{{Content: "c1 r3", Style: Green}, {Content: "c2 r3", Style: Black}}},
   112  				},
   113  				CounterHeader: "Entry #",
   114  				WithCounter:   true,
   115  			},
   116  		}
   117  		res, err := report.ToHTML()
   118  		result := string(res)
   119  		assert.NoError(t, err)
   120  		assert.Contains(t, result, `<th>Entry #</th>`)
   121  		assert.Contains(t, result, `<td>1</td>`)
   122  		assert.Contains(t, result, `<td>c1 r1</td>`)
   123  		assert.Contains(t, result, `<td>c2 r1</td>`)
   124  		assert.Contains(t, result, `<td>2</td>`)
   125  		assert.Contains(t, result, `<td>c1 r2</td>`)
   126  		assert.Contains(t, result, `<td>c2 r2</td>`)
   127  		assert.Contains(t, result, `<td>3</td>`)
   128  		assert.Contains(t, result, `<td class="green-cell">c1 r3</td>`)
   129  		assert.Contains(t, result, `<td class="black-cell">c2 r3</td>`)
   130  	})
   131  }
   132  
   133  func TestToMarkdown(t *testing.T) {
   134  	t.Run("table with details", func(t *testing.T) {
   135  		report := ScanReport{
   136  			ReportTitle: "Report Test Title",
   137  			Subheaders:  []Subheader{{Description: "sub 1", Details: "1"}, {Description: "sub 2", Details: "2"}},
   138  			Overview: []OverviewRow{
   139  				{"overview 1", "1", Green},
   140  				{"overview 2", "2", Green},
   141  			},
   142  			FurtherInfo:    "this is further information",
   143  			SuccessfulScan: true,
   144  			ReportTime:     time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
   145  			DetailTable: ScanDetailTable{
   146  				Headers: []string{"column 1", "column 2"},
   147  				Rows: []ScanRow{
   148  					{Columns: []ScanCell{{Content: "c1 r1"}, {Content: "c2 r1"}}},
   149  					{Columns: []ScanCell{{Content: "c1 r2"}, {Content: "c2 r2"}}},
   150  					{Columns: []ScanCell{{Content: "c1 r3", Style: Green}, {Content: "c2 r3", Style: Black}}},
   151  				},
   152  				CounterHeader: "Entry #",
   153  				WithCounter:   true,
   154  			},
   155  		}
   156  
   157  		res, err := report.ToMarkdown()
   158  		result := string(res)
   159  		assert.NoError(t, err)
   160  		assert.Contains(t, result, `## :white_check_mark: Report Test Title`)
   161  		assert.Contains(t, result, `<td><b>sub 1:</b></td><td>1</td>`)
   162  		assert.Contains(t, result, `<td><b>sub 2:</b></td><td>2</td>`)
   163  		assert.Contains(t, result, `<tr><td>overview 1:</td><td>1</td></tr>`)
   164  		assert.Contains(t, result, `<tr><td>overview 2:</td><td>2</td></tr>`)
   165  		assert.Contains(t, result, `this is further information`)
   166  		assert.Contains(t, result, "Snapshot taken: <i>Jan 01, 2021 - 00:00:00 UTC</i>")
   167  		assert.Contains(t, result, "<details><summary><i>Report Test Title details:</i></summary>")
   168  	})
   169  
   170  	t.Run("table without details", func(t *testing.T) {
   171  		report := ScanReport{
   172  			ReportTitle: "Report Test Title",
   173  			Subheaders:  []Subheader{{Description: "sub 1", Details: "1"}, {Description: "sub 2", Details: "2"}},
   174  			Overview: []OverviewRow{
   175  				{"overview 1", "1", Green},
   176  				{"overview 2", "2", Green},
   177  			},
   178  			FurtherInfo: "this is further information",
   179  			ReportTime:  time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
   180  		}
   181  
   182  		res, err := report.ToMarkdown()
   183  		result := string(res)
   184  		assert.NoError(t, err)
   185  		assert.Contains(t, result, `## :x: Report Test Title`)
   186  		assert.NotContains(t, result, "<details><summary><i>Report Test Title details:</i></summary>")
   187  	})
   188  }
   189  
   190  func TestTableColumnCount(t *testing.T) {
   191  	t.Run("table without counter", func(t *testing.T) {
   192  		details := ScanDetailTable{
   193  			Headers:     []string{"column 1", "column 1"},
   194  			WithCounter: false,
   195  		}
   196  		assert.Equal(t, 2, tableColumnCount(details))
   197  	})
   198  	t.Run("table with counter", func(t *testing.T) {
   199  		details := ScanDetailTable{
   200  			Headers:     []string{"column 1", "column 1"},
   201  			WithCounter: true,
   202  		}
   203  		assert.Equal(t, 3, tableColumnCount(details))
   204  	})
   205  }
   206  
   207  func TestShouldDrawTable(t *testing.T) {
   208  	tt := []struct {
   209  		table    ScanDetailTable
   210  		expected bool
   211  	}{
   212  		{table: ScanDetailTable{}, expected: false},
   213  		{table: ScanDetailTable{Headers: []string{"column1", "column2"}}, expected: true},
   214  	}
   215  
   216  	for _, test := range tt {
   217  		assert.Equal(t, test.expected, shouldDrawTable(test.table))
   218  	}
   219  }