@@ -39,7 +39,7 @@ const bodyTpl = `
39
39
</html>
40
40
`
41
41
42
- func TestComposeIssueCommentMessage (t * testing.T ) {
42
+ func prepareMailerTest (t * testing.T ) ( doer * models. User , repo * models. Repository , issue * models. Issue , comment * models. Comment ) {
43
43
assert .NoError (t , models .PrepareTestDatabase ())
44
44
var mailService = setting.Mailer {
45
45
@@ -48,18 +48,24 @@ func TestComposeIssueCommentMessage(t *testing.T) {
48
48
setting .MailService = & mailService
49
49
setting .Domain = "localhost"
50
50
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 )
55
61
56
62
stpl := texttmpl .Must (texttmpl .New ("issue/comment" ).Parse (subjectTpl ))
57
63
btpl := template .Must (template .New ("issue/comment" ).Parse (bodyTpl ))
58
64
InitMailRender (stpl , btpl )
59
65
60
-
66
+ recipients := []
* models. User {{ Name : "Test" , Email : " [email protected] "}, { Name : "Test2" , Email : " [email protected] "} }
61
67
msgs , err := composeIssueCommentMessages (& mailCommentContext {Issue : issue , Doer : doer , ActionType : models .ActionCommentIssue ,
62
- Content : "test body" , Comment : comment }, "en-US" , tos , false , "issue comment" )
68
+ Content : "test body" , Comment : comment }, "en-US" , recipients , false , "issue comment" )
63
69
assert .NoError (t , err )
64
70
assert .Len (t , msgs , 2 )
65
71
gomailMsg := msgs [0 ].ToMessage ()
@@ -76,25 +82,15 @@ func TestComposeIssueCommentMessage(t *testing.T) {
76
82
}
77
83
78
84
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 )
90
86
91
87
stpl := texttmpl .Must (texttmpl .New ("issue/new" ).Parse (subjectTpl ))
92
88
btpl := template .Must (template .New ("issue/new" ).Parse (bodyTpl ))
93
89
InitMailRender (stpl , btpl )
94
90
95
-
91
+ recipients := []
* models. User {{ Name : "Test" , Email : " [email protected] "}, { Name : "Test2" , Email : " [email protected] "} }
96
92
msgs , err := composeIssueCommentMessages (& mailCommentContext {Issue : issue , Doer : doer , ActionType : models .ActionCreateIssue ,
97
- Content : "test body" }, "en-US" , tos , false , "issue create" )
93
+ Content : "test body" }, "en-US" , recipients , false , "issue create" )
98
94
assert .NoError (t , err )
99
95
assert .Len (t , msgs , 2 )
100
96
@@ -111,18 +107,8 @@ func TestComposeIssueMessage(t *testing.T) {
111
107
}
112
108
113
109
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 )
125
- tos := []
string {
"[email protected] " }
110
+ doer , repo , issue , comment := prepareMailerTest (t )
111
+ recipients := []
* models.
User {{
Name :
"Test" ,
Email :
"[email protected] " }}
126
112
127
113
stpl := texttmpl .Must (texttmpl .New ("issue/default" ).Parse ("issue/default/subject" ))
128
114
texttmpl .Must (stpl .New ("issue/new" ).Parse ("issue/new/subject" ))
@@ -146,38 +132,26 @@ func TestTemplateSelection(t *testing.T) {
146
132
}
147
133
148
134
msg := testComposeIssueCommentMessage (t , & mailCommentContext {Issue : issue , Doer : doer , ActionType : models .ActionCreateIssue ,
149
- Content : "test body" }, tos , false , "TestTemplateSelection" )
135
+ Content : "test body" }, recipients , false , "TestTemplateSelection" )
150
136
expect (t , msg , "issue/new/subject" , "issue/new/body" )
151
137
152
- comment := models .AssertExistsAndLoadBean (t , & models.Comment {ID : 2 , Issue : issue }).(* models.Comment )
153
138
msg = testComposeIssueCommentMessage (t , & mailCommentContext {Issue : issue , Doer : doer , ActionType : models .ActionCommentIssue ,
154
- Content : "test body" , Comment : comment }, tos , false , "TestTemplateSelection" )
139
+ Content : "test body" , Comment : comment }, recipients , false , "TestTemplateSelection" )
155
140
expect (t , msg , "issue/default/subject" , "issue/default/body" )
156
141
157
142
pull := models .AssertExistsAndLoadBean (t , & models.Issue {ID : 2 , Repo : repo , Poster : doer }).(* models.Issue )
158
143
comment = models .AssertExistsAndLoadBean (t , & models.Comment {ID : 4 , Issue : pull }).(* models.Comment )
159
144
msg = testComposeIssueCommentMessage (t , & mailCommentContext {Issue : pull , Doer : doer , ActionType : models .ActionCommentPull ,
160
- Content : "test body" , Comment : comment }, tos , false , "TestTemplateSelection" )
145
+ Content : "test body" , Comment : comment }, recipients , false , "TestTemplateSelection" )
161
146
expect (t , msg , "pull/comment/subject" , "pull/comment/body" )
162
147
163
148
msg = testComposeIssueCommentMessage (t , & mailCommentContext {Issue : issue , Doer : doer , ActionType : models .ActionCloseIssue ,
164
- Content : "test body" , Comment : comment }, tos , false , "TestTemplateSelection" )
149
+ Content : "test body" , Comment : comment }, recipients , false , "TestTemplateSelection" )
165
150
expect (t , msg , "Re: [user2/repo1] issue1 (#1)" , "issue/close/body" )
166
151
}
167
152
168
153
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 )
181
155
assert .NoError (t , issue .LoadRepo ())
182
156
183
157
expect := func (t * testing.T , issue * models.Issue , comment * models.Comment , doer * models.User ,
@@ -187,9 +161,9 @@ func TestTemplateServices(t *testing.T) {
187
161
btpl := template .Must (template .New ("issue/default" ).Parse (tplBody ))
188
162
InitMailRender (stpl , btpl )
189
163
190
- tos := []
string { " [email protected] "}
164
+ recipients := []
* models. User {{ Name : "Test" , Email : " [email protected] "} }
191
165
msg := testComposeIssueCommentMessage (t , & mailCommentContext {Issue : issue , Doer : doer , ActionType : actionType ,
192
- Content : "test body" , Comment : comment }, tos , fromMention , "TestTemplateServices" )
166
+ Content : "test body" , Comment : comment }, recipients , fromMention , "TestTemplateServices" )
193
167
194
168
subject := msg .ToMessage ().GetHeader ("Subject" )
195
169
msgbuf := new (bytes.Buffer )
@@ -219,9 +193,38 @@ func TestTemplateServices(t *testing.T) {
219
193
"//Re: //" )
220
194
}
221
195
222
- func testComposeIssueCommentMessage (t * testing.T , ctx * mailCommentContext , tos []string , fromMention bool , info string ) * Message {
223
- msgs , err := composeIssueCommentMessages (ctx , "en-US" , tos , fromMention , info )
196
+ func testComposeIssueCommentMessage (t * testing.T , ctx * mailCommentContext , recipients []* models. User , fromMention bool , info string ) * Message {
197
+ msgs , err := composeIssueCommentMessages (ctx , "en-US" , recipients , fromMention , info )
224
198
assert .NoError (t , err )
225
199
assert .Len (t , msgs , 1 )
226
200
return msgs [0 ]
227
201
}
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