2019-07-17 15:02:42 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2019-09-24 01:02:49 -04:00
|
|
|
package mailer
|
2019-07-17 15:02:42 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-07 08:34:28 -05:00
|
|
|
"bytes"
|
2022-03-22 11:22:54 -04:00
|
|
|
"context"
|
2022-02-03 18:01:16 -05:00
|
|
|
"fmt"
|
2019-07-17 15:02:42 -04:00
|
|
|
"html/template"
|
2024-03-13 06:34:58 -04:00
|
|
|
"io"
|
|
|
|
"mime/quotedprintable"
|
2023-01-14 10:57:10 -05:00
|
|
|
"regexp"
|
2022-02-03 18:01:16 -05:00
|
|
|
"strings"
|
2019-07-17 15:02:42 -04:00
|
|
|
"testing"
|
2019-11-07 08:34:28 -05:00
|
|
|
texttmpl "text/template"
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-04-08 05:11:15 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-03-13 06:34:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2019-07-17 15:02:42 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-05-17 10:47:35 -04:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-02-06 10:44:30 -05:00
|
|
|
|
2019-07-17 15:02:42 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-17 15:02:42 -04:00
|
|
|
)
|
|
|
|
|
2019-11-07 08:34:28 -05:00
|
|
|
const subjectTpl = `
|
|
|
|
{{.SubjectPrefix}}[{{.Repo}}] @{{.Doer.Name}} #{{.Issue.Index}} - {{.Issue.Title}}
|
|
|
|
`
|
|
|
|
|
|
|
|
const bodyTpl = `
|
2019-07-17 15:02:42 -04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<p>{{.Body}}</p>
|
|
|
|
<p>
|
|
|
|
---
|
|
|
|
<br>
|
|
|
|
<a href="{{.Link}}">View it on Gitea</a>.
|
|
|
|
</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
func prepareMailerTest(t *testing.T) (doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
doer = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1, Owner: doer})
|
|
|
|
issue = unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1, Repo: repo, Poster: doer})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, issue.LoadRepo(db.DefaultContext))
|
2022-08-15 22:22:25 -04:00
|
|
|
comment = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2, Issue: issue})
|
2022-06-20 06:02:49 -04:00
|
|
|
return doer, repo, issue, comment
|
2021-05-22 02:47:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestComposeIssueCommentMessage(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2021-05-22 02:47:16 -04:00
|
|
|
doer, _, issue, comment := prepareMailerTest(t)
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2024-03-13 06:34:58 -04:00
|
|
|
markup.Init(&markup.ProcessorHelper{
|
|
|
|
IsUsernameMentionable: func(ctx context.Context, username string) bool {
|
|
|
|
return username == doer.Name
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-05-17 10:47:35 -04:00
|
|
|
defer test.MockVariableValue(&setting.IncomingEmail.Enabled, true)()
|
2023-01-14 10:57:10 -05:00
|
|
|
|
2022-08-08 14:04:28 -04:00
|
|
|
subjectTemplates = texttmpl.Must(texttmpl.New("issue/comment").Parse(subjectTpl))
|
|
|
|
bodyTemplates = template.Must(template.New("issue/comment").Parse(bodyTpl))
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}}
|
2022-01-20 12:46:10 -05:00
|
|
|
msgs, err := composeIssueCommentMessages(&mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2022-08-24 22:31:57 -04:00
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCommentIssue,
|
2024-03-13 06:34:58 -04:00
|
|
|
Content: fmt.Sprintf("test @%s %s#%d body", doer.Name, issue.Repo.FullName(), issue.Index),
|
|
|
|
Comment: comment,
|
2022-01-20 12:46:10 -05:00
|
|
|
}, "en-US", recipients, false, "issue comment")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2019-11-18 03:08:20 -05:00
|
|
|
assert.Len(t, msgs, 2)
|
2020-01-16 12:55:36 -05:00
|
|
|
gomailMsg := msgs[0].ToMessage()
|
2023-01-14 10:57:10 -05:00
|
|
|
replyTo := gomailMsg.GetHeader("Reply-To")[0]
|
|
|
|
subject := gomailMsg.GetHeader("Subject")[0]
|
|
|
|
|
|
|
|
assert.Len(t, gomailMsg.GetHeader("To"), 1, "exactly one recipient is expected in the To field")
|
|
|
|
tokenRegex := regexp.MustCompile(`\Aincoming\+(.+)@localhost\z`)
|
|
|
|
assert.Regexp(t, tokenRegex, replyTo)
|
|
|
|
token := tokenRegex.FindAllStringSubmatch(replyTo, 1)[0][1]
|
|
|
|
assert.Equal(t, "Re: ", subject[:4], "Comment reply subject should contain Re:")
|
|
|
|
assert.Equal(t, "Re: [user2/repo1] @user2 #1 - issue1", subject)
|
|
|
|
assert.Equal(t, "<user2/repo1/issues/1@localhost>", gomailMsg.GetHeader("In-Reply-To")[0], "In-Reply-To header doesn't match")
|
|
|
|
assert.ElementsMatch(t, []string{"<user2/repo1/issues/1@localhost>", "<reply-" + token + "@localhost>"}, gomailMsg.GetHeader("References"), "References header doesn't match")
|
|
|
|
assert.Equal(t, "<user2/repo1/issues/1/comment/2@localhost>", gomailMsg.GetHeader("Message-ID")[0], "Message-ID header doesn't match")
|
|
|
|
assert.Equal(t, "<mailto:"+replyTo+">", gomailMsg.GetHeader("List-Post")[0])
|
|
|
|
assert.Len(t, gomailMsg.GetHeader("List-Unsubscribe"), 2) // url + mailto
|
2024-03-13 06:34:58 -04:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
gomailMsg.WriteTo(&buf)
|
|
|
|
|
|
|
|
b, err := io.ReadAll(quotedprintable.NewReader(&buf))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-13 06:34:58 -04:00
|
|
|
|
|
|
|
// text/plain
|
|
|
|
assert.Contains(t, string(b), fmt.Sprintf(`( %s )`, doer.HTMLURL()))
|
|
|
|
assert.Contains(t, string(b), fmt.Sprintf(`( %s )`, issue.HTMLURL()))
|
|
|
|
|
|
|
|
// text/html
|
|
|
|
assert.Contains(t, string(b), fmt.Sprintf(`href="%s"`, doer.HTMLURL()))
|
|
|
|
assert.Contains(t, string(b), fmt.Sprintf(`href="%s"`, issue.HTMLURL()))
|
2019-07-17 15:02:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestComposeIssueMessage(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2021-05-22 02:47:16 -04:00
|
|
|
doer, _, issue, _ := prepareMailerTest(t)
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}}
|
2022-01-20 12:46:10 -05:00
|
|
|
msgs, err := composeIssueCommentMessages(&mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2022-08-24 22:31:57 -04:00
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCreateIssue,
|
2022-01-20 12:46:10 -05:00
|
|
|
Content: "test body",
|
|
|
|
}, "en-US", recipients, false, "issue create")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2019-11-18 03:08:20 -05:00
|
|
|
assert.Len(t, msgs, 2)
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2020-01-16 12:55:36 -05:00
|
|
|
gomailMsg := msgs[0].ToMessage()
|
|
|
|
mailto := gomailMsg.GetHeader("To")
|
|
|
|
subject := gomailMsg.GetHeader("Subject")
|
|
|
|
messageID := gomailMsg.GetHeader("Message-ID")
|
2021-10-01 11:24:43 -04:00
|
|
|
inReplyTo := gomailMsg.GetHeader("In-Reply-To")
|
|
|
|
references := gomailMsg.GetHeader("References")
|
2019-07-17 15:02:42 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
assert.Len(t, mailto, 1, "exactly one recipient is expected in the To field")
|
2024-05-17 10:47:35 -04:00
|
|
|
assert.Equal(t, "[user2/repo1] issue1 (#1)", subject[0])
|
2021-10-01 11:24:43 -04:00
|
|
|
assert.Equal(t, "<user2/repo1/issues/1@localhost>", inReplyTo[0], "In-Reply-To header doesn't match")
|
|
|
|
assert.Equal(t, "<user2/repo1/issues/1@localhost>", references[0], "References header doesn't match")
|
|
|
|
assert.Equal(t, "<user2/repo1/issues/1@localhost>", messageID[0], "Message-ID header doesn't match")
|
2023-01-14 10:57:10 -05:00
|
|
|
assert.Empty(t, gomailMsg.GetHeader("List-Post")) // incoming mail feature disabled
|
|
|
|
assert.Len(t, gomailMsg.GetHeader("List-Unsubscribe"), 1) // url without mailto
|
2019-07-17 15:02:42 -04:00
|
|
|
}
|
2019-11-07 08:34:28 -05:00
|
|
|
|
2024-05-17 10:47:35 -04:00
|
|
|
func TestMailerIssueTemplate(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2024-05-17 10:47:35 -04:00
|
|
|
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
|
|
|
|
expect := func(t *testing.T, msg *Message, issue *issues_model.Issue, expected ...string) {
|
|
|
|
subject := msg.ToMessage().GetHeader("Subject")
|
|
|
|
msgbuf := new(bytes.Buffer)
|
|
|
|
_, _ = msg.ToMessage().WriteTo(msgbuf)
|
|
|
|
wholemsg := msgbuf.String()
|
|
|
|
assert.Contains(t, subject[0], fallbackMailSubject(issue))
|
|
|
|
for _, s := range expected {
|
|
|
|
assert.Contains(t, wholemsg, s)
|
|
|
|
}
|
2024-07-22 18:17:06 -04:00
|
|
|
AssertTranslatedLocale(t, wholemsg, "mail.issue")
|
2024-05-17 10:47:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
testCompose := func(t *testing.T, ctx *mailCommentContext) *Message {
|
|
|
|
t.Helper()
|
|
|
|
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}}
|
|
|
|
|
|
|
|
ctx.Context = context.Background()
|
|
|
|
fromMention := false
|
|
|
|
msgs, err := composeIssueCommentMessages(ctx, "en-US", recipients, fromMention, "TestMailerIssueTemplate")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-17 10:47:35 -04:00
|
|
|
assert.Len(t, msgs, 1)
|
|
|
|
return msgs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, issue.LoadRepo(db.DefaultContext))
|
2024-05-17 10:47:35 -04:00
|
|
|
|
|
|
|
msg := testCompose(t, &mailCommentContext{
|
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCreateIssue,
|
|
|
|
Content: issue.Content,
|
|
|
|
})
|
|
|
|
expect(t, msg, issue, issue.Content)
|
|
|
|
|
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2, Issue: issue})
|
|
|
|
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCommentIssue,
|
|
|
|
Content: comment.Content, Comment: comment,
|
|
|
|
})
|
|
|
|
expect(t, msg, issue, comment.Content)
|
|
|
|
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCloseIssue,
|
|
|
|
Content: comment.Content, Comment: comment,
|
|
|
|
})
|
|
|
|
expect(t, msg, issue, comment.Content)
|
|
|
|
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionReopenIssue,
|
|
|
|
Content: comment.Content, Comment: comment,
|
|
|
|
})
|
|
|
|
expect(t, msg, issue, comment.Content)
|
|
|
|
|
|
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pull.LoadAttributes(db.DefaultContext))
|
2024-05-17 10:47:35 -04:00
|
|
|
pullComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 4, Issue: pull})
|
|
|
|
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: pull, Doer: doer, ActionType: activities_model.ActionCommentPull,
|
|
|
|
Content: pullComment.Content, Comment: pullComment,
|
|
|
|
})
|
|
|
|
expect(t, msg, pull, pullComment.Content)
|
|
|
|
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: pull, Doer: doer, ActionType: activities_model.ActionMergePullRequest,
|
|
|
|
Content: pullComment.Content, Comment: pullComment,
|
|
|
|
})
|
|
|
|
expect(t, msg, pull, pullComment.Content, pull.PullRequest.BaseBranch)
|
|
|
|
|
|
|
|
reviewComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 9})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, reviewComment.LoadReview(db.DefaultContext))
|
2024-05-17 10:47:35 -04:00
|
|
|
|
|
|
|
approveComment := reviewComment
|
|
|
|
approveComment.Review.Type = issues_model.ReviewTypeApprove
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: pull, Doer: doer, ActionType: activities_model.ActionApprovePullRequest,
|
|
|
|
Content: approveComment.Content, Comment: approveComment,
|
|
|
|
})
|
|
|
|
expect(t, msg, pull, approveComment.Content)
|
|
|
|
|
|
|
|
rejectComment := reviewComment
|
|
|
|
rejectComment.Review.Type = issues_model.ReviewTypeReject
|
|
|
|
msg = testCompose(t, &mailCommentContext{
|
|
|
|
Issue: pull, Doer: doer, ActionType: activities_model.ActionRejectPullRequest,
|
|
|
|
Content: rejectComment.Content, Comment: rejectComment,
|
|
|
|
})
|
|
|
|
expect(t, msg, pull, rejectComment.Content)
|
|
|
|
}
|
|
|
|
|
2019-11-07 08:34:28 -05:00
|
|
|
func TestTemplateSelection(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2021-05-22 02:47:16 -04:00
|
|
|
doer, repo, issue, comment := prepareMailerTest(t)
|
2021-11-24 04:49:20 -05:00
|
|
|
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}}
|
2019-11-07 08:34:28 -05:00
|
|
|
|
2022-08-08 14:04:28 -04:00
|
|
|
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
|
2019-11-07 08:34:28 -05:00
|
|
|
|
2022-08-08 14:04:28 -04:00
|
|
|
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"))
|
2019-11-07 08:34:28 -05:00
|
|
|
|
|
|
|
expect := func(t *testing.T, msg *Message, expSubject, expBody string) {
|
2020-01-16 12:55:36 -05:00
|
|
|
subject := msg.ToMessage().GetHeader("Subject")
|
2019-11-07 08:34:28 -05:00
|
|
|
msgbuf := new(bytes.Buffer)
|
2020-01-16 12:55:36 -05:00
|
|
|
_, _ = msg.ToMessage().WriteTo(msgbuf)
|
2019-11-07 08:34:28 -05:00
|
|
|
wholemsg := msgbuf.String()
|
|
|
|
assert.Equal(t, []string{expSubject}, subject)
|
|
|
|
assert.Contains(t, wholemsg, expBody)
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
msg := testComposeIssueCommentMessage(t, &mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2022-08-24 22:31:57 -04:00
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCreateIssue,
|
2022-01-20 12:46:10 -05:00
|
|
|
Content: "test body",
|
|
|
|
}, recipients, false, "TestTemplateSelection")
|
2019-11-07 08:34:28 -05:00
|
|
|
expect(t, msg, "issue/new/subject", "issue/new/body")
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
msg = testComposeIssueCommentMessage(t, &mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2022-08-24 22:31:57 -04:00
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCommentIssue,
|
2022-01-20 12:46:10 -05:00
|
|
|
Content: "test body", Comment: comment,
|
|
|
|
}, recipients, false, "TestTemplateSelection")
|
2019-11-07 08:34:28 -05:00
|
|
|
expect(t, msg, "issue/default/subject", "issue/default/body")
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2, Repo: repo, Poster: doer})
|
|
|
|
comment = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 4, Issue: pull})
|
2022-01-20 12:46:10 -05:00
|
|
|
msg = testComposeIssueCommentMessage(t, &mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2022-08-24 22:31:57 -04:00
|
|
|
Issue: pull, Doer: doer, ActionType: activities_model.ActionCommentPull,
|
2022-01-20 12:46:10 -05:00
|
|
|
Content: "test body", Comment: comment,
|
|
|
|
}, recipients, false, "TestTemplateSelection")
|
2019-11-07 08:34:28 -05:00
|
|
|
expect(t, msg, "pull/comment/subject", "pull/comment/body")
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
msg = testComposeIssueCommentMessage(t, &mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2022-08-24 22:31:57 -04:00
|
|
|
Issue: issue, Doer: doer, ActionType: activities_model.ActionCloseIssue,
|
2022-01-20 12:46:10 -05:00
|
|
|
Content: "test body", Comment: comment,
|
|
|
|
}, recipients, false, "TestTemplateSelection")
|
2019-11-18 03:08:20 -05:00
|
|
|
expect(t, msg, "Re: [user2/repo1] issue1 (#1)", "issue/close/body")
|
2019-11-07 08:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateServices(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2021-05-22 02:47:16 -04:00
|
|
|
doer, _, issue, comment := prepareMailerTest(t)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, issue.LoadRepo(db.DefaultContext))
|
2019-11-07 08:34:28 -05:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
expect := func(t *testing.T, issue *issues_model.Issue, comment *issues_model.Comment, doer *user_model.User,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType activities_model.ActionType, fromMention bool, tplSubject, tplBody, expSubject, expBody string,
|
2022-01-20 12:46:10 -05:00
|
|
|
) {
|
2022-08-08 14:04:28 -04:00
|
|
|
subjectTemplates = texttmpl.Must(texttmpl.New("issue/default").Parse(tplSubject))
|
|
|
|
bodyTemplates = template.Must(template.New("issue/default").Parse(tplBody))
|
2019-11-07 08:34:28 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}}
|
2022-01-20 12:46:10 -05:00
|
|
|
msg := testComposeIssueCommentMessage(t, &mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
|
|
|
Issue: issue, Doer: doer, ActionType: actionType,
|
2022-01-20 12:46:10 -05:00
|
|
|
Content: "test body", Comment: comment,
|
|
|
|
}, recipients, fromMention, "TestTemplateServices")
|
2019-11-07 08:34:28 -05:00
|
|
|
|
2020-01-16 12:55:36 -05:00
|
|
|
subject := msg.ToMessage().GetHeader("Subject")
|
2019-11-07 08:34:28 -05:00
|
|
|
msgbuf := new(bytes.Buffer)
|
2020-01-16 12:55:36 -05:00
|
|
|
_, _ = msg.ToMessage().WriteTo(msgbuf)
|
2019-11-07 08:34:28 -05:00
|
|
|
wholemsg := msgbuf.String()
|
|
|
|
|
|
|
|
assert.Equal(t, []string{expSubject}, subject)
|
|
|
|
assert.Contains(t, wholemsg, "\r\n"+expBody+"\r\n")
|
|
|
|
}
|
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
expect(t, issue, comment, doer, activities_model.ActionCommentIssue, false,
|
2019-11-07 08:34:28 -05:00
|
|
|
"{{.SubjectPrefix}}[{{.Repo}}]: @{{.Doer.Name}} commented on #{{.Issue.Index}} - {{.Issue.Title}}",
|
|
|
|
"//{{.ActionType}},{{.ActionName}},{{if .IsMention}}norender{{end}}//",
|
|
|
|
"Re: [user2/repo1]: @user2 commented on #1 - issue1",
|
|
|
|
"//issue,comment,//")
|
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
expect(t, issue, comment, doer, activities_model.ActionCommentIssue, true,
|
2019-11-07 08:34:28 -05:00
|
|
|
"{{if .IsMention}}must render{{end}}",
|
|
|
|
"//subject is: {{.Subject}}//",
|
|
|
|
"must render",
|
|
|
|
"//subject is: must render//")
|
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
expect(t, issue, comment, doer, activities_model.ActionCommentIssue, true,
|
2019-11-07 08:34:28 -05:00
|
|
|
"{{.FallbackSubject}}",
|
|
|
|
"//{{.SubjectPrefix}}//",
|
|
|
|
"Re: [user2/repo1] issue1 (#1)",
|
|
|
|
"//Re: //")
|
|
|
|
}
|
2019-11-18 03:08:20 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recipients []*user_model.User, fromMention bool, info string) *Message {
|
2021-05-22 02:47:16 -04:00
|
|
|
msgs, err := composeIssueCommentMessages(ctx, "en-US", recipients, fromMention, info)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2019-11-18 03:08:20 -05:00
|
|
|
assert.Len(t, msgs, 1)
|
|
|
|
return msgs[0]
|
|
|
|
}
|
2021-05-22 02:47:16 -04:00
|
|
|
|
|
|
|
func TestGenerateAdditionalHeaders(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2021-05-22 02:47:16 -04:00
|
|
|
doer, _, issue, _ := prepareMailerTest(t)
|
|
|
|
|
2022-03-22 11:22:54 -04:00
|
|
|
ctx := &mailCommentContext{Context: context.TODO() /* TODO: use a correct context */, Issue: issue, Doer: doer}
|
2024-02-02 19:41:27 -05:00
|
|
|
recipient := &user_model.User{Name: "test", Email: "test@gitea.com"}
|
2021-05-22 02:47:16 -04:00
|
|
|
|
|
|
|
headers := generateAdditionalHeaders(ctx, "dummy-reason", recipient)
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"List-ID": "user2/repo1 <repo1.user2.localhost>",
|
|
|
|
"List-Archive": "<https://try.gitea.io/user2/repo1>",
|
2024-05-28 02:19:03 -04:00
|
|
|
"X-Mailer": "Forgejo",
|
2021-05-22 02:47:16 -04:00
|
|
|
"X-Gitea-Reason": "dummy-reason",
|
2024-02-02 19:41:27 -05:00
|
|
|
"X-Gitea-Sender": "user2",
|
|
|
|
"X-Gitea-Recipient": "test",
|
2021-05-22 02:47:16 -04:00
|
|
|
"X-Gitea-Recipient-Address": "test@gitea.com",
|
|
|
|
"X-Gitea-Repository": "repo1",
|
|
|
|
"X-Gitea-Repository-Path": "user2/repo1",
|
|
|
|
"X-Gitea-Repository-Link": "https://try.gitea.io/user2/repo1",
|
|
|
|
"X-Gitea-Issue-ID": "1",
|
|
|
|
"X-Gitea-Issue-Link": "https://try.gitea.io/user2/repo1/issues/1",
|
2024-05-28 02:20:43 -04:00
|
|
|
"X-Forgejo-Sender": "user2",
|
|
|
|
"X-Forgejo-Recipient": "test",
|
2021-05-22 02:47:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range expected {
|
|
|
|
if assert.Contains(t, headers, key) {
|
|
|
|
assert.Equal(t, value, headers[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 18:01:16 -05:00
|
|
|
|
|
|
|
func Test_createReference(t *testing.T) {
|
2024-07-22 18:17:06 -04:00
|
|
|
defer MockMailSettings(nil)()
|
2022-02-03 18:01:16 -05:00
|
|
|
_, _, issue, comment := prepareMailerTest(t)
|
|
|
|
_, _, pullIssue, _ := prepareMailerTest(t)
|
|
|
|
pullIssue.IsPull = true
|
|
|
|
|
|
|
|
type args struct {
|
2022-06-13 05:37:59 -04:00
|
|
|
issue *issues_model.Issue
|
|
|
|
comment *issues_model.Comment
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType activities_model.ActionType
|
2022-02-03 18:01:16 -05:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
prefix string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Open Issue",
|
|
|
|
args: args{
|
|
|
|
issue: issue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionCreateIssue,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/issues/%d@%s>", issue.Repo.FullName(), issue.Index, setting.Domain),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Open Pull",
|
|
|
|
args: args{
|
|
|
|
issue: pullIssue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionCreatePullRequest,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/pulls/%d@%s>", issue.Repo.FullName(), issue.Index, setting.Domain),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Comment Issue",
|
|
|
|
args: args{
|
|
|
|
issue: issue,
|
|
|
|
comment: comment,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionCommentIssue,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/issues/%d/comment/%d@%s>", issue.Repo.FullName(), issue.Index, comment.ID, setting.Domain),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Comment Pull",
|
|
|
|
args: args{
|
|
|
|
issue: pullIssue,
|
|
|
|
comment: comment,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionCommentPull,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/pulls/%d/comment/%d@%s>", issue.Repo.FullName(), issue.Index, comment.ID, setting.Domain),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Close Issue",
|
|
|
|
args: args{
|
|
|
|
issue: issue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionCloseIssue,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/issues/%d/close/", issue.Repo.FullName(), issue.Index),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Close Pull",
|
|
|
|
args: args{
|
|
|
|
issue: pullIssue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionClosePullRequest,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/pulls/%d/close/", issue.Repo.FullName(), issue.Index),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Reopen Issue",
|
|
|
|
args: args{
|
|
|
|
issue: issue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionReopenIssue,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/issues/%d/reopen/", issue.Repo.FullName(), issue.Index),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Reopen Pull",
|
|
|
|
args: args{
|
|
|
|
issue: pullIssue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionReopenPullRequest,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/pulls/%d/reopen/", issue.Repo.FullName(), issue.Index),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Merge Pull",
|
|
|
|
args: args{
|
|
|
|
issue: pullIssue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionMergePullRequest,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/pulls/%d/merge/", issue.Repo.FullName(), issue.Index),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Ready Pull",
|
|
|
|
args: args{
|
|
|
|
issue: pullIssue,
|
2022-08-24 22:31:57 -04:00
|
|
|
actionType: activities_model.ActionPullRequestReadyForReview,
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
2023-01-14 10:57:10 -05:00
|
|
|
prefix: fmt.Sprintf("<%s/pulls/%d/ready/", issue.Repo.FullName(), issue.Index),
|
2022-02-03 18:01:16 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := createReference(tt.args.issue, tt.args.comment, tt.args.actionType)
|
|
|
|
if !strings.HasPrefix(got, tt.prefix) {
|
|
|
|
t.Errorf("createReference() = %v, want %v", got, tt.prefix)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-14 17:27:00 -04:00
|
|
|
|
|
|
|
func TestFromDisplayName(t *testing.T) {
|
|
|
|
template, err := texttmpl.New("mailFrom").Parse("{{ .DisplayName }}")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-07-14 17:27:00 -04:00
|
|
|
setting.MailService = &setting.Mailer{FromDisplayNameFormatTemplate: template}
|
|
|
|
defer func() { setting.MailService = nil }()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
userDisplayName string
|
|
|
|
fromDisplayName string
|
|
|
|
}{{
|
|
|
|
userDisplayName: "test",
|
|
|
|
fromDisplayName: "test",
|
|
|
|
}, {
|
|
|
|
userDisplayName: "Hi Its <Mee>",
|
|
|
|
fromDisplayName: "Hi Its <Mee>",
|
|
|
|
}, {
|
|
|
|
userDisplayName: "Æsir",
|
|
|
|
fromDisplayName: "=?utf-8?q?=C3=86sir?=",
|
|
|
|
}, {
|
|
|
|
userDisplayName: "new😀user",
|
|
|
|
fromDisplayName: "=?utf-8?q?new=F0=9F=98=80user?=",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.userDisplayName, func(t *testing.T) {
|
|
|
|
user := &user_model.User{FullName: tc.userDisplayName, Name: "tmp"}
|
|
|
|
got := fromDisplayName(user)
|
|
|
|
assert.EqualValues(t, tc.fromDisplayName, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("template with all available vars", func(t *testing.T) {
|
|
|
|
template, err = texttmpl.New("mailFrom").Parse("{{ .DisplayName }} (by {{ .AppName }} on [{{ .Domain }}])")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-07-14 17:27:00 -04:00
|
|
|
setting.MailService = &setting.Mailer{FromDisplayNameFormatTemplate: template}
|
|
|
|
oldAppName := setting.AppName
|
|
|
|
setting.AppName = "Code IT"
|
|
|
|
oldDomain := setting.Domain
|
|
|
|
setting.Domain = "code.it"
|
|
|
|
defer func() {
|
|
|
|
setting.AppName = oldAppName
|
|
|
|
setting.Domain = oldDomain
|
|
|
|
}()
|
|
|
|
|
|
|
|
assert.EqualValues(t, "Mister X (by Code IT on [code.it])", fromDisplayName(&user_model.User{FullName: "Mister X", Name: "tmp"}))
|
|
|
|
})
|
|
|
|
}
|