Files
forge/tests/integration/config_instance_test.go
zeekayandHanzo Dev 9a07b4d089 legal: Hanzo AI, Inc. copyright above the upstream notices
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git),
so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form
used by the handful of files that had ours.

Shape is now:

	// Copyright 2026 Hanzo AI, Inc. All rights reserved.
	// Copyright 2015 The Gogs Authors. All rights reserved.
	// Copyright 2019 The Gitea Authors. All rights reserved.
	// SPDX-License-Identifier: MIT

Ours on top, theirs retained verbatim. Retained because MIT requires it, and
separately because they are true — those authors did write that code.

Not renamed to "The Git Authors": Git is a different project under a different
license, and putting its name here would attribute this code to people who have
nothing to do with it. Adding our line is the fix; overwriting theirs would have
been both a licence violation and a false statement.

gofmt still reports import-order drift in some of these files. It predates this
change (verified against a stash) and is left alone rather than burying a
one-line-per-file legal change under 3000 files of unrelated reordering.

Build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 13:49:52 -07:00

128 lines
4.7 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
"time"
system_model "github.com/hanzoai/git/models/system"
"github.com/hanzoai/git/modules/json"
"github.com/hanzoai/git/modules/setting"
"github.com/hanzoai/git/modules/setting/config"
"github.com/hanzoai/git/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func mockSystemConfig[T any](t *testing.T, opt *config.Option[T], v T) func() {
jsonBuf, _ := json.Marshal(v)
old := opt.Value(t.Context())
require.NoError(t, system_model.SetSettings(t.Context(), map[string]string{opt.DynKey(): string(jsonBuf)}))
config.GetDynGetter().InvalidateCache()
return func() {
jsonBuf, _ := json.Marshal(old)
require.NoError(t, system_model.SetSettings(t.Context(), map[string]string{opt.DynKey(): string(jsonBuf)}))
config.GetDynGetter().InvalidateCache()
}
}
func TestInstance(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("WebBanner", func(t *testing.T) {
t.Run("Visibility", func(t *testing.T) {
defer mockSystemConfig(t, setting.Config().Instance.WebBanner, setting.WebBannerType{
DisplayEnabled: true,
ContentMessage: "Planned **upgrade** in progress.",
})()
t.Run("AnonymousUserSeesBanner", func(t *testing.T) {
resp := MakeRequest(t, NewRequest(t, "GET", "/"), http.StatusOK)
assert.Contains(t, resp.Body.String(), "Planned <strong>upgrade</strong> in progress.")
})
t.Run("NormalUserSeesBanner", func(t *testing.T) {
sess := loginUser(t, "user2")
resp := sess.MakeRequest(t, NewRequest(t, "GET", "/user/settings"), http.StatusOK)
assert.Contains(t, resp.Body.String(), "Planned <strong>upgrade</strong> in progress.")
})
t.Run("AdminSeesBannerWithoutEditHint", func(t *testing.T) {
sess := loginUser(t, "user1")
resp := sess.MakeRequest(t, NewRequest(t, "GET", "/-/admin"), http.StatusOK)
assert.Contains(t, resp.Body.String(), "Planned <strong>upgrade</strong> in progress.")
assert.NotContains(t, resp.Body.String(), "Edit this banner")
})
t.Run("APIRequestUnchanged", func(t *testing.T) {
MakeRequest(t, NewRequest(t, "GET", "/v1/version"), http.StatusOK)
})
})
t.Run("TimeWindow", func(t *testing.T) {
now := time.Now().Unix()
defer mockSystemConfig(t, setting.Config().Instance.WebBanner, setting.WebBannerType{
DisplayEnabled: true,
ContentMessage: "Future banner",
StartTimeUnix: now + 3600,
EndTimeUnix: now + 7200,
})()
resp := MakeRequest(t, NewRequest(t, "GET", "/"), http.StatusOK)
assert.NotContains(t, resp.Body.String(), "Future banner")
defer mockSystemConfig(t, setting.Config().Instance.WebBanner, setting.WebBannerType{
DisplayEnabled: true,
ContentMessage: "Expired banner",
StartTimeUnix: now - 7200,
EndTimeUnix: now - 3600,
})()
resp = MakeRequest(t, NewRequest(t, "GET", "/"), http.StatusOK)
assert.NotContains(t, resp.Body.String(), "Expired banner")
})
})
t.Run("MaintenanceMode", func(t *testing.T) {
defer mockSystemConfig(t, setting.Config().Instance.WebBanner, setting.WebBannerType{
DisplayEnabled: true,
ContentMessage: "MaintenanceModeBanner",
})()
defer mockSystemConfig(t, setting.Config().Instance.MaintenanceMode, setting.MaintenanceModeType{AdminWebAccessOnly: true})()
t.Run("AnonymousUser", func(t *testing.T) {
req := NewRequest(t, "GET", "/")
req.Header.Add("Accept", "text/html")
resp := MakeRequest(t, req, http.StatusServiceUnavailable)
assert.Contains(t, resp.Body.String(), "MaintenanceModeBanner")
assert.Contains(t, resp.Body.String(), `href="/user/login"`) // it must contain the login link
MakeRequest(t, NewRequest(t, "GET", "/user/login"), http.StatusOK)
MakeRequest(t, NewRequest(t, "GET", "/-/admin"), http.StatusSeeOther)
MakeRequest(t, NewRequest(t, "GET", "/v1/internal/dummy"), http.StatusForbidden)
})
t.Run("AdminLogin", func(t *testing.T) {
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{"user_name": "user1", "password": userPassword})
resp := MakeRequest(t, req, http.StatusSeeOther)
assert.Equal(t, "/-/admin", resp.Header().Get("Location"))
sess := loginUser(t, "user1")
req = NewRequest(t, "GET", "/")
req.Header.Add("Accept", "text/html")
resp = sess.MakeRequest(t, req, http.StatusServiceUnavailable)
assert.Contains(t, resp.Body.String(), "MaintenanceModeBanner")
resp = sess.MakeRequest(t, NewRequest(t, "GET", "/user/login"), http.StatusSeeOther)
assert.Equal(t, "/-/admin", resp.Header().Get("Location"))
sess.MakeRequest(t, NewRequest(t, "GET", "/-/admin"), http.StatusOK)
})
})
}