本站源代码
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.

209 lines
6.6KB

  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. "path/filepath"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/modules/test"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestMain(m *testing.M) {
  14. models.MainTest(m, filepath.Join("..", ".."))
  15. }
  16. func getExpectedReadmeContentsResponse() *api.ContentsResponse {
  17. treePath := "README.md"
  18. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  19. encoding := "base64"
  20. content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
  21. selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  22. htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
  23. gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
  24. downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
  25. return &api.ContentsResponse{
  26. Name: treePath,
  27. Path: treePath,
  28. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  29. Type: "file",
  30. Size: 30,
  31. Encoding: &encoding,
  32. Content: &content,
  33. URL: &selfURL,
  34. HTMLURL: &htmlURL,
  35. GitURL: &gitURL,
  36. DownloadURL: &downloadURL,
  37. Links: &api.FileLinksResponse{
  38. Self: &selfURL,
  39. GitURL: &gitURL,
  40. HTMLURL: &htmlURL,
  41. },
  42. }
  43. }
  44. func TestGetContents(t *testing.T) {
  45. models.PrepareTestEnv(t)
  46. ctx := test.MockContext(t, "user2/repo1")
  47. ctx.SetParams(":id", "1")
  48. test.LoadRepo(t, ctx, 1)
  49. test.LoadRepoCommit(t, ctx)
  50. test.LoadUser(t, ctx, 2)
  51. test.LoadGitRepo(t, ctx)
  52. treePath := "README.md"
  53. ref := ctx.Repo.Repository.DefaultBranch
  54. expectedContentsResponse := getExpectedReadmeContentsResponse()
  55. t.Run("Get README.md contents with GetContents()", func(t *testing.T) {
  56. fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false)
  57. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  58. assert.Nil(t, err)
  59. })
  60. t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch) with GetContents()", func(t *testing.T) {
  61. fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false)
  62. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  63. assert.Nil(t, err)
  64. })
  65. }
  66. func TestGetContentsOrListForDir(t *testing.T) {
  67. models.PrepareTestEnv(t)
  68. ctx := test.MockContext(t, "user2/repo1")
  69. ctx.SetParams(":id", "1")
  70. test.LoadRepo(t, ctx, 1)
  71. test.LoadRepoCommit(t, ctx)
  72. test.LoadUser(t, ctx, 2)
  73. test.LoadGitRepo(t, ctx)
  74. treePath := "" // root dir
  75. ref := ctx.Repo.Repository.DefaultBranch
  76. readmeContentsResponse := getExpectedReadmeContentsResponse()
  77. // because will be in a list, doesn't have encoding and content
  78. readmeContentsResponse.Encoding = nil
  79. readmeContentsResponse.Content = nil
  80. expectedContentsListResponse := []*api.ContentsResponse{
  81. readmeContentsResponse,
  82. }
  83. t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) {
  84. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
  85. assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
  86. assert.Nil(t, err)
  87. })
  88. t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) {
  89. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
  90. assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
  91. assert.Nil(t, err)
  92. })
  93. }
  94. func TestGetContentsOrListForFile(t *testing.T) {
  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. treePath := "README.md"
  103. ref := ctx.Repo.Repository.DefaultBranch
  104. expectedContentsResponse := getExpectedReadmeContentsResponse()
  105. t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) {
  106. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
  107. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  108. assert.Nil(t, err)
  109. })
  110. t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) {
  111. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
  112. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  113. assert.Nil(t, err)
  114. })
  115. }
  116. func TestGetContentsErrors(t *testing.T) {
  117. models.PrepareTestEnv(t)
  118. ctx := test.MockContext(t, "user2/repo1")
  119. ctx.SetParams(":id", "1")
  120. test.LoadRepo(t, ctx, 1)
  121. test.LoadRepoCommit(t, ctx)
  122. test.LoadUser(t, ctx, 2)
  123. test.LoadGitRepo(t, ctx)
  124. repo := ctx.Repo.Repository
  125. treePath := "README.md"
  126. ref := repo.DefaultBranch
  127. t.Run("bad treePath", func(t *testing.T) {
  128. badTreePath := "bad/tree.md"
  129. fileContentResponse, err := GetContents(repo, badTreePath, ref, false)
  130. assert.Error(t, err)
  131. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  132. assert.Nil(t, fileContentResponse)
  133. })
  134. t.Run("bad ref", func(t *testing.T) {
  135. badRef := "bad_ref"
  136. fileContentResponse, err := GetContents(repo, treePath, badRef, false)
  137. assert.Error(t, err)
  138. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  139. assert.Nil(t, fileContentResponse)
  140. })
  141. }
  142. func TestGetContentsOrListErrors(t *testing.T) {
  143. models.PrepareTestEnv(t)
  144. ctx := test.MockContext(t, "user2/repo1")
  145. ctx.SetParams(":id", "1")
  146. test.LoadRepo(t, ctx, 1)
  147. test.LoadRepoCommit(t, ctx)
  148. test.LoadUser(t, ctx, 2)
  149. test.LoadGitRepo(t, ctx)
  150. repo := ctx.Repo.Repository
  151. treePath := "README.md"
  152. ref := repo.DefaultBranch
  153. t.Run("bad treePath", func(t *testing.T) {
  154. badTreePath := "bad/tree.md"
  155. fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref)
  156. assert.Error(t, err)
  157. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  158. assert.Nil(t, fileContentResponse)
  159. })
  160. t.Run("bad ref", func(t *testing.T) {
  161. badRef := "bad_ref"
  162. fileContentResponse, err := GetContentsOrList(repo, treePath, badRef)
  163. assert.Error(t, err)
  164. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  165. assert.Nil(t, fileContentResponse)
  166. })
  167. }
  168. func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
  169. models.PrepareTestEnv(t)
  170. ctx := test.MockContext(t, "user2/repo15")
  171. ctx.SetParams(":id", "15")
  172. test.LoadRepo(t, ctx, 15)
  173. test.LoadUser(t, ctx, 2)
  174. test.LoadGitRepo(t, ctx)
  175. repo := ctx.Repo.Repository
  176. t.Run("empty repo", func(t *testing.T) {
  177. contents, err := GetContentsOrList(repo, "", "")
  178. assert.NoError(t, err)
  179. assert.Empty(t, contents)
  180. })
  181. }
上海开阖软件有限公司 沪ICP备12045867号-1