@@ -5,6 +5,7 @@ package mailer
55
66import  (
77	"bytes" 
8+ 	"code.gitea.io/gitea/services/attachment" 
89	"context" 
910	"fmt" 
1011	"html/template" 
@@ -23,7 +24,6 @@ import (
2324	user_model "code.gitea.io/gitea/models/user" 
2425	"code.gitea.io/gitea/modules/markup" 
2526	"code.gitea.io/gitea/modules/setting" 
26- 	"code.gitea.io/gitea/modules/test" 
2727	sender_service "code.gitea.io/gitea/services/mailer/sender" 
2828
2929	"github.com/stretchr/testify/assert" 
@@ -55,23 +55,37 @@ const bodyTpl = `
5555
5656func  prepareMailerTest (t  * testing.T ) (doer  * user_model.User , repo  * repo_model.Repository , issue  * issues_model.Issue , comment  * issues_model.Comment ) {
5757	assert .NoError (t , unittest .PrepareTestDatabase ())
58- 	mailService  :=  setting.Mailer {
59- 60- 	}
61- 
62- 	setting .MailService  =  & mailService 
58+ 	setting .
MailService  =  & setting.
Mailer {
From : 
"[email protected] " }
 6359	setting .Domain  =  "localhost" 
6460	setting .AppURL  =  "https://try.gitea.io/" 
6561
6662	doer  =  unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
6763	repo  =  unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 , Owner : doer })
6864	issue  =  unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 1 , Repo : repo , Poster : doer })
69- 	assert .NoError (t , issue .LoadRepo (db .DefaultContext ))
7065	comment  =  unittest .AssertExistsAndLoadBean (t , & issues_model.Comment {ID : 2 , Issue : issue })
66+ 	require .NoError (t , issue .LoadRepo (db .DefaultContext ))
7167	return  doer , repo , issue , comment 
7268}
7369
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+ 		UUID :       "1234" ,
76+ 		RepoID :     repo .ID ,
77+ 		IssueID :    issue .ID ,
78+ 		UploaderID : user .ID ,
79+ 		CommentID :  comment .ID ,
80+ 		Name :       "test.png" ,
81+ 	}, bytes .NewReader ([]byte ("\x89 \x50 \x4e \x47 \x0d \x0a \x1a \x0a " )), 8 )
82+ 	require .NoError (t , err )
83+ 	issue .Content  =  fmt .Sprintf (`MSG-BEFORE <image src="attachments/%s"> MSG-AFTER` , att .UUID )
84+ 	require .NoError (t , issues_model .UpdateIssueCols (t .Context (), issue , "content" ))
85+ 	return  user , repo , issue , att 
86+ }
87+ 
88+ func  TestComposeIssueCommen (t  * testing.T ) {
7589	doer , _ , issue , comment  :=  prepareMailerTest (t )
7690
7791	markup .Init (& markup.RenderHelperFuncs {
@@ -455,20 +469,7 @@ func TestFromDisplayName(t *testing.T) {
455469}
456470
457471func  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 )
472473	subjectTemplates  =  texttmpl .Must (texttmpl .New ("issue/new" ).Parse (subjectTpl ))
473474	bodyTemplates  =  template .Must (template .New ("issue/new" ).Parse (bodyTpl ))
474475
@@ -480,52 +481,26 @@ func TestEmbedBase64ImagesInEmail(t *testing.T) {
480481		ActionType : activities_model .ActionCreateIssue ,
481482		Content :    strings .ReplaceAll (issue .Content , `src="` , `src="` + setting .AppURL ),
482483	}, "en-US" , recipients , false , "issue create" )
484+ 	require .NoError (t , err )
483485
484486	mailBody  :=  msgs [0 ].Body 
485- 	re  :=  regexp .MustCompile (`(?s)<body>(.*?)</body> ` )
487+ 	re  :=  regexp .MustCompile (`MSG-BEFORE.*MSG-AFTER ` )
486488	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 )
504492}
505493
506494func  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 )
521496	ctx  :=  & mailCommentContext {Context : t .Context (), Issue : issue , Doer : user }
522497
523498	img1ExternalURL  :=  "https://via.placeholder.com/10" 
524499	img1ExternalImg  :=  "<img src=\" "  +  img1ExternalURL  +  "\" />" 
525500
526- 	img2InternalURL  :=  setting .AppURL  +  repo .Owner .Name  +  "/"  +  repo .Name  +  "/attachments/"  +  attachment .UUID 
501+ 	img2InternalURL  :=  setting .AppURL  +  repo .Owner .Name  +  "/"  +  repo .Name  +  "/attachments/"  +  att .UUID 
527502	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= " 
529504	img2InternalBase64Img  :=  "<img src=\" "  +  img2InternalBase64  +  "\" />" 
530505
531506	// 1st Test: convert internal image to base64 
@@ -558,7 +533,7 @@ func TestEmbedBase64Images(t *testing.T) {
558533
559534	// 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 
560535	t .Run ("generateEmailBodyWithMaxSize" , func (t  * testing.T ) {
561- 		setting .MailService .Base64EmbedImagesMaxSizePerEmail  =  int64 ( len ( img2InternalBase64 )  +   1 ) 
536+ 		setting .MailService .Base64EmbedImagesMaxSizePerEmail  =  10 
562537
563538		mailBody  :=  "<html><head></head><body><p>Test1</p>"  +  img2InternalImg  +  "<p>Test2</p>"  +  img2InternalImg  +  "<p>Test3</p></body></html>" 
564539		expectedMailBody  :=  "<html><head></head><body><p>Test1</p>"  +  img2InternalBase64Img  +  "<p>Test2</p>"  +  img2InternalImg  +  "<p>Test3</p></body></html>" 
0 commit comments