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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. "time"
  13. "code.gitea.io/gitea/models"
  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: "Making this new file new/file.txt",
  28. Author: api.Identity{
  29. Name: "Anne Doe",
  30. Email: "annedoe@example.com",
  31. },
  32. Committer: api.Identity{
  33. Name: "John Doe",
  34. Email: "johndoe@example.com",
  35. },
  36. Dates: api.CommitDateOptions{
  37. Author: time.Unix(946684810, 0),
  38. Committer: time.Unix(978307190, 0),
  39. },
  40. },
  41. Content: contentEncoded,
  42. }
  43. }
  44. func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileResponse {
  45. sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  46. encoding := "base64"
  47. content := "VGhpcyBpcyBuZXcgdGV4dA=="
  48. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  49. htmlURL := setting.AppURL + "user2/repo1/src/branch/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. return &api.FileResponse{
  53. Content: &api.ContentsResponse{
  54. Name: filepath.Base(treePath),
  55. Path: treePath,
  56. SHA: sha,
  57. Size: 16,
  58. Type: "file",
  59. Encoding: &encoding,
  60. Content: &content,
  61. URL: &selfURL,
  62. HTMLURL: &htmlURL,
  63. GitURL: &gitURL,
  64. DownloadURL: &downloadURL,
  65. Links: &api.FileLinksResponse{
  66. Self: &selfURL,
  67. GitURL: &gitURL,
  68. HTMLURL: &htmlURL,
  69. },
  70. },
  71. Commit: &api.FileCommitResponse{
  72. CommitMeta: api.CommitMeta{
  73. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  74. SHA: commitID,
  75. },
  76. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  77. Author: &api.CommitUser{
  78. Identity: api.Identity{
  79. Name: "Anne Doe",
  80. Email: "annedoe@example.com",
  81. },
  82. Date: "2000-01-01T00:00:10Z",
  83. },
  84. Committer: &api.CommitUser{
  85. Identity: api.Identity{
  86. Name: "John Doe",
  87. Email: "johndoe@example.com",
  88. },
  89. Date: "2000-12-31T23:59:50Z",
  90. },
  91. Message: "Updates README.md\n",
  92. },
  93. Verification: &api.PayloadCommitVerification{
  94. Verified: false,
  95. Reason: "gpg.error.not_signed_commit",
  96. Signature: "",
  97. Payload: "",
  98. },
  99. }
  100. }
  101. func BenchmarkAPICreateFileSmall(b *testing.B) {
  102. onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
  103. b := t.(*testing.B)
  104. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  105. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  106. for n := 0; n < b.N; n++ {
  107. treePath := fmt.Sprintf("update/file%d.txt", n)
  108. createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
  109. }
  110. })
  111. }
  112. func BenchmarkAPICreateFileMedium(b *testing.B) {
  113. data := make([]byte, 10*1024*1024)
  114. onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
  115. b := t.(*testing.B)
  116. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  117. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  118. b.ResetTimer()
  119. for n := 0; n < b.N; n++ {
  120. treePath := fmt.Sprintf("update/file%d.txt", n)
  121. copy(data, treePath)
  122. createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
  123. }
  124. })
  125. }
  126. func TestAPICreateFile(t *testing.T) {
  127. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  128. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  129. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  130. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  131. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  132. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  133. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  134. fileID := 0
  135. // Get user2's token
  136. session := loginUser(t, user2.Name)
  137. token2 := getTokenForLoggedInUser(t, session)
  138. session = emptyTestSession(t)
  139. // Get user4's token
  140. session = loginUser(t, user4.Name)
  141. token4 := getTokenForLoggedInUser(t, session)
  142. session = emptyTestSession(t)
  143. // Test creating a file in repo1 which user2 owns, try both with branch and empty branch
  144. for _, branch := range [...]string{
  145. "master", // Branch
  146. "", // Empty branch
  147. } {
  148. createFileOptions := getCreateFileOptions()
  149. createFileOptions.BranchName = branch
  150. fileID++
  151. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  152. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  153. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  154. resp := session.MakeRequest(t, req, http.StatusCreated)
  155. gitRepo, _ := git.OpenRepository(repo1.RepoPath())
  156. commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
  157. expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath)
  158. var fileResponse api.FileResponse
  159. DecodeJSON(t, resp, &fileResponse)
  160. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  161. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  162. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  163. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
  164. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
  165. assert.EqualValues(t, expectedFileResponse.Commit.Author.Date, fileResponse.Commit.Author.Date)
  166. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
  167. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
  168. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
  169. gitRepo.Close()
  170. }
  171. // Test creating a file in a new branch
  172. createFileOptions := getCreateFileOptions()
  173. createFileOptions.BranchName = repo1.DefaultBranch
  174. createFileOptions.NewBranchName = "new_branch"
  175. fileID++
  176. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  177. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  178. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  179. resp := session.MakeRequest(t, req, http.StatusCreated)
  180. var fileResponse api.FileResponse
  181. DecodeJSON(t, resp, &fileResponse)
  182. expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  183. expectedHTMLURL := fmt.Sprintf(setting.AppURL+"user2/repo1/src/branch/new_branch/new/file%d.txt", fileID)
  184. expectedDownloadURL := fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
  185. assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
  186. assert.EqualValues(t, expectedHTMLURL, *fileResponse.Content.HTMLURL)
  187. assert.EqualValues(t, expectedDownloadURL, *fileResponse.Content.DownloadURL)
  188. assert.EqualValues(t, createFileOptions.Message+"\n", fileResponse.Commit.Message)
  189. // Test creating a file without a message
  190. createFileOptions = getCreateFileOptions()
  191. createFileOptions.Message = ""
  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, repo1.Name, treePath, token2)
  195. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  196. resp = session.MakeRequest(t, req, http.StatusCreated)
  197. DecodeJSON(t, resp, &fileResponse)
  198. expectedMessage := "Add '" + treePath + "'\n"
  199. assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
  200. // Test trying to create a file that already exists, should fail
  201. createFileOptions = getCreateFileOptions()
  202. treePath = "README.md"
  203. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  204. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  205. resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  206. expectedAPIError := context.APIError{
  207. Message: "repository file already exists [path: " + treePath + "]",
  208. URL: setting.API.SwaggerURL,
  209. }
  210. var apiError context.APIError
  211. DecodeJSON(t, resp, &apiError)
  212. assert.Equal(t, expectedAPIError, apiError)
  213. // Test creating a file in repo1 by user4 who does not have write access
  214. createFileOptions = getCreateFileOptions()
  215. fileID++
  216. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  217. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  218. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  219. session.MakeRequest(t, req, http.StatusNotFound)
  220. // Tests a repo with no token given so will fail
  221. createFileOptions = getCreateFileOptions()
  222. fileID++
  223. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  224. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  225. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  226. session.MakeRequest(t, req, http.StatusNotFound)
  227. // Test using access token for a private repo that the user of the token owns
  228. createFileOptions = getCreateFileOptions()
  229. fileID++
  230. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  231. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  232. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  233. session.MakeRequest(t, req, http.StatusCreated)
  234. // Test using org repo "user3/repo3" where user2 is a collaborator
  235. createFileOptions = getCreateFileOptions()
  236. fileID++
  237. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  238. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  239. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  240. session.MakeRequest(t, req, http.StatusCreated)
  241. // Test using org repo "user3/repo3" with no user token
  242. createFileOptions = getCreateFileOptions()
  243. fileID++
  244. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  245. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  246. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  247. session.MakeRequest(t, req, http.StatusNotFound)
  248. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  249. createFileOptions = getCreateFileOptions()
  250. fileID++
  251. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  252. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  253. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  254. session.MakeRequest(t, req, http.StatusForbidden)
  255. })
  256. }