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.

90 lines
3.0 KiB

  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. "code.gitea.io/gitea/modules/test"
  10. "code.gitea.io/sdk/gitea"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestMain(m *testing.M) {
  14. models.MainTest(m, filepath.Join("..", ".."))
  15. }
  16. func TestGetFileContents(t *testing.T) {
  17. models.PrepareTestEnv(t)
  18. ctx := test.MockContext(t, "user2/repo1")
  19. ctx.SetParams(":id", "1")
  20. test.LoadRepo(t, ctx, 1)
  21. test.LoadRepoCommit(t, ctx)
  22. test.LoadUser(t, ctx, 2)
  23. test.LoadGitRepo(t, ctx)
  24. treePath := "README.md"
  25. ref := ctx.Repo.Repository.DefaultBranch
  26. expectedFileContentResponse := &gitea.FileContentResponse{
  27. Name: treePath,
  28. Path: treePath,
  29. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  30. Size: 30,
  31. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
  32. HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
  33. GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  34. DownloadURL: "https://try.gitea.io/user2/repo1/raw/branch/master/README.md",
  35. Type: "blob",
  36. Links: &gitea.FileLinksResponse{
  37. Self: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
  38. GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  39. HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
  40. },
  41. }
  42. t.Run("Get README.md contents", func(t *testing.T) {
  43. fileContentResponse, err := GetFileContents(ctx.Repo.Repository, treePath, ref)
  44. assert.EqualValues(t, expectedFileContentResponse, fileContentResponse)
  45. assert.Nil(t, err)
  46. })
  47. t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch)", func(t *testing.T) {
  48. fileContentResponse, err := GetFileContents(ctx.Repo.Repository, treePath, "")
  49. assert.EqualValues(t, expectedFileContentResponse, fileContentResponse)
  50. assert.Nil(t, err)
  51. })
  52. }
  53. func TestGetFileContentsErrors(t *testing.T) {
  54. models.PrepareTestEnv(t)
  55. ctx := test.MockContext(t, "user2/repo1")
  56. ctx.SetParams(":id", "1")
  57. test.LoadRepo(t, ctx, 1)
  58. test.LoadRepoCommit(t, ctx)
  59. test.LoadUser(t, ctx, 2)
  60. test.LoadGitRepo(t, ctx)
  61. repo := ctx.Repo.Repository
  62. treePath := "README.md"
  63. ref := repo.DefaultBranch
  64. t.Run("bad treePath", func(t *testing.T) {
  65. badTreePath := "bad/tree.md"
  66. fileContentResponse, err := GetFileContents(repo, badTreePath, ref)
  67. assert.Error(t, err)
  68. assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
  69. assert.Nil(t, fileContentResponse)
  70. })
  71. t.Run("bad ref", func(t *testing.T) {
  72. badRef := "bad_ref"
  73. fileContentResponse, err := GetFileContents(repo, treePath, badRef)
  74. assert.Error(t, err)
  75. assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
  76. assert.Nil(t, fileContentResponse)
  77. })
  78. }