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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/test"
  10. api "code.gitea.io/sdk/gitea"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func getExpectedFileResponse() *api.FileResponse {
  14. return &api.FileResponse{
  15. Content: &api.FileContentResponse{
  16. Name: "README.md",
  17. Path: "README.md",
  18. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  19. Size: 30,
  20. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
  21. HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
  22. GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  23. DownloadURL: "https://try.gitea.io/user2/repo1/raw/branch/master/README.md",
  24. Type: "blob",
  25. Links: &api.FileLinksResponse{
  26. Self: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
  27. GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  28. HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
  29. },
  30. },
  31. Commit: &api.FileCommitResponse{
  32. CommitMeta: api.CommitMeta{
  33. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  34. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  35. },
  36. HTMLURL: "https://try.gitea.io/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  37. Author: &api.CommitUser{
  38. Identity: api.Identity{
  39. Name: "user1",
  40. Email: "address1@example.com",
  41. },
  42. Date: "2017-03-19T20:47:59Z",
  43. },
  44. Committer: &api.CommitUser{
  45. Identity: api.Identity{
  46. Name: "Ethan Koenig",
  47. Email: "ethantkoenig@gmail.com",
  48. },
  49. Date: "2017-03-19T20:47:59Z",
  50. },
  51. Parents: []*api.CommitMeta{},
  52. Message: "Initial commit\n",
  53. Tree: &api.CommitMeta{
  54. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/trees/2a2f1d4670728a2e10049e345bd7a276468beab6",
  55. SHA: "2a2f1d4670728a2e10049e345bd7a276468beab6",
  56. },
  57. },
  58. Verification: &api.PayloadCommitVerification{
  59. Verified: false,
  60. Reason: "",
  61. Signature: "",
  62. Payload: "",
  63. },
  64. }
  65. }
  66. func TestGetFileResponseFromCommit(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. repo := ctx.Repo.Repository
  75. branch := repo.DefaultBranch
  76. treePath := "README.md"
  77. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  78. commit, _ := gitRepo.GetBranchCommit(branch)
  79. expectedFileResponse := getExpectedFileResponse()
  80. fileResponse, err := GetFileResponseFromCommit(repo, commit, branch, treePath)
  81. assert.Nil(t, err)
  82. assert.EqualValues(t, expectedFileResponse, fileResponse)
  83. }