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.

file_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/test"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func getExpectedFileResponse() *api.FileResponse {
  15. treePath := "README.md"
  16. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  17. encoding := "base64"
  18. content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
  19. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  20. htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath
  21. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
  22. downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
  23. return &api.FileResponse{
  24. Content: &api.ContentsResponse{
  25. Name: treePath,
  26. Path: treePath,
  27. SHA: sha,
  28. Type: "file",
  29. Size: 30,
  30. Encoding: &encoding,
  31. Content: &content,
  32. URL: &selfURL,
  33. HTMLURL: &htmlURL,
  34. GitURL: &gitURL,
  35. DownloadURL: &downloadURL,
  36. Links: &api.FileLinksResponse{
  37. Self: &selfURL,
  38. GitURL: &gitURL,
  39. HTMLURL: &htmlURL,
  40. },
  41. },
  42. Commit: &api.FileCommitResponse{
  43. CommitMeta: api.CommitMeta{
  44. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  45. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  46. },
  47. HTMLURL: "https://try.gitea.io/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  48. Author: &api.CommitUser{
  49. Identity: api.Identity{
  50. Name: "user1",
  51. Email: "address1@example.com",
  52. },
  53. Date: "2017-03-19T20:47:59Z",
  54. },
  55. Committer: &api.CommitUser{
  56. Identity: api.Identity{
  57. Name: "Ethan Koenig",
  58. Email: "ethantkoenig@gmail.com",
  59. },
  60. Date: "2017-03-19T20:47:59Z",
  61. },
  62. Parents: []*api.CommitMeta{},
  63. Message: "Initial commit\n",
  64. Tree: &api.CommitMeta{
  65. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/trees/2a2f1d4670728a2e10049e345bd7a276468beab6",
  66. SHA: "2a2f1d4670728a2e10049e345bd7a276468beab6",
  67. },
  68. },
  69. Verification: &api.PayloadCommitVerification{
  70. Verified: false,
  71. Reason: "",
  72. Signature: "",
  73. Payload: "",
  74. },
  75. }
  76. }
  77. func TestGetFileResponseFromCommit(t *testing.T) {
  78. models.PrepareTestEnv(t)
  79. ctx := test.MockContext(t, "user2/repo1")
  80. ctx.SetParams(":id", "1")
  81. test.LoadRepo(t, ctx, 1)
  82. test.LoadRepoCommit(t, ctx)
  83. test.LoadUser(t, ctx, 2)
  84. test.LoadGitRepo(t, ctx)
  85. repo := ctx.Repo.Repository
  86. branch := repo.DefaultBranch
  87. treePath := "README.md"
  88. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  89. commit, _ := gitRepo.GetBranchCommit(branch)
  90. expectedFileResponse := getExpectedFileResponse()
  91. fileResponse, err := GetFileResponseFromCommit(repo, commit, branch, treePath)
  92. assert.Nil(t, err)
  93. assert.EqualValues(t, expectedFileResponse, fileResponse)
  94. }