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

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

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

64 lines
1.8 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 parsemail
import (
"testing"
"github.com/Jinnrry/pmail/models"
)
func TestNewEmailAuthentication(t *testing.T) {
tests := []struct {
name string
spfPassed bool
dkimPassed bool
dangerous bool
}{
{name: "SPF和DKIM均通过", spfPassed: true, dkimPassed: true},
{name: "仅SPF通过", spfPassed: true, dkimPassed: false},
{name: "仅DKIM通过", spfPassed: false, dkimPassed: true},
{name: "SPF和DKIM均未通过", spfPassed: false, dkimPassed: false, dangerous: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
authentication := NewEmailAuthentication(tt.spfPassed, tt.dkimPassed)
if authentication.SPFPassed != tt.spfPassed {
t.Fatalf("SPFPassed = %v,期望 %v", authentication.SPFPassed, tt.spfPassed)
}
if authentication.DKIMPassed != tt.dkimPassed {
t.Fatalf("DKIMPassed = %v,期望 %v", authentication.DKIMPassed, tt.dkimPassed)
}
if authentication.Dangerous != tt.dangerous {
t.Fatalf("Dangerous = %v,期望 %v", authentication.Dangerous, tt.dangerous)
}
})
}
}
func TestNewEmailFromModelRestoresAuthenticationForReceivedEmail(t *testing.T) {
email := NewEmailFromModel(models.Email{
Type: 0,
SPFCheck: 1,
DKIMCheck: 0,
})
if email.Authentication == nil {
t.Fatal("收件邮件缺少认证结果")
}
if !email.Authentication.SPFPassed || email.Authentication.DKIMPassed || email.Authentication.Dangerous {
t.Fatalf("认证结果不正确:%+v", email.Authentication)
}
}
func TestNewEmailFromModelLeavesAuthenticationNilForSentEmail(t *testing.T) {
email := NewEmailFromModel(models.Email{
Type: 1,
SPFCheck: 1,
DKIMCheck: 1,
})
if email.Authentication != nil {
t.Fatalf("发件邮件不应包含收信认证结果:%+v", email.Authentication)
}
}