@@ -7,17 +7,23 @@ import (
77 "bytes"
88 "image"
99 "image/png"
10+ "io/fs"
1011 "mime/multipart"
1112 "net/http"
13+ "os"
1214 "strings"
1315 "testing"
1416
1517 repo_model "code.gitea.io/gitea/models/repo"
1618 "code.gitea.io/gitea/modules/storage"
1719 "code.gitea.io/gitea/modules/test"
20+ "code.gitea.io/gitea/modules/web"
21+ route_web "code.gitea.io/gitea/routers/web"
22+ "code.gitea.io/gitea/services/context"
1823 "code.gitea.io/gitea/tests"
1924
2025 "github.com/stretchr/testify/assert"
26+ "github.com/stretchr/testify/require"
2127)
2228
2329func testGeneratePngBytes () []byte {
@@ -52,14 +58,39 @@ func testCreateIssueAttachment(t *testing.T, session *TestSession, csrf, repoURL
5258 return obj ["uuid" ]
5359}
5460
55- func TestCreateAnonymousAttachment (t * testing.T ) {
61+ func Test0Attachments (t * testing.T ) {
5662 defer tests .PrepareTestEnv (t )()
63+ t .Run ("CreateAnonymousAttachment" , testCreateAnonymousAttachment )
64+ t .Run ("CreateUser2IssueAttachment" , testCreateUser2IssueAttachment )
65+ t .Run ("UploadAttachmentDeleteTemp" , testUploadAttachmentDeleteTemp )
66+ t .Run ("GetAttachment" , testGetAttachment )
67+ }
68+
69+ func testUploadAttachmentDeleteTemp (t * testing.T ) {
70+ defer test .MockVariableValue (& context .ParseMultipartFormMaxMemory , 1 )()
71+ session := loginUser (t , "user2" )
72+ var uploadTempFile string
73+ defer web .RouteMock (route_web .RouterMockPointBeforeWebRoutes , func (resp http.ResponseWriter , req * http.Request ) {
74+ // TODO: GOLANG-HTTP-TMPDIR: Golang saves the uploaded file to os.TempDir() when it exceeds the max memory limit.
75+ files , err := fs .Glob (os .DirFS (os .Getenv ("TMPDIR" )), "multipart-*" )
76+ require .NoError (t , err )
77+ require .Len (t , files , 1 )
78+
79+ uploadTempFile = os .Getenv ("TMPDIR" ) + "/" + files [0 ]
80+ _ , err = os .Stat (uploadTempFile )
81+ assert .NoError (t , err , "the temp file should exist during upload handling because size exceeds the parse form's max memory" )
82+ })()
83+ _ = testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusOK )
84+ _ , err := os .Stat (uploadTempFile )
85+ assert .ErrorIs (t , err , os .ErrNotExist , "the temp file should be deleted after upload" )
86+ }
87+
88+ func testCreateAnonymousAttachment (t * testing.T ) {
5789 session := emptyTestSession (t )
5890 testCreateIssueAttachment (t , session , GetAnonymousCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusSeeOther )
5991}
6092
61- func TestCreateIssueAttachment (t * testing.T ) {
62- defer tests .PrepareTestEnv (t )()
93+ func testCreateUser2IssueAttachment (t * testing.T ) {
6394 const repoURL = "user2/repo1"
6495 session := loginUser (t , "user2" )
6596 uuid := testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), repoURL , "image.png" , testGeneratePngBytes (), http .StatusOK )
@@ -90,8 +121,7 @@ func TestCreateIssueAttachment(t *testing.T) {
90121 MakeRequest (t , req , http .StatusOK )
91122}
92123
93- func TestGetAttachment (t * testing.T ) {
94- defer tests .PrepareTestEnv (t )()
124+ func testGetAttachment (t * testing.T ) {
95125 adminSession := loginUser (t , "user1" )
96126 user2Session := loginUser (t , "user2" )
97127 user8Session := loginUser (t , "user8" )
0 commit comments