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.

91 lines
3.0 KiB

  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. "html/template"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. const tmpl = `
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17. <title>{{.Subject}}</title>
  18. </head>
  19. <body>
  20. <p>{{.Body}}</p>
  21. <p>
  22. ---
  23. <br>
  24. <a href="{{.Link}}">View it on Gitea</a>.
  25. </p>
  26. </body>
  27. </html>
  28. `
  29. func TestComposeIssueCommentMessage(t *testing.T) {
  30. assert.NoError(t, models.PrepareTestDatabase())
  31. var mailService = setting.Mailer{
  32. From: "test@gitea.com",
  33. }
  34. setting.MailService = &mailService
  35. setting.Domain = "localhost"
  36. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  37. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
  38. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  39. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
  40. email := template.Must(template.New("issue/comment").Parse(tmpl))
  41. InitMailRender(email)
  42. tos := []string{"test@gitea.com", "test2@gitea.com"}
  43. msg := composeIssueCommentMessage(issue, doer, "test body", comment, mailIssueComment, tos, "issue comment")
  44. subject := msg.GetHeader("Subject")
  45. inreplyTo := msg.GetHeader("In-Reply-To")
  46. references := msg.GetHeader("References")
  47. assert.Equal(t, subject[0], "Re: "+mailSubject(issue), "Comment reply subject should contain Re:")
  48. assert.Equal(t, inreplyTo[0], "<user2/repo1/issues/1@localhost>", "In-Reply-To header doesn't match")
  49. assert.Equal(t, references[0], "<user2/repo1/issues/1@localhost>", "References header doesn't match")
  50. }
  51. func TestComposeIssueMessage(t *testing.T) {
  52. assert.NoError(t, models.PrepareTestDatabase())
  53. var mailService = setting.Mailer{
  54. From: "test@gitea.com",
  55. }
  56. setting.MailService = &mailService
  57. setting.Domain = "localhost"
  58. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  59. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
  60. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
  61. email := template.Must(template.New("issue/comment").Parse(tmpl))
  62. InitMailRender(email)
  63. tos := []string{"test@gitea.com", "test2@gitea.com"}
  64. msg := composeIssueCommentMessage(issue, doer, "test body", nil, mailIssueComment, tos, "issue create")
  65. subject := msg.GetHeader("Subject")
  66. messageID := msg.GetHeader("Message-ID")
  67. assert.Equal(t, subject[0], mailSubject(issue), "Subject not equal to issue.mailSubject()")
  68. assert.Nil(t, msg.GetHeader("In-Reply-To"))
  69. assert.Nil(t, msg.GetHeader("References"))
  70. assert.Equal(t, messageID[0], "<user2/repo1/issues/1@localhost>", "Message-ID header doesn't match")
  71. }