From 2b101994a6f4724500908226f9a55816062f1da6 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 8 Aug 2022 20:04:28 +0200 Subject: [PATCH] Fix init mail render logic (#20704) This bug affects tests which are sending emails (#20307). Some tests reinitialise the web routes (like `TestNodeinfo`) which messed up the mail templates. There is no reason why the templates should be loaded in the routes method. --- routers/web/web.go | 4 ---- services/mailer/mail.go | 6 ------ services/mailer/mail_test.go | 33 ++++++++++++++------------------- services/mailer/mailer.go | 3 +++ 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/routers/web/web.go b/routers/web/web.go index a9f43fb2c..34d3de6fd 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -22,7 +22,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web/routing" @@ -43,7 +42,6 @@ import ( context_service "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/lfs" - "code.gitea.io/gitea/services/mailer" _ "code.gitea.io/gitea/modules/session" // to registers all internal adapters @@ -152,8 +150,6 @@ func Routes() *web.Route { common = append(common, h) } - mailer.InitMailRender(templates.Mailer()) - if setting.Service.EnableCaptcha { // The captcha http.Handler should only fire on /captcha/* so we can just mount this on that url routes.Route("/captcha/*", "GET,HEAD", append(common, captcha.Captchaer(context.GetImageCaptcha()))...) diff --git a/services/mailer/mail.go b/services/mailer/mail.go index b8d79bd81..738a207ce 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -55,12 +55,6 @@ var ( subjectRemoveSpaces = regexp.MustCompile(`[\s]+`) ) -// InitMailRender initializes the mail renderer -func InitMailRender(subjectTpl *texttmpl.Template, bodyTpl *template.Template) { - subjectTemplates = subjectTpl - bodyTemplates = bodyTpl -} - // SendTestMail sends a test mail func SendTestMail(email string) error { if setting.MailService == nil { diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index 93837ba8c..604efe37b 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -67,9 +67,8 @@ func prepareMailerTest(t *testing.T) (doer *user_model.User, repo *repo_model.Re func TestComposeIssueCommentMessage(t *testing.T) { doer, _, issue, comment := prepareMailerTest(t) - stpl := texttmpl.Must(texttmpl.New("issue/comment").Parse(subjectTpl)) - btpl := template.Must(template.New("issue/comment").Parse(bodyTpl)) - InitMailRender(stpl, btpl) + subjectTemplates = texttmpl.Must(texttmpl.New("issue/comment").Parse(subjectTpl)) + bodyTemplates = template.Must(template.New("issue/comment").Parse(bodyTpl)) recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}} msgs, err := composeIssueCommentMessages(&mailCommentContext{ @@ -97,9 +96,8 @@ func TestComposeIssueCommentMessage(t *testing.T) { func TestComposeIssueMessage(t *testing.T) { doer, _, issue, _ := prepareMailerTest(t) - stpl := texttmpl.Must(texttmpl.New("issue/new").Parse(subjectTpl)) - btpl := template.Must(template.New("issue/new").Parse(bodyTpl)) - InitMailRender(stpl, btpl) + subjectTemplates = texttmpl.Must(texttmpl.New("issue/new").Parse(subjectTpl)) + bodyTemplates = template.Must(template.New("issue/new").Parse(bodyTpl)) recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}} msgs, err := composeIssueCommentMessages(&mailCommentContext{ @@ -128,17 +126,15 @@ func TestTemplateSelection(t *testing.T) { doer, repo, issue, comment := prepareMailerTest(t) recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}} - stpl := texttmpl.Must(texttmpl.New("issue/default").Parse("issue/default/subject")) - texttmpl.Must(stpl.New("issue/new").Parse("issue/new/subject")) - texttmpl.Must(stpl.New("pull/comment").Parse("pull/comment/subject")) - texttmpl.Must(stpl.New("issue/close").Parse("")) // Must default to fallback subject + subjectTemplates = texttmpl.Must(texttmpl.New("issue/default").Parse("issue/default/subject")) + texttmpl.Must(subjectTemplates.New("issue/new").Parse("issue/new/subject")) + texttmpl.Must(subjectTemplates.New("pull/comment").Parse("pull/comment/subject")) + texttmpl.Must(subjectTemplates.New("issue/close").Parse("")) // Must default to fallback subject - btpl := template.Must(template.New("issue/default").Parse("issue/default/body")) - template.Must(btpl.New("issue/new").Parse("issue/new/body")) - template.Must(btpl.New("pull/comment").Parse("pull/comment/body")) - template.Must(btpl.New("issue/close").Parse("issue/close/body")) - - InitMailRender(stpl, btpl) + bodyTemplates = template.Must(template.New("issue/default").Parse("issue/default/body")) + template.Must(bodyTemplates.New("issue/new").Parse("issue/new/body")) + template.Must(bodyTemplates.New("pull/comment").Parse("pull/comment/body")) + template.Must(bodyTemplates.New("issue/close").Parse("issue/close/body")) expect := func(t *testing.T, msg *Message, expSubject, expBody string) { subject := msg.ToMessage().GetHeader("Subject") @@ -187,9 +183,8 @@ func TestTemplateServices(t *testing.T) { expect := func(t *testing.T, issue *issues_model.Issue, comment *issues_model.Comment, doer *user_model.User, actionType models.ActionType, fromMention bool, tplSubject, tplBody, expSubject, expBody string, ) { - stpl := texttmpl.Must(texttmpl.New("issue/default").Parse(tplSubject)) - btpl := template.Must(template.New("issue/default").Parse(tplBody)) - InitMailRender(stpl, btpl) + subjectTemplates = texttmpl.Must(texttmpl.New("issue/default").Parse(tplSubject)) + bodyTemplates = template.Must(template.New("issue/default").Parse(tplBody)) recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}} msg := testComposeIssueCommentMessage(t, &mailCommentContext{ diff --git a/services/mailer/mailer.go b/services/mailer/mailer.go index c86c54c74..fdbb6e562 100644 --- a/services/mailer/mailer.go +++ b/services/mailer/mailer.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/templates" "github.com/jaytaylor/html2text" "gopkg.in/gomail.v2" @@ -379,6 +380,8 @@ func NewContext() { }, &Message{}) go graceful.GetManager().RunWithShutdownFns(mailQueue.Run) + + subjectTemplates, bodyTemplates = templates.Mailer() } // SendAsync send mail asynchronously