Improve spamassassin report
This commit is contained in:
parent
abfd1f0155
commit
e77bffb04f
6 changed files with 203 additions and 106 deletions
|
|
@ -674,7 +674,12 @@ components:
|
||||||
- score
|
- score
|
||||||
- required_score
|
- required_score
|
||||||
- is_spam
|
- is_spam
|
||||||
|
- test_details
|
||||||
properties:
|
properties:
|
||||||
|
version:
|
||||||
|
type: string
|
||||||
|
description: SpamAssassin version
|
||||||
|
example: "SpamAssassin 4.0.1"
|
||||||
score:
|
score:
|
||||||
type: number
|
type: number
|
||||||
format: float
|
format: float
|
||||||
|
|
@ -695,10 +700,44 @@ components:
|
||||||
type: string
|
type: string
|
||||||
description: List of triggered SpamAssassin tests
|
description: List of triggered SpamAssassin tests
|
||||||
example: ["BAYES_00", "DKIM_SIGNED"]
|
example: ["BAYES_00", "DKIM_SIGNED"]
|
||||||
|
test_details:
|
||||||
|
type: object
|
||||||
|
additionalProperties:
|
||||||
|
$ref: '#/components/schemas/SpamTestDetail'
|
||||||
|
description: Map of test names to their detailed results
|
||||||
|
example:
|
||||||
|
BAYES_00:
|
||||||
|
name: "BAYES_00"
|
||||||
|
score: -1.9
|
||||||
|
description: "Bayes spam probability is 0 to 1%"
|
||||||
|
DKIM_SIGNED:
|
||||||
|
name: "DKIM_SIGNED"
|
||||||
|
score: 0.1
|
||||||
|
description: "Message has a DKIM or DK signature, not necessarily valid"
|
||||||
report:
|
report:
|
||||||
type: string
|
type: string
|
||||||
description: Full SpamAssassin report
|
description: Full SpamAssassin report
|
||||||
|
|
||||||
|
SpamTestDetail:
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- name
|
||||||
|
- score
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: Test name
|
||||||
|
example: "BAYES_00"
|
||||||
|
score:
|
||||||
|
type: number
|
||||||
|
format: float
|
||||||
|
description: Score contribution of this test
|
||||||
|
example: -1.9
|
||||||
|
description:
|
||||||
|
type: string
|
||||||
|
description: Human-readable description of what this test checks
|
||||||
|
example: "Bayes spam probability is 0 to 1%"
|
||||||
|
|
||||||
DNSResults:
|
DNSResults:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ type AnalysisResults struct {
|
||||||
DNS *api.DNSResults
|
DNS *api.DNSResults
|
||||||
Headers *api.HeaderAnalysis
|
Headers *api.HeaderAnalysis
|
||||||
RBL *RBLResults
|
RBL *RBLResults
|
||||||
SpamAssassin *SpamAssassinResult
|
SpamAssassin *api.SpamAssassinResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalyzeEmail performs complete email analysis
|
// AnalyzeEmail performs complete email analysis
|
||||||
|
|
@ -157,21 +157,7 @@ func (r *ReportGenerator) GenerateReport(testID uuid.UUID, results *AnalysisResu
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add SpamAssassin result
|
// Add SpamAssassin result
|
||||||
if results.SpamAssassin != nil {
|
report.Spamassassin = results.SpamAssassin
|
||||||
report.Spamassassin = &api.SpamAssassinResult{
|
|
||||||
Score: float32(results.SpamAssassin.Score),
|
|
||||||
RequiredScore: float32(results.SpamAssassin.RequiredScore),
|
|
||||||
IsSpam: results.SpamAssassin.IsSpam,
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(results.SpamAssassin.Tests) > 0 {
|
|
||||||
report.Spamassassin.Tests = &results.SpamAssassin.Tests
|
|
||||||
}
|
|
||||||
|
|
||||||
if results.SpamAssassin.RawReport != "" {
|
|
||||||
report.Spamassassin.Report = &results.SpamAssassin.RawReport
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add raw headers
|
// Add raw headers
|
||||||
if results.Email != nil && results.Email.RawHeaders != "" {
|
if results.Email != nil && results.Email.RawHeaders != "" {
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.happydns.org/happyDeliver/internal/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SpamAssassinAnalyzer analyzes SpamAssassin results from email headers
|
// SpamAssassinAnalyzer analyzes SpamAssassin results from email headers
|
||||||
|
|
@ -36,33 +38,15 @@ func NewSpamAssassinAnalyzer() *SpamAssassinAnalyzer {
|
||||||
return &SpamAssassinAnalyzer{}
|
return &SpamAssassinAnalyzer{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpamAssassinResult represents parsed SpamAssassin results
|
|
||||||
type SpamAssassinResult struct {
|
|
||||||
IsSpam bool
|
|
||||||
Score float64
|
|
||||||
RequiredScore float64
|
|
||||||
Tests []string
|
|
||||||
TestDetails map[string]SpamTestDetail
|
|
||||||
Version string
|
|
||||||
RawReport string
|
|
||||||
}
|
|
||||||
|
|
||||||
// SpamTestDetail contains details about a specific spam test
|
|
||||||
type SpamTestDetail struct {
|
|
||||||
Name string
|
|
||||||
Score float64
|
|
||||||
Description string
|
|
||||||
}
|
|
||||||
|
|
||||||
// AnalyzeSpamAssassin extracts and analyzes SpamAssassin results from email headers
|
// AnalyzeSpamAssassin extracts and analyzes SpamAssassin results from email headers
|
||||||
func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAssassinResult {
|
func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *api.SpamAssassinResult {
|
||||||
headers := email.GetSpamAssassinHeaders()
|
headers := email.GetSpamAssassinHeaders()
|
||||||
if len(headers) == 0 {
|
if len(headers) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result := &SpamAssassinResult{
|
result := &api.SpamAssassinResult{
|
||||||
TestDetails: make(map[string]SpamTestDetail),
|
TestDetails: make(map[string]api.SpamTestDetail),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse X-Spam-Status header
|
// Parse X-Spam-Status header
|
||||||
|
|
@ -73,7 +57,7 @@ func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAss
|
||||||
// Parse X-Spam-Score header (as fallback if not in X-Spam-Status)
|
// Parse X-Spam-Score header (as fallback if not in X-Spam-Status)
|
||||||
if scoreHeader, ok := headers["X-Spam-Score"]; ok && result.Score == 0 {
|
if scoreHeader, ok := headers["X-Spam-Score"]; ok && result.Score == 0 {
|
||||||
if score, err := strconv.ParseFloat(strings.TrimSpace(scoreHeader), 64); err == nil {
|
if score, err := strconv.ParseFloat(strings.TrimSpace(scoreHeader), 64); err == nil {
|
||||||
result.Score = score
|
result.Score = float32(score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,13 +68,13 @@ func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAss
|
||||||
|
|
||||||
// Parse X-Spam-Report header for detailed test results
|
// Parse X-Spam-Report header for detailed test results
|
||||||
if reportHeader, ok := headers["X-Spam-Report"]; ok {
|
if reportHeader, ok := headers["X-Spam-Report"]; ok {
|
||||||
result.RawReport = strings.Replace(reportHeader, " * ", "\n* ", -1)
|
result.Report = api.PtrTo(strings.Replace(reportHeader, " * ", "\n* ", -1))
|
||||||
a.parseSpamReport(reportHeader, result)
|
a.parseSpamReport(reportHeader, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse X-Spam-Checker-Version
|
// Parse X-Spam-Checker-Version
|
||||||
if versionHeader, ok := headers["X-Spam-Checker-Version"]; ok {
|
if versionHeader, ok := headers["X-Spam-Checker-Version"]; ok {
|
||||||
result.Version = strings.TrimSpace(versionHeader)
|
result.Version = api.PtrTo(strings.TrimSpace(versionHeader))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
@ -98,7 +82,7 @@ func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAss
|
||||||
|
|
||||||
// parseSpamStatus parses the X-Spam-Status header
|
// parseSpamStatus parses the X-Spam-Status header
|
||||||
// Format: Yes/No, score=5.5 required=5.0 tests=TEST1,TEST2,TEST3 autolearn=no
|
// Format: Yes/No, score=5.5 required=5.0 tests=TEST1,TEST2,TEST3 autolearn=no
|
||||||
func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssassinResult) {
|
func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *api.SpamAssassinResult) {
|
||||||
// Check if spam (first word)
|
// Check if spam (first word)
|
||||||
parts := strings.SplitN(header, ",", 2)
|
parts := strings.SplitN(header, ",", 2)
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
|
|
@ -110,7 +94,7 @@ func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssass
|
||||||
scoreRe := regexp.MustCompile(`score=(-?\d+\.?\d*)`)
|
scoreRe := regexp.MustCompile(`score=(-?\d+\.?\d*)`)
|
||||||
if matches := scoreRe.FindStringSubmatch(header); len(matches) > 1 {
|
if matches := scoreRe.FindStringSubmatch(header); len(matches) > 1 {
|
||||||
if score, err := strconv.ParseFloat(matches[1], 64); err == nil {
|
if score, err := strconv.ParseFloat(matches[1], 64); err == nil {
|
||||||
result.Score = score
|
result.Score = float32(score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,19 +102,19 @@ func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssass
|
||||||
requiredRe := regexp.MustCompile(`required=(-?\d+\.?\d*)`)
|
requiredRe := regexp.MustCompile(`required=(-?\d+\.?\d*)`)
|
||||||
if matches := requiredRe.FindStringSubmatch(header); len(matches) > 1 {
|
if matches := requiredRe.FindStringSubmatch(header); len(matches) > 1 {
|
||||||
if required, err := strconv.ParseFloat(matches[1], 64); err == nil {
|
if required, err := strconv.ParseFloat(matches[1], 64); err == nil {
|
||||||
result.RequiredScore = required
|
result.RequiredScore = float32(required)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract tests
|
// Extract tests
|
||||||
testsRe := regexp.MustCompile(`tests=([^\s]+)`)
|
testsRe := regexp.MustCompile(`tests=([^=]+)(?:\s|$)`)
|
||||||
if matches := testsRe.FindStringSubmatch(header); len(matches) > 1 {
|
if matches := testsRe.FindStringSubmatch(header); len(matches) > 1 {
|
||||||
testsStr := matches[1]
|
testsStr := matches[1]
|
||||||
// Tests can be comma or space separated
|
// Tests can be comma or space separated
|
||||||
tests := strings.FieldsFunc(testsStr, func(r rune) bool {
|
tests := strings.FieldsFunc(testsStr, func(r rune) bool {
|
||||||
return r == ',' || r == ' '
|
return r == ',' || r == ' '
|
||||||
})
|
})
|
||||||
result.Tests = tests
|
result.Tests = &tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,17 +122,20 @@ func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssass
|
||||||
// Format varies, but typically:
|
// Format varies, but typically:
|
||||||
// * 1.5 TEST_NAME Description of test
|
// * 1.5 TEST_NAME Description of test
|
||||||
// * 0.0 TEST_NAME2 Description
|
// * 0.0 TEST_NAME2 Description
|
||||||
// Note: mail.Header.Get() joins continuation lines, so newlines are removed.
|
// Multiline descriptions continue on lines starting with * but without score:
|
||||||
// We split on '*' to separate individual tests.
|
// * 0.0 TEST_NAME Description line 1
|
||||||
func (a *SpamAssassinAnalyzer) parseSpamReport(report string, result *SpamAssassinResult) {
|
// * continuation line 2
|
||||||
// The report header has been joined by mail.Header.Get(), so we split on '*'
|
// * continuation line 3
|
||||||
// Each segment starting with '*' is either a test line or continuation
|
func (a *SpamAssassinAnalyzer) parseSpamReport(report string, result *api.SpamAssassinResult) {
|
||||||
segments := strings.Split(report, "*")
|
segments := strings.Split(report, "*")
|
||||||
|
|
||||||
// Regex to match test lines: score TEST_NAME Description
|
// Regex to match test lines: score TEST_NAME Description
|
||||||
// Format: " 0.0 TEST_NAME Description" or " -0.1 TEST_NAME Description"
|
// Format: " 0.0 TEST_NAME Description" or " -0.1 TEST_NAME Description"
|
||||||
testRe := regexp.MustCompile(`^\s*(-?\d+\.?\d*)\s+(\S+)\s+(.*)$`)
|
testRe := regexp.MustCompile(`^\s*(-?\d+\.?\d*)\s+(\S+)\s+(.*)$`)
|
||||||
|
|
||||||
|
var currentTestName string
|
||||||
|
var currentDescription strings.Builder
|
||||||
|
|
||||||
for _, segment := range segments {
|
for _, segment := range segments {
|
||||||
segment = strings.TrimSpace(segment)
|
segment = strings.TrimSpace(segment)
|
||||||
if segment == "" {
|
if segment == "" {
|
||||||
|
|
@ -158,22 +145,55 @@ func (a *SpamAssassinAnalyzer) parseSpamReport(report string, result *SpamAssass
|
||||||
// Try to match as a test line
|
// Try to match as a test line
|
||||||
matches := testRe.FindStringSubmatch(segment)
|
matches := testRe.FindStringSubmatch(segment)
|
||||||
if len(matches) > 3 {
|
if len(matches) > 3 {
|
||||||
|
// Save previous test if exists
|
||||||
|
if currentTestName != "" {
|
||||||
|
description := strings.TrimSpace(currentDescription.String())
|
||||||
|
detail := api.SpamTestDetail{
|
||||||
|
Name: currentTestName,
|
||||||
|
Score: result.TestDetails[currentTestName].Score,
|
||||||
|
Description: &description,
|
||||||
|
}
|
||||||
|
result.TestDetails[currentTestName] = detail
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start new test
|
||||||
testName := matches[2]
|
testName := matches[2]
|
||||||
score, _ := strconv.ParseFloat(matches[1], 64)
|
score, _ := strconv.ParseFloat(matches[1], 64)
|
||||||
description := strings.TrimSpace(matches[3])
|
description := strings.TrimSpace(matches[3])
|
||||||
|
|
||||||
detail := SpamTestDetail{
|
currentTestName = testName
|
||||||
Name: testName,
|
currentDescription.Reset()
|
||||||
Score: score,
|
currentDescription.WriteString(description)
|
||||||
Description: description,
|
|
||||||
|
// Initialize with score
|
||||||
|
result.TestDetails[testName] = api.SpamTestDetail{
|
||||||
|
Name: testName,
|
||||||
|
Score: float32(score),
|
||||||
}
|
}
|
||||||
result.TestDetails[testName] = detail
|
} else if currentTestName != "" {
|
||||||
|
// This is a continuation line for the current test
|
||||||
|
// Add a space before appending to ensure proper word separation
|
||||||
|
if currentDescription.Len() > 0 {
|
||||||
|
currentDescription.WriteString(" ")
|
||||||
|
}
|
||||||
|
currentDescription.WriteString(segment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the last test if exists
|
||||||
|
if currentTestName != "" {
|
||||||
|
description := strings.TrimSpace(currentDescription.String())
|
||||||
|
detail := api.SpamTestDetail{
|
||||||
|
Name: currentTestName,
|
||||||
|
Score: result.TestDetails[currentTestName].Score,
|
||||||
|
Description: &description,
|
||||||
|
}
|
||||||
|
result.TestDetails[currentTestName] = detail
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CalculateSpamAssassinScore calculates the SpamAssassin contribution to deliverability
|
// CalculateSpamAssassinScore calculates the SpamAssassin contribution to deliverability
|
||||||
func (a *SpamAssassinAnalyzer) CalculateSpamAssassinScore(result *SpamAssassinResult) int {
|
func (a *SpamAssassinAnalyzer) CalculateSpamAssassinScore(result *api.SpamAssassinResult) int {
|
||||||
if result == nil {
|
if result == nil {
|
||||||
return 100 // No spam scan results, assume good
|
return 100 // No spam scan results, assume good
|
||||||
}
|
}
|
||||||
|
|
@ -192,6 +212,6 @@ func (a *SpamAssassinAnalyzer) CalculateSpamAssassinScore(result *SpamAssassinRe
|
||||||
return 0 // Failed spam test
|
return 0 // Failed spam test
|
||||||
} else {
|
} else {
|
||||||
// Linear scale between 0 and required threshold
|
// Linear scale between 0 and required threshold
|
||||||
return 100 - int(math.Round(score*100/result.RequiredScore))
|
return 100 - int(math.Round(float64(score*100/result.RequiredScore)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ import (
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.happydns.org/happyDeliver/internal/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseSpamStatus(t *testing.T) {
|
func TestParseSpamStatus(t *testing.T) {
|
||||||
|
|
@ -33,8 +35,8 @@ func TestParseSpamStatus(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
header string
|
header string
|
||||||
expectedIsSpam bool
|
expectedIsSpam bool
|
||||||
expectedScore float64
|
expectedScore float32
|
||||||
expectedReq float64
|
expectedReq float32
|
||||||
expectedTests []string
|
expectedTests []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
|
@ -75,8 +77,8 @@ func TestParseSpamStatus(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
result := &SpamAssassinResult{
|
result := &api.SpamAssassinResult{
|
||||||
TestDetails: make(map[string]SpamTestDetail),
|
TestDetails: make(map[string]api.SpamTestDetail),
|
||||||
}
|
}
|
||||||
analyzer.parseSpamStatus(tt.header, result)
|
analyzer.parseSpamStatus(tt.header, result)
|
||||||
|
|
||||||
|
|
@ -89,8 +91,12 @@ func TestParseSpamStatus(t *testing.T) {
|
||||||
if result.RequiredScore != tt.expectedReq {
|
if result.RequiredScore != tt.expectedReq {
|
||||||
t.Errorf("RequiredScore = %v, want %v", result.RequiredScore, tt.expectedReq)
|
t.Errorf("RequiredScore = %v, want %v", result.RequiredScore, tt.expectedReq)
|
||||||
}
|
}
|
||||||
if len(tt.expectedTests) > 0 && !stringSliceEqual(result.Tests, tt.expectedTests) {
|
if len(tt.expectedTests) > 0 {
|
||||||
t.Errorf("Tests = %v, want %v", result.Tests, tt.expectedTests)
|
if result.Tests == nil {
|
||||||
|
t.Errorf("Tests = nil, want %v", tt.expectedTests)
|
||||||
|
} else if !stringSliceEqual(*result.Tests, tt.expectedTests) {
|
||||||
|
t.Errorf("Tests = %v, want %v", *result.Tests, tt.expectedTests)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -109,27 +115,27 @@ func TestParseSpamReport(t *testing.T) {
|
||||||
`
|
`
|
||||||
|
|
||||||
analyzer := NewSpamAssassinAnalyzer()
|
analyzer := NewSpamAssassinAnalyzer()
|
||||||
result := &SpamAssassinResult{
|
result := &api.SpamAssassinResult{
|
||||||
TestDetails: make(map[string]SpamTestDetail),
|
TestDetails: make(map[string]api.SpamTestDetail),
|
||||||
}
|
}
|
||||||
|
|
||||||
analyzer.parseSpamReport(report, result)
|
analyzer.parseSpamReport(report, result)
|
||||||
|
|
||||||
expectedTests := map[string]SpamTestDetail{
|
expectedTests := map[string]api.SpamTestDetail{
|
||||||
"BAYES_99": {
|
"BAYES_99": {
|
||||||
Name: "BAYES_99",
|
Name: "BAYES_99",
|
||||||
Score: 5.0,
|
Score: 5.0,
|
||||||
Description: "Bayes spam probability is 99 to 100%",
|
Description: api.PtrTo("Bayes spam probability is 99 to 100%"),
|
||||||
},
|
},
|
||||||
"SPOOFED_SENDER": {
|
"SPOOFED_SENDER": {
|
||||||
Name: "SPOOFED_SENDER",
|
Name: "SPOOFED_SENDER",
|
||||||
Score: 3.5,
|
Score: 3.5,
|
||||||
Description: "From address doesn't match envelope sender",
|
Description: api.PtrTo("From address doesn't match envelope sender"),
|
||||||
},
|
},
|
||||||
"ALL_TRUSTED": {
|
"ALL_TRUSTED": {
|
||||||
Name: "ALL_TRUSTED",
|
Name: "ALL_TRUSTED",
|
||||||
Score: -1.0,
|
Score: -1.0,
|
||||||
Description: "All mail servers are trusted",
|
Description: api.PtrTo("All mail servers are trusted"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,8 +148,8 @@ func TestParseSpamReport(t *testing.T) {
|
||||||
if detail.Score != expected.Score {
|
if detail.Score != expected.Score {
|
||||||
t.Errorf("Test %s score = %v, want %v", testName, detail.Score, expected.Score)
|
t.Errorf("Test %s score = %v, want %v", testName, detail.Score, expected.Score)
|
||||||
}
|
}
|
||||||
if detail.Description != expected.Description {
|
if *detail.Description != *expected.Description {
|
||||||
t.Errorf("Test %s description = %q, want %q", testName, detail.Description, expected.Description)
|
t.Errorf("Test %s description = %q, want %q", testName, *detail.Description, *expected.Description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +157,7 @@ func TestParseSpamReport(t *testing.T) {
|
||||||
func TestGetSpamAssassinScore(t *testing.T) {
|
func TestGetSpamAssassinScore(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
result *SpamAssassinResult
|
result *api.SpamAssassinResult
|
||||||
expectedScore int
|
expectedScore int
|
||||||
minScore int
|
minScore int
|
||||||
maxScore int
|
maxScore int
|
||||||
|
|
@ -159,11 +165,11 @@ func TestGetSpamAssassinScore(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "Nil result",
|
name: "Nil result",
|
||||||
result: nil,
|
result: nil,
|
||||||
expectedScore: 0,
|
expectedScore: 100,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Excellent score (negative)",
|
name: "Excellent score (negative)",
|
||||||
result: &SpamAssassinResult{
|
result: &api.SpamAssassinResult{
|
||||||
Score: -2.5,
|
Score: -2.5,
|
||||||
RequiredScore: 5.0,
|
RequiredScore: 5.0,
|
||||||
},
|
},
|
||||||
|
|
@ -171,38 +177,43 @@ func TestGetSpamAssassinScore(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Good score (below threshold)",
|
name: "Good score (below threshold)",
|
||||||
result: &SpamAssassinResult{
|
result: &api.SpamAssassinResult{
|
||||||
Score: 2.0,
|
Score: 2.0,
|
||||||
RequiredScore: 5.0,
|
RequiredScore: 5.0,
|
||||||
},
|
},
|
||||||
minScore: 80,
|
expectedScore: 60, // 100 - round(2*100/5) = 100 - 40 = 60
|
||||||
maxScore: 100,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Borderline (just above threshold)",
|
name: "Score at threshold",
|
||||||
result: &SpamAssassinResult{
|
result: &api.SpamAssassinResult{
|
||||||
|
Score: 5.0,
|
||||||
|
RequiredScore: 5.0,
|
||||||
|
},
|
||||||
|
expectedScore: 0, // >= threshold = 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Above threshold (spam)",
|
||||||
|
result: &api.SpamAssassinResult{
|
||||||
Score: 6.0,
|
Score: 6.0,
|
||||||
RequiredScore: 5.0,
|
RequiredScore: 5.0,
|
||||||
},
|
},
|
||||||
minScore: 60,
|
expectedScore: 0, // >= threshold = 0
|
||||||
maxScore: 80,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "High spam score",
|
name: "High spam score",
|
||||||
result: &SpamAssassinResult{
|
result: &api.SpamAssassinResult{
|
||||||
Score: 12.0,
|
Score: 12.0,
|
||||||
RequiredScore: 5.0,
|
RequiredScore: 5.0,
|
||||||
},
|
},
|
||||||
minScore: 20,
|
expectedScore: 0, // >= threshold = 0
|
||||||
maxScore: 50,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Very high spam score",
|
name: "Very high spam score",
|
||||||
result: &SpamAssassinResult{
|
result: &api.SpamAssassinResult{
|
||||||
Score: 20.0,
|
Score: 20.0,
|
||||||
RequiredScore: 5.0,
|
RequiredScore: 5.0,
|
||||||
},
|
},
|
||||||
expectedScore: 0,
|
expectedScore: 0, // >= threshold = 0
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,7 +221,7 @@ func TestGetSpamAssassinScore(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
score := analyzer.GetSpamAssassinScore(tt.result)
|
score, _ := analyzer.CalculateSpamAssassinScore(tt.result)
|
||||||
|
|
||||||
if tt.minScore > 0 || tt.maxScore > 0 {
|
if tt.minScore > 0 || tt.maxScore > 0 {
|
||||||
if score < tt.minScore || score > tt.maxScore {
|
if score < tt.minScore || score > tt.maxScore {
|
||||||
|
|
@ -230,7 +241,7 @@ func TestAnalyzeSpamAssassin(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
expectedIsSpam bool
|
expectedIsSpam bool
|
||||||
expectedScore float64
|
expectedScore float32
|
||||||
expectedHasDetails bool
|
expectedHasDetails bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
|
@ -370,24 +381,26 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate score (should be -0.1)
|
// Validate score (should be -0.1)
|
||||||
expectedScore := -0.1
|
var expectedScore float32 = -0.1
|
||||||
if result.Score != expectedScore {
|
if result.Score != expectedScore {
|
||||||
t.Errorf("Score = %v, want %v", result.Score, expectedScore)
|
t.Errorf("Score = %v, want %v", result.Score, expectedScore)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate required score (should be 5.0)
|
// Validate required score (should be 5.0)
|
||||||
expectedRequired := 5.0
|
var expectedRequired float32 = 5.0
|
||||||
if result.RequiredScore != expectedRequired {
|
if result.RequiredScore != expectedRequired {
|
||||||
t.Errorf("RequiredScore = %v, want %v", result.RequiredScore, expectedRequired)
|
t.Errorf("RequiredScore = %v, want %v", result.RequiredScore, expectedRequired)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate version
|
// Validate version
|
||||||
if !strings.Contains(result.Version, "SpamAssassin") {
|
if result.Version == nil {
|
||||||
t.Errorf("Version should contain 'SpamAssassin', got: %s", result.Version)
|
t.Errorf("Version should contain 'SpamAssassin', got: nil")
|
||||||
|
} else if !strings.Contains(*result.Version, "SpamAssassin") {
|
||||||
|
t.Errorf("Version should contain 'SpamAssassin', got: %s", *result.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that tests were extracted
|
// Validate that tests were extracted
|
||||||
if len(result.Tests) == 0 {
|
if len(*result.Tests) == 0 {
|
||||||
t.Error("Expected tests to be extracted, got none")
|
t.Error("Expected tests to be extracted, got none")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -400,7 +413,7 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
|
||||||
"SPF_HELO_NONE": true,
|
"SPF_HELO_NONE": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testName := range result.Tests {
|
for _, testName := range *result.Tests {
|
||||||
if expectedTests[testName] {
|
if expectedTests[testName] {
|
||||||
t.Logf("Found expected test: %s", testName)
|
t.Logf("Found expected test: %s", testName)
|
||||||
}
|
}
|
||||||
|
|
@ -414,11 +427,11 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
|
||||||
// Log what we actually got for debugging
|
// Log what we actually got for debugging
|
||||||
t.Logf("Parsed %d test details from X-Spam-Report", len(result.TestDetails))
|
t.Logf("Parsed %d test details from X-Spam-Report", len(result.TestDetails))
|
||||||
for name, detail := range result.TestDetails {
|
for name, detail := range result.TestDetails {
|
||||||
t.Logf(" %s: score=%v, description=%s", name, detail.Score, detail.Description)
|
t.Logf(" %s: score=%v, description=%s", name, detail.Score, *detail.Description)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define expected test details with their scores
|
// Define expected test details with their scores
|
||||||
expectedTestDetails := map[string]float64{
|
expectedTestDetails := map[string]float32{
|
||||||
"SPF_PASS": -0.0,
|
"SPF_PASS": -0.0,
|
||||||
"SPF_HELO_NONE": 0.0,
|
"SPF_HELO_NONE": 0.0,
|
||||||
"DKIM_VALID": -0.1,
|
"DKIM_VALID": -0.1,
|
||||||
|
|
@ -439,13 +452,13 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
|
||||||
if detail.Score != expectedScore {
|
if detail.Score != expectedScore {
|
||||||
t.Errorf("Test %s score = %v, want %v", testName, detail.Score, expectedScore)
|
t.Errorf("Test %s score = %v, want %v", testName, detail.Score, expectedScore)
|
||||||
}
|
}
|
||||||
if detail.Description == "" {
|
if detail.Description == nil || *detail.Description == "" {
|
||||||
t.Errorf("Test %s should have a description", testName)
|
t.Errorf("Test %s should have a description", testName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test GetSpamAssassinScore
|
// Test GetSpamAssassinScore
|
||||||
score := analyzer.GetSpamAssassinScore(result)
|
score, _ := analyzer.CalculateSpamAssassinScore(result)
|
||||||
if score != 100 {
|
if score != 100 {
|
||||||
t.Errorf("GetSpamAssassinScore() = %v, want 100 (excellent score for negative spam score)", score)
|
t.Errorf("GetSpamAssassinScore() = %v, want 100 (excellent score for negative spam score)", score)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,25 @@
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
spamassassin: SpamAssassinResult;
|
spamassassin: SpamAssassinResult;
|
||||||
|
spamScore: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { spamassassin }: Props = $props();
|
let { spamassassin, spamScore }: Props = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header bg-warning bg-opacity-10">
|
<div class="card-header bg-white">
|
||||||
<h5 class="mb-0 fw-bold">
|
<h4 class="mb-0 d-flex justify-content-between align-items-center">
|
||||||
<i class="bi bi-bug me-2"></i>SpamAssassin Analysis
|
<span>
|
||||||
</h5>
|
<i class="bi bi-bug me-2"></i>
|
||||||
|
SpamAssassin Analysis
|
||||||
|
</span>
|
||||||
|
{#if spamScore !== undefined}
|
||||||
|
<span class="badge bg-secondary">
|
||||||
|
{spamScore}%
|
||||||
|
</span>
|
||||||
|
{/if}
|
||||||
|
</h4>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
|
|
@ -30,7 +39,34 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if spamassassin.tests && spamassassin.tests.length > 0}
|
{#if spamassassin.test_details && Object.keys(spamassassin.test_details).length > 0}
|
||||||
|
<div class="mb-3">
|
||||||
|
<div class="table-responsive mt-2">
|
||||||
|
<table class="table table-sm table-hover">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Test Name</th>
|
||||||
|
<th class="text-end">Score</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{#each Object.entries(spamassassin.test_details) as [testName, detail]}
|
||||||
|
<tr class={detail.score > 0 ? 'table-warning' : detail.score < 0 ? 'table-success' : ''}>
|
||||||
|
<td class="font-monospace">{testName}</td>
|
||||||
|
<td class="text-end">
|
||||||
|
<span class={detail.score > 0 ? 'text-danger fw-bold' : detail.score < 0 ? 'text-success fw-bold' : 'text-muted'}>
|
||||||
|
{detail.score > 0 ? '+' : ''}{detail.score.toFixed(1)}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td class="small">{detail.description || ''}</td>
|
||||||
|
</tr>
|
||||||
|
{/each}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{:else if spamassassin.tests && spamassassin.tests.length > 0}
|
||||||
<div class="mb-2">
|
<div class="mb-2">
|
||||||
<strong>Tests Triggered:</strong>
|
<strong>Tests Triggered:</strong>
|
||||||
<div class="mt-2">
|
<div class="mt-2">
|
||||||
|
|
@ -43,7 +79,7 @@
|
||||||
|
|
||||||
{#if spamassassin.report}
|
{#if spamassassin.report}
|
||||||
<details class="mt-3">
|
<details class="mt-3">
|
||||||
<summary class="cursor-pointer fw-bold">Full Report</summary>
|
<summary class="cursor-pointer fw-bold">Raw Report</summary>
|
||||||
<pre class="mt-2 small bg-light p-3 rounded">{spamassassin.report}</pre>
|
<pre class="mt-2 small bg-light p-3 rounded">{spamassassin.report}</pre>
|
||||||
</details>
|
</details>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
|
||||||
|
|
@ -197,7 +197,10 @@
|
||||||
{#if report.spamassassin}
|
{#if report.spamassassin}
|
||||||
<div class="row mb-4" id="spam">
|
<div class="row mb-4" id="spam">
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<SpamAssassinCard spamassassin={report.spamassassin} />
|
<SpamAssassinCard
|
||||||
|
spamassassin={report.spamassassin}
|
||||||
|
spamScore={report.summary?.spam_score}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue