@@ -49,14 +49,14 @@ func getCreateFileOptions() api.CreateFileOptions {
4949 }
5050}
5151
52- func getExpectedFileResponseForCreate (commitID , treePath string ) * api.FileResponse {
52+ func getExpectedFileResponseForCreate (repoFullName , commitID , treePath string ) * api.FileResponse {
5353 sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5454 encoding := "base64"
5555 content := "VGhpcyBpcyBuZXcgdGV4dA=="
56- selfURL := setting .AppURL + "api/v1/repos/user2/repo1 /contents/" + treePath + "?ref=master"
57- htmlURL := setting .AppURL + "user2/repo1 /src/branch/master/" + treePath
58- gitURL := setting .AppURL + "api/v1/repos/user2/repo1 /git/blobs/" + sha
59- downloadURL := setting .AppURL + "user2/repo1 /raw/branch/master/" + treePath
56+ selfURL := setting .AppURL + "api/v1/repos/" + repoFullName + " /contents/" + treePath + "?ref=master"
57+ htmlURL := setting .AppURL + repoFullName + " /src/branch/master/" + treePath
58+ gitURL := setting .AppURL + "api/v1/repos/" + repoFullName + " /git/blobs/" + sha
59+ downloadURL := setting .AppURL + repoFullName + " /raw/branch/master/" + treePath
6060 return & api.FileResponse {
6161 Content : & api.ContentsResponse {
6262 Name : filepath .Base (treePath ),
@@ -78,10 +78,10 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
7878 },
7979 Commit : & api.FileCommitResponse {
8080 CommitMeta : api.CommitMeta {
81- URL : setting .AppURL + "api/v1/repos/user2/repo1 /git/commits/" + commitID ,
81+ URL : setting .AppURL + "api/v1/repos/" + repoFullName + " /git/commits/" + commitID ,
8282 SHA : commitID ,
8383 },
84- HTMLURL : setting .AppURL + "user2/repo1 /commit/" + commitID ,
84+ HTMLURL : setting .AppURL + repoFullName + " /commit/" + commitID ,
8585 Author : & api.CommitUser {
8686 Identity : api.Identity {
8787 Name : "Anne Doe" ,
@@ -169,7 +169,7 @@ func TestAPICreateFile(t *testing.T) {
169169 resp := session .MakeRequest (t , req , http .StatusCreated )
170170 gitRepo , _ := git .OpenRepository (repo1 .RepoPath ())
171171 commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
172- expectedFileResponse := getExpectedFileResponseForCreate (commitID , treePath )
172+ expectedFileResponse := getExpectedFileResponseForCreate ("user2/repo1" , commitID , treePath )
173173 var fileResponse api.FileResponse
174174 DecodeJSON (t , resp , & fileResponse )
175175 assert .EqualValues (t , expectedFileResponse .Content , fileResponse .Content )
@@ -276,5 +276,29 @@ func TestAPICreateFile(t *testing.T) {
276276 url = fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 .Name , repo1 .Name , treePath , token4 )
277277 req = NewRequestWithJSON (t , "POST" , url , & createFileOptions )
278278 session .MakeRequest (t , req , http .StatusForbidden )
279+
280+ // Test creating a file in an empty repository
281+ doAPICreateRepository (NewAPITestContext (t , "user2" , "empty-repo" ), true )(t )
282+ createFileOptions = getCreateFileOptions ()
283+ fileID ++
284+ treePath = fmt .Sprintf ("new/file%d.txt" , fileID )
285+ url = fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 .Name , "empty-repo" , treePath , token2 )
286+ req = NewRequestWithJSON (t , "POST" , url , & createFileOptions )
287+ resp = session .MakeRequest (t , req , http .StatusCreated )
288+ emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }).(* repo_model.Repository ) // public repo
289+ gitRepo , _ := git .OpenRepository (emptyRepo .RepoPath ())
290+ commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
291+ expectedFileResponse := getExpectedFileResponseForCreate ("user2/empty-repo" , commitID , treePath )
292+ DecodeJSON (t , resp , & fileResponse )
293+ assert .EqualValues (t , expectedFileResponse .Content , fileResponse .Content )
294+ assert .EqualValues (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
295+ assert .EqualValues (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
296+ assert .EqualValues (t , expectedFileResponse .Commit .Author .Email , fileResponse .Commit .Author .Email )
297+ assert .EqualValues (t , expectedFileResponse .Commit .Author .Name , fileResponse .Commit .Author .Name )
298+ assert .EqualValues (t , expectedFileResponse .Commit .Author .Date , fileResponse .Commit .Author .Date )
299+ assert .EqualValues (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
300+ assert .EqualValues (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
301+ assert .EqualValues (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
302+ gitRepo .Close ()
279303 })
280304}
0 commit comments