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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: nil,
  23. Committer: nil,
  24. }
  25. }
  26. func getExpectedDeleteFileResponse(u *url.URL) *api.FileResponse {
  27. return &api.FileResponse{
  28. Content: nil,
  29. Commit: &api.FileCommitResponse{
  30. CommitMeta: api.CommitMeta{
  31. URL: u.String() + "api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  32. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  33. },
  34. HTMLURL: u.String() + "user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  35. Author: &api.CommitUser{
  36. Identity: api.Identity{
  37. Name: "user1",
  38. Email: "address1@example.com",
  39. },
  40. Date: "2017-03-19T20:47:59Z",
  41. },
  42. Committer: &api.CommitUser{
  43. Identity: api.Identity{
  44. Name: "Ethan Koenig",
  45. Email: "ethantkoenig@gmail.com",
  46. },
  47. Date: "2017-03-19T20:47:59Z",
  48. },
  49. Parents: []*api.CommitMeta{},
  50. Message: "Initial commit\n",
  51. Tree: &api.CommitMeta{
  52. URL: u.String() + "api/v1/repos/user2/repo1/git/trees/2a2f1d4670728a2e10049e345bd7a276468beab6",
  53. SHA: "2a2f1d4670728a2e10049e345bd7a276468beab6",
  54. },
  55. },
  56. Verification: &api.PayloadCommitVerification{
  57. Verified: false,
  58. Reason: "",
  59. Signature: "",
  60. Payload: "",
  61. },
  62. }
  63. }
  64. func TestDeleteRepoFile(t *testing.T) {
  65. onGiteaRun(t, testDeleteRepoFile)
  66. }
  67. func testDeleteRepoFile(t *testing.T, u *url.URL) {
  68. // setup
  69. models.PrepareTestEnv(t)
  70. ctx := test.MockContext(t, "user2/repo1")
  71. ctx.SetParams(":id", "1")
  72. test.LoadRepo(t, ctx, 1)
  73. test.LoadRepoCommit(t, ctx)
  74. test.LoadUser(t, ctx, 2)
  75. test.LoadGitRepo(t, ctx)
  76. repo := ctx.Repo.Repository
  77. doer := ctx.User
  78. opts := getDeleteRepoFileOptions(repo)
  79. t.Run("Delete README.md file", func(t *testing.T) {
  80. fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
  81. assert.Nil(t, err)
  82. expectedFileResponse := getExpectedDeleteFileResponse(u)
  83. assert.EqualValues(t, expectedFileResponse, fileResponse)
  84. })
  85. t.Run("Verify README.md has been deleted", func(t *testing.T) {
  86. fileResponse, err := repofiles.DeleteRepoFile(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. models.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. 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 := repofiles.DeleteRepoFile(repo, doer, opts)
  112. assert.Nil(t, err)
  113. expectedFileResponse := getExpectedDeleteFileResponse(u)
  114. assert.EqualValues(t, expectedFileResponse, fileResponse)
  115. })
  116. }
  117. func TestDeleteRepoFileErrors(t *testing.T) {
  118. // setup
  119. models.PrepareTestEnv(t)
  120. ctx := test.MockContext(t, "user2/repo1")
  121. ctx.SetParams(":id", "1")
  122. test.LoadRepo(t, ctx, 1)
  123. test.LoadRepoCommit(t, ctx)
  124. test.LoadUser(t, ctx, 2)
  125. test.LoadGitRepo(t, ctx)
  126. repo := ctx.Repo.Repository
  127. doer := ctx.User
  128. t.Run("Bad branch", func(t *testing.T) {
  129. opts := getDeleteRepoFileOptions(repo)
  130. opts.OldBranch = "bad_branch"
  131. fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
  132. assert.Error(t, err)
  133. assert.Nil(t, fileResponse)
  134. expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
  135. assert.EqualError(t, err, expectedError)
  136. })
  137. t.Run("Bad SHA", func(t *testing.T) {
  138. opts := getDeleteRepoFileOptions(repo)
  139. origSHA := opts.SHA
  140. opts.SHA = "bad_sha"
  141. fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
  142. assert.Nil(t, fileResponse)
  143. assert.Error(t, err)
  144. expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
  145. assert.EqualError(t, err, expectedError)
  146. })
  147. t.Run("New branch already exists", func(t *testing.T) {
  148. opts := getDeleteRepoFileOptions(repo)
  149. opts.NewBranch = "develop"
  150. fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
  151. assert.Nil(t, fileResponse)
  152. assert.Error(t, err)
  153. expectedError := "branch already exists [name: " + opts.NewBranch + "]"
  154. assert.EqualError(t, err, expectedError)
  155. })
  156. t.Run("TreePath is empty:", func(t *testing.T) {
  157. opts := getDeleteRepoFileOptions(repo)
  158. opts.TreePath = ""
  159. fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
  160. assert.Nil(t, fileResponse)
  161. assert.Error(t, err)
  162. expectedError := "path contains a malformed path component [path: ]"
  163. assert.EqualError(t, err, expectedError)
  164. })
  165. t.Run("TreePath is a git directory:", func(t *testing.T) {
  166. opts := getDeleteRepoFileOptions(repo)
  167. opts.TreePath = ".git"
  168. fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
  169. assert.Nil(t, fileResponse)
  170. assert.Error(t, err)
  171. expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
  172. assert.EqualError(t, err, expectedError)
  173. })
  174. }