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.
This commit is contained in:
parent
ccf03e19c2
commit
2b101994a6
4 changed files with 17 additions and 29 deletions
|
@ -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()))...)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue