gitforge/tests/integration/appearance_settings_test.go
0ko 61091e0027 fix(ui): fall back to default theme from non-existent (#13110)
When a user has a theme in the DB that is not among the themes in the configuration, the following happens to this user's UI:

Image: https://codeberg.org/attachments/bf8d4ff1-8216-4df5-ab90-8dc7e03784d9

The workaround is to manually go to Appearance settings and update the theme.

This can happen if the theme was removed from the server config. For example, admins don't want to have it anymore. Maybe it even was the default theme, which is being saved in the DB during sign up.

It will be useful for Forgejo if we, for example, want to separate colorblind them variants from the actual themes, or if we ever want to remove the Gitea themes. Rel: https://codeberg.org/forgejo/forgejo/pulls/13054.

And instance admins will also find it useful to not have to manually update the DB in case they want to get rid of some custom theme.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13110
Reviewed-by: Robert Wolff <mahlzahn@posteo.de>
2026-06-17 19:18:09 +02:00

81 lines
2.8 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"net/http"
"strings"
"testing"
"forgejo.org/modules/translation"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
)
func TestThemeChange(t *testing.T) {
defer tests.PrepareTestEnv(t)()
locale := translation.NewLocale("en-US")
user := loginUser(t, "user2")
// Verify default theme
testSelectedTheme(t, user, "forgejo-auto", "Forgejo (follow system theme)", "")
// Change theme to forgejo-dark and verify it works fine
testChangeTheme(t, user, "forgejo-dark")
testSelectedTheme(t, user, "forgejo-dark", "Forgejo dark", "")
// Change theme to gitea-dark and also verify that it's name is not translated
testChangeTheme(t, user, "gitea-dark")
testSelectedTheme(t, user, "gitea-dark", "gitea-dark", "")
// Attempt to change theme to a non-existent one and verify that it wasn't
// applied, and that there's an error message
testChangeTheme(t, user, "non-existent")
testSelectedTheme(t, user, "gitea-dark", "gitea-dark", locale.TrString("settings.theme_update_error"))
}
// Test UI fallback in case user's DB entry has a non-existent theme. This can
// happen if a theme was removed from the instance config at some point
func TestUserHasNonExistentTheme(t *testing.T) {
defer tests.PrepareTestEnv(t)()
// user40 has a "frog" theme in the DB
user := loginUser(t, "user40")
// Verify that the UI falls back to the default theme
testSelectedTheme(t, user, "forgejo-auto", "Forgejo (follow system theme)", "")
}
// testSelectedTheme checks that the expected theme is used in html[data-theme]
// and is default on appearance page
func testSelectedTheme(t *testing.T, session *TestSession, expectedTheme, expectedName, expectedErr string) {
t.Helper()
response := session.MakeRequest(t, NewRequest(t, "GET", "/user/settings/appearance"), http.StatusOK)
page := NewHTMLParser(t, response.Body)
dataTheme, dataThemeExists := page.Find("html").Attr("data-theme")
assert.True(t, dataThemeExists)
assert.Equal(t, expectedTheme, dataTheme)
selectedTheme := page.Find("form[action='/user/settings/appearance/theme'] .menu .item.selected")
selectorTheme, selectorThemeExists := selectedTheme.Attr("data-value")
assert.True(t, selectorThemeExists)
assert.Equal(t, expectedTheme, selectorTheme)
assert.Equal(t, expectedName, strings.TrimSpace(selectedTheme.Text()))
if expectedErr != "" {
errMsg := strings.TrimSpace(page.Find("#flash-message").Text())
assert.Equal(t, expectedErr, errMsg)
}
}
// testChangeTheme changes user's theme
func testChangeTheme(t *testing.T, session *TestSession, newTheme string) {
t.Helper()
session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user/settings/appearance/theme", map[string]string{
"theme": newTheme,
}), http.StatusSeeOther)
}