You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_repo_file_create_test.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "net/http"
  9. "path/filepath"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/sdk/gitea"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func getCreateFileOptions() api.CreateFileOptions {
  20. content := "This is new text"
  21. contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
  22. return api.CreateFileOptions{
  23. FileOptions: api.FileOptions{
  24. BranchName: "master",
  25. NewBranchName: "master",
  26. Message: "Creates new/file.txt",
  27. Author: api.Identity{
  28. Name: "John Doe",
  29. Email: "johndoe@example.com",
  30. },
  31. Committer: api.Identity{
  32. Name: "Jane Doe",
  33. Email: "janedoe@example.com",
  34. },
  35. },
  36. Content: contentEncoded,
  37. }
  38. }
  39. func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileResponse {
  40. sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  41. return &api.FileResponse{
  42. Content: &api.FileContentResponse{
  43. Name: filepath.Base(treePath),
  44. Path: treePath,
  45. SHA: sha,
  46. Size: 16,
  47. URL: setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath,
  48. HTMLURL: setting.AppURL + "user2/repo1/blob/master/" + treePath,
  49. GitURL: setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha,
  50. DownloadURL: setting.AppURL + "user2/repo1/raw/branch/master/" + treePath,
  51. Type: "blob",
  52. Links: &api.FileLinksResponse{
  53. Self: setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath,
  54. GitURL: setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha,
  55. HTMLURL: setting.AppURL + "user2/repo1/blob/master/" + treePath,
  56. },
  57. },
  58. Commit: &api.FileCommitResponse{
  59. CommitMeta: api.CommitMeta{
  60. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  61. SHA: commitID,
  62. },
  63. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  64. Author: &api.CommitUser{
  65. Identity: api.Identity{
  66. Name: "Jane Doe",
  67. Email: "janedoe@example.com",
  68. },
  69. },
  70. Committer: &api.CommitUser{
  71. Identity: api.Identity{
  72. Name: "John Doe",
  73. Email: "johndoe@example.com",
  74. },
  75. },
  76. Message: "Updates README.md\n",
  77. },
  78. Verification: &api.PayloadCommitVerification{
  79. Verified: false,
  80. Reason: "unsigned",
  81. Signature: "",
  82. Payload: "",
  83. },
  84. }
  85. }
  86. func TestAPICreateFile(t *testing.T) {
  87. prepareTestEnv(t)
  88. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  89. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  90. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  91. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  92. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  93. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  94. fileID := 0
  95. // Get user2's token
  96. session := loginUser(t, user2.Name)
  97. token2 := getTokenForLoggedInUser(t, session)
  98. session = emptyTestSession(t)
  99. // Get user4's token
  100. session = loginUser(t, user4.Name)
  101. token4 := getTokenForLoggedInUser(t, session)
  102. session = emptyTestSession(t)
  103. // Test creating a file in repo1 which user2 owns, try both with branch and empty branch
  104. for _, branch := range [...]string{
  105. "master", // Branch
  106. "", // Empty branch
  107. } {
  108. createFileOptions := getCreateFileOptions()
  109. createFileOptions.BranchName = branch
  110. fileID++
  111. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  112. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  113. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  114. resp := session.MakeRequest(t, req, http.StatusCreated)
  115. gitRepo, _ := git.OpenRepository(repo1.RepoPath())
  116. commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
  117. expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath)
  118. var fileResponse api.FileResponse
  119. DecodeJSON(t, resp, &fileResponse)
  120. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  121. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  122. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  123. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
  124. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
  125. }
  126. // Test creating a file in a new branch
  127. createFileOptions := getCreateFileOptions()
  128. createFileOptions.BranchName = repo1.DefaultBranch
  129. createFileOptions.NewBranchName = "new_branch"
  130. fileID++
  131. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  132. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  133. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  134. resp := session.MakeRequest(t, req, http.StatusCreated)
  135. var fileResponse api.FileResponse
  136. DecodeJSON(t, resp, &fileResponse)
  137. expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  138. expectedHTMLURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/blob/new_branch/new/file%d.txt", fileID)
  139. expectedDownloadURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
  140. assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
  141. assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
  142. assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
  143. // Test trying to create a file that already exists, should fail
  144. createFileOptions = getCreateFileOptions()
  145. treePath = "README.md"
  146. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  147. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  148. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  149. expectedAPIError := context.APIError{
  150. Message: "repository file already exists [path: " + treePath + "]",
  151. URL: base.DocURL,
  152. }
  153. var apiError context.APIError
  154. DecodeJSON(t, resp, &apiError)
  155. assert.Equal(t, expectedAPIError, apiError)
  156. // Test creating a file in repo1 by user4 who does not have write access
  157. createFileOptions = getCreateFileOptions()
  158. fileID++
  159. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  160. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  161. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  162. session.MakeRequest(t, req, http.StatusNotFound)
  163. // Tests a repo with no token given so will fail
  164. createFileOptions = getCreateFileOptions()
  165. fileID++
  166. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  167. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  168. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  169. session.MakeRequest(t, req, http.StatusNotFound)
  170. // Test using access token for a private repo that the user of the token owns
  171. createFileOptions = getCreateFileOptions()
  172. fileID++
  173. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  174. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  175. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  176. session.MakeRequest(t, req, http.StatusCreated)
  177. // Test using org repo "user3/repo3" where user2 is a collaborator
  178. createFileOptions = getCreateFileOptions()
  179. fileID++
  180. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  181. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  182. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  183. session.MakeRequest(t, req, http.StatusCreated)
  184. // Test using org repo "user3/repo3" with no user token
  185. createFileOptions = getCreateFileOptions()
  186. fileID++
  187. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  188. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  189. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  190. session.MakeRequest(t, req, http.StatusNotFound)
  191. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  192. createFileOptions = getCreateFileOptions()
  193. fileID++
  194. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  195. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  196. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  197. session.MakeRequest(t, req, http.StatusForbidden)
  198. }