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

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