81 lines
2.5 KiB
Go
81 lines
2.5 KiB
Go
package checker
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDiagnoseAndReportRender(t *testing.T) {
|
|
d := &BlacklistData{
|
|
Domain: "example.com",
|
|
RegisteredDomain: "example.com",
|
|
CollectedAt: time.Now(),
|
|
Results: []SourceResult{
|
|
{
|
|
SourceID: "dnsbl", SourceName: "Spamhaus DBL",
|
|
Subject: "dbl.spamhaus.org",
|
|
Enabled: true, Listed: true, Severity: SeverityCrit,
|
|
Reasons: []string{"Phishing domain"},
|
|
LookupURL: "https://check.spamhaus.org/results/?query=example.com",
|
|
RemovalURL: "https://www.spamhaus.org/dbl/removal/",
|
|
},
|
|
{
|
|
SourceID: "dnsbl", SourceName: "URIBL multi",
|
|
Subject: "multi.uribl.com",
|
|
Enabled: true, Error: "i/o timeout",
|
|
},
|
|
{
|
|
SourceID: "openphish", SourceName: "OpenPhish feed",
|
|
Enabled: true, Listed: true, Severity: SeverityCrit,
|
|
Evidence: []Evidence{{Label: "URL", Value: "http://example.com/login"}},
|
|
},
|
|
},
|
|
}
|
|
|
|
diags := diagnose(d)
|
|
if len(diags) < 2 {
|
|
t.Fatalf("expected at least 2 diagnoses, got %d", len(diags))
|
|
}
|
|
if diags[0].Severity != SeverityCrit {
|
|
t.Errorf("first diagnosis severity = %q, want crit", diags[0].Severity)
|
|
}
|
|
|
|
p := &blacklistProvider{}
|
|
html, err := p.GetHTMLReport(staticCtx{data: jsonOf(t, d)})
|
|
if err != nil {
|
|
t.Fatalf("GetHTMLReport: %v", err)
|
|
}
|
|
for _, want := range []string{"Spamhaus DBL", "Action required", "OpenPhish"} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("report missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHeadline(t *testing.T) {
|
|
if h, c := headline(0); c != SeverityOK || !strings.Contains(h, "clean") {
|
|
t.Errorf("headline(0) = %q/%q", h, c)
|
|
}
|
|
if h, c := headline(1); c != SeverityCrit || !strings.Contains(h, "1") {
|
|
t.Errorf("headline(1) = %q/%q", h, c)
|
|
}
|
|
if h, c := headline(3); c != SeverityCrit || !strings.Contains(h, "3") {
|
|
t.Errorf("headline(3) = %q/%q", h, c)
|
|
}
|
|
}
|
|
|
|
func TestSectionStatus(t *testing.T) {
|
|
if l, c := sectionStatus([]SourceResult{{Enabled: true, Listed: true, Severity: SeverityCrit}}); c != "crit" || !strings.HasPrefix(l, "LISTED") {
|
|
t.Errorf("sectionStatus listed = %q/%q", l, c)
|
|
}
|
|
if l, c := sectionStatus([]SourceResult{{Enabled: true}}); c != "ok" || l != "Clean" {
|
|
t.Errorf("sectionStatus clean = %q/%q", l, c)
|
|
}
|
|
if l, c := sectionStatus([]SourceResult{{Enabled: false}}); c != "muted" || l != "Disabled" {
|
|
t.Errorf("sectionStatus disabled = %q/%q", l, c)
|
|
}
|
|
if l, c := sectionStatus([]SourceResult{{Enabled: true, Error: "boom"}}); c != "warn" || l != "Errors" {
|
|
t.Errorf("sectionStatus error = %q/%q", l, c)
|
|
}
|
|
}
|