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_create_test.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. gitRepo.Close()
  134. }
  135. // Test creating a file in a new branch
  136. createFileOptions := getCreateFileOptions()
  137. createFileOptions.BranchName = repo1.DefaultBranch
  138. createFileOptions.NewBranchName = "new_branch"
  139. fileID++
  140. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  141. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  142. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  143. resp := session.MakeRequest(t, req, http.StatusCreated)
  144. var fileResponse api.FileResponse
  145. DecodeJSON(t, resp, &fileResponse)
  146. expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  147. expectedHTMLURL := fmt.Sprintf(setting.AppURL+"user2/repo1/src/branch/new_branch/new/file%d.txt", fileID)
  148. expectedDownloadURL := fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/new_branch/new/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. assert.EqualValues(t, createFileOptions.Message+"\n", fileResponse.Commit.Message)
  153. // Test creating a file without a message
  154. createFileOptions = getCreateFileOptions()
  155. createFileOptions.Message = ""
  156. fileID++
  157. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  158. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  159. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  160. resp = session.MakeRequest(t, req, http.StatusCreated)
  161. DecodeJSON(t, resp, &fileResponse)
  162. expectedMessage := "Add '" + treePath + "'\n"
  163. assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
  164. // Test trying to create a file that already exists, should fail
  165. createFileOptions = getCreateFileOptions()
  166. treePath = "README.md"
  167. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  168. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  169. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  170. expectedAPIError := context.APIError{
  171. Message: "repository file already exists [path: " + treePath + "]",
  172. URL: setting.API.SwaggerURL,
  173. }
  174. var apiError context.APIError
  175. DecodeJSON(t, resp, &apiError)
  176. assert.Equal(t, expectedAPIError, apiError)
  177. // Test creating a file in repo1 by user4 who does not have write access
  178. createFileOptions = getCreateFileOptions()
  179. fileID++
  180. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  181. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  182. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  183. session.MakeRequest(t, req, http.StatusNotFound)
  184. // Tests a repo with no token given so will fail
  185. createFileOptions = getCreateFileOptions()
  186. fileID++
  187. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  188. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  189. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  190. session.MakeRequest(t, req, http.StatusNotFound)
  191. // Test using access token for a private repo that the user of the token owns
  192. createFileOptions = getCreateFileOptions()
  193. fileID++
  194. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  195. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  196. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  197. session.MakeRequest(t, req, http.StatusCreated)
  198. // Test using org repo "user3/repo3" where user2 is a collaborator
  199. createFileOptions = getCreateFileOptions()
  200. fileID++
  201. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  202. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  203. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  204. session.MakeRequest(t, req, http.StatusCreated)
  205. // Test using org repo "user3/repo3" with no user token
  206. createFileOptions = getCreateFileOptions()
  207. fileID++
  208. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  209. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  210. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  211. session.MakeRequest(t, req, http.StatusNotFound)
  212. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  213. createFileOptions = getCreateFileOptions()
  214. fileID++
  215. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  216. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  217. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  218. session.MakeRequest(t, req, http.StatusForbidden)
  219. })
  220. }