0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-26 11:04:27 -04:00
This commit is contained in:
wxiaoguang 2025-07-02 09:02:50 +08:00
parent 2f9231daf6
commit 127274defc
8 changed files with 46 additions and 57 deletions

View File

@ -65,7 +65,7 @@ func notificationUnreadCount(ctx *context.Context) int64 {
return count
}
type pageHeadNavbarDataType struct {
type pageGlobalDataType struct {
IsSigned bool
IsSiteAdmin bool
@ -73,11 +73,11 @@ type pageHeadNavbarDataType struct {
GetActiveStopwatch func() *StopwatchTmplInfo
}
func PageHeadNavbarData(ctx *context.Context) {
var data pageHeadNavbarDataType
func PageGlobalData(ctx *context.Context) {
var data pageGlobalDataType
data.IsSigned = ctx.Doer != nil
data.IsSiteAdmin = ctx.Doer != nil && ctx.Doer.IsAdmin
data.GetNotificationUnreadCount = sync.OnceValue(func() int64 { return notificationUnreadCount(ctx) })
data.GetActiveStopwatch = sync.OnceValue(func() *StopwatchTmplInfo { return getActiveStopwatch(ctx) })
ctx.Data["HeadNavbarData"] = data
ctx.Data["PageGlobalData"] = data
}

View File

@ -281,7 +281,7 @@ func Routes() *web.Router {
}
mid = append(mid, goGet)
mid = append(mid, common.PageHeadNavbarData)
mid = append(mid, common.PageGlobalData)
webRoutes := web.NewRouter()
webRoutes.Use(mid...)

View File

@ -7,7 +7,7 @@
<!-- mobile right menu, it must be here because in mobile view, each item is a flex column, the first item is a full row column -->
<div class="ui secondary menu navbar-mobile-right only-mobile">
{{template "base/head_navbar_icons" dict "HeadNavbarData" .HeadNavbarData}}
{{template "base/head_navbar_icons" dict "PageGlobalData" .PageGlobalData}}
<button class="item ui icon mini button tw-m-0" id="navbar-expand-toggle" aria-label="{{ctx.Locale.Tr "home.nav_menu"}}">{{svg "octicon-three-bars"}}</button>
</div>
@ -62,7 +62,7 @@
</div><!-- end content avatar menu -->
</div><!-- end dropdown avatar menu -->
{{else if .IsSigned}}
{{template "base/head_navbar_icons" dict "ItemExtraClass" "not-mobile" "HeadNavbarData" .HeadNavbarData}}
{{template "base/head_navbar_icons" dict "ItemExtraClass" "not-mobile" "PageGlobalData" .PageGlobalData}}
<div class="ui dropdown jump item" data-tooltip-content="{{ctx.Locale.Tr "create_new"}}">
<span class="text">
{{svg "octicon-plus"}}
@ -120,14 +120,6 @@
{{svg "octicon-question"}}
{{ctx.Locale.Tr "help"}}
</a>
{{if .IsAdmin}}
<div class="divider"></div>
<a class="{{if .PageIsAdmin}}active {{end}}item" href="{{AppSubUrl}}/-/admin">
{{svg "octicon-server"}}
{{ctx.Locale.Tr "admin_panel"}}
</a>
{{end}}
<div class="divider"></div>
<a class="item link-action" href data-url="{{AppSubUrl}}/user/logout">
{{svg "octicon-sign-out"}}
@ -149,7 +141,7 @@
{{end}}
</div><!-- end full right menu -->
{{$activeStopwatch := and .HeadNavbarData (call .HeadNavbarData.GetActiveStopwatch)}}
{{$activeStopwatch := and .PageGlobalData (call .PageGlobalData.GetActiveStopwatch)}}
{{if $activeStopwatch}}
<div class="active-stopwatch-popup tippy-target">
<div class="tw-flex tw-items-center tw-gap-2 tw-p-3">

View File

@ -1,6 +1,6 @@
{{- $itemExtraClass := .ItemExtraClass -}}
{{- $data := .HeadNavbarData -}}
{{if and $data $data.IsSigned}}{{/* data may not exist, for example: rendering 503 page before the HeadNavbarData middleware */}}
{{- $data := .PageGlobalData -}}
{{if and $data $data.IsSigned}}{{/* data may not exist, for example: rendering 503 page before the PageGlobalData middleware */}}
{{- $activeStopwatch := call $data.GetActiveStopwatch -}}
{{- $notificationUnreadCount := call $data.GetNotificationUnreadCount -}}
{{if $activeStopwatch}}

View File

@ -7,6 +7,7 @@
<body>
<a class="swagger-back-link" href="{{AppSubUrl}}/">{{svg "octicon-reply"}}{{ctx.Locale.Tr "return_to_gitea"}}</a>
<div id="swagger-ui" data-source="{{AppSubUrl}}/swagger.{{.APIJSONVersion}}.json"></div>
<footer class="page-footer"></footer>
<script src="{{AssetUrlPrefix}}/js/swagger.js?v={{AssetVersion}}"></script>
</body>
</html>

View File

@ -1,6 +1,6 @@
<div role="main" aria-label="{{.Title}}" class="page-content user notification" id="notification_div" data-sequence-number="{{.SequenceNumber}}">
<div class="ui container">
{{$notificationUnreadCount := call .NotificationUnreadCount ctx}}
{{$notificationUnreadCount := call .PageGlobalData.GetNotificationUnreadCount}}
<div class="tw-flex tw-items-center tw-justify-between tw-mb-[--page-spacing]">
<div class="small-menu-items ui compact tiny menu">
<a class="{{if eq .Status 1}}active {{end}}item" href="{{AppSubUrl}}/notifications?q=unread">

View File

@ -148,6 +148,9 @@ func (s *TestSession) GetCookieFlashMessage() *middleware.Flash {
func (s *TestSession) MakeRequest(t testing.TB, rw *RequestWrapper, expectedStatus int) *httptest.ResponseRecorder {
t.Helper()
if s == nil {
return MakeRequest(t, rw, expectedStatus)
}
req := rw.Request
baseURL, err := url.Parse(setting.AppURL)
assert.NoError(t, err)

View File

@ -17,38 +17,48 @@ import (
"github.com/stretchr/testify/assert"
)
func TestLinksNoLogin(t *testing.T) {
func assertLinkPageComplete(t *testing.T, session *TestSession, link string) {
req := NewRequest(t, "GET", link)
resp := session.MakeRequest(t, req, http.StatusOK)
assert.True(t, test.IsNormalPageCompleted(resp.Body.String()), "Page did not complete: "+link)
}
func TestLinks(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("NoLogin", testLinksNoLogin)
t.Run("RedirectsNoLogin", testLinksRedirectsNoLogin)
t.Run("NoLoginNotExist", testLinksNoLoginNotExist)
t.Run("AsUser", testLinksAsUser)
t.Run("RepoCommon", testLinksRepoCommon)
}
func testLinksNoLogin(t *testing.T) {
links := []string{
"/",
"/explore/repos",
"/explore/repos?q=test",
"/explore/users",
"/explore/users?q=test",
"/explore/organizations",
"/explore/organizations?q=test",
"/",
"/user/sign_up",
"/user/login",
"/user/forgot_password",
"/api/swagger",
"/user2/repo1",
"/user2/repo1/",
"/user2/repo1/projects",
"/user2/repo1/projects/1",
"/user2/repo1/releases/tag/delete-tag", // It's the only one existing record on release.yml which has is_tag: true
"/.well-known/security.txt",
"/api/swagger",
}
for _, link := range links {
req := NewRequest(t, "GET", link)
MakeRequest(t, req, http.StatusOK)
assertLinkPageComplete(t, nil, link)
}
MakeRequest(t, NewRequest(t, "GET", "/.well-known/security.txt"), http.StatusOK)
}
func TestRedirectsNoLogin(t *testing.T) {
defer tests.PrepareTestEnv(t)()
func testLinksRedirectsNoLogin(t *testing.T) {
redirects := []struct{ from, to string }{
{"/user2/repo1/commits/master", "/user2/repo1/commits/branch/master"},
{"/user2/repo1/src/master", "/user2/repo1/src/branch/master"},
@ -68,9 +78,7 @@ func TestRedirectsNoLogin(t *testing.T) {
}
}
func TestNoLoginNotExist(t *testing.T) {
defer tests.PrepareTestEnv(t)()
func testLinksNoLoginNotExist(t *testing.T) {
links := []string{
"/user5/repo4/projects",
"/user5/repo4/projects/3",
@ -82,7 +90,8 @@ func TestNoLoginNotExist(t *testing.T) {
}
}
func testLinksAsUser(userName string, t *testing.T) {
func testLinksAsUser(t *testing.T) {
session := loginUser(t, "user2")
links := []string{
"/explore/repos",
"/explore/repos?q=test",
@ -130,18 +139,14 @@ func testLinksAsUser(userName string, t *testing.T) {
"/user/settings/repos",
}
session := loginUser(t, userName)
for _, link := range links {
req := NewRequest(t, "GET", link)
session.MakeRequest(t, req, http.StatusOK)
assertLinkPageComplete(t, session, link)
}
reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName)
reqAPI := NewRequestf(t, "GET", "/api/v1/users/user2/repos")
respAPI := MakeRequest(t, reqAPI, http.StatusOK)
var apiRepos []*api.Repository
DecodeJSON(t, respAPI, &apiRepos)
repoLinks := []string{
"",
"/issues",
@ -164,24 +169,15 @@ func testLinksAsUser(userName string, t *testing.T) {
"/wiki/?action=_new",
"/activity",
}
for _, repo := range apiRepos {
for _, link := range repoLinks {
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s%s", userName, repo.Name, link))
session.MakeRequest(t, req, http.StatusOK)
link = fmt.Sprintf("/user2/%s%s", repo.Name, link)
assertLinkPageComplete(t, session, link)
}
}
}
func TestLinksLogin(t *testing.T) {
defer tests.PrepareTestEnv(t)()
testLinksAsUser("user2", t)
}
func TestRepoLinks(t *testing.T) {
defer tests.PrepareTestEnv(t)()
func testLinksRepoCommon(t *testing.T) {
// repo1 has enabled almost features, so we can test most links
repoLink := "/user2/repo1"
links := []string{
@ -192,21 +188,18 @@ func TestRepoLinks(t *testing.T) {
// anonymous user
for _, link := range links {
req := NewRequest(t, "GET", repoLink+link)
MakeRequest(t, req, http.StatusOK)
assertLinkPageComplete(t, nil, repoLink+link)
}
// admin/owner user
session := loginUser(t, "user1")
for _, link := range links {
req := NewRequest(t, "GET", repoLink+link)
session.MakeRequest(t, req, http.StatusOK)
assertLinkPageComplete(t, session, repoLink+link)
}
// non-admin non-owner user
session = loginUser(t, "user2")
for _, link := range links {
req := NewRequest(t, "GET", repoLink+link)
session.MakeRequest(t, req, http.StatusOK)
assertLinkPageComplete(t, session, repoLink+link)
}
}