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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. defer ctx.Repo.GitRepo.Close()
  53. treePath := "README.md"
  54. ref := ctx.Repo.Repository.DefaultBranch
  55. expectedContentsResponse := getExpectedReadmeContentsResponse()
  56. t.Run("Get README.md contents with GetContents()", func(t *testing.T) {
  57. fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false)
  58. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  59. assert.Nil(t, err)
  60. })
  61. 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) {
  62. fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false)
  63. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  64. assert.Nil(t, err)
  65. })
  66. }
  67. func TestGetContentsOrListForDir(t *testing.T) {
  68. models.PrepareTestEnv(t)
  69. ctx := test.MockContext(t, "user2/repo1")
  70. ctx.SetParams(":id", "1")
  71. test.LoadRepo(t, ctx, 1)
  72. test.LoadRepoCommit(t, ctx)
  73. test.LoadUser(t, ctx, 2)
  74. test.LoadGitRepo(t, ctx)
  75. defer ctx.Repo.GitRepo.Close()
  76. treePath := "" // root dir
  77. ref := ctx.Repo.Repository.DefaultBranch
  78. readmeContentsResponse := getExpectedReadmeContentsResponse()
  79. // because will be in a list, doesn't have encoding and content
  80. readmeContentsResponse.Encoding = nil
  81. readmeContentsResponse.Content = nil
  82. expectedContentsListResponse := []*api.ContentsResponse{
  83. readmeContentsResponse,
  84. }
  85. t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) {
  86. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
  87. assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
  88. assert.Nil(t, err)
  89. })
  90. 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) {
  91. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
  92. assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
  93. assert.Nil(t, err)
  94. })
  95. }
  96. func TestGetContentsOrListForFile(t *testing.T) {
  97. models.PrepareTestEnv(t)
  98. ctx := test.MockContext(t, "user2/repo1")
  99. ctx.SetParams(":id", "1")
  100. test.LoadRepo(t, ctx, 1)
  101. test.LoadRepoCommit(t, ctx)
  102. test.LoadUser(t, ctx, 2)
  103. test.LoadGitRepo(t, ctx)
  104. defer ctx.Repo.GitRepo.Close()
  105. treePath := "README.md"
  106. ref := ctx.Repo.Repository.DefaultBranch
  107. expectedContentsResponse := getExpectedReadmeContentsResponse()
  108. t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) {
  109. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
  110. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  111. assert.Nil(t, err)
  112. })
  113. 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) {
  114. fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
  115. assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
  116. assert.Nil(t, err)
  117. })
  118. }
  119. func TestGetContentsErrors(t *testing.T) {
  120. models.PrepareTestEnv(t)
  121. ctx := test.MockContext(t, "user2/repo1")
  122. ctx.SetParams(":id", "1")
  123. test.LoadRepo(t, ctx, 1)
  124. test.LoadRepoCommit(t, ctx)
  125. test.LoadUser(t, ctx, 2)
  126. test.LoadGitRepo(t, ctx)
  127. defer ctx.Repo.GitRepo.Close()
  128. repo := ctx.Repo.Repository
  129. treePath := "README.md"
  130. ref := repo.DefaultBranch
  131. t.Run("bad treePath", func(t *testing.T) {
  132. badTreePath := "bad/tree.md"
  133. fileContentResponse, err := GetContents(repo, badTreePath, ref, false)
  134. assert.Error(t, err)
  135. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  136. assert.Nil(t, fileContentResponse)
  137. })
  138. t.Run("bad ref", func(t *testing.T) {
  139. badRef := "bad_ref"
  140. fileContentResponse, err := GetContents(repo, treePath, badRef, false)
  141. assert.Error(t, err)
  142. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  143. assert.Nil(t, fileContentResponse)
  144. })
  145. }
  146. func TestGetContentsOrListErrors(t *testing.T) {
  147. models.PrepareTestEnv(t)
  148. ctx := test.MockContext(t, "user2/repo1")
  149. ctx.SetParams(":id", "1")
  150. test.LoadRepo(t, ctx, 1)
  151. test.LoadRepoCommit(t, ctx)
  152. test.LoadUser(t, ctx, 2)
  153. test.LoadGitRepo(t, ctx)
  154. defer ctx.Repo.GitRepo.Close()
  155. repo := ctx.Repo.Repository
  156. treePath := "README.md"
  157. ref := repo.DefaultBranch
  158. t.Run("bad treePath", func(t *testing.T) {
  159. badTreePath := "bad/tree.md"
  160. fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref)
  161. assert.Error(t, err)
  162. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  163. assert.Nil(t, fileContentResponse)
  164. })
  165. t.Run("bad ref", func(t *testing.T) {
  166. badRef := "bad_ref"
  167. fileContentResponse, err := GetContentsOrList(repo, treePath, badRef)
  168. assert.Error(t, err)
  169. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  170. assert.Nil(t, fileContentResponse)
  171. })
  172. }
  173. func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
  174. models.PrepareTestEnv(t)
  175. ctx := test.MockContext(t, "user2/repo15")
  176. ctx.SetParams(":id", "15")
  177. test.LoadRepo(t, ctx, 15)
  178. test.LoadUser(t, ctx, 2)
  179. test.LoadGitRepo(t, ctx)
  180. defer ctx.Repo.GitRepo.Close()
  181. repo := ctx.Repo.Repository
  182. t.Run("empty repo", func(t *testing.T) {
  183. contents, err := GetContentsOrList(repo, "", "")
  184. assert.NoError(t, err)
  185. assert.Empty(t, contents)
  186. })
  187. }