veza/veza-backend-api/internal/services/moderation_service_test.go
senke e6f1d7f18a feat(v0.11.2): F411-F420 moderation service with queue, spam, fingerprints, strikes
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 17:45:34 +01:00

139 lines
2.9 KiB
Go

package services
import (
"testing"
)
func TestCheckExcessiveLinks(t *testing.T) {
svc := NewModerationService(nil, nil)
tests := []struct {
name string
content string
maxLinks int
expected bool
}{
{
name: "no links",
content: "just some text without links",
maxLinks: 5,
expected: false,
},
{
name: "under limit",
content: "check https://example.com and https://test.com",
maxLinks: 5,
expected: false,
},
{
name: "over limit",
content: "https://a.com https://b.com https://c.com https://d.com https://e.com https://f.com",
maxLinks: 5,
expected: true,
},
{
name: "invalid urls not counted",
content: "not-a-url ftp://something https://real.com",
maxLinks: 5,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := map[string]interface{}{"max_links": float64(tt.maxLinks)}
detected, _ := svc.checkExcessiveLinks(tt.content, config)
if detected != tt.expected {
t.Errorf("expected detected=%v, got %v", tt.expected, detected)
}
})
}
}
func TestModerationActionValidation(t *testing.T) {
validActions := map[string]bool{
"approve": true, "reject": true, "ban_temp": true,
"ban_perm": true, "warn": true, "dismiss": true,
}
tests := []struct {
action string
valid bool
}{
{"approve", true},
{"reject", true},
{"ban_temp", true},
{"ban_perm", true},
{"warn", true},
{"dismiss", true},
{"invalid", false},
{"", false},
{"delete", false},
}
for _, tt := range tests {
t.Run(tt.action, func(t *testing.T) {
if validActions[tt.action] != tt.valid {
t.Errorf("action %q: expected valid=%v", tt.action, tt.valid)
}
})
}
}
func TestEnhancedReportCategoryValidation(t *testing.T) {
validCategories := map[string]bool{"spam": true, "offensive": true, "copyright": true, "fake": true, "other": true}
tests := []struct {
category string
valid bool
}{
{"spam", true},
{"offensive", true},
{"copyright", true},
{"fake", true},
{"other", true},
{"invalid", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.category, func(t *testing.T) {
if validCategories[tt.category] != tt.valid {
t.Errorf("category %q: expected valid=%v", tt.category, tt.valid)
}
})
}
}
func TestFingerprintStatusValidation(t *testing.T) {
validStatuses := map[string]bool{"clean": true, "matched": true}
tests := []struct {
status string
valid bool
}{
{"clean", true},
{"matched", true},
{"pending", false},
{"error", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.status, func(t *testing.T) {
if validStatuses[tt.status] != tt.valid {
t.Errorf("status %q: expected valid=%v", tt.status, tt.valid)
}
})
}
}
func TestNewModerationServiceNilLogger(t *testing.T) {
svc := NewModerationService(nil, nil)
if svc == nil {
t.Fatal("expected non-nil service")
}
if svc.logger == nil {
t.Fatal("expected non-nil logger (nop)")
}
}