2024-11-30 09:15:41 +08:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-08 15:34:23 +08:00
|
|
|
|
2024-11-30 09:15:41 +08:00
|
|
|
package sender
|
2021-12-08 15:34:23 +08:00
|
|
|
|
|
|
|
import (
|
2024-06-04 02:42:52 +08:00
|
|
|
"strings"
|
2021-12-08 15:34:23 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-12-15 13:39:34 +08:00
|
|
|
|
2021-12-08 15:34:23 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateMessageID(t *testing.T) {
|
2022-01-05 14:27:53 +08:00
|
|
|
mailService := setting.Mailer{
|
2021-12-11 03:43:28 +08:00
|
|
|
From: "test@gitea.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.MailService = &mailService
|
|
|
|
setting.Domain = "localhost"
|
2021-12-08 15:34:23 +08:00
|
|
|
|
2022-01-05 14:27:53 +08:00
|
|
|
date := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC)
|
2023-01-22 22:23:52 +08:00
|
|
|
m := NewMessageFrom("", "display-name", "from-address", "subject", "body")
|
2021-12-08 15:34:23 +08:00
|
|
|
m.Date = date
|
|
|
|
gm := m.ToMessage()
|
2024-12-05 06:33:43 +08:00
|
|
|
assert.Equal(t, "<autogen-946782245000-41e8fc54a8ad3a3f@localhost>", gm.GetGenHeader("Message-ID")[0])
|
2021-12-08 15:34:23 +08:00
|
|
|
|
2023-01-22 22:23:52 +08:00
|
|
|
m = NewMessageFrom("a@b.com", "display-name", "from-address", "subject", "body")
|
2021-12-08 15:34:23 +08:00
|
|
|
m.Date = date
|
|
|
|
gm = m.ToMessage()
|
2024-12-05 06:33:43 +08:00
|
|
|
assert.Equal(t, "<autogen-946782245000-cc88ce3cfe9bd04f@localhost>", gm.GetGenHeader("Message-ID")[0])
|
2021-12-08 15:34:23 +08:00
|
|
|
|
2023-01-22 22:23:52 +08:00
|
|
|
m = NewMessageFrom("a@b.com", "display-name", "from-address", "subject", "body")
|
2021-12-08 15:34:23 +08:00
|
|
|
m.SetHeader("Message-ID", "<msg-d@domain.com>")
|
|
|
|
gm = m.ToMessage()
|
2024-12-05 06:33:43 +08:00
|
|
|
assert.Equal(t, "<msg-d@domain.com>", gm.GetGenHeader("Message-ID")[0])
|
2021-12-08 15:34:23 +08:00
|
|
|
}
|
2024-06-04 02:42:52 +08:00
|
|
|
|
|
|
|
func TestToMessage(t *testing.T) {
|
2024-12-05 06:33:43 +08:00
|
|
|
oldConf := setting.MailService
|
2024-06-04 02:42:52 +08:00
|
|
|
defer func() {
|
2024-12-05 06:33:43 +08:00
|
|
|
setting.MailService = oldConf
|
2024-06-04 02:42:52 +08:00
|
|
|
}()
|
2024-12-05 06:33:43 +08:00
|
|
|
setting.MailService = &setting.Mailer{
|
|
|
|
From: "test@gitea.com",
|
|
|
|
}
|
2024-06-04 02:42:52 +08:00
|
|
|
|
|
|
|
m1 := Message{
|
|
|
|
Info: "info",
|
|
|
|
FromAddress: "test@gitea.com",
|
|
|
|
FromDisplayName: "Test Gitea",
|
|
|
|
To: "a@b.com",
|
|
|
|
Subject: "Issue X Closed",
|
|
|
|
Body: "Some Issue got closed by Y-Man",
|
|
|
|
}
|
|
|
|
|
2024-12-05 06:33:43 +08:00
|
|
|
assertHeaders := func(t *testing.T, expected, header map[string]string) {
|
|
|
|
for k, v := range expected {
|
|
|
|
assert.Equal(t, v, header[k], "Header %s should be %s but got %s", k, v, header[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:42:52 +08:00
|
|
|
buf := &strings.Builder{}
|
|
|
|
_, err := m1.ToMessage().WriteTo(buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
header, _ := extractMailHeaderAndContent(t, buf.String())
|
2024-12-05 06:33:43 +08:00
|
|
|
assertHeaders(t, map[string]string{
|
2024-06-04 02:42:52 +08:00
|
|
|
"Content-Type": "multipart/alternative;",
|
|
|
|
"Date": "Mon, 01 Jan 0001 00:00:00 +0000",
|
|
|
|
"From": "\"Test Gitea\" <test@gitea.com>",
|
|
|
|
"Message-ID": "<autogen--6795364578871-69c000786adc60dc@localhost>",
|
2024-12-05 06:33:43 +08:00
|
|
|
"MIME-Version": "1.0",
|
2024-06-04 02:42:52 +08:00
|
|
|
"Subject": "Issue X Closed",
|
2024-12-05 06:33:43 +08:00
|
|
|
"To": "<a@b.com>",
|
2024-06-04 02:42:52 +08:00
|
|
|
"X-Auto-Response-Suppress": "All",
|
|
|
|
}, header)
|
|
|
|
|
|
|
|
setting.MailService.OverrideHeader = map[string][]string{
|
|
|
|
"Message-ID": {""}, // delete message id
|
|
|
|
"Auto-Submitted": {"auto-generated"}, // suppress auto replay
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = &strings.Builder{}
|
|
|
|
_, err = m1.ToMessage().WriteTo(buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
header, _ = extractMailHeaderAndContent(t, buf.String())
|
2024-12-05 06:33:43 +08:00
|
|
|
assertHeaders(t, map[string]string{
|
2024-06-04 02:42:52 +08:00
|
|
|
"Content-Type": "multipart/alternative;",
|
|
|
|
"Date": "Mon, 01 Jan 0001 00:00:00 +0000",
|
|
|
|
"From": "\"Test Gitea\" <test@gitea.com>",
|
|
|
|
"Message-ID": "",
|
2024-12-05 06:33:43 +08:00
|
|
|
"MIME-Version": "1.0",
|
2024-06-04 02:42:52 +08:00
|
|
|
"Subject": "Issue X Closed",
|
2024-12-05 06:33:43 +08:00
|
|
|
"To": "<a@b.com>",
|
2024-06-04 02:42:52 +08:00
|
|
|
"X-Auto-Response-Suppress": "All",
|
|
|
|
"Auto-Submitted": "auto-generated",
|
|
|
|
}, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractMailHeaderAndContent(t *testing.T, mail string) (map[string]string, string) {
|
|
|
|
header := make(map[string]string)
|
|
|
|
|
|
|
|
parts := strings.SplitN(mail, "boundary=", 2)
|
|
|
|
if !assert.Len(t, parts, 2) {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
content := strings.TrimSpace("boundary=" + parts[1])
|
|
|
|
|
|
|
|
hParts := strings.Split(parts[0], "\n")
|
|
|
|
|
|
|
|
for _, hPart := range hParts {
|
|
|
|
parts := strings.SplitN(hPart, ":", 2)
|
|
|
|
hk := strings.TrimSpace(parts[0])
|
|
|
|
if hk != "" {
|
|
|
|
header[hk] = strings.TrimSpace(parts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return header, content
|
|
|
|
}
|