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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. auth_model "code.gitea.io/gitea/models/auth"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/models/unittest"
  12. user_model "code.gitea.io/gitea/models/user"
  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 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
  37. org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the repo3, is an org
  38. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // owner of neither repos
  39. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
  40. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
  41. repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
  42. fileID := 0
  43. // Get user2's token
  44. session := loginUser(t, user2.Name)
  45. token2 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  46. // Get user4's token
  47. session = loginUser(t, user4.Name)
  48. token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  49. // Test deleting a file in repo1 which user2 owns, try both with branch and empty branch
  50. for _, branch := range [...]string{
  51. "master", // Branch
  52. "", // Empty branch
  53. } {
  54. fileID++
  55. treePath := fmt.Sprintf("delete/file%d.txt", fileID)
  56. createFile(user2, repo1, treePath)
  57. deleteFileOptions := getDeleteFileOptions()
  58. deleteFileOptions.BranchName = branch
  59. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  60. req := NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  61. resp := MakeRequest(t, req, http.StatusOK)
  62. var fileResponse api.FileResponse
  63. DecodeJSON(t, resp, &fileResponse)
  64. assert.NotNil(t, fileResponse)
  65. assert.Nil(t, fileResponse.Content)
  66. }
  67. // Test deleting file and making the delete in a new branch
  68. fileID++
  69. treePath := fmt.Sprintf("delete/file%d.txt", fileID)
  70. createFile(user2, repo1, treePath)
  71. deleteFileOptions := getDeleteFileOptions()
  72. deleteFileOptions.BranchName = repo1.DefaultBranch
  73. deleteFileOptions.NewBranchName = "new_branch"
  74. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  75. req := NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  76. resp := MakeRequest(t, req, http.StatusOK)
  77. var fileResponse api.FileResponse
  78. DecodeJSON(t, resp, &fileResponse)
  79. assert.NotNil(t, fileResponse)
  80. assert.Nil(t, fileResponse.Content)
  81. assert.EqualValues(t, deleteFileOptions.Message+"\n", fileResponse.Commit.Message)
  82. // Test deleting file without a message
  83. fileID++
  84. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  85. createFile(user2, repo1, treePath)
  86. deleteFileOptions = getDeleteFileOptions()
  87. deleteFileOptions.Message = ""
  88. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  89. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  90. resp = MakeRequest(t, req, http.StatusOK)
  91. DecodeJSON(t, resp, &fileResponse)
  92. expectedMessage := "Delete " + treePath + "\n"
  93. assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
  94. // Test deleting a file with the wrong SHA
  95. fileID++
  96. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  97. createFile(user2, repo1, treePath)
  98. deleteFileOptions = getDeleteFileOptions()
  99. deleteFileOptions.SHA = "badsha"
  100. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  101. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  102. MakeRequest(t, req, http.StatusBadRequest)
  103. // Test creating a file in repo16 by user4 who does not have write access
  104. fileID++
  105. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  106. createFile(user2, repo16, treePath)
  107. deleteFileOptions = getDeleteFileOptions()
  108. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  109. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  110. MakeRequest(t, req, http.StatusNotFound)
  111. // Tests a repo with no token given so will fail
  112. fileID++
  113. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  114. createFile(user2, repo16, treePath)
  115. deleteFileOptions = getDeleteFileOptions()
  116. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  117. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  118. MakeRequest(t, req, http.StatusNotFound)
  119. // Test using access token for a private repo that the user of the token owns
  120. fileID++
  121. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  122. createFile(user2, repo16, treePath)
  123. deleteFileOptions = getDeleteFileOptions()
  124. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  125. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  126. MakeRequest(t, req, http.StatusOK)
  127. // Test using org repo "org3/repo3" where user2 is a collaborator
  128. fileID++
  129. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  130. createFile(org3, repo3, treePath)
  131. deleteFileOptions = getDeleteFileOptions()
  132. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", org3.Name, repo3.Name, treePath, token2)
  133. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  134. MakeRequest(t, req, http.StatusOK)
  135. // Test using org repo "org3/repo3" with no user token
  136. fileID++
  137. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  138. createFile(org3, repo3, treePath)
  139. deleteFileOptions = getDeleteFileOptions()
  140. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", org3.Name, repo3.Name, treePath)
  141. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  142. MakeRequest(t, req, http.StatusNotFound)
  143. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  144. fileID++
  145. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  146. createFile(user2, repo1, treePath)
  147. deleteFileOptions = getDeleteFileOptions()
  148. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  149. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  150. MakeRequest(t, req, http.StatusForbidden)
  151. })
  152. }