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.

content_test.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 files
  5. import (
  6. "path/filepath"
  7. "testing"
  8. "code.gitea.io/gitea/models/unittest"
  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. unittest.MainTest(m, &unittest.TestOptions{
  15. GiteaRootPath: filepath.Join("..", "..", ".."),
  16. })
  17. }
  18. func getExpectedReadmeContentsResponse() *api.ContentsResponse {
  19. treePath := "README.md"
  20. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  21. encoding := "base64"
  22. content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
  23. selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  24. htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
  25. gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
  26. downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
  27. return &api.ContentsResponse{
  28. Name: treePath,
  29. Path: treePath,
  30. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  31. Type: "file",
  32. Size: 30,
  33. Encoding: &encoding,
  34. Content: &content,
  35. URL: &selfURL,
  36. HTMLURL: &htmlURL,
  37. GitURL: &gitURL,
  38. DownloadURL: &downloadURL,
  39. Links: &api.FileLinksResponse{
  40. Self: &selfURL,
  41. GitURL: &gitURL,
  42. HTMLURL: &htmlURL,
  43. },
  44. }
  45. }
  46. func TestGetContents(t *testing.T) {
  47. unittest.PrepareTestEnv(t)
  48. ctx := test.MockContext(t, "user2/repo1")
  49. ctx.SetParams(":id", "1")
  50. test.LoadRepo(t, ctx, 1)
  51. test.LoadRepoCommit(t, ctx)
  52. test.LoadUser(t, ctx, 2)
  53. test.LoadGitRepo(t, ctx)
  54. defer ctx.Repo.GitRepo.Close()
  55. treePath := "README.md"
  56. ref := ctx.Repo.Repository.DefaultBranch
  57. expectedContentsResponse := getExpectedReadmeContentsResponse()
  58. t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) {
  59. fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false)
  60. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  61. assert.NoError(t, err)
  62. })
  63. t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) {
  64. fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false)
  65. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  66. assert.NoError(t, err)
  67. })
  68. }
  69. func TestGetContentsOrListForDir(t *testing.T) {
  70. unittest.PrepareTestEnv(t)
  71. ctx := test.MockContext(t, "user2/repo1")
  72. ctx.SetParams(":id", "1")
  73. test.LoadRepo(t, ctx, 1)
  74. test.LoadRepoCommit(t, ctx)
  75. test.LoadUser(t, ctx, 2)
  76. test.LoadGitRepo(t, ctx)
  77. defer ctx.Repo.GitRepo.Close()
  78. treePath := "" // root dir
  79. ref := ctx.Repo.Repository.DefaultBranch
  80. readmeContentsResponse := getExpectedReadmeContentsResponse()
  81. // because will be in a list, doesn't have encoding and content
  82. readmeContentsResponse.Encoding = nil
  83. readmeContentsResponse.Content = nil
  84. expectedContentsListResponse := []*api.ContentsResponse{
  85. readmeContentsResponse,
  86. }
  87. t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) {
  88. fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
  89. assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
  90. assert.NoError(t, err)
  91. })
  92. t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
  93. fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
  94. assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
  95. assert.NoError(t, err)
  96. })
  97. }
  98. func TestGetContentsOrListForFile(t *testing.T) {
  99. unittest.PrepareTestEnv(t)
  100. ctx := test.MockContext(t, "user2/repo1")
  101. ctx.SetParams(":id", "1")
  102. test.LoadRepo(t, ctx, 1)
  103. test.LoadRepoCommit(t, ctx)
  104. test.LoadUser(t, ctx, 2)
  105. test.LoadGitRepo(t, ctx)
  106. defer ctx.Repo.GitRepo.Close()
  107. treePath := "README.md"
  108. ref := ctx.Repo.Repository.DefaultBranch
  109. expectedContentsResponse := getExpectedReadmeContentsResponse()
  110. t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) {
  111. fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
  112. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  113. assert.NoError(t, err)
  114. })
  115. t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
  116. fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
  117. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  118. assert.NoError(t, err)
  119. })
  120. }
  121. func TestGetContentsErrors(t *testing.T) {
  122. unittest.PrepareTestEnv(t)
  123. ctx := test.MockContext(t, "user2/repo1")
  124. ctx.SetParams(":id", "1")
  125. test.LoadRepo(t, ctx, 1)
  126. test.LoadRepoCommit(t, ctx)
  127. test.LoadUser(t, ctx, 2)
  128. test.LoadGitRepo(t, ctx)
  129. defer ctx.Repo.GitRepo.Close()
  130. repo := ctx.Repo.Repository
  131. treePath := "README.md"
  132. ref := repo.DefaultBranch
  133. t.Run("bad treePath", func(t *testing.T) {
  134. badTreePath := "bad/tree.md"
  135. fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false)
  136. assert.Error(t, err)
  137. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  138. assert.Nil(t, fileContentResponse)
  139. })
  140. t.Run("bad ref", func(t *testing.T) {
  141. badRef := "bad_ref"
  142. fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false)
  143. assert.Error(t, err)
  144. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  145. assert.Nil(t, fileContentResponse)
  146. })
  147. }
  148. func TestGetContentsOrListErrors(t *testing.T) {
  149. unittest.PrepareTestEnv(t)
  150. ctx := test.MockContext(t, "user2/repo1")
  151. ctx.SetParams(":id", "1")
  152. test.LoadRepo(t, ctx, 1)
  153. test.LoadRepoCommit(t, ctx)
  154. test.LoadUser(t, ctx, 2)
  155. test.LoadGitRepo(t, ctx)
  156. defer ctx.Repo.GitRepo.Close()
  157. repo := ctx.Repo.Repository
  158. treePath := "README.md"
  159. ref := repo.DefaultBranch
  160. t.Run("bad treePath", func(t *testing.T) {
  161. badTreePath := "bad/tree.md"
  162. fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref)
  163. assert.Error(t, err)
  164. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  165. assert.Nil(t, fileContentResponse)
  166. })
  167. t.Run("bad ref", func(t *testing.T) {
  168. badRef := "bad_ref"
  169. fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef)
  170. assert.Error(t, err)
  171. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  172. assert.Nil(t, fileContentResponse)
  173. })
  174. }
  175. func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
  176. unittest.PrepareTestEnv(t)
  177. ctx := test.MockContext(t, "user30/empty")
  178. ctx.SetParams(":id", "52")
  179. test.LoadRepo(t, ctx, 52)
  180. test.LoadUser(t, ctx, 30)
  181. test.LoadGitRepo(t, ctx)
  182. defer ctx.Repo.GitRepo.Close()
  183. repo := ctx.Repo.Repository
  184. t.Run("empty repo", func(t *testing.T) {
  185. contents, err := GetContentsOrList(ctx, repo, "", "")
  186. assert.NoError(t, err)
  187. assert.Empty(t, contents)
  188. })
  189. }
  190. func TestGetBlobBySHA(t *testing.T) {
  191. unittest.PrepareTestEnv(t)
  192. ctx := test.MockContext(t, "user2/repo1")
  193. test.LoadRepo(t, ctx, 1)
  194. test.LoadRepoCommit(t, ctx)
  195. test.LoadUser(t, ctx, 2)
  196. test.LoadGitRepo(t, ctx)
  197. defer ctx.Repo.GitRepo.Close()
  198. sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  199. ctx.SetParams(":id", "1")
  200. ctx.SetParams(":sha", sha)
  201. gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha"))
  202. expectedGBR := &api.GitBlobResponse{
  203. Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
  204. Encoding: "base64",
  205. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  206. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  207. Size: 180,
  208. }
  209. assert.NoError(t, err)
  210. assert.Equal(t, expectedGBR, gbr)
  211. }