mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-24 13:43:43 +09:00
Since #39229 the commit page header dereferences `.Verification.CommittingUser` when the committer is not the author. `Verification` is `nil` for unsigned commits (see `repo.Diff`), so opening such a commit — a rebased or cherry-picked one, for example — logs a template error and the page comes out truncated: ``` Render failed: failed to render template: repo/commit_page, error: template error: builtin(bindata):repo/commit_page:138:22 : executing "repo/commit_page" at <.Verification.CommittingUser>: nil pointer evaluating interface {}.CommittingUser ``` This guards the access and adds an integration test that creates a commit with distinct author and committer identities and checks the page renders completely (the status stays 200 on a mid-render failure, so the test looks at the body). _The fix was worked out with help from an AI assistant; I reviewed and tested it myself._ --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
48 lines
1.6 KiB
Go
48 lines
1.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"gitea.dev/models/unittest"
|
|
"gitea.dev/modules/reqctx"
|
|
"gitea.dev/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRenderErrorPage(t *testing.T) {
|
|
t.Run("PanicHTML", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
req := &http.Request{URL: &url.URL{}, Header: http.Header{"Accept": []string{"text/html"}}}
|
|
req = req.WithContext(reqctx.NewRequestContextForTest(t))
|
|
renderPanicErrorPage(w, req, errors.New("fake panic error (for test only)"))
|
|
respContent := w.Body.String()
|
|
assert.Contains(t, respContent, `class="page-content status-page-500"`)
|
|
assert.Contains(t, respContent, `</html>`)
|
|
assert.Contains(t, respContent, `lang="en-US"`) // make sure the locale work
|
|
|
|
// the 500 page doesn't have normal pages footer, it makes it easier to distinguish a normal page and a failed page.
|
|
// especially when a sub-template causes page error, the HTTP response code is still 200,
|
|
// the different "footer" is the only way to know whether a page is fully rendered without error.
|
|
assert.False(t, test.IsNormalPageCompleted(respContent))
|
|
})
|
|
t.Run("ServiceUnavailablePlain", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
req := &http.Request{URL: &url.URL{}}
|
|
req = req.WithContext(reqctx.NewRequestContextForTest(t))
|
|
renderServiceUnavailable(w, req)
|
|
assert.Equal(t, "Service Unavailable", w.Body.String())
|
|
})
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
unittest.MainTest(m)
|
|
}
|