精简初始化通知设置并默认使用Smail系统通知身份发信
This commit is contained in:
1 parent
229d150a08
commit
a9937c3e6a
16 files changed
+186
-48
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/mail"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SenderIdentity 根据系统通知账号确定发件人,不能将管理员邮箱当作 SMTP 发件人。
|
||||
func (c NotificationConfig) SenderIdentity() (string, string, error) {
|
||||
name := strings.TrimSpace(c.SenderName)
|
||||
email := strings.TrimSpace(c.SenderEmail)
|
||||
if !c.SMTPEnabled {
|
||||
return name, email, nil
|
||||
}
|
||||
username := strings.ToLower(strings.TrimSpace(c.SMTPUsername))
|
||||
if strings.HasPrefix(username, "notify:") {
|
||||
account := strings.TrimPrefix(username, "notify:")
|
||||
parsed, err := mail.ParseAddress(account)
|
||||
parts := strings.Split(account, "@")
|
||||
if err != nil || parsed.Address != account || parsed.Name != "" || len(parts) != 2 ||
|
||||
strings.ContainsAny(account, "\r\n<>()") {
|
||||
return "", "", errors.New("Smail 系统通知用户名必须使用 notify:账号@邮件域名 格式")
|
||||
}
|
||||
// Smail 保留该发件地址;不能使用登录账号、管理员邮箱或自定义名称替代。
|
||||
return "系统通知", "notification+system@" + parts[1], nil
|
||||
}
|
||||
if email == "" {
|
||||
parsed, err := mail.ParseAddress(username)
|
||||
if err != nil || parsed.Address != username || parsed.Name != "" {
|
||||
return "", "", errors.New("请填写 Smail 系统通知用户名;兼容 SMTP 账号必须是完整邮箱地址")
|
||||
}
|
||||
email = username
|
||||
}
|
||||
return name, email, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNotificationSenderIdentity(t *testing.T) {
|
||||
for _, item := range []struct {
|
||||
name, username, sender, wantName, wantEmail string
|
||||
}{
|
||||
{"system", "notify:scert@example.test", "", "系统通知", "notification+system@example.test"},
|
||||
{"stale_admin", "notify:scert@example.test", "admin@other.test", "系统通知", "notification+system@example.test"},
|
||||
{"normalized", " NOTIFY:SCERT@EXAMPLE.TEST ", "", "系统通知", "notification+system@example.test"},
|
||||
{"smtp_compatibility", "sender@example.test", "", "Scert", "sender@example.test"},
|
||||
{"explicit_legacy", "smtp-login", "sender@example.test", "Scert", "sender@example.test"},
|
||||
} {
|
||||
t.Run(item.name, func(t *testing.T) {
|
||||
cfg := NotificationConfig{SMTPEnabled: true, SMTPUsername: item.username, SenderName: "Scert", SenderEmail: item.sender}
|
||||
name, email, err := cfg.SenderIdentity()
|
||||
if err != nil || name != item.wantName || email != item.wantEmail {
|
||||
t.Fatalf("发件身份不匹配:name=%q email=%q err=%v", name, email, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationSenderRejectsInvalidSystemAccount(t *testing.T) {
|
||||
for _, username := range []string{"notify:", "notify:user", "notify:@example.test", "notify:a@b@example.test", "notify:User <a@example.test>", "notify:a@example.test\r\nBcc:other@example.test", "smtp-login", ""} {
|
||||
cfg := NotificationConfig{SMTPEnabled: true, SMTPUsername: username}
|
||||
if _, _, err := cfg.SenderIdentity(); err == nil {
|
||||
t.Fatal("无效账号不得回退到管理员邮箱")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationSenderPreservesHTTPIdentity(t *testing.T) {
|
||||
cfg := NotificationConfig{SMTPUsername: "notify:unused@example.test", SenderName: "兼容通知", SenderEmail: "sender@example.test"}
|
||||
name, email, err := cfg.SenderIdentity()
|
||||
if err != nil || name != cfg.SenderName || email != cfg.SenderEmail {
|
||||
t.Fatal("未启用 SMTP 时不得改写原有 HTTP 发件人")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user