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_delete_test.go 6.5KB

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