gitea源码

view_home_test.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "testing"
  7. "code.gitea.io/gitea/models/unittest"
  8. git_module "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/services/contexttest"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestViewHomeSubmoduleRedirect(t *testing.T) {
  13. unittest.PrepareTestEnv(t)
  14. ctx, _ := contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule")
  15. submodule := git_module.NewCommitSubmoduleFile("/user2/repo1", "test-submodule", "../repo-other", "any-ref-id")
  16. handleRepoViewSubmodule(ctx, submodule)
  17. assert.Equal(t, http.StatusSeeOther, ctx.Resp.WrittenStatus())
  18. assert.Equal(t, "/user2/repo-other/tree/any-ref-id", ctx.Resp.Header().Get("Location"))
  19. ctx, _ = contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule")
  20. submodule = git_module.NewCommitSubmoduleFile("/user2/repo1", "test-submodule", "https://other/user2/repo-other.git", "any-ref-id")
  21. handleRepoViewSubmodule(ctx, submodule)
  22. // do not auto-redirect for external URLs, to avoid open redirect or phishing
  23. assert.Equal(t, http.StatusNotFound, ctx.Resp.WrittenStatus())
  24. ctx, respWriter := contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule?only_content=true")
  25. submodule = git_module.NewCommitSubmoduleFile("/user2/repo1", "test-submodule", "../repo-other", "any-ref-id")
  26. handleRepoViewSubmodule(ctx, submodule)
  27. assert.Equal(t, http.StatusOK, ctx.Resp.WrittenStatus())
  28. assert.Equal(t, `<a href="/user2/repo-other/tree/any-ref-id">/user2/repo-other/tree/any-ref-id</a>`, respWriter.Body.String())
  29. }