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 11 kB

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