package services
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRenderNotificationEmail_ValidTemplates(t *testing.T) {
ms := &MailService{}
templates := []struct {
name string
data EmailTemplateData
wantTitle string
}{
{
name: "email_security_alert.html",
data: EmailTemplateData{
EventType: "security_waf",
Title: "WAF Block Detected",
Message: "Blocked suspicious request",
Timestamp: "2026-03-07T10:00:00Z",
SourceIP: "192.168.1.100",
},
wantTitle: "WAF Block Detected",
},
{
name: "email_ssl_event.html",
data: EmailTemplateData{
EventType: "cert",
Title: "Certificate Expiring",
Message: "Certificate will expire soon",
Timestamp: "2026-03-07T10:00:00Z",
Domain: "example.com",
ExpiryDate: "2026-04-07",
},
wantTitle: "Certificate Expiring",
},
{
name: "email_uptime_event.html",
data: EmailTemplateData{
EventType: "uptime",
Title: "Host Down",
Message: "Host is unreachable",
Timestamp: "2026-03-07T10:00:00Z",
HostName: "web-server-01",
StatusCode: "503",
},
wantTitle: "Host Down",
},
{
name: "email_system_event.html",
data: EmailTemplateData{
EventType: "proxy_host",
Title: "Proxy Host Updated",
Message: "Configuration has changed",
Timestamp: "2026-03-07T10:00:00Z",
},
wantTitle: "Proxy Host Updated",
},
}
for _, tc := range templates {
t.Run(tc.name, func(t *testing.T) {
result, err := ms.RenderNotificationEmail(tc.name, tc.data)
require.NoError(t, err)
assert.Contains(t, result, tc.wantTitle)
assert.Contains(t, result, "Charon")
assert.Contains(t, result, "Charon Reverse Proxy Manager")
assert.Contains(t, result, tc.data.Timestamp)
assert.Contains(t, result, tc.data.EventType)
assert.Contains(t, result, "")
})
}
}
func TestRenderNotificationEmail_XSSPrevention(t *testing.T) {
ms := &MailService{}
data := EmailTemplateData{
EventType: "security_waf",
Title: "",
Message: "
",
Timestamp: "2026-03-07T10:00:00Z",
SourceIP: "injected",
}
result, err := ms.RenderNotificationEmail("email_security_alert.html", data)
require.NoError(t, err)
assert.NotContains(t, result, "