You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package mailer
  5. import (
  6. "bytes"
  7. "html/template"
  8. "testing"
  9. texttmpl "text/template"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const subjectTpl = `
  15. {{.SubjectPrefix}}[{{.Repo}}] @{{.Doer.Name}} #{{.Issue.Index}} - {{.Issue.Title}}
  16. `
  17. const bodyTpl = `
  18. <!DOCTYPE html>
  19. <html>
  20. <head>
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  22. <title>{{.Subject}}</title>
  23. </head>
  24. <body>
  25. <p>{{.Body}}</p>
  26. <p>
  27. ---
  28. <br>
  29. <a href="{{.Link}}">View it on Gitea</a>.
  30. </p>
  31. </body>
  32. </html>
  33. `
  34. func TestComposeIssueCommentMessage(t *testing.T) {
  35. assert.NoError(t, models.PrepareTestDatabase())
  36. var mailService = setting.Mailer{
  37. From: "test@gitea.com",
  38. }
  39. setting.MailService = &mailService
  40. setting.Domain = "localhost"
  41. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  42. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
  43. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  44. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
  45. stpl := texttmpl.Must(texttmpl.New("issue/comment").Parse(subjectTpl))
  46. btpl := template.Must(template.New("issue/comment").Parse(bodyTpl))
  47. InitMailRender(stpl, btpl)
  48. tos := []string{"test@gitea.com", "test2@gitea.com"}
  49. msg := composeIssueCommentMessage(issue, doer, models.ActionCommentIssue, false, "test body", comment, tos, "issue comment")
  50. subject := msg.GetHeader("Subject")
  51. inreplyTo := msg.GetHeader("In-Reply-To")
  52. references := msg.GetHeader("References")
  53. assert.Equal(t, "Re: ", subject[0][:4], "Comment reply subject should contain Re:")
  54. assert.Equal(t, "Re: [user2/repo1] @user2 #1 - issue1", subject[0])
  55. assert.Equal(t, inreplyTo[0], "<user2/repo1/issues/1@localhost>", "In-Reply-To header doesn't match")
  56. assert.Equal(t, references[0], "<user2/repo1/issues/1@localhost>", "References header doesn't match")
  57. }
  58. func TestComposeIssueMessage(t *testing.T) {
  59. assert.NoError(t, models.PrepareTestDatabase())
  60. var mailService = setting.Mailer{
  61. From: "test@gitea.com",
  62. }
  63. setting.MailService = &mailService
  64. setting.Domain = "localhost"
  65. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  66. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
  67. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  68. stpl := texttmpl.Must(texttmpl.New("issue/new").Parse(subjectTpl))
  69. btpl := template.Must(template.New("issue/new").Parse(bodyTpl))
  70. InitMailRender(stpl, btpl)
  71. tos := []string{"test@gitea.com", "test2@gitea.com"}
  72. msg := composeIssueCommentMessage(issue, doer, models.ActionCreateIssue, false, "test body", nil, tos, "issue create")
  73. subject := msg.GetHeader("Subject")
  74. messageID := msg.GetHeader("Message-ID")
  75. assert.Equal(t, "[user2/repo1] @user2 #1 - issue1", subject[0])
  76. assert.Nil(t, msg.GetHeader("In-Reply-To"))
  77. assert.Nil(t, msg.GetHeader("References"))
  78. assert.Equal(t, messageID[0], "<user2/repo1/issues/1@localhost>", "Message-ID header doesn't match")
  79. }
  80. func TestTemplateSelection(t *testing.T) {
  81. assert.NoError(t, models.PrepareTestDatabase())
  82. var mailService = setting.Mailer{
  83. From: "test@gitea.com",
  84. }
  85. setting.MailService = &mailService
  86. setting.Domain = "localhost"
  87. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  88. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
  89. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  90. tos := []string{"test@gitea.com"}
  91. stpl := texttmpl.Must(texttmpl.New("issue/default").Parse("issue/default/subject"))
  92. texttmpl.Must(stpl.New("issue/new").Parse("issue/new/subject"))
  93. texttmpl.Must(stpl.New("pull/comment").Parse("pull/comment/subject"))
  94. texttmpl.Must(stpl.New("issue/close").Parse("")) // Must default to fallback subject
  95. btpl := template.Must(template.New("issue/default").Parse("issue/default/body"))
  96. template.Must(btpl.New("issue/new").Parse("issue/new/body"))
  97. template.Must(btpl.New("pull/comment").Parse("pull/comment/body"))
  98. template.Must(btpl.New("issue/close").Parse("issue/close/body"))
  99. InitMailRender(stpl, btpl)
  100. expect := func(t *testing.T, msg *Message, expSubject, expBody string) {
  101. subject := msg.GetHeader("Subject")
  102. msgbuf := new(bytes.Buffer)
  103. _, _ = msg.WriteTo(msgbuf)
  104. wholemsg := msgbuf.String()
  105. assert.Equal(t, []string{expSubject}, subject)
  106. assert.Contains(t, wholemsg, expBody)
  107. }
  108. msg := composeIssueCommentMessage(issue, doer, models.ActionCreateIssue, false, "test body", nil, tos, "TestTemplateSelection")
  109. expect(t, msg, "issue/new/subject", "issue/new/body")
  110. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
  111. msg = composeIssueCommentMessage(issue, doer, models.ActionCommentIssue, false, "test body", comment, tos, "TestTemplateSelection")
  112. expect(t, msg, "issue/default/subject", "issue/default/body")
  113. pull := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue)
  114. comment = models.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment)
  115. msg = composeIssueCommentMessage(pull, doer, models.ActionCommentIssue, false, "test body", comment, tos, "TestTemplateSelection")
  116. expect(t, msg, "pull/comment/subject", "pull/comment/body")
  117. msg = composeIssueCommentMessage(issue, doer, models.ActionCloseIssue, false, "test body", nil, tos, "TestTemplateSelection")
  118. expect(t, msg, "[user2/repo1] issue1 (#1)", "issue/close/body")
  119. }
  120. func TestTemplateServices(t *testing.T) {
  121. assert.NoError(t, models.PrepareTestDatabase())
  122. var mailService = setting.Mailer{
  123. From: "test@gitea.com",
  124. }
  125. setting.MailService = &mailService
  126. setting.Domain = "localhost"
  127. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  128. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
  129. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  130. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
  131. assert.NoError(t, issue.LoadRepo())
  132. expect := func(t *testing.T, issue *models.Issue, comment *models.Comment, doer *models.User,
  133. actionType models.ActionType, fromMention bool, tplSubject, tplBody, expSubject, expBody string) {
  134. stpl := texttmpl.Must(texttmpl.New("issue/default").Parse(tplSubject))
  135. btpl := template.Must(template.New("issue/default").Parse(tplBody))
  136. InitMailRender(stpl, btpl)
  137. tos := []string{"test@gitea.com"}
  138. msg := composeIssueCommentMessage(issue, doer, actionType, fromMention, "test body", comment, tos, "TestTemplateServices")
  139. subject := msg.GetHeader("Subject")
  140. msgbuf := new(bytes.Buffer)
  141. _, _ = msg.WriteTo(msgbuf)
  142. wholemsg := msgbuf.String()
  143. assert.Equal(t, []string{expSubject}, subject)
  144. assert.Contains(t, wholemsg, "\r\n"+expBody+"\r\n")
  145. }
  146. expect(t, issue, comment, doer, models.ActionCommentIssue, false,
  147. "{{.SubjectPrefix}}[{{.Repo}}]: @{{.Doer.Name}} commented on #{{.Issue.Index}} - {{.Issue.Title}}",
  148. "//{{.ActionType}},{{.ActionName}},{{if .IsMention}}norender{{end}}//",
  149. "Re: [user2/repo1]: @user2 commented on #1 - issue1",
  150. "//issue,comment,//")
  151. expect(t, issue, comment, doer, models.ActionCommentIssue, true,
  152. "{{if .IsMention}}must render{{end}}",
  153. "//subject is: {{.Subject}}//",
  154. "must render",
  155. "//subject is: must render//")
  156. expect(t, issue, comment, doer, models.ActionCommentIssue, true,
  157. "{{.FallbackSubject}}",
  158. "//{{.SubjectPrefix}}//",
  159. "Re: [user2/repo1] issue1 (#1)",
  160. "//Re: //")
  161. }