Smail/server/hooks/framework/framework_test.go
P家单推人 430c9c6c10
feat(email): 新增 SPF/DKIM 邮件认证追踪 (#354)
新增 EmailAuthentication 统一封装 SPF/DKIM 验证结果与 Dangerous 结论。

重构邮件列表与 SMTP 收信流程,保证认证结果在插件、数据库和列表展示之间一致,并保留伪造邮件处理优先级。

扩展解析邮件 DTO 的认证字段,覆盖历史收件恢复、发送路径隔离、HookDTO 序列化及旧版插件兼容性测试。
2026-08-13 14:24:46 +08:00

67 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package framework
import (
"encoding/json"
"testing"
"github.com/Jinnrry/pmail/dto/parsemail"
)
func TestHookDTOIncludesEmailAuthentication(t *testing.T) {
dto := HookDTO{
Email: &parsemail.Email{
Subject: "认证结果测试",
Authentication: parsemail.NewEmailAuthentication(false, false),
},
}
body, err := json.Marshal(dto)
if err != nil {
t.Fatalf("序列化 HookDTO 失败:%v", err)
}
var decoded struct {
Email *struct {
Authentication *parsemail.EmailAuthentication
}
}
if err = json.Unmarshal(body, &decoded); err != nil {
t.Fatalf("反序列化 HookDTO 失败:%v", err)
}
if decoded.Email == nil {
t.Fatal("HookDTO 中缺少邮件")
}
authentication := decoded.Email.Authentication
if authentication == nil {
t.Fatal("HookDTO 中缺少邮件认证结果")
}
if authentication.SPFPassed || authentication.DKIMPassed || !authentication.Dangerous {
t.Fatalf("HookDTO 中的认证结果不正确:%+v", authentication)
}
}
func TestOlderPluginEmailShapeIgnoresAuthentication(t *testing.T) {
dto := HookDTO{
Email: &parsemail.Email{
Subject: "兼容性测试",
Authentication: parsemail.NewEmailAuthentication(true, false),
},
}
body, err := json.Marshal(dto)
if err != nil {
t.Fatalf("序列化 HookDTO 失败:%v", err)
}
var oldDTO struct {
Email *struct {
Subject string
}
}
if err = json.Unmarshal(body, &oldDTO); err != nil {
t.Fatalf("旧插件结构无法读取新增字段后的 HookDTO:%v", err)
}
if oldDTO.Email == nil || oldDTO.Email.Subject != "兼容性测试" {
t.Fatalf("旧插件结构读取到的邮件不正确:%+v", oldDTO.Email)
}
}