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_update_test.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. "net/url"
  10. "path/filepath"
  11. "testing"
  12. "code.gitea.io/gitea/models"
  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/gitea/modules/structs"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func getUpdateFileOptions() *api.UpdateFileOptions {
  20. content := "This is updated text"
  21. contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
  22. return &api.UpdateFileOptions{
  23. DeleteFileOptions: *getDeleteFileOptions(),
  24. Content: contentEncoded,
  25. }
  26. }
  27. func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileResponse {
  28. sha := "08bd14b2e2852529157324de9c226b3364e76136"
  29. return &api.FileResponse{
  30. Content: &api.FileContentResponse{
  31. Name: filepath.Base(treePath),
  32. Path: treePath,
  33. SHA: sha,
  34. Size: 20,
  35. URL: setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath,
  36. HTMLURL: setting.AppURL + "user2/repo1/blob/master/" + treePath,
  37. GitURL: setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha,
  38. DownloadURL: setting.AppURL + "user2/repo1/raw/branch/master/" + treePath,
  39. Type: "blob",
  40. Links: &api.FileLinksResponse{
  41. Self: setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath,
  42. GitURL: setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha,
  43. HTMLURL: setting.AppURL + "user2/repo1/blob/master/" + treePath,
  44. },
  45. },
  46. Commit: &api.FileCommitResponse{
  47. CommitMeta: api.CommitMeta{
  48. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  49. SHA: commitID,
  50. },
  51. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  52. Author: &api.CommitUser{
  53. Identity: api.Identity{
  54. Name: "Jane Doe",
  55. Email: "janedoe@example.com",
  56. },
  57. },
  58. Committer: &api.CommitUser{
  59. Identity: api.Identity{
  60. Name: "John Doe",
  61. Email: "johndoe@example.com",
  62. },
  63. },
  64. Message: "Updates README.md\n",
  65. },
  66. Verification: &api.PayloadCommitVerification{
  67. Verified: false,
  68. Reason: "unsigned",
  69. Signature: "",
  70. Payload: "",
  71. },
  72. }
  73. }
  74. func TestAPIUpdateFile(t *testing.T) {
  75. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  76. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  77. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  78. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  79. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  80. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  81. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  82. fileID := 0
  83. // Get user2's token
  84. session := loginUser(t, user2.Name)
  85. token2 := getTokenForLoggedInUser(t, session)
  86. session = emptyTestSession(t)
  87. // Get user4's token
  88. session = loginUser(t, user4.Name)
  89. token4 := getTokenForLoggedInUser(t, session)
  90. session = emptyTestSession(t)
  91. // Test updating a file in repo1 which user2 owns, try both with branch and empty branch
  92. for _, branch := range [...]string{
  93. "master", // Branch
  94. "", // Empty branch
  95. } {
  96. fileID++
  97. treePath := fmt.Sprintf("update/file%d.txt", fileID)
  98. createFile(user2, repo1, treePath)
  99. updateFileOptions := getUpdateFileOptions()
  100. updateFileOptions.BranchName = branch
  101. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  102. req := NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  103. resp := session.MakeRequest(t, req, http.StatusOK)
  104. gitRepo, _ := git.OpenRepository(repo1.RepoPath())
  105. commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
  106. expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath)
  107. var fileResponse api.FileResponse
  108. DecodeJSON(t, resp, &fileResponse)
  109. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  110. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  111. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  112. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
  113. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
  114. }
  115. // Test updating a file in a new branch
  116. updateFileOptions := getUpdateFileOptions()
  117. updateFileOptions.BranchName = repo1.DefaultBranch
  118. updateFileOptions.NewBranchName = "new_branch"
  119. fileID++
  120. treePath := fmt.Sprintf("update/file%d.txt", fileID)
  121. createFile(user2, repo1, treePath)
  122. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  123. req := NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  124. resp := session.MakeRequest(t, req, http.StatusOK)
  125. var fileResponse api.FileResponse
  126. DecodeJSON(t, resp, &fileResponse)
  127. expectedSHA := "08bd14b2e2852529157324de9c226b3364e76136"
  128. expectedHTMLURL := fmt.Sprintf(setting.AppURL+"user2/repo1/blob/new_branch/update/file%d.txt", fileID)
  129. expectedDownloadURL := fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/new_branch/update/file%d.txt", fileID)
  130. assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
  131. assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
  132. assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
  133. // Test updating a file and renaming it
  134. updateFileOptions = getUpdateFileOptions()
  135. updateFileOptions.BranchName = repo1.DefaultBranch
  136. fileID++
  137. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  138. createFile(user2, repo1, treePath)
  139. updateFileOptions.FromPath = treePath
  140. treePath = "rename/" + treePath
  141. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  142. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  143. resp = session.MakeRequest(t, req, http.StatusOK)
  144. DecodeJSON(t, resp, &fileResponse)
  145. expectedSHA = "08bd14b2e2852529157324de9c226b3364e76136"
  146. expectedHTMLURL = fmt.Sprintf(setting.AppURL+"user2/repo1/blob/master/rename/update/file%d.txt", fileID)
  147. expectedDownloadURL = fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/master/rename/update/file%d.txt", fileID)
  148. assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
  149. assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
  150. assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
  151. // Test updating a file with the wrong SHA
  152. fileID++
  153. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  154. createFile(user2, repo1, treePath)
  155. updateFileOptions = getUpdateFileOptions()
  156. correctSHA := updateFileOptions.SHA
  157. updateFileOptions.SHA = "badsha"
  158. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  159. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  160. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  161. expectedAPIError := context.APIError{
  162. Message: "sha does not match [given: " + updateFileOptions.SHA + ", expected: " + correctSHA + "]",
  163. URL: setting.API.SwaggerURL,
  164. }
  165. var apiError context.APIError
  166. DecodeJSON(t, resp, &apiError)
  167. assert.Equal(t, expectedAPIError, apiError)
  168. // Test creating a file in repo1 by user4 who does not have write access
  169. fileID++
  170. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  171. createFile(user2, repo16, treePath)
  172. updateFileOptions = getUpdateFileOptions()
  173. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  174. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  175. session.MakeRequest(t, req, http.StatusNotFound)
  176. // Tests a repo with no token given so will fail
  177. fileID++
  178. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  179. createFile(user2, repo16, treePath)
  180. updateFileOptions = getUpdateFileOptions()
  181. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  182. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  183. session.MakeRequest(t, req, http.StatusNotFound)
  184. // Test using access token for a private repo that the user of the token owns
  185. fileID++
  186. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  187. createFile(user2, repo16, treePath)
  188. updateFileOptions = getUpdateFileOptions()
  189. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  190. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  191. session.MakeRequest(t, req, http.StatusOK)
  192. // Test using org repo "user3/repo3" where user2 is a collaborator
  193. fileID++
  194. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  195. createFile(user3, repo3, treePath)
  196. updateFileOptions = getUpdateFileOptions()
  197. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  198. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  199. session.MakeRequest(t, req, http.StatusOK)
  200. // Test using org repo "user3/repo3" with no user token
  201. fileID++
  202. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  203. createFile(user3, repo3, treePath)
  204. updateFileOptions = getUpdateFileOptions()
  205. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  206. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  207. session.MakeRequest(t, req, http.StatusNotFound)
  208. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  209. fileID++
  210. treePath = fmt.Sprintf("update/file%d.txt", fileID)
  211. createFile(user2, repo1, treePath)
  212. updateFileOptions = getUpdateFileOptions()
  213. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  214. req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
  215. session.MakeRequest(t, req, http.StatusForbidden)
  216. })
  217. }