Skip to content

Commit ca5f302

Browse files
wxiaoguangGiteaBot
andauthored
Fix admin config page error, use tests to cover the admin config and 500 error page (#24965)
The admin config page has been broken for many many times, a little refactoring would make this page panic. So, add a test for it, and add another test to cover the 500 error page. Co-authored-by: Giteabot <[email protected]>
1 parent 73b57c2 commit ca5f302

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

modules/test/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ package test
55

66
import (
77
"net/http"
8+
"strings"
89
)
910

1011
// RedirectURL returns the redirect URL of a http response.
1112
func RedirectURL(resp http.ResponseWriter) string {
1213
return resp.Header().Get("Location")
1314
}
15+
16+
func IsNormalPageCompleted(s string) bool {
17+
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
18+
}

options/locale/locale_en-US.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,8 +3039,6 @@ config.git_disable_diff_highlight = Disable Diff Syntax Highlight
30393039
config.git_max_diff_lines = Max Diff Lines (for a single file)
30403040
config.git_max_diff_line_characters = Max Diff Characters (for a single line)
30413041
config.git_max_diff_files = Max Diff Files (to be shown)
3042-
config.git_enable_reflogs = Enable Reflogs
3043-
config.git_reflog_expiry_time = Expiry Time
30443042
config.git_gc_args = GC Arguments
30453043
config.git_migrate_timeout = Migration Timeout
30463044
config.git_mirror_timeout = Mirror Update Timeout

routers/common/errpage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func RenderPanicErrorPage(w http.ResponseWriter, req *http.Request, err any) {
2626

2727
defer func() {
2828
if err := recover(); err != nil {
29-
log.Error("Panic occurs again when rendering error page: %v", err)
29+
log.Error("Panic occurs again when rendering error page: %v. Stack:\n%s", err, log.Stack(2))
3030
}
3131
}()
3232

routers/common/errpage_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package common
5+
6+
import (
7+
"context"
8+
"errors"
9+
"net/http"
10+
"net/http/httptest"
11+
"net/url"
12+
"path/filepath"
13+
"testing"
14+
15+
"code.gitea.io/gitea/models/unittest"
16+
"code.gitea.io/gitea/modules/test"
17+
"code.gitea.io/gitea/modules/web/middleware"
18+
19+
"github.com/stretchr/testify/assert"
20+
)
21+
22+
func TestRenderPanicErrorPage(t *testing.T) {
23+
w := httptest.NewRecorder()
24+
req := &http.Request{URL: &url.URL{}}
25+
req = req.WithContext(middleware.WithContextData(context.Background()))
26+
RenderPanicErrorPage(w, req, errors.New("fake panic error (for test only)"))
27+
respContent := w.Body.String()
28+
assert.Contains(t, respContent, `class="page-content status-page-500"`)
29+
assert.Contains(t, respContent, `</html>`)
30+
31+
// the 500 page doesn't have normal pages footer, it makes it easier to distinguish a normal page and a failed page.
32+
// especially when a sub-template causes page error, the HTTP response code is still 200,
33+
// the different "footer" is the only way to know whether a page is fully rendered without error.
34+
assert.False(t, test.IsNormalPageCompleted(respContent))
35+
}
36+
37+
func TestMain(m *testing.M) {
38+
unittest.MainTest(m, &unittest.TestOptions{
39+
GiteaRootPath: filepath.Join("..", ".."),
40+
})
41+
}

templates/admin/config.tmpl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,6 @@
333333

334334
<div class="ui divider"></div>
335335

336-
<dt>{{.locale.Tr "admin.config.git_enable_reflogs"}}</dt>
337-
<dd>{{if .Git.Reflog.Enabled}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
338-
339-
{{if .Git.Reflog.Enabled}}
340-
<dt>{{.locale.Tr "admin.config.git_reflog_expiry_time"}}</dt>
341-
<dd>{{.locale.Tr "tool.days" .Git.Reflog.Expiration}}</dd>
342-
{{end}}
343-
344-
<div class="ui divider"></div>
345-
346336
<dt>{{.locale.Tr "admin.config.git_migrate_timeout"}}</dt>
347337
<dd>{{.Git.Timeout.Migrate}} {{.locale.Tr "tool.raw_seconds"}}</dd>
348338
<dt>{{.locale.Tr "admin.config.git_mirror_timeout"}}</dt>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/test"
11+
"code.gitea.io/gitea/tests"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestAdminConfig(t *testing.T) {
17+
defer tests.PrepareTestEnv(t)()
18+
19+
session := loginUser(t, "user1")
20+
req := NewRequest(t, "GET", "/admin/config")
21+
resp := session.MakeRequest(t, req, http.StatusOK)
22+
assert.True(t, test.IsNormalPageCompleted(resp.Body.String()))
23+
}

0 commit comments

Comments
 (0)