mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
127 lines
4.6 KiB
Go
127 lines
4.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc. 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)
|
|
})
|
|
})
|
|
}
|