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_create_test.go 9.1 kB

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