Skip to content

Commit

Permalink
Fix init mail render logic (#20704)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KN4CK3R authored Aug 8, 2022
1 parent ccf03e1 commit 2b10199
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
4 changes: 0 additions & 4 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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()))...)
Expand Down
6 changes: 0 additions & 6 deletions services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 14 additions & 19 deletions services/mailer/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 3 additions & 0 deletions services/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -379,6 +380,8 @@ func NewContext() {
}, &Message{})

go graceful.GetManager().RunWithShutdownFns(mailQueue.Run)

subjectTemplates, bodyTemplates = templates.Mailer()
}

// SendAsync send mail asynchronously
Expand Down

0 comments on commit 2b10199

Please sign in to comment.