@@ -5,6 +5,7 @@ package mailer
55
66import (
77 "bytes"
8+ "code.gitea.io/gitea/services/attachment"
89 "context"
910 "fmt"
1011 "html/template"
@@ -55,23 +56,37 @@ const bodyTpl = `
5556
5657func prepareMailerTest (t * testing.T ) (doer * user_model.User , repo * repo_model.Repository , issue * issues_model.Issue , comment * issues_model.Comment ) {
5758 assert .NoError (t , unittest .PrepareTestDatabase ())
58- mailService := setting.Mailer {
59- 60- }
61-
62- setting .MailService = & mailService
59+ setting .
MailService = & setting.
Mailer {
From :
"[email protected] " }
6360 setting .Domain = "localhost"
6461 setting .AppURL = "https://try.gitea.io/"
6562
6663 doer = unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
6764 repo = unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 , Owner : doer })
6865 issue = unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 1 , Repo : repo , Poster : doer })
69- assert .NoError (t , issue .LoadRepo (db .DefaultContext ))
7066 comment = unittest .AssertExistsAndLoadBean (t , & issues_model.Comment {ID : 2 , Issue : issue })
67+ require .NoError (t , issue .LoadRepo (db .DefaultContext ))
7168 return doer , repo , issue , comment
7269}
7370
74- func TestComposeIssueCommentMessage (t * testing.T ) {
71+ func prepareMailerBase64Test (t * testing.T ) (doer * user_model.User , repo * repo_model.Repository , issue * issues_model.Issue ) {
72+ user , repo , issue , comment := prepareMailerTest (t )
73+ setting .MailService .Base64EmbedImages = true
74+ setting .MailService .Base64EmbedImagesMaxSizePerEmail = 10 * 1024 * 1024
75+ att , err := attachment .NewAttachment (t .Context (), & repo_model.Attachment {
76+ UUID : "1234" ,
77+ RepoID : repo .ID ,
78+ IssueID : issue .ID ,
79+ UploaderID : user .ID ,
80+ CommentID : comment .ID ,
81+ Name : "test.png" ,
82+ }, bytes .NewReader ([]byte ("\x89 \x50 \x4e \x47 \x0d \x0a \x1a \x0a " )), 8 )
83+ require .NoError (t , err )
84+ issue .Content = fmt .Sprintf (`MSG-BEFORE <image src="attachments/%s"> MSG-AFTER` , att .UUID )
85+ require .NoError (t , issues_model .UpdateIssueCols (t .Context (), issue , "content" ))
86+ return user , repo , issue
87+ }
88+
89+ func TestComposeIssueCommen (t * testing.T ) {
7590 doer , _ , issue , comment := prepareMailerTest (t )
7691
7792 markup .Init (& markup.RenderHelperFuncs {
@@ -455,20 +470,7 @@ func TestFromDisplayName(t *testing.T) {
455470}
456471
457472func 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-
473+ doer , _ , issue := prepareMailerBase64Test (t )
472474 subjectTemplates = texttmpl .Must (texttmpl .New ("issue/new" ).Parse (subjectTpl ))
473475 bodyTemplates = template .Must (template .New ("issue/new" ).Parse (bodyTpl ))
474476
@@ -480,43 +482,21 @@ func TestEmbedBase64ImagesInEmail(t *testing.T) {
480482 ActionType : activities_model .ActionCreateIssue ,
481483 Content : strings .ReplaceAll (issue .Content , `src="` , `src="` + setting .AppURL ),
482484 }, "en-US" , recipients , false , "issue create" )
485+ require .NoError (t , err )
483486
484487 mailBody := msgs [0 ].Body
485- re := regexp .MustCompile (`(?s)<body>(.*?)</body> ` )
488+ re := regexp .MustCompile (`MSG-BEFORE.*MSG-AFTER ` )
486489 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 )
490+ require .NotEmpty (t , matches )
491+ mailBody = matches [0 ]
492+ assert .Equal (t , `MSG-BEFORE <img src="data:image/png;base64,iVBORw0KGgo="/> MSG-AFTER` , mailBody )
504493}
505494
506495func TestEmbedBase64Images (t * testing.T ) {
507- user , repo , _ , _ := prepareMailerTest (t )
496+ user , repo , issue := prepareMailerBase64Test (t )
508497 defer test .MockVariableValue (& setting .MailService .Base64EmbedImages , true )
509498 defer test .MockVariableValue (& setting .MailService .Base64EmbedImagesMaxSizePerEmail , 10 * 1024 * 1024 )
510499
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 ()))
520500 attachment := unittest .AssertExistsAndLoadBean (t , & repo_model.Attachment {ID : 13 , IssueID : issue .ID , RepoID : repo .ID })
521501 ctx := & mailCommentContext {Context : t .Context (), Issue : issue , Doer : user }
522502
0 commit comments