@@ -23,7 +23,7 @@ import (
23
23
user_model "code.gitea.io/gitea/models/user"
24
24
"code.gitea.io/gitea/modules/markup"
25
25
"code.gitea.io/gitea/modules/setting"
26
- "code.gitea.io/gitea/modules/test "
26
+ "code.gitea.io/gitea/services/attachment "
27
27
sender_service "code.gitea.io/gitea/services/mailer/sender"
28
28
29
29
"github.com/stretchr/testify/assert"
@@ -55,23 +55,36 @@ const bodyTpl = `
55
55
56
56
func prepareMailerTest (t * testing.T ) (doer * user_model.User , repo * repo_model.Repository , issue * issues_model.Issue , comment * issues_model.Comment ) {
57
57
assert .NoError (t , unittest .PrepareTestDatabase ())
58
- mailService := setting.Mailer {
59
- From : "test@gitea.com" ,
60
- }
61
-
62
- setting .MailService = & mailService
58
+ setting .MailService = & setting.Mailer {From : "test@gitea.com" }
63
59
setting .Domain = "localhost"
64
60
setting .AppURL = "https://try.gitea.io/"
65
61
66
62
doer = unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
67
63
repo = unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 , Owner : doer })
68
64
issue = unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 1 , Repo : repo , Poster : doer })
69
- assert .NoError (t , issue .LoadRepo (db .DefaultContext ))
70
65
comment = unittest .AssertExistsAndLoadBean (t , & issues_model.Comment {ID : 2 , Issue : issue })
66
+ require .NoError (t , issue .LoadRepo (db .DefaultContext ))
71
67
return doer , repo , issue , comment
72
68
}
73
69
74
- func TestComposeIssueCommentMessage (t * testing.T ) {
70
+ func prepareMailerBase64Test (t * testing.T ) (doer * user_model.User , repo * repo_model.Repository , issue * issues_model.Issue , att * repo_model.Attachment ) {
71
+ user , repo , issue , comment := prepareMailerTest (t )
72
+ setting .MailService .Base64EmbedImages = true
73
+ setting .MailService .Base64EmbedImagesMaxSizePerEmail = 10 * 1024 * 1024
74
+ att , err := attachment .NewAttachment (t .Context (), & repo_model.Attachment {
75
+ RepoID : repo .ID ,
76
+ IssueID : issue .ID ,
77
+ UploaderID : user .ID ,
78
+ CommentID : comment .ID ,
79
+ Name : "test.png" ,
80
+ }, bytes .NewReader ([]byte ("\x89 \x50 \x4e \x47 \x0d \x0a \x1a \x0a " )), 8 )
81
+ require .NoError (t , err )
82
+ issue .Content = fmt .Sprintf (`MSG-BEFORE <image src="attachments/%s"> MSG-AFTER` , att .UUID )
83
+ require .NoError (t , issues_model .UpdateIssueCols (t .Context (), issue , "content" ))
84
+ return user , repo , issue , att
85
+ }
86
+
87
+ func TestComposeIssueComment (t * testing.T ) {
75
88
doer , _ , issue , comment := prepareMailerTest (t )
76
89
77
90
markup .Init (& markup.RenderHelperFuncs {
@@ -112,7 +125,8 @@ func TestComposeIssueCommentMessage(t *testing.T) {
112
125
assert .Len (t , gomailMsg .GetGenHeader ("List-Unsubscribe" ), 2 ) // url + mailto
113
126
114
127
var buf bytes.Buffer
115
- gomailMsg .WriteTo (& buf )
128
+ _ , err = gomailMsg .WriteTo (& buf )
129
+ require .NoError (t , err )
116
130
117
131
b , err := io .ReadAll (quotedprintable .NewReader (& buf ))
118
132
assert .NoError (t , err )
@@ -407,9 +421,9 @@ func TestGenerateMessageIDForRelease(t *testing.T) {
407
421
}
408
422
409
423
func TestFromDisplayName (t * testing.T ) {
410
- template , err := texttmpl .New ("mailFrom" ).Parse ("{{ .DisplayName }}" )
424
+ tmpl , err := texttmpl .New ("mailFrom" ).Parse ("{{ .DisplayName }}" )
411
425
assert .NoError (t , err )
412
- setting .MailService = & setting.Mailer {FromDisplayNameFormatTemplate : template }
426
+ setting .MailService = & setting.Mailer {FromDisplayNameFormatTemplate : tmpl }
413
427
defer func () { setting .MailService = nil }()
414
428
415
429
tests := []struct {
@@ -438,9 +452,9 @@ func TestFromDisplayName(t *testing.T) {
438
452
}
439
453
440
454
t .Run ("template with all available vars" , func (t * testing.T ) {
441
- template , err = texttmpl .New ("mailFrom" ).Parse ("{{ .DisplayName }} (by {{ .AppName }} on [{{ .Domain }}])" )
455
+ tmpl , err = texttmpl .New ("mailFrom" ).Parse ("{{ .DisplayName }} (by {{ .AppName }} on [{{ .Domain }}])" )
442
456
assert .NoError (t , err )
443
- setting .MailService = & setting.Mailer {FromDisplayNameFormatTemplate : template }
457
+ setting .MailService = & setting.Mailer {FromDisplayNameFormatTemplate : tmpl }
444
458
oldAppName := setting .AppName
445
459
setting .AppName = "Code IT"
446
460
oldDomain := setting .Domain
@@ -455,20 +469,7 @@ func TestFromDisplayName(t *testing.T) {
455
469
}
456
470
457
471
func TestEmbedBase64ImagesInEmail (t * testing.T ) {
458
- doer , repo , _ , _ := prepareMailerTest (t )
459
- defer test .MockVariableValue (& setting .MailService .Base64EmbedImages , true )
460
- defer test .MockVariableValue (& setting .MailService .Base64EmbedImagesMaxSizePerEmail , 10 * 1024 * 1024 )
461
-
462
- err := issues_model .NewIssue (t .Context (), repo , & issues_model.Issue {
463
- Poster : doer ,
464
- RepoID : repo .ID ,
465
- Title : "test issue attachment" ,
466
- Content : `content including this image: <image alt="gitea.png" src="attachments/1b267670-1793-4cd0-abc1-449269b7cff9" /> with some more content behind it` ,
467
- }, nil , nil )
468
- require .NoError (t , err )
469
- issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {Title : "test issue attachment" })
470
- require .NoError (t , issue .LoadRepo (t .Context ()))
471
-
472
+ doer , _ , issue , _ := prepareMailerBase64Test (t )
472
473
subjectTemplates = texttmpl .Must (texttmpl .New ("issue/new" ).Parse (subjectTpl ))
473
474
bodyTemplates = template .Must (template .New ("issue/new" ).Parse (bodyTpl ))
474
475
@@ -480,52 +481,26 @@ func TestEmbedBase64ImagesInEmail(t *testing.T) {
480
481
ActionType : activities_model .ActionCreateIssue ,
481
482
Content : strings .ReplaceAll (issue .Content , `src="` , `src="` + setting .AppURL ),
482
483
}, "en-US" , recipients , false , "issue create" )
484
+ require .NoError (t , err )
483
485
484
486
mailBody := msgs [0 ].Body
485
- re := regexp .MustCompile (`(?s)<body>(.*?)</body> ` )
487
+ re := regexp .MustCompile (`MSG-BEFORE.*MSG-AFTER ` )
486
488
matches := re .FindStringSubmatch (mailBody )
487
- if len (matches ) > 1 {
488
- mailBody = matches [1 ]
489
- }
490
- // check if the mail body was correctly generated
491
- assert .NoError (t , err )
492
- assert .Contains (t , mailBody , "content including this image" )
493
-
494
- // check if an image was embedded
495
- assert .Contains (t , mailBody , "data:image/png;base64," )
496
-
497
- // check if the image was embedded only once
498
- assert .Equal (t , 1 , strings .Count (mailBody , "data:image/png;base64," ))
499
-
500
- img2InternalBase64 := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAxAQMAAAB3d7wRAAAABlBMVEVgmyF6qkqITHmkAAAAAXRSTlMBN+Ho8AAAAJhJREFUKM+V0DsOwyAQBNCxXLjkCFwk0t7McDQfhS4tpQuEzWc/iaUU2eo1zC4DUMWYF3DxVKzGTXjBGb2RsjJEo6ZhN1Zj+cEgi/9hBQl3YflkkIsbo5IO5glKTuhPpavM3Hp4C7WdjEWYrL5GMkp/R+s4GPlh/CZn4MEwv9aHHiyD3ujm5X22eaMyDa5yAm+O0B1TPa1l3W2qZWMg+KgtAAAAAElFTkSuQmCC"
501
-
502
- // check if the image was embedded correctly
503
- assert .Contains (t , mailBody , img2InternalBase64 )
489
+ require .NotEmpty (t , matches )
490
+ mailBody = matches [0 ]
491
+ assert .Equal (t , `MSG-BEFORE <img src="data:image/png;base64,iVBORw0KGgo="/> MSG-AFTER` , mailBody )
504
492
}
505
493
506
494
func TestEmbedBase64Images (t * testing.T ) {
507
- user , repo , _ , _ := prepareMailerTest (t )
508
- defer test .MockVariableValue (& setting .MailService .Base64EmbedImages , true )
509
- defer test .MockVariableValue (& setting .MailService .Base64EmbedImagesMaxSizePerEmail , 10 * 1024 * 1024 )
510
-
511
- err := issues_model .NewIssue (t .Context (), repo , & issues_model.Issue {
512
- Poster : user ,
513
- RepoID : repo .ID ,
514
- Title : "test issue attachment" ,
515
- Content : `content including this image: <image alt="gitea.png" src="attachments/1b267670-1793-4cd0-abc1-449269b7cff9" /> with some more content behind it` ,
516
- }, nil , nil )
517
- require .NoError (t , err )
518
- issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {Title : "test issue attachment" })
519
- require .NoError (t , issue .LoadRepo (t .Context ()))
520
- attachment := unittest .AssertExistsAndLoadBean (t , & repo_model.Attachment {ID : 13 , IssueID : issue .ID , RepoID : repo .ID })
495
+ user , repo , issue , att := prepareMailerBase64Test (t )
521
496
ctx := & mailCommentContext {Context : t .Context (), Issue : issue , Doer : user }
522
497
523
498
img1ExternalURL := "https://via.placeholder.com/10"
524
499
img1ExternalImg := "<img src=\" " + img1ExternalURL + "\" />"
525
500
526
- img2InternalURL := setting .AppURL + repo .Owner .Name + "/" + repo .Name + "/attachments/" + attachment .UUID
501
+ img2InternalURL := setting .AppURL + repo .Owner .Name + "/" + repo .Name + "/attachments/" + att .UUID
527
502
img2InternalImg := "<img src=\" " + img2InternalURL + "\" />"
528
- img2InternalBase64 := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAxAQMAAAB3d7wRAAAABlBMVEVgmyF6qkqITHmkAAAAAXRSTlMBN+Ho8AAAAJhJREFUKM+V0DsOwyAQBNCxXLjkCFwk0t7McDQfhS4tpQuEzWc/iaUU2eo1zC4DUMWYF3DxVKzGTXjBGb2RsjJEo6ZhN1Zj+cEgi/9hBQl3YflkkIsbo5IO5glKTuhPpavM3Hp4C7WdjEWYrL5GMkp/R+s4GPlh/CZn4MEwv9aHHiyD3ujm5X22eaMyDa5yAm+O0B1TPa1l3W2qZWMg+KgtAAAAAElFTkSuQmCC "
503
+ img2InternalBase64 := "data:image/png;base64,iVBORw0KGgo= "
529
504
img2InternalBase64Img := "<img src=\" " + img2InternalBase64 + "\" />"
530
505
531
506
// 1st Test: convert internal image to base64
@@ -558,7 +533,7 @@ func TestEmbedBase64Images(t *testing.T) {
558
533
559
534
// 4th Test, generate email body with 2 internal images, but set Mailer.Base64EmbedImagesMaxSizePerEmail to the size of the first image (+1), expect the first image to be replaced and the second not
560
535
t .Run ("generateEmailBodyWithMaxSize" , func (t * testing.T ) {
561
- setting .MailService .Base64EmbedImagesMaxSizePerEmail = int64 ( len ( img2InternalBase64 ) + 1 )
536
+ setting .MailService .Base64EmbedImagesMaxSizePerEmail = 10
562
537
563
538
mailBody := "<html><head></head><body><p>Test1</p>" + img2InternalImg + "<p>Test2</p>" + img2InternalImg + "<p>Test3</p></body></html>"
564
539
expectedMailBody := "<html><head></head><body><p>Test1</p>" + img2InternalBase64Img + "<p>Test2</p>" + img2InternalImg + "<p>Test3</p></body></html>"
0 commit comments