@@ -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+ 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+ files , err := fs .Glob (os .DirFS (os .Getenv ("TMPDIR" )), "multipart-*" )
75+ require .NoError (t , err )
76+ require .Len (t , files , 1 )
77+
78+ uploadTempFile = os .Getenv ("TMPDIR" ) + "/" + files [0 ]
79+ _ , err = os .Stat (uploadTempFile )
80+ assert .NoError (t , err , "the temp file should exist during upload handling because size exceeds the parse form's max memory" )
81+ })()
82+ _ = testCreateIssueAttachment (t , session , GetUserCSRFToken (t , session ), "user2/repo1" , "image.png" , testGeneratePngBytes (), http .StatusOK )
83+ _ , err := os .Stat (uploadTempFile )
84+ assert .ErrorIs (t , err , os .ErrNotExist , "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