Smail/backend/dto/parsemail/email_authentication_test.go
smail f60d3b0880
Some checks are pending
Security Checks / security (push) Waiting to run
整理前后端与测试构建目录并完善配置和安全引导
统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
2026-09-05 19:19:52 +08:00

70 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"
"git.pcux.cn/xuans/Smail/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, err := NewEmailFromModel(models.Email{
Type: 0,
SPFCheck: 1,
DKIMCheck: 0,
})
if err != nil {
t.Fatal(err)
}
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, err := NewEmailFromModel(models.Email{
Type: 1,
SPFCheck: 1,
DKIMCheck: 1,
})
if err != nil {
t.Fatal(err)
}
if email.Authentication != nil {
t.Fatalf("发件邮件不应包含收信认证结果:%+v", email.Authentication)
}
}