@@ -5,17 +5,21 @@ package integration
55
66import (
77 "bytes"
8+ "encoding/base64"
9+ "fmt"
810 "io"
911 "mime/multipart"
1012 "net/http"
1113 "testing"
1214
15+ auth_model "code.gitea.io/gitea/models/auth"
1316 "code.gitea.io/gitea/models/db"
1417 repo_model "code.gitea.io/gitea/models/repo"
1518 "code.gitea.io/gitea/models/unittest"
1619 user_model "code.gitea.io/gitea/models/user"
1720 "code.gitea.io/gitea/modules/json"
1821 "code.gitea.io/gitea/modules/setting"
22+ api "code.gitea.io/gitea/modules/structs"
1923 "code.gitea.io/gitea/modules/test"
2024 "code.gitea.io/gitea/tests"
2125
@@ -105,3 +109,32 @@ func TestEmptyRepoUploadFile(t *testing.T) {
105109 resp = session .MakeRequest (t , req , http .StatusOK )
106110 assert .Contains (t , resp .Body .String (), "uploaded-file.txt" )
107111}
112+
113+ func TestEmptyRepoAddFileByAPI (t * testing.T ) {
114+ defer tests .PrepareTestEnv (t )()
115+
116+ err := user_model .UpdateUserCols (db .DefaultContext , & user_model.User {ID : 30 , ProhibitLogin : false }, "prohibit_login" )
117+ assert .NoError (t , err )
118+
119+ session := loginUser (t , "user30" )
120+ token := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeRepo )
121+
122+ url := fmt .Sprintf ("/api/v1/repos/user30/empty/contents/new-file.txt?token=%s" , token )
123+ req := NewRequestWithJSON (t , "POST" , url , & api.CreateFileOptions {
124+ FileOptions : api.FileOptions {
125+ NewBranchName : "new_branch" ,
126+ Message : "init" ,
127+ },
128+ Content : base64 .StdEncoding .EncodeToString ([]byte ("newly-added-api-file" )),
129+ })
130+
131+ resp := MakeRequest (t , req , http .StatusCreated )
132+ var fileResponse api.FileResponse
133+ DecodeJSON (t , resp , & fileResponse )
134+ expectedHTMLURL := setting .AppURL + "user30/empty/src/branch/new_branch/new-file.txt"
135+ assert .EqualValues (t , expectedHTMLURL , * fileResponse .Content .HTMLURL )
136+
137+ req = NewRequest (t , "GET" , "/user30/empty/src/branch/new_branch/new-file.txt" )
138+ resp = session .MakeRequest (t , req , http .StatusOK )
139+ assert .Contains (t , resp .Body .String (), "newly-added-api-file" )
140+ }
0 commit comments