Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions internal/report/report_markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func TestGenerateMarkdownReport(t *testing.T) {
## XSS PoCs
| # | Type | Severity | Method | Param | Inject-Type | CWE |
|---|---|---|---|---|---|---|
| #0 | XSS | High | GET | param1 | inHTML | CWE-79 |
| [PoC1](#PoC1) | XSS | High | GET | param1 | inHTML | CWE-79 |

### PoC #0
### PoC1
` + "```\n<script>alert(1)</script>\n```" + `

` // Adding the code block directly as it contains backticks
Expand Down Expand Up @@ -153,9 +153,9 @@ func TestGenerateMarkdownReport_NoParams(t *testing.T) {
## XSS PoCs
| # | Type | Severity | Method | Param | Inject-Type | CWE |
|---|---|---|---|---|---|---|
| #0 | XSS | High | GET | param1 | inHTML | CWE-79 |
| [PoC1](#PoC1) | XSS | High | GET | param1 | inHTML | CWE-79 |

### PoC #0
### PoC1
` + "```\n<script>alert(1)</script>\n```" + `

`
Expand Down Expand Up @@ -240,16 +240,16 @@ func TestGenerateMarkdownReport_MultiplePoCs(t *testing.T) {

report := GenerateMarkdownReport(scanResult, options)

if !strings.Contains(report, "### PoC #0") {
t.Errorf("Report does not contain PoC #0")
if !strings.Contains(report, "### PoC1") {
t.Errorf("Report does not contain PoC1")
}
if !strings.Contains(report, "<script>alert(1)</script>") {
t.Errorf("Report does not contain data for PoC #0")
t.Errorf("Report does not contain data for PoC1")
}
if !strings.Contains(report, "### PoC #1") {
t.Errorf("Report does not contain PoC #1")
if !strings.Contains(report, "### PoC2") {
t.Errorf("Report does not contain PoC2")
}
if !strings.Contains(report, "';alert(2);'") {
t.Errorf("Report does not contain data for PoC #1")
t.Errorf("Report does not contain data for PoC2")
}
Comment on lines +243 to 254
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This series of if statements can be made more readable and maintainable by using a table-driven approach, which is a common pattern in Go tests. This refactoring also allows for more descriptive error messages, making it easier to debug test failures.

Using a loop over a slice of test cases makes it clearer what is being tested and simplifies adding or removing checks in the future.

	testCases := []struct {
		substr, purpose string
	}{
		{"### PoC1", "PoC1 header"},
		{"<script>alert(1)</script>", "PoC1 data"},
		{"### PoC2", "PoC2 header"},
		{"';alert(2);'", "PoC2 data"},
	}

	for _, tc := range testCases {
		if !strings.Contains(report, tc.substr) {
			t.Errorf("report is missing %s: expected to find %q", tc.purpose, tc.substr)
		}
	}

}