mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-13 06:20:24 +00:00
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>
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
"testing"
|
|
|
|
"forgejo.org/modules/test"
|
|
"forgejo.org/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestViewTimetrackingControls(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
session := loginUser(t, "user2")
|
|
testViewTimetrackingControls(t, session, "user2", "repo1", "1", true)
|
|
// user2/repo1
|
|
}
|
|
|
|
func TestNotViewTimetrackingControls(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
session := loginUser(t, "user5")
|
|
testViewTimetrackingControls(t, session, "user2", "repo1", "1", false)
|
|
// user2/repo1
|
|
}
|
|
|
|
func TestViewTimetrackingControlsDisabled(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
session := loginUser(t, "user2")
|
|
testViewTimetrackingControls(t, session, "org3", "repo3", "1", false)
|
|
}
|
|
|
|
func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo, issue string, canTrackTime bool) {
|
|
req := NewRequest(t, "GET", path.Join(user, repo, "issues", issue))
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
htmlDoc.AssertElement(t, ".timetrack .issue-start-time", canTrackTime)
|
|
htmlDoc.AssertElement(t, ".timetrack .issue-add-time", canTrackTime)
|
|
|
|
req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{})
|
|
if canTrackTime {
|
|
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
req = NewRequest(t, "GET", test.RedirectURL(resp))
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
htmlDoc = NewHTMLParser(t, resp.Body)
|
|
|
|
events := htmlDoc.doc.Find(".event > span.text")
|
|
assert.Contains(t, events.Last().Text(), "started working")
|
|
|
|
htmlDoc.AssertElement(t, ".timetrack .issue-stop-time", true)
|
|
htmlDoc.AssertElement(t, ".timetrack .issue-cancel-time", true)
|
|
|
|
req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{})
|
|
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
req = NewRequest(t, "GET", test.RedirectURL(resp))
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
htmlDoc = NewHTMLParser(t, resp.Body)
|
|
|
|
events = htmlDoc.doc.Find(".event > span.text")
|
|
assert.Contains(t, events.Last().Text(), "stopped working")
|
|
htmlDoc.AssertElement(t, ".event .detail .octicon-clock", true)
|
|
} else {
|
|
session.MakeRequest(t, req, http.StatusNotFound)
|
|
}
|
|
}
|