@@ -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,38 @@ func testCreateIssueAttachment(t *testing.T, session *TestSession, csrf, repoURL
5258 return obj ["uuid" ]
5359}
5460
55- func TestCreateAnonymousAttachment (t * testing.T ) {
61+ func TestAttachments (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+ session := loginUser (t , "user2" )
71+ countTmpFile := func () int {
72+ // TODO: GOLANG-HTTP-TMPDIR: Golang saves the uploaded file to os.TempDir() when it exceeds the max memory limit.
73+ files , err := fs .Glob (os .DirFS (os .TempDir ()), "multipart-*" ) //nolint:usetesting // Golang's "http" package's behavior
74+ require .NoError (t , err )
75+ return len (files )
76+ }
77+ var tmpFileCountDuringUpload int
78+ defer test .MockVariableValue (& context .ParseMultipartFormMaxMemory , 1 )()
79+ defer web .RouteMock (route_web .RouterMockPointBeforeWebRoutes , func (resp http.ResponseWriter , req * http.Request ) {
80+ tmpFileCountDuringUpload = countTmpFile ()
81+ })()
82+ _ = testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusOK )
83+ assert .Equal (t , 1 , tmpFileCountDuringUpload , "the temp file should exist when uploaded size exceeds the parse form's max memory" )
84+ assert .Equal (t , 0 , countTmpFile (), "the temp file should be deleted after upload" )
85+ }
86+
87+ func testCreateAnonymousAttachment (t * testing.T ) {
5788 session := emptyTestSession (t )
5889 testCreateIssueAttachment (t , session , GetAnonymousCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusSeeOther )
5990}
6091
61- func TestCreateIssueAttachment (t * testing.T ) {
62- defer tests .PrepareTestEnv (t )()
92+ func testCreateUser2IssueAttachment (t * testing.T ) {
6393 const repoURL = "user2/repo1"
6494 session := loginUser (t , "user2" )
6595 uuid := testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), repoURL , "image.png" , testGeneratePngBytes (), http .StatusOK )
@@ -90,8 +120,7 @@ func TestCreateIssueAttachment(t *testing.T) {
90120 MakeRequest (t , req , http .StatusOK )
91121}
92122
93- func TestGetAttachment (t * testing.T ) {
94- defer tests .PrepareTestEnv (t )()
123+ func testGetAttachment (t * testing.T ) {
95124 adminSession := loginUser (t , "user1" )
96125 user2Session := loginUser (t , "user2" )
97126 user8Session := loginUser (t , "user8" )
0 commit comments