Skip to content

Commit 4c8b905

Browse files
committed
Added tests.
1 parent 38e9f74 commit 4c8b905

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

services/mailer/mail.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
289289
return msgs, nil
290290
}
291291

292-
func generateAdditionalHeaders(ctx *mailCommentContext, actionType string, recipient *models.User) map[string]string {
292+
func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient *models.User) map[string]string {
293293
repo := ctx.Issue.Repo
294294

295295
return map[string]string{
@@ -301,7 +301,7 @@ func generateAdditionalHeaders(ctx *mailCommentContext, actionType string, recip
301301
//"List-Post": https://github.com/go-gitea/gitea/pull/13585
302302
//"List-Unsubscribe": https://github.com/go-gitea/gitea/issues/10808, https://github.com/go-gitea/gitea/issues/13283
303303

304-
"X-Gitea-Reason": actionType,
304+
"X-Gitea-Reason": reason,
305305
"X-Gitea-Sender": ctx.Doer.DisplayName(),
306306
"X-Gitea-Recipient": recipient.DisplayName(),
307307
"X-Gitea-Recipient-Address": recipient.Email,
@@ -311,12 +311,12 @@ func generateAdditionalHeaders(ctx *mailCommentContext, actionType string, recip
311311
"X-Gitea-Issue-ID": strconv.FormatInt(ctx.Issue.Index, 10),
312312
"X-Gitea-Issue-Link": ctx.Issue.HTMLURL(),
313313

314-
"X-GitHub-Reason": actionType,
314+
"X-GitHub-Reason": reason,
315315
"X-GitHub-Sender": ctx.Doer.DisplayName(),
316316
"X-GitHub-Recipient": recipient.DisplayName(),
317317
"X-GitHub-Recipient-Address": recipient.Email,
318318

319-
"X-GitLab-NotificationReason": actionType,
319+
"X-GitLab-NotificationReason": reason,
320320
"X-GitLab-Project": repo.Name,
321321
"X-GitLab-Project-Path": repo.FullName(),
322322
"X-GitLab-Issue-IID": strconv.FormatInt(ctx.Issue.Index, 10),

services/mailer/mail_test.go

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const bodyTpl = `
3939
</html>
4040
`
4141

42-
func TestComposeIssueCommentMessage(t *testing.T) {
42+
func prepareMailerTest(t *testing.T) (doer *models.User, repo *models.Repository, issue *models.Issue, comment *models.Comment) {
4343
assert.NoError(t, models.PrepareTestDatabase())
4444
var mailService = setting.Mailer{
4545
@@ -48,10 +48,16 @@ func TestComposeIssueCommentMessage(t *testing.T) {
4848
setting.MailService = &mailService
4949
setting.Domain = "localhost"
5050

51-
doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
52-
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
53-
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
54-
comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
51+
doer = models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
52+
repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
53+
issue = models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
54+
assert.NoError(t, issue.LoadRepo())
55+
comment = models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
56+
return
57+
}
58+
59+
func TestComposeIssueCommentMessage(t *testing.T) {
60+
doer, _, issue, comment := prepareMailerTest(t)
5561

5662
stpl := texttmpl.Must(texttmpl.New("issue/comment").Parse(subjectTpl))
5763
btpl := template.Must(template.New("issue/comment").Parse(bodyTpl))
@@ -76,17 +82,7 @@ func TestComposeIssueCommentMessage(t *testing.T) {
7682
}
7783

7884
func TestComposeIssueMessage(t *testing.T) {
79-
assert.NoError(t, models.PrepareTestDatabase())
80-
var mailService = setting.Mailer{
81-
82-
}
83-
84-
setting.MailService = &mailService
85-
setting.Domain = "localhost"
86-
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)
85+
doer, _, issue, _ := prepareMailerTest(t)
9086

9187
stpl := texttmpl.Must(texttmpl.New("issue/new").Parse(subjectTpl))
9288
btpl := template.Must(template.New("issue/new").Parse(bodyTpl))
@@ -111,17 +107,7 @@ func TestComposeIssueMessage(t *testing.T) {
111107
}
112108

113109
func TestTemplateSelection(t *testing.T) {
114-
assert.NoError(t, models.PrepareTestDatabase())
115-
var mailService = setting.Mailer{
116-
117-
}
118-
119-
setting.MailService = &mailService
120-
setting.Domain = "localhost"
121-
122-
doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
123-
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
124-
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
110+
doer, repo, issue, comment := prepareMailerTest(t)
125111
recipients := []*models.User{{Name: "Test", Email: "[email protected]"}}
126112

127113
stpl := texttmpl.Must(texttmpl.New("issue/default").Parse("issue/default/subject"))
@@ -149,7 +135,6 @@ func TestTemplateSelection(t *testing.T) {
149135
Content: "test body"}, recipients, false, "TestTemplateSelection")
150136
expect(t, msg, "issue/new/subject", "issue/new/body")
151137

152-
comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
153138
msg = testComposeIssueCommentMessage(t, &mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue,
154139
Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection")
155140
expect(t, msg, "issue/default/subject", "issue/default/body")
@@ -166,18 +151,7 @@ func TestTemplateSelection(t *testing.T) {
166151
}
167152

168153
func TestTemplateServices(t *testing.T) {
169-
assert.NoError(t, models.PrepareTestDatabase())
170-
var mailService = setting.Mailer{
171-
172-
}
173-
174-
setting.MailService = &mailService
175-
setting.Domain = "localhost"
176-
177-
doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
178-
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
179-
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
180-
comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
154+
doer, _, issue, comment := prepareMailerTest(t)
181155
assert.NoError(t, issue.LoadRepo())
182156

183157
expect := func(t *testing.T, issue *models.Issue, comment *models.Comment, doer *models.User,
@@ -225,3 +199,32 @@ func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recip
225199
assert.Len(t, msgs, 1)
226200
return msgs[0]
227201
}
202+
203+
func TestGenerateAdditionalHeaders(t *testing.T) {
204+
doer, _, issue, _ := prepareMailerTest(t)
205+
206+
ctx := &mailCommentContext{Issue: issue, Doer: doer}
207+
recipient := &models.User{Name: "Test", Email: "[email protected]"}
208+
209+
headers := generateAdditionalHeaders(ctx, "dummy-reason", recipient)
210+
211+
expected := map[string]string{
212+
"List-ID": "user2/repo1 <repo1.user2.localhost>",
213+
"List-Archive": "<https://try.gitea.io/user2/repo1>",
214+
"X-Gitea-Reason": "dummy-reason",
215+
"X-Gitea-Sender": "< U<se>r Tw<o > ><",
216+
"X-Gitea-Recipient": "Test",
217+
"X-Gitea-Recipient-Address": "[email protected]",
218+
"X-Gitea-Repository": "repo1",
219+
"X-Gitea-Repository-Path": "user2/repo1",
220+
"X-Gitea-Repository-Link": "https://try.gitea.io/user2/repo1",
221+
"X-Gitea-Issue-ID": "1",
222+
"X-Gitea-Issue-Link": "https://try.gitea.io/user2/repo1/issues/1",
223+
}
224+
225+
for key, value := range expected {
226+
if assert.Contains(t, headers, key) {
227+
assert.Equal(t, value, headers[key])
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)