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.

mail_test.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/setting"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. const subjectTpl = `
  18. {{.SubjectPrefix}}[{{.Repo}}] @{{.Doer.Name}} #{{.Issue.Index}} - {{.Issue.Title}}
  19. `
  20. const bodyTpl = `
  21. <!DOCTYPE html>
  22. <html>
  23. <head>
  24. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  25. <title>{{.Subject}}</title>
  26. </head>
  27. <body>
  28. <p>{{.Body}}</p>
  29. <p>
  30. ---
  31. <br>
  32. <a href="{{.Link}}">View it on Gitea</a>.
  33. </p>
  34. </body>
  35. </html>
  36. `
  37. func prepareMailerTest(t *testing.T) (doer *user_model.User, repo *repo_model.Repository, issue *models.Issue, comment *models.Comment) {
  38. assert.NoError(t, unittest.PrepareTestDatabase())
  39. var mailService = setting.Mailer{
  40. From: "test@gitea.com",
  41. }
  42. setting.MailService = &mailService
  43. setting.Domain = "localhost"
  44. doer = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  45. repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1, Owner: doer}).(*repo_model.Repository)
  46. issue = unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  47. assert.NoError(t, issue.LoadRepo())
  48. comment = unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
  49. return
  50. }
  51. func TestComposeIssueCommentMessage(t *testing.T) {
  52. doer, _, issue, comment := prepareMailerTest(t)
  53. stpl := texttmpl.Must(texttmpl.New("issue/comment").Parse(subjectTpl))
  54. btpl := template.Must(template.New("issue/comment").Parse(bodyTpl))
  55. InitMailRender(stpl, btpl)
  56. recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}}
  57. msgs, err := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue,
  58. Content: "test body", Comment: comment}, "en-US", recipients, false, "issue comment")
  59. assert.NoError(t, err)
  60. assert.Len(t, msgs, 2)
  61. gomailMsg := msgs[0].ToMessage()
  62. mailto := gomailMsg.GetHeader("To")
  63. subject := gomailMsg.GetHeader("Subject")
  64. messageID := gomailMsg.GetHeader("Message-ID")
  65. inReplyTo := gomailMsg.GetHeader("In-Reply-To")
  66. references := gomailMsg.GetHeader("References")
  67. assert.Len(t, mailto, 1, "exactly one recipient is expected in the To field")
  68. assert.Equal(t, "Re: ", subject[0][:4], "Comment reply subject should contain Re:")
  69. assert.Equal(t, "Re: [user2/repo1] @user2 #1 - issue1", subject[0])
  70. assert.Equal(t, "<user2/repo1/issues/1@localhost>", inReplyTo[0], "In-Reply-To header doesn't match")
  71. assert.Equal(t, "<user2/repo1/issues/1@localhost>", references[0], "References header doesn't match")
  72. assert.Equal(t, "<user2/repo1/issues/1/comment/2@localhost>", messageID[0], "Message-ID header doesn't match")
  73. }
  74. func TestComposeIssueMessage(t *testing.T) {
  75. doer, _, issue, _ := prepareMailerTest(t)
  76. stpl := texttmpl.Must(texttmpl.New("issue/new").Parse(subjectTpl))
  77. btpl := template.Must(template.New("issue/new").Parse(bodyTpl))
  78. InitMailRender(stpl, btpl)
  79. recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}}
  80. msgs, err := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue,
  81. Content: "test body"}, "en-US", recipients, false, "issue create")
  82. assert.NoError(t, err)
  83. assert.Len(t, msgs, 2)
  84. gomailMsg := msgs[0].ToMessage()
  85. mailto := gomailMsg.GetHeader("To")
  86. subject := gomailMsg.GetHeader("Subject")
  87. messageID := gomailMsg.GetHeader("Message-ID")
  88. inReplyTo := gomailMsg.GetHeader("In-Reply-To")
  89. references := gomailMsg.GetHeader("References")
  90. assert.Len(t, mailto, 1, "exactly one recipient is expected in the To field")
  91. assert.Equal(t, "[user2/repo1] @user2 #1 - issue1", subject[0])
  92. assert.Equal(t, "<user2/repo1/issues/1@localhost>", inReplyTo[0], "In-Reply-To header doesn't match")
  93. assert.Equal(t, "<user2/repo1/issues/1@localhost>", references[0], "References header doesn't match")
  94. assert.Equal(t, "<user2/repo1/issues/1@localhost>", messageID[0], "Message-ID header doesn't match")
  95. }
  96. func TestTemplateSelection(t *testing.T) {
  97. doer, repo, issue, comment := prepareMailerTest(t)
  98. recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}}
  99. stpl := texttmpl.Must(texttmpl.New("issue/default").Parse("issue/default/subject"))
  100. texttmpl.Must(stpl.New("issue/new").Parse("issue/new/subject"))
  101. texttmpl.Must(stpl.New("pull/comment").Parse("pull/comment/subject"))
  102. texttmpl.Must(stpl.New("issue/close").Parse("")) // Must default to fallback subject
  103. btpl := template.Must(template.New("issue/default").Parse("issue/default/body"))
  104. template.Must(btpl.New("issue/new").Parse("issue/new/body"))
  105. template.Must(btpl.New("pull/comment").Parse("pull/comment/body"))
  106. template.Must(btpl.New("issue/close").Parse("issue/close/body"))
  107. InitMailRender(stpl, btpl)
  108. expect := func(t *testing.T, msg *Message, expSubject, expBody string) {
  109. subject := msg.ToMessage().GetHeader("Subject")
  110. msgbuf := new(bytes.Buffer)
  111. _, _ = msg.ToMessage().WriteTo(msgbuf)
  112. wholemsg := msgbuf.String()
  113. assert.Equal(t, []string{expSubject}, subject)
  114. assert.Contains(t, wholemsg, expBody)
  115. }
  116. msg := testComposeIssueCommentMessage(t, &mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue,
  117. Content: "test body"}, recipients, false, "TestTemplateSelection")
  118. expect(t, msg, "issue/new/subject", "issue/new/body")
  119. msg = testComposeIssueCommentMessage(t, &mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue,
  120. Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection")
  121. expect(t, msg, "issue/default/subject", "issue/default/body")
  122. pull := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue)
  123. comment = unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment)
  124. msg = testComposeIssueCommentMessage(t, &mailCommentContext{Issue: pull, Doer: doer, ActionType: models.ActionCommentPull,
  125. Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection")
  126. expect(t, msg, "pull/comment/subject", "pull/comment/body")
  127. msg = testComposeIssueCommentMessage(t, &mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCloseIssue,
  128. Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection")
  129. expect(t, msg, "Re: [user2/repo1] issue1 (#1)", "issue/close/body")
  130. }
  131. func TestTemplateServices(t *testing.T) {
  132. doer, _, issue, comment := prepareMailerTest(t)
  133. assert.NoError(t, issue.LoadRepo())
  134. expect := func(t *testing.T, issue *models.Issue, comment *models.Comment, doer *user_model.User,
  135. actionType models.ActionType, fromMention bool, tplSubject, tplBody, expSubject, expBody string) {
  136. stpl := texttmpl.Must(texttmpl.New("issue/default").Parse(tplSubject))
  137. btpl := template.Must(template.New("issue/default").Parse(tplBody))
  138. InitMailRender(stpl, btpl)
  139. recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}}
  140. msg := testComposeIssueCommentMessage(t, &mailCommentContext{Issue: issue, Doer: doer, ActionType: actionType,
  141. Content: "test body", Comment: comment}, recipients, fromMention, "TestTemplateServices")
  142. subject := msg.ToMessage().GetHeader("Subject")
  143. msgbuf := new(bytes.Buffer)
  144. _, _ = msg.ToMessage().WriteTo(msgbuf)
  145. wholemsg := msgbuf.String()
  146. assert.Equal(t, []string{expSubject}, subject)
  147. assert.Contains(t, wholemsg, "\r\n"+expBody+"\r\n")
  148. }
  149. expect(t, issue, comment, doer, models.ActionCommentIssue, false,
  150. "{{.SubjectPrefix}}[{{.Repo}}]: @{{.Doer.Name}} commented on #{{.Issue.Index}} - {{.Issue.Title}}",
  151. "//{{.ActionType}},{{.ActionName}},{{if .IsMention}}norender{{end}}//",
  152. "Re: [user2/repo1]: @user2 commented on #1 - issue1",
  153. "//issue,comment,//")
  154. expect(t, issue, comment, doer, models.ActionCommentIssue, true,
  155. "{{if .IsMention}}must render{{end}}",
  156. "//subject is: {{.Subject}}//",
  157. "must render",
  158. "//subject is: must render//")
  159. expect(t, issue, comment, doer, models.ActionCommentIssue, true,
  160. "{{.FallbackSubject}}",
  161. "//{{.SubjectPrefix}}//",
  162. "Re: [user2/repo1] issue1 (#1)",
  163. "//Re: //")
  164. }
  165. func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recipients []*user_model.User, fromMention bool, info string) *Message {
  166. msgs, err := composeIssueCommentMessages(ctx, "en-US", recipients, fromMention, info)
  167. assert.NoError(t, err)
  168. assert.Len(t, msgs, 1)
  169. return msgs[0]
  170. }
  171. func TestGenerateAdditionalHeaders(t *testing.T) {
  172. doer, _, issue, _ := prepareMailerTest(t)
  173. ctx := &mailCommentContext{Issue: issue, Doer: doer}
  174. recipient := &user_model.User{Name: "Test", Email: "test@gitea.com"}
  175. headers := generateAdditionalHeaders(ctx, "dummy-reason", recipient)
  176. expected := map[string]string{
  177. "List-ID": "user2/repo1 <repo1.user2.localhost>",
  178. "List-Archive": "<https://try.gitea.io/user2/repo1>",
  179. "X-Gitea-Reason": "dummy-reason",
  180. "X-Gitea-Sender": "< U<se>r Tw<o > ><",
  181. "X-Gitea-Recipient": "Test",
  182. "X-Gitea-Recipient-Address": "test@gitea.com",
  183. "X-Gitea-Repository": "repo1",
  184. "X-Gitea-Repository-Path": "user2/repo1",
  185. "X-Gitea-Repository-Link": "https://try.gitea.io/user2/repo1",
  186. "X-Gitea-Issue-ID": "1",
  187. "X-Gitea-Issue-Link": "https://try.gitea.io/user2/repo1/issues/1",
  188. }
  189. for key, value := range expected {
  190. if assert.Contains(t, headers, key) {
  191. assert.Equal(t, value, headers[key])
  192. }
  193. }
  194. }