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

api_repo_file_update_test.go 10 kB

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