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.

repofiles_delete_test.go 6.7KB

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