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.

delete_test.go 5.5KB

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