@@ -5,17 +5,21 @@ package integration
5
5
6
6
import (
7
7
"bytes"
8
+ "encoding/base64"
9
+ "fmt"
8
10
"io"
9
11
"mime/multipart"
10
12
"net/http"
11
13
"testing"
12
14
15
+ auth_model "code.gitea.io/gitea/models/auth"
13
16
"code.gitea.io/gitea/models/db"
14
17
repo_model "code.gitea.io/gitea/models/repo"
15
18
"code.gitea.io/gitea/models/unittest"
16
19
user_model "code.gitea.io/gitea/models/user"
17
20
"code.gitea.io/gitea/modules/json"
18
21
"code.gitea.io/gitea/modules/setting"
22
+ api "code.gitea.io/gitea/modules/structs"
19
23
"code.gitea.io/gitea/modules/test"
20
24
"code.gitea.io/gitea/tests"
21
25
@@ -105,3 +109,32 @@ func TestEmptyRepoUploadFile(t *testing.T) {
105
109
resp = session .MakeRequest (t , req , http .StatusOK )
106
110
assert .Contains (t , resp .Body .String (), "uploaded-file.txt" )
107
111
}
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