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

repofiles_delete_test.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "net/url"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/models/unittest"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/test"
  12. files_service "code.gitea.io/gitea/services/repository/files"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func getDeleteRepoFileOptions(repo *models.Repository) *files_service.DeleteRepoFileOptions {
  16. return &files_service.DeleteRepoFileOptions{
  17. LastCommitID: "",
  18. OldBranch: repo.DefaultBranch,
  19. NewBranch: repo.DefaultBranch,
  20. TreePath: "README.md",
  21. Message: "Deletes README.md",
  22. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  23. Author: &files_service.IdentityOptions{
  24. Name: "Bob Smith",
  25. Email: "bob@smith.com",
  26. },
  27. Committer: nil,
  28. }
  29. }
  30. func getExpectedDeleteFileResponse(u *url.URL) *api.FileResponse {
  31. // Just returns fields that don't change, i.e. fields with commit SHAs and dates can't be determined
  32. return &api.FileResponse{
  33. Content: nil,
  34. Commit: &api.FileCommitResponse{
  35. Author: &api.CommitUser{
  36. Identity: api.Identity{
  37. Name: "Bob Smith",
  38. Email: "bob@smith.com",
  39. },
  40. },
  41. Committer: &api.CommitUser{
  42. Identity: api.Identity{
  43. Name: "Bob Smith",
  44. Email: "bob@smith.com",
  45. },
  46. },
  47. Message: "Deletes README.md\n",
  48. },
  49. Verification: &api.PayloadCommitVerification{
  50. Verified: false,
  51. Reason: "gpg.error.not_signed_commit",
  52. Signature: "",
  53. Payload: "",
  54. },
  55. }
  56. }
  57. func TestDeleteRepoFile(t *testing.T) {
  58. onGiteaRun(t, testDeleteRepoFile)
  59. }
  60. func testDeleteRepoFile(t *testing.T, u *url.URL) {
  61. // setup
  62. unittest.PrepareTestEnv(t)
  63. ctx := test.MockContext(t, "user2/repo1")
  64. ctx.SetParams(":id", "1")
  65. test.LoadRepo(t, ctx, 1)
  66. test.LoadRepoCommit(t, ctx)
  67. test.LoadUser(t, ctx, 2)
  68. test.LoadGitRepo(t, ctx)
  69. defer ctx.Repo.GitRepo.Close()
  70. repo := ctx.Repo.Repository
  71. doer := ctx.User
  72. opts := getDeleteRepoFileOptions(repo)
  73. t.Run("Delete README.md file", func(t *testing.T) {
  74. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  75. assert.NoError(t, err)
  76. expectedFileResponse := getExpectedDeleteFileResponse(u)
  77. assert.NotNil(t, fileResponse)
  78. assert.Nil(t, fileResponse.Content)
  79. assert.EqualValues(t, expectedFileResponse.Commit.Message, fileResponse.Commit.Message)
  80. assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, fileResponse.Commit.Author.Identity)
  81. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, fileResponse.Commit.Committer.Identity)
  82. assert.EqualValues(t, expectedFileResponse.Verification, fileResponse.Verification)
  83. })
  84. t.Run("Verify README.md has been deleted", func(t *testing.T) {
  85. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  86. assert.Nil(t, fileResponse)
  87. expectedError := "repository file does not exist [path: " + opts.TreePath + "]"
  88. assert.EqualError(t, err, expectedError)
  89. })
  90. }
  91. // Test opts with branch names removed, same results
  92. func TestDeleteRepoFileWithoutBranchNames(t *testing.T) {
  93. onGiteaRun(t, testDeleteRepoFileWithoutBranchNames)
  94. }
  95. func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
  96. // setup
  97. unittest.PrepareTestEnv(t)
  98. ctx := test.MockContext(t, "user2/repo1")
  99. ctx.SetParams(":id", "1")
  100. test.LoadRepo(t, ctx, 1)
  101. test.LoadRepoCommit(t, ctx)
  102. test.LoadUser(t, ctx, 2)
  103. test.LoadGitRepo(t, ctx)
  104. defer ctx.Repo.GitRepo.Close()
  105. repo := ctx.Repo.Repository
  106. doer := ctx.User
  107. opts := getDeleteRepoFileOptions(repo)
  108. opts.OldBranch = ""
  109. opts.NewBranch = ""
  110. t.Run("Delete README.md without Branch Name", func(t *testing.T) {
  111. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  112. assert.NoError(t, err)
  113. expectedFileResponse := getExpectedDeleteFileResponse(u)
  114. assert.NotNil(t, fileResponse)
  115. assert.Nil(t, fileResponse.Content)
  116. assert.EqualValues(t, expectedFileResponse.Commit.Message, fileResponse.Commit.Message)
  117. assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, fileResponse.Commit.Author.Identity)
  118. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, fileResponse.Commit.Committer.Identity)
  119. assert.EqualValues(t, expectedFileResponse.Verification, fileResponse.Verification)
  120. })
  121. }
  122. func TestDeleteRepoFileErrors(t *testing.T) {
  123. // setup
  124. unittest.PrepareTestEnv(t)
  125. ctx := test.MockContext(t, "user2/repo1")
  126. ctx.SetParams(":id", "1")
  127. test.LoadRepo(t, ctx, 1)
  128. test.LoadRepoCommit(t, ctx)
  129. test.LoadUser(t, ctx, 2)
  130. test.LoadGitRepo(t, ctx)
  131. defer ctx.Repo.GitRepo.Close()
  132. repo := ctx.Repo.Repository
  133. doer := ctx.User
  134. t.Run("Bad branch", func(t *testing.T) {
  135. opts := getDeleteRepoFileOptions(repo)
  136. opts.OldBranch = "bad_branch"
  137. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  138. assert.Error(t, err)
  139. assert.Nil(t, fileResponse)
  140. expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
  141. assert.EqualError(t, err, expectedError)
  142. })
  143. t.Run("Bad SHA", func(t *testing.T) {
  144. opts := getDeleteRepoFileOptions(repo)
  145. origSHA := opts.SHA
  146. opts.SHA = "bad_sha"
  147. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  148. assert.Nil(t, fileResponse)
  149. assert.Error(t, err)
  150. expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
  151. assert.EqualError(t, err, expectedError)
  152. })
  153. t.Run("New branch already exists", func(t *testing.T) {
  154. opts := getDeleteRepoFileOptions(repo)
  155. opts.NewBranch = "develop"
  156. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  157. assert.Nil(t, fileResponse)
  158. assert.Error(t, err)
  159. expectedError := "branch already exists [name: " + opts.NewBranch + "]"
  160. assert.EqualError(t, err, expectedError)
  161. })
  162. t.Run("TreePath is empty:", func(t *testing.T) {
  163. opts := getDeleteRepoFileOptions(repo)
  164. opts.TreePath = ""
  165. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  166. assert.Nil(t, fileResponse)
  167. assert.Error(t, err)
  168. expectedError := "path contains a malformed path component [path: ]"
  169. assert.EqualError(t, err, expectedError)
  170. })
  171. t.Run("TreePath is a git directory:", func(t *testing.T) {
  172. opts := getDeleteRepoFileOptions(repo)
  173. opts.TreePath = ".git"
  174. fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
  175. assert.Nil(t, fileResponse)
  176. assert.Error(t, err)
  177. expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
  178. assert.EqualError(t, err, expectedError)
  179. })
  180. }