test(services/mailer): Add option for mailer to override mail headers

Use MockVariableValue to avoid undesirable side effects between tests
modifying global variables. TestToMessage relies
on *setting.MailService being set, which will not be the case if run
individually with test-sqlite#TestToMessage and fail.
This commit is contained in:
Earl Warren 2024-06-09 08:04:11 +02:00
parent 1d4bff4f65
commit 21b94765ce
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -10,17 +10,16 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestGenerateMessageID(t *testing.T) { func TestGenerateMessageID(t *testing.T) {
mailService := setting.Mailer{ defer test.MockVariableValue(&setting.MailService, &setting.Mailer{
From: "test@gitea.com", From: "test@gitea.com",
} })()
defer test.MockVariableValue(&setting.Domain, "localhost")()
setting.MailService = &mailService
setting.Domain = "localhost"
date := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC) date := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC)
m := NewMessageFrom("", "display-name", "from-address", "subject", "body") m := NewMessageFrom("", "display-name", "from-address", "subject", "body")
@ -40,7 +39,7 @@ func TestGenerateMessageID(t *testing.T) {
} }
func TestGenerateMessageIDForRelease(t *testing.T) { func TestGenerateMessageIDForRelease(t *testing.T) {
setting.Domain = "localhost" defer test.MockVariableValue(&setting.Domain, "localhost")()
rel := repo_model.Release{ rel := repo_model.Release{
ID: 42, ID: 42,
@ -54,11 +53,10 @@ func TestGenerateMessageIDForRelease(t *testing.T) {
} }
func TestToMessage(t *testing.T) { func TestToMessage(t *testing.T) {
oldConf := *setting.MailService defer test.MockVariableValue(&setting.MailService, &setting.Mailer{
defer func() { From: "test@gitea.com",
setting.MailService = &oldConf })()
}() defer test.MockVariableValue(&setting.Domain, "localhost")()
setting.MailService.From = "test@gitea.com"
m1 := Message{ m1 := Message{
Info: "info", Info: "info",