diff --git a/report/api/models.go b/report/api/models.go index 01158ed..c0a9988 100644 --- a/report/api/models.go +++ b/report/api/models.go @@ -2,8 +2,9 @@ package api // CreateReportParameters ... type CreateReportParameters struct { - Title string `json:"title"` - Assets []CreateReportAsset `json:"assets"` + Title string `json:"title"` + Category string `json:"category,omitempty"` + Assets []CreateReportAsset `json:"assets"` } // CreateReportAsset ... diff --git a/report/collect.go b/report/collect.go index cba86df..9aca664 100644 --- a/report/collect.go +++ b/report/collect.go @@ -1,6 +1,8 @@ package report import ( + "encoding/json" + "fmt" "io" "io/fs" "net/http" @@ -8,6 +10,10 @@ import ( "path/filepath" ) +const ( + htmlReportInfoFile = "report-info.json" +) + func collectReports(dir string) ([]Report, error) { var reports []Report @@ -28,7 +34,7 @@ func collectReports(dir string) ([]Report, error) { return err } - if d.IsDir() || d.Name() == ".DS_Store" { + if d.IsDir() || d.Name() == ".DS_Store" || d.Name() == htmlReportInfoFile { return nil } @@ -61,8 +67,16 @@ func collectReports(dir string) ([]Report, error) { continue } + var reportInfo Info + if infoFileData, err := os.ReadFile(filepath.Join(testDir, htmlReportInfoFile)); err == nil { + if err := json.Unmarshal(infoFileData, &reportInfo); err != nil { + return nil, fmt.Errorf("cannot parse report info file: %w", err) + } + } + reports = append(reports, Report{ Name: entry.Name(), + Info: reportInfo, Assets: assets, }) } diff --git a/report/models.go b/report/models.go index 53262f0..2755d62 100644 --- a/report/models.go +++ b/report/models.go @@ -3,6 +3,7 @@ package report // Report ... type Report struct { Name string + Info Info Assets []Asset } @@ -19,3 +20,8 @@ type ServerReport struct { Identifier string AssetURLs map[string]string } + +// Info ... +type Info struct { + Category string `json:"category"` +} diff --git a/report/report.go b/report/report.go index 5466f3d..2babfd6 100644 --- a/report/report.go +++ b/report/report.go @@ -91,8 +91,9 @@ func (h *HTMLReportUploader) createReport(report Report) (ServerReport, error) { } resp, err := h.client.CreateReport(api.CreateReportParameters{ - Title: report.Name, - Assets: assets, + Title: report.Name, + Category: report.Info.Category, + Assets: assets, }) if err != nil { return ServerReport{}, err diff --git a/report/report_test.go b/report/report_test.go index 32304fa..1b9bc75 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -2,6 +2,7 @@ package report import ( "encoding/base64" + "encoding/json" "fmt" "os" "path/filepath" @@ -27,6 +28,7 @@ func TestFindsAndUploadsReports(t *testing.T) { mockClient := mocks.NewClientAPI(t) setupMockingForReport(mockClient, reports[0]) setupMockingForReport(mockClient, reports[2]) + setupMockingForReport(mockClient, reports[3]) uploader := HTMLReportUploader{ client: mockClient, @@ -79,6 +81,19 @@ func createReports(t *testing.T) (string, []Report) { }, }, }, + { + "name": "Test D", + "info": map[string]string{ + "category": "random", + }, + "assets": []map[string]string{ + { + "name": "index.html", + "size": "1", + "type": "text/plain; charset=utf-8", + }, + }, + }, } var reports []Report @@ -135,8 +150,20 @@ func createReports(t *testing.T) (string, []Report) { }) } + var reportInfo Info + if reportInfoRaw, ok := data["info"].(map[string]string); ok { + reportInfoData, err := json.Marshal(reportInfoRaw) + require.NoError(t, err) + + reportInfoFile := filepath.Join(reportPath, htmlReportInfoFile) + require.NoError(t, os.WriteFile(reportInfoFile, reportInfoData, 0755)) + + require.NoError(t, json.Unmarshal(reportInfoData, &reportInfo)) + } + reports = append(reports, Report{ Name: reportName, + Info: reportInfo, Assets: assets, }) } @@ -160,8 +187,9 @@ func setupMockingForReport(client *mocks.ClientAPI, report Report) { }) } requestParams := api.CreateReportParameters{ - Title: report.Name, - Assets: requestAssets, + Title: report.Name, + Category: report.Info.Category, + Assets: requestAssets, } response := api.CreateReportResponse{