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.

237 lines
9.6 KiB

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