gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package files
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/unittest"
  7. api "code.gitea.io/gitea/modules/structs"
  8. "code.gitea.io/gitea/modules/util"
  9. "code.gitea.io/gitea/services/contexttest"
  10. _ "code.gitea.io/gitea/models/actions"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestMain(m *testing.M) {
  14. unittest.MainTest(m)
  15. }
  16. func TestGetContents(t *testing.T) {
  17. unittest.PrepareTestEnv(t)
  18. ctx, _ := contexttest.MockContext(t, "user2/repo1")
  19. ctx.SetPathParam("id", "1")
  20. contexttest.LoadRepo(t, ctx, 1)
  21. contexttest.LoadRepoCommit(t, ctx)
  22. contexttest.LoadUser(t, ctx, 2)
  23. contexttest.LoadGitRepo(t, ctx)
  24. // GetContentsOrList's behavior is fully tested in integration tests, so we don't need to test it here.
  25. t.Run("GetBlobBySHA", func(t *testing.T) {
  26. sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  27. ctx.SetPathParam("id", "1")
  28. ctx.SetPathParam("sha", sha)
  29. gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.PathParam("sha"))
  30. expectedGBR := &api.GitBlobResponse{
  31. Content: util.ToPointer("dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK"),
  32. Encoding: util.ToPointer("base64"),
  33. URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  34. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  35. Size: 180,
  36. }
  37. assert.NoError(t, err)
  38. assert.Equal(t, expectedGBR, gbr)
  39. })
  40. }