您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

api_repo_file_delete_test.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 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}).(*user_model.User) // owner of the repo1 & repo16
  37. user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) // owner of the repo3, is an org
  38. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) // owner of neither repos
  39. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) // public repo
  40. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository) // public repo
  41. repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}).(*repo_model.Repository) // private repo
  42. fileID := 0
  43. // Get user2's token
  44. session := loginUser(t, user2.Name)
  45. token2 := getTokenForLoggedInUser(t, session)
  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. assert.EqualValues(t, deleteFileOptions.Message+"\n", fileResponse.Commit.Message)
  83. // Test deleting file without a message
  84. fileID++
  85. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  86. createFile(user2, repo1, treePath)
  87. deleteFileOptions = getDeleteFileOptions()
  88. deleteFileOptions.Message = ""
  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.StatusOK)
  92. DecodeJSON(t, resp, &fileResponse)
  93. expectedMessage := "Delete '" + treePath + "'\n"
  94. assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
  95. // Test deleting a file with the wrong SHA
  96. fileID++
  97. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  98. createFile(user2, repo1, treePath)
  99. deleteFileOptions = getDeleteFileOptions()
  100. deleteFileOptions.SHA = "badsha"
  101. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  102. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  103. session.MakeRequest(t, req, http.StatusBadRequest)
  104. // Test creating a file in repo16 by user4 who does not have write access
  105. fileID++
  106. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  107. createFile(user2, repo16, treePath)
  108. deleteFileOptions = getDeleteFileOptions()
  109. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  110. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  111. session.MakeRequest(t, req, http.StatusNotFound)
  112. // Tests a repo with no token given so will fail
  113. fileID++
  114. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  115. createFile(user2, repo16, treePath)
  116. deleteFileOptions = getDeleteFileOptions()
  117. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  118. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  119. session.MakeRequest(t, req, http.StatusNotFound)
  120. // Test using access token for a private repo that the user of the token owns
  121. fileID++
  122. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  123. createFile(user2, repo16, treePath)
  124. deleteFileOptions = getDeleteFileOptions()
  125. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  126. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  127. session.MakeRequest(t, req, http.StatusOK)
  128. // Test using org repo "user3/repo3" where user2 is a collaborator
  129. fileID++
  130. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  131. createFile(user3, repo3, treePath)
  132. deleteFileOptions = getDeleteFileOptions()
  133. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  134. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  135. session.MakeRequest(t, req, http.StatusOK)
  136. // Test using org repo "user3/repo3" with no user token
  137. fileID++
  138. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  139. createFile(user3, repo3, treePath)
  140. deleteFileOptions = getDeleteFileOptions()
  141. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  142. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  143. session.MakeRequest(t, req, http.StatusNotFound)
  144. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  145. fileID++
  146. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  147. createFile(user2, repo1, treePath)
  148. deleteFileOptions = getDeleteFileOptions()
  149. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  150. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  151. session.MakeRequest(t, req, http.StatusForbidden)
  152. })
  153. }