gitea源码

git_lfs_ssh_test.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/url"
  6. "slices"
  7. "strings"
  8. "sync"
  9. "testing"
  10. auth_model "code.gitea.io/gitea/models/auth"
  11. "code.gitea.io/gitea/modules/git/gitcmd"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/web"
  14. "code.gitea.io/gitea/routers/common"
  15. "code.gitea.io/gitea/services/context"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func TestGitLFSSSH(t *testing.T) {
  20. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  21. dstPath := t.TempDir()
  22. apiTestContext := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
  23. var mu sync.Mutex
  24. var routerCalls []string
  25. web.RouteMock(common.RouterMockPointCommonLFS, func(ctx *context.Base) {
  26. mu.Lock()
  27. routerCalls = append(routerCalls, ctx.Req.Method+" "+ctx.Req.URL.Path)
  28. mu.Unlock()
  29. })
  30. withKeyFile(t, "my-testing-key", func(keyFile string) {
  31. t.Run("CreateUserKey", doAPICreateUserKey(apiTestContext, "test-key", keyFile))
  32. cloneURL := createSSHUrl(apiTestContext.GitPath(), u)
  33. t.Run("Clone", doGitClone(dstPath, cloneURL))
  34. cfg, err := setting.CfgProvider.PrepareSaving()
  35. require.NoError(t, err)
  36. cfg.Section("server").Key("LFS_ALLOW_PURE_SSH").SetValue("true")
  37. setting.LFS.AllowPureSSH = true
  38. require.NoError(t, cfg.Save())
  39. _, _, cmdErr := gitcmd.NewCommand("config", "lfs.sshtransfer", "always").RunStdString(t.Context(), &gitcmd.RunOpts{Dir: dstPath})
  40. assert.NoError(t, cmdErr)
  41. lfsCommitAndPushTest(t, dstPath, 10)
  42. })
  43. countBatch := slices.ContainsFunc(routerCalls, func(s string) bool {
  44. return strings.Contains(s, "POST /api/internal/repo/user2/repo1.git/info/lfs/objects/batch")
  45. })
  46. countUpload := slices.ContainsFunc(routerCalls, func(s string) bool {
  47. return strings.Contains(s, "PUT /api/internal/repo/user2/repo1.git/info/lfs/objects/")
  48. })
  49. nonAPIRequests := slices.ContainsFunc(routerCalls, func(s string) bool {
  50. fields := strings.Fields(s)
  51. return !strings.HasPrefix(fields[1], "/api/")
  52. })
  53. assert.NotZero(t, countBatch)
  54. assert.NotZero(t, countUpload)
  55. assert.Zero(t, nonAPIRequests)
  56. })
  57. }