forgejo/tests/integration/appearance_settings_test.go
Gusted a4642af51a
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing-integration / test-mariadb (v10.6) (push) Waiting to run
testing-integration / test-mariadb (v11.8) (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
feat: replace cross origin protection (#9830)
Replace the anti-CSRF token with a [cross origin protection by Go](https://go.dev/doc/go1.25#nethttppkgnethttp) that uses a stateless way of verifying if a request was cross origin or not. This allows is to remove al lot of code and replace it with a few lines of code and we no longer have to hand roll this protection. The new protection uses indicators by the browser itself that indicate if the request is cross-origin, thus we no longer have to take care of ensuring the generated CSRF token is passed back to the server any request by the the browser will have send this indicator.

Resolves forgejo/forgejo#3538

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9830
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-10-29 22:43:22 +01:00

56 lines
1.9 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/tests"
"github.com/stretchr/testify/assert"
)
func TestThemeChange(t *testing.T) {
defer tests.PrepareTestEnv(t)()
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")
}
// 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 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()))
}
// testSelectedTheme 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)
}