@ -49,14 +49,14 @@ func getCreateFileOptions() api.CreateFileOptions {
}
}
}
}
func getExpectedFileResponseForCreate ( commitID , treePath string ) * api . FileResponse {
func getExpectedFileResponseForCreate ( repoFullName , commitID , treePath string ) * api . FileResponse {
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
encoding := "base64"
encoding := "base64"
content := "VGhpcyBpcyBuZXcgdGV4dA=="
content := "VGhpcyBpcyBuZXcgdGV4dA=="
selfURL := setting . AppURL + "api/v1/repos/user2/repo1 /contents/" + treePath + "?ref=master"
selfURL := setting . AppURL + "api/v1/repos/" + repoFullName + " /contents/" + treePath + "?ref=master"
htmlURL := setting . AppURL + "user2/repo1 /src/branch/master/" + treePath
htmlURL := setting . AppURL + repoFullName + "/src/branch/master/" + treePath
gitURL := setting . AppURL + "api/v1/repos/user2/repo1 /git/blobs/" + sha
gitURL := setting . AppURL + "api/v1/repos/" + repoFullName + " /git/blobs/" + sha
downloadURL := setting . AppURL + "user2/repo1 /raw/branch/master/" + treePath
downloadURL := setting . AppURL + repoFullName + "/raw/branch/master/" + treePath
return & api . FileResponse {
return & api . FileResponse {
Content : & api . ContentsResponse {
Content : & api . ContentsResponse {
Name : filepath . Base ( treePath ) ,
Name : filepath . Base ( treePath ) ,
@ -78,10 +78,10 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
} ,
} ,
Commit : & api . FileCommitResponse {
Commit : & api . FileCommitResponse {
CommitMeta : api . CommitMeta {
CommitMeta : api . CommitMeta {
URL : setting . AppURL + "api/v1/repos/user2/repo1 /git/commits/" + commitID ,
URL : setting . AppURL + "api/v1/repos/" + repoFullName + " /git/commits/" + commitID ,
SHA : commitID ,
SHA : commitID ,
} ,
} ,
HTMLURL : setting . AppURL + "user2/repo1 /commit/" + commitID ,
HTMLURL : setting . AppURL + repoFullName + "/commit/" + commitID ,
Author : & api . CommitUser {
Author : & api . CommitUser {
Identity : api . Identity {
Identity : api . Identity {
Name : "Anne Doe" ,
Name : "Anne Doe" ,
@ -169,7 +169,7 @@ func TestAPICreateFile(t *testing.T) {
resp := session . MakeRequest ( t , req , http . StatusCreated )
resp := session . MakeRequest ( t , req , http . StatusCreated )
gitRepo , _ := git . OpenRepository ( repo1 . RepoPath ( ) )
gitRepo , _ := git . OpenRepository ( repo1 . RepoPath ( ) )
commitID , _ := gitRepo . GetBranchCommitID ( createFileOptions . NewBranchName )
commitID , _ := gitRepo . GetBranchCommitID ( createFileOptions . NewBranchName )
expectedFileResponse := getExpectedFileResponseForCreate ( commitID , treePath )
expectedFileResponse := getExpectedFileResponseForCreate ( "user2/repo1" , commitID , treePath )
var fileResponse api . FileResponse
var fileResponse api . FileResponse
DecodeJSON ( t , resp , & fileResponse )
DecodeJSON ( t , resp , & fileResponse )
assert . EqualValues ( t , expectedFileResponse . Content , fileResponse . Content )
assert . EqualValues ( t , expectedFileResponse . Content , fileResponse . Content )
@ -276,5 +276,29 @@ func TestAPICreateFile(t *testing.T) {
url = fmt . Sprintf ( "/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 . Name , repo1 . Name , treePath , token4 )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 . Name , repo1 . Name , treePath , token4 )
req = NewRequestWithJSON ( t , "POST" , url , & createFileOptions )
req = NewRequestWithJSON ( t , "POST" , url , & createFileOptions )
session . MakeRequest ( t , req , http . StatusForbidden )
session . MakeRequest ( t , req , http . StatusForbidden )
// Test creating a file in an empty repository
doAPICreateRepository ( NewAPITestContext ( t , "user2" , "empty-repo" ) , true ) ( t )
createFileOptions = getCreateFileOptions ( )
fileID ++
treePath = fmt . Sprintf ( "new/file%d.txt" , fileID )
url = fmt . Sprintf ( "/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 . Name , "empty-repo" , treePath , token2 )
req = NewRequestWithJSON ( t , "POST" , url , & createFileOptions )
resp = session . MakeRequest ( t , req , http . StatusCreated )
emptyRepo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { OwnerName : "user2" , Name : "empty-repo" } ) . ( * repo_model . Repository ) // public repo
gitRepo , _ := git . OpenRepository ( emptyRepo . RepoPath ( ) )
commitID , _ := gitRepo . GetBranchCommitID ( createFileOptions . NewBranchName )
expectedFileResponse := getExpectedFileResponseForCreate ( "user2/empty-repo" , commitID , treePath )
DecodeJSON ( t , resp , & fileResponse )
assert . EqualValues ( t , expectedFileResponse . Content , fileResponse . Content )
assert . EqualValues ( t , expectedFileResponse . Commit . SHA , fileResponse . Commit . SHA )
assert . EqualValues ( t , expectedFileResponse . Commit . HTMLURL , fileResponse . Commit . HTMLURL )
assert . EqualValues ( t , expectedFileResponse . Commit . Author . Email , fileResponse . Commit . Author . Email )
assert . EqualValues ( t , expectedFileResponse . Commit . Author . Name , fileResponse . Commit . Author . Name )
assert . EqualValues ( t , expectedFileResponse . Commit . Author . Date , fileResponse . Commit . Author . Date )
assert . EqualValues ( t , expectedFileResponse . Commit . Committer . Email , fileResponse . Commit . Committer . Email )
assert . EqualValues ( t , expectedFileResponse . Commit . Committer . Name , fileResponse . Commit . Committer . Name )
assert . EqualValues ( t , expectedFileResponse . Commit . Committer . Date , fileResponse . Commit . Committer . Date )
gitRepo . Close ( )
} )
} )
}
}