forgejo/tests/integration/pull_wip_convert_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

57 lines
2.1 KiB
Go

// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"net/url"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPullWIPConvertSidebar(t *testing.T) {
onApplicationRun(t, func(t *testing.T, u *url.URL) {
testRepo := "repo1"
branchOld := "master"
branchNew := "wip"
userOwner := "user2"
userUnrelated := "user4"
sessionOwner := loginUser(t, userOwner) // Owner of the repo. Expected to see the offers.
sessionUnrelated := loginUser(t, userUnrelated) // Unrelated user. Not expected to see the offers.
// Create a branch with commit, open a PR and check who is seeing the Add WIP offering
testEditFileToNewBranch(t, sessionOwner, userOwner, testRepo, branchOld, branchNew, "README.md", "test of wip offering")
url := path.Join(userOwner, testRepo, "compare", branchOld+"..."+branchNew)
req := NewRequestWithValues(t, "POST", url,
map[string]string{
"title": "pull used for testing wip offering",
},
)
sessionOwner.MakeRequest(t, req, http.StatusOK)
testPullWIPConvertSidebar(t, sessionOwner, userOwner, testRepo, "6", "Still in progress? Add WIP: prefix")
testPullWIPConvertSidebar(t, sessionUnrelated, userOwner, testRepo, "6", "")
// Add WIP: prefix and check who is seeing the Remove WIP offering
req = NewRequestWithValues(t, "POST", path.Join(userOwner, testRepo, "pulls/6/title"),
map[string]string{
"title": "WIP: pull used for testing wip offering",
},
)
sessionOwner.MakeRequest(t, req, http.StatusOK)
testPullWIPConvertSidebar(t, sessionOwner, userOwner, testRepo, "6", "Ready for review? Remove WIP: prefix")
testPullWIPConvertSidebar(t, sessionUnrelated, userOwner, testRepo, "6", "")
})
}
func testPullWIPConvertSidebar(t *testing.T, session *TestSession, user, repo, pullNum, expected string) {
t.Helper()
req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullNum))
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
text := strings.TrimSpace(doc.doc.Find(".toggle-wip a").Text())
assert.Equal(t, expected, text)
}