@@ -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,36 @@ 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+ // TODO: GOLANG-HTTP-TMPDIR: Golang saves the uploaded file to os.TempDir() when it exceeds the max memory limit.
72+ countTmpFile := func () int {
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+ defer test .MockVariableValue (& context .ParseMultipartFormMaxMemory , 1 )()
78+ defer web .RouteMock (route_web .RouterMockPointBeforeWebRoutes , func (resp http.ResponseWriter , req * http.Request ) {
79+ assert .Equal (t , 1 , countTmpFile (), "the temp file should exist when uploaded size exceeds the parse form's max memory" )
80+ })()
81+ _ = testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusOK )
82+ assert .Equal (t , 0 , countTmpFile (), "the temp file should be deleted after upload" )
83+ }
84+
85+ func testCreateAnonymousAttachment (t * testing.T ) {
5786 session := emptyTestSession (t )
5887 testCreateIssueAttachment (t , session , GetAnonymousCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusSeeOther )
5988}
6089
61- func TestCreateIssueAttachment (t * testing.T ) {
62- defer tests .PrepareTestEnv (t )()
90+ func testCreateUser2IssueAttachment (t * testing.T ) {
6391 const repoURL = "user2/repo1"
6492 session := loginUser (t , "user2" )
6593 uuid := testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), repoURL , "image.png" , testGeneratePngBytes (), http .StatusOK )
@@ -90,8 +118,7 @@ func TestCreateIssueAttachment(t *testing.T) {
90118 MakeRequest (t , req , http .StatusOK )
91119}
92120
93- func TestGetAttachment (t * testing.T ) {
94- defer tests .PrepareTestEnv (t )()
121+ func testGetAttachment (t * testing.T ) {
95122 adminSession := loginUser (t , "user1" )
96123 user2Session := loginUser (t , "user2" )
97124 user8Session := loginUser (t , "user8" )
0 commit comments