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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/base"
  12. "code.gitea.io/gitea/modules/context"
  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: "Updates 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. // Test deleting a file with the wrong SHA
  84. fileID++
  85. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  86. createFile(user2, repo1, treePath)
  87. deleteFileOptions = getDeleteFileOptions()
  88. correctSHA := deleteFileOptions.SHA
  89. deleteFileOptions.SHA = "badsha"
  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.StatusInternalServerError)
  93. expectedAPIError := context.APIError{
  94. Message: "sha does not match [given: " + deleteFileOptions.SHA + ", expected: " + correctSHA + "]",
  95. URL: base.DocURL,
  96. }
  97. var apiError context.APIError
  98. DecodeJSON(t, resp, &apiError)
  99. assert.Equal(t, expectedAPIError, apiError)
  100. // Test creating a file in repo16 by user4 who does not have write access
  101. fileID++
  102. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  103. createFile(user2, repo16, treePath)
  104. deleteFileOptions = getDeleteFileOptions()
  105. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  106. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  107. session.MakeRequest(t, req, http.StatusNotFound)
  108. // Tests a repo with no token given so will fail
  109. fileID++
  110. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  111. createFile(user2, repo16, treePath)
  112. deleteFileOptions = getDeleteFileOptions()
  113. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  114. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  115. session.MakeRequest(t, req, http.StatusNotFound)
  116. // Test using access token for a private repo that the user of the token owns
  117. fileID++
  118. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  119. createFile(user2, repo16, treePath)
  120. deleteFileOptions = getDeleteFileOptions()
  121. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  122. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  123. session.MakeRequest(t, req, http.StatusOK)
  124. // Test using org repo "user3/repo3" where user2 is a collaborator
  125. fileID++
  126. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  127. createFile(user3, repo3, treePath)
  128. deleteFileOptions = getDeleteFileOptions()
  129. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  130. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  131. session.MakeRequest(t, req, http.StatusOK)
  132. // Test using org repo "user3/repo3" with no user token
  133. fileID++
  134. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  135. createFile(user3, repo3, treePath)
  136. deleteFileOptions = getDeleteFileOptions()
  137. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  138. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  139. session.MakeRequest(t, req, http.StatusNotFound)
  140. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  141. fileID++
  142. treePath = fmt.Sprintf("delete/file%d.txt", fileID)
  143. createFile(user2, repo1, treePath)
  144. deleteFileOptions = getDeleteFileOptions()
  145. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  146. req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
  147. session.MakeRequest(t, req, http.StatusForbidden)
  148. })
  149. }