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

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