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.

179 lines
7.3 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. "fmt"
  7. "net/http"
  8. "net/url"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func getDeleteFileOptions() *api.DeleteFileOptions {
  17. return &api.DeleteFileOptions{
  18. FileOptions: api.FileOptions{
  19. BranchName: "master",
  20. NewBranchName: "master",
  21. Message: "Removing the file new/file.txt",
  22. Author: api.Identity{
  23. Name: "John Doe",
  24. Email: "johndoe@example.com",
  25. },
  26. Committer: api.Identity{
  27. Name: "Jane Doe",
  28. Email: "janedoe@example.com",
  29. },
  30. },
  31. SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
  32. }
  33. }
  34. func TestAPIDeleteFile(t *testing.T) {
  35. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  36. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  37. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  38. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  39. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  40. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  41. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  42. fileID := 0
  43. // Get user2's token
  44. session := loginUser(t, user2.Name)
  45. token2 := getTokenForLoggedInUser(t, session)
  46. session = emptyTestSession(t)
  47. // Get user4's token
  48. session = loginUser(t, user4.Name)
  49. token4 := getTokenForLoggedInUser(t, session)
  50. session = emptyTestSession(t)
  51. // Test deleting a file in repo1 which user2 owns, try both with branch and empty branch
  52. for _, branch := range [...]string{
  53. "master", // Branch
  54. "", // Empty branch
  55. } {
  56. fileID++
  57. treePath := fmt.Sprintf("delete/file%d.txt", fileID)
  58. createFile(user2, repo1, treePath)
  59. deleteFileOptions := getDeleteFileOptions()
  60. deleteFileOptions.BranchName = branch
  61. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  62. req := NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  63. resp := session.MakeRequest(t, req, http.StatusOK)
  64. var fileResponse api.FileResponse
  65. DecodeJSON(t, resp, &fileResponse)
  66. assert.NotNil(t, fileResponse)
  67. assert.Nil(t, fileResponse.Content)
  68. }
  69. // Test deleting file and making the delete in a new branch
  70. fileID++
  71. treePath := fmt.Sprintf("delete/file%d.txt", fileID)
  72. createFile(user2, repo1, treePath)
  73. deleteFileOptions := getDeleteFileOptions()
  74. deleteFileOptions.BranchName = repo1.DefaultBranch
  75. deleteFileOptions.NewBranchName = "new_branch"
  76. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  77. req := NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  78. resp := session.MakeRequest(t, req, http.StatusOK)
  79. var fileResponse api.FileResponse
  80. DecodeJSON(t, resp, &fileResponse)
  81. assert.NotNil(t, fileResponse)
  82. assert.Nil(t, fileResponse.Content)
  83. assert.EqualValues(t, deleteFileOptions.Message+"\n", fileResponse.Commit.Message)
  84. // Test deleting file without a message
  85. fileID++
  86. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  87. createFile(user2, repo1, treePath)
  88. deleteFileOptions = getDeleteFileOptions()
  89. deleteFileOptions.Message = ""
  90. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  91. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  92. resp = session.MakeRequest(t, req, http.StatusOK)
  93. DecodeJSON(t, resp, &fileResponse)
  94. expectedMessage := "Delete '" + treePath + "'\n"
  95. assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
  96. // Test deleting a file with the wrong SHA
  97. fileID++
  98. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  99. createFile(user2, repo1, treePath)
  100. deleteFileOptions = getDeleteFileOptions()
  101. correctSHA := deleteFileOptions.SHA
  102. deleteFileOptions.SHA = "badsha"
  103. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  104. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  105. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  106. expectedAPIError := context.APIError{
  107. Message: "sha does not match [given: " + deleteFileOptions.SHA + ", expected: " + correctSHA + "]",
  108. URL: setting.API.SwaggerURL,
  109. }
  110. var apiError context.APIError
  111. DecodeJSON(t, resp, &apiError)
  112. assert.Equal(t, expectedAPIError, apiError)
  113. // Test creating a file in repo16 by user4 who does not have write access
  114. fileID++
  115. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  116. createFile(user2, repo16, treePath)
  117. deleteFileOptions = getDeleteFileOptions()
  118. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  119. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  120. session.MakeRequest(t, req, http.StatusNotFound)
  121. // Tests a repo with no token given so will fail
  122. fileID++
  123. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  124. createFile(user2, repo16, treePath)
  125. deleteFileOptions = getDeleteFileOptions()
  126. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  127. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  128. session.MakeRequest(t, req, http.StatusNotFound)
  129. // Test using access token for a private repo that the user of the token owns
  130. fileID++
  131. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  132. createFile(user2, repo16, treePath)
  133. deleteFileOptions = getDeleteFileOptions()
  134. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  135. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  136. session.MakeRequest(t, req, http.StatusOK)
  137. // Test using org repo "user3/repo3" where user2 is a collaborator
  138. fileID++
  139. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  140. createFile(user3, repo3, treePath)
  141. deleteFileOptions = getDeleteFileOptions()
  142. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  143. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  144. session.MakeRequest(t, req, http.StatusOK)
  145. // Test using org repo "user3/repo3" with no user token
  146. fileID++
  147. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  148. createFile(user3, repo3, treePath)
  149. deleteFileOptions = getDeleteFileOptions()
  150. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  151. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  152. session.MakeRequest(t, req, http.StatusNotFound)
  153. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  154. fileID++
  155. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  156. createFile(user2, repo1, treePath)
  157. deleteFileOptions = getDeleteFileOptions()
  158. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  159. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  160. session.MakeRequest(t, req, http.StatusForbidden)
  161. })
  162. }