mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-16 07:46:35 +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>
137 lines
4.8 KiB
Go
137 lines
4.8 KiB
Go
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package integration
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"forgejo.org/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestUserProfileFollows is a test for user following counters, pages and titles.
|
|
// It tests that:
|
|
// - Followers and Following tabs always have titles present and always use correct plurals
|
|
// - Followers and Following lists have correct amounts of items
|
|
// - %d followers and %following counters are always present and always have correct numbers and use correct plurals
|
|
func TestUserProfileFollows(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
// This test needs 3 users to check for all possible states
|
|
// The accounts of user3 and user4 are not functioning
|
|
user1 := loginUser(t, "user1")
|
|
user2 := loginUser(t, "user2")
|
|
user5 := loginUser(t, "user5")
|
|
|
|
followersLink := "#profile-avatar-card a[href='/user1?tab=followers']"
|
|
followingLink := "#profile-avatar-card a[href='/user1?tab=following']"
|
|
listHeader := ".user-cards h2"
|
|
listItems := ".user-cards .list"
|
|
listMsg := ".user-cards > div"
|
|
|
|
// = No follows =
|
|
|
|
var followCount int
|
|
|
|
// Request the profile of user1, the Followers tab
|
|
response := user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK)
|
|
page := NewHTMLParser(t, response.Body)
|
|
|
|
// Verify that user1 has no followers
|
|
testSelectorEquals(t, page, followersLink, "0 followers")
|
|
testSelectorEquals(t, page, listHeader, "Followers")
|
|
page.AssertElement(t, listItems, false)
|
|
page.AssertElement(t, listMsg, true)
|
|
|
|
// Request the profile of user1, the Following tab
|
|
response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK)
|
|
page = NewHTMLParser(t, response.Body)
|
|
|
|
// Verify that user1 does not follow anyone
|
|
testSelectorEquals(t, page, followingLink, "0 following")
|
|
testSelectorEquals(t, page, listHeader, "Following")
|
|
page.AssertElement(t, listItems, false)
|
|
page.AssertElement(t, listMsg, true)
|
|
|
|
// Make user1 and user2 follow each other
|
|
testUserFollowUser(t, user1, "user2")
|
|
testUserFollowUser(t, user2, "user1")
|
|
|
|
// = 1 follow each =
|
|
|
|
followCount++
|
|
|
|
// Request the profile of user1, the Followers tab
|
|
response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK)
|
|
page = NewHTMLParser(t, response.Body)
|
|
|
|
// Verify it is now followed by 1 user
|
|
testSelectorEquals(t, page, followersLink, "1 follower")
|
|
testSelectorEquals(t, page, listHeader, "Follower")
|
|
testListCount(t, page, listItems, followCount)
|
|
page.AssertElement(t, listMsg, false)
|
|
|
|
// Request the profile of user1, the Following tab
|
|
response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK)
|
|
page = NewHTMLParser(t, response.Body)
|
|
|
|
// Verify it now follows follows 1 user
|
|
testSelectorEquals(t, page, followingLink, "1 following")
|
|
testSelectorEquals(t, page, listHeader, "Following")
|
|
testListCount(t, page, listItems, followCount)
|
|
page.AssertElement(t, listMsg, false)
|
|
|
|
// Make user1 and user3 follow each other
|
|
testUserFollowUser(t, user1, "user5")
|
|
testUserFollowUser(t, user5, "user1")
|
|
|
|
// = 2 follows =
|
|
|
|
followCount++
|
|
|
|
// Request the profile of user1, the Followers tab
|
|
response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK)
|
|
page = NewHTMLParser(t, response.Body)
|
|
|
|
// Verify it is now followed by 2 users
|
|
testSelectorEquals(t, page, followersLink, "2 followers")
|
|
testSelectorEquals(t, page, listHeader, "Followers")
|
|
testListCount(t, page, listItems, followCount)
|
|
page.AssertElement(t, listMsg, false)
|
|
|
|
// Request the profile of user1, the Following tab
|
|
response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK)
|
|
page = NewHTMLParser(t, response.Body)
|
|
|
|
// Verify it now follows follows 2 users
|
|
testSelectorEquals(t, page, followingLink, "2 following")
|
|
testSelectorEquals(t, page, listHeader, "Following")
|
|
testListCount(t, page, listItems, followCount)
|
|
page.AssertElement(t, listMsg, false)
|
|
}
|
|
|
|
// testUserFollowUser simply follows a user `following` by session of user `follower`
|
|
func testUserFollowUser(t *testing.T, follower *TestSession, following string) {
|
|
t.Helper()
|
|
follower.MakeRequest(t, NewRequest(t, "POST", fmt.Sprintf("/%s?action=follow", following)), http.StatusOK)
|
|
}
|
|
|
|
// testSelectorEquals prevents duplication of a lot of code for tests with many checks
|
|
func testSelectorEquals(t *testing.T, page *HTMLDoc, selector, expectedContent string) {
|
|
t.Helper()
|
|
element := page.Find(selector)
|
|
content := strings.TrimSpace(element.Text())
|
|
assert.Equal(t, expectedContent, content)
|
|
}
|
|
|
|
// testListCount checks that the list on the page has the right amount of items
|
|
func testListCount(t *testing.T, page *HTMLDoc, selector string, expectedCount int) {
|
|
t.Helper()
|
|
itemCount := page.Find(selector).Children().Length()
|
|
assert.Equal(t, expectedCount, itemCount)
|
|
}
|