@@ -6,7 +6,9 @@ package mailer
6
6
7
7
import (
8
8
"bytes"
9
+ "fmt"
9
10
"html/template"
11
+ "strings"
10
12
"testing"
11
13
texttmpl "text/template"
12
14
@@ -15,7 +17,6 @@ import (
15
17
"code.gitea.io/gitea/models/unittest"
16
18
user_model "code.gitea.io/gitea/models/user"
17
19
"code.gitea.io/gitea/modules/setting"
18
-
19
20
"github.com/stretchr/testify/assert"
20
21
)
21
22
@@ -236,3 +237,115 @@ func TestGenerateAdditionalHeaders(t *testing.T) {
236
237
}
237
238
}
238
239
}
240
+
241
+ func Test_createReference (t * testing.T ) {
242
+ _ , _ , issue , comment := prepareMailerTest (t )
243
+ _ , _ , pullIssue , _ := prepareMailerTest (t )
244
+ pullIssue .IsPull = true
245
+
246
+ type args struct {
247
+ issue * models.Issue
248
+ comment * models.Comment
249
+ actionType models.ActionType
250
+ }
251
+ tests := []struct {
252
+ name string
253
+ args args
254
+ prefix string
255
+ suffix string
256
+ }{
257
+ {
258
+ name : "Open Issue" ,
259
+ args : args {
260
+ issue : issue ,
261
+ actionType : models .ActionCreateIssue ,
262
+ },
263
+ prefix : fmt .Sprintf ("%s/issues/%d@%s" , issue .Repo .FullName (), issue .Index , setting .Domain ),
264
+ },
265
+ {
266
+ name : "Open Pull" ,
267
+ args : args {
268
+ issue : pullIssue ,
269
+ actionType : models .ActionCreatePullRequest ,
270
+ },
271
+ prefix : fmt .Sprintf ("%s/pulls/%d@%s" , issue .Repo .FullName (), issue .Index , setting .Domain ),
272
+ },
273
+ {
274
+ name : "Comment Issue" ,
275
+ args : args {
276
+ issue : issue ,
277
+ comment : comment ,
278
+ actionType : models .ActionCommentIssue ,
279
+ },
280
+ prefix : fmt .Sprintf ("%s/issues/%d/comment/%d@%s" , issue .Repo .FullName (), issue .Index , comment .ID , setting .Domain ),
281
+ },
282
+ {
283
+ name : "Comment Pull" ,
284
+ args : args {
285
+ issue : pullIssue ,
286
+ comment : comment ,
287
+ actionType : models .ActionCommentPull ,
288
+ },
289
+ prefix : fmt .Sprintf ("%s/pulls/%d/comment/%d@%s" , issue .Repo .FullName (), issue .Index , comment .ID , setting .Domain ),
290
+ },
291
+ {
292
+ name : "Close Issue" ,
293
+ args : args {
294
+ issue : issue ,
295
+ actionType : models .ActionCloseIssue ,
296
+ },
297
+ prefix : fmt .Sprintf ("%s/issues/%d/close/" , issue .Repo .FullName (), issue .Index ),
298
+ },
299
+ {
300
+ name : "Close Pull" ,
301
+ args : args {
302
+ issue : pullIssue ,
303
+ actionType : models .ActionClosePullRequest ,
304
+ },
305
+ prefix : fmt .Sprintf ("%s/pulls/%d/close/" , issue .Repo .FullName (), issue .Index ),
306
+ },
307
+ {
308
+ name : "Reopen Issue" ,
309
+ args : args {
310
+ issue : issue ,
311
+ actionType : models .ActionReopenIssue ,
312
+ },
313
+ prefix : fmt .Sprintf ("%s/issues/%d/reopen/" , issue .Repo .FullName (), issue .Index ),
314
+ },
315
+ {
316
+ name : "Reopen Pull" ,
317
+ args : args {
318
+ issue : pullIssue ,
319
+ actionType : models .ActionReopenPullRequest ,
320
+ },
321
+ prefix : fmt .Sprintf ("%s/pulls/%d/reopen/" , issue .Repo .FullName (), issue .Index ),
322
+ },
323
+ {
324
+ name : "Merge Pull" ,
325
+ args : args {
326
+ issue : pullIssue ,
327
+ actionType : models .ActionMergePullRequest ,
328
+ },
329
+ prefix : fmt .Sprintf ("%s/pulls/%d/merge/" , issue .Repo .FullName (), issue .Index ),
330
+ },
331
+ {
332
+ name : "Ready Pull" ,
333
+ args : args {
334
+ issue : pullIssue ,
335
+ actionType : models .ActionPullRequestReadyForReview ,
336
+ },
337
+ prefix : fmt .Sprintf ("%s/pulls/%d/ready/" , issue .Repo .FullName (), issue .Index ),
338
+ },
339
+ }
340
+ for _ , tt := range tests {
341
+ t .Run (tt .name , func (t * testing.T ) {
342
+ got := createReference (tt .args .issue , tt .args .comment , tt .args .actionType )
343
+ if ! strings .HasPrefix (got , tt .prefix ) {
344
+ t .Errorf ("createReference() = %v, want %v" , got , tt .prefix )
345
+ }
346
+ if ! strings .HasSuffix (got , tt .suffix ) {
347
+ t .Errorf ("createReference() = %v, want %v" , got , tt .prefix )
348
+ }
349
+ })
350
+ }
351
+ }
0 commit comments