mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
This separates our the user mute handling from the typeconverter code, and creates a new "mutes" filter type (in a similar vein to the visibility filter) subpkg with its own result cache. This is a heavy mix of both chore given that mute calculation shouldn't have been handled in the conversion to frontend API types, and a performance bonus since we don't need to load and calculate so many things each time, just the single result each time with all necessary invalidation handled by database cache hooks. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4202 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
238 lines
7.7 KiB
Go
238 lines
7.7 KiB
Go
// GoToSocial
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package polls_test
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"net/http"
|
|
"testing"
|
|
|
|
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
|
|
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
|
|
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
|
|
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
|
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
|
"code.superseriousbusiness.org/gotosocial/internal/media"
|
|
"code.superseriousbusiness.org/gotosocial/internal/processing/common"
|
|
"code.superseriousbusiness.org/gotosocial/internal/processing/polls"
|
|
"code.superseriousbusiness.org/gotosocial/internal/state"
|
|
"code.superseriousbusiness.org/gotosocial/internal/typeutils"
|
|
"code.superseriousbusiness.org/gotosocial/testrig"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type PollTestSuite struct {
|
|
suite.Suite
|
|
state state.State
|
|
visFilter *visibility.Filter
|
|
muteFilter *mutes.Filter
|
|
polls polls.Processor
|
|
|
|
testAccounts map[string]*gtsmodel.Account
|
|
testPolls map[string]*gtsmodel.Poll
|
|
}
|
|
|
|
func (suite *PollTestSuite) SetupTest() {
|
|
testrig.InitTestConfig()
|
|
testrig.InitTestLog()
|
|
suite.state.Caches.Init()
|
|
testrig.StartNoopWorkers(&suite.state)
|
|
testrig.NewTestDB(&suite.state)
|
|
converter := typeutils.NewConverter(&suite.state)
|
|
controller := testrig.NewTestTransportController(&suite.state, nil)
|
|
mediaMgr := media.NewManager(&suite.state)
|
|
federator := testrig.NewTestFederator(&suite.state, controller, mediaMgr)
|
|
suite.visFilter = visibility.NewFilter(&suite.state)
|
|
suite.muteFilter = mutes.NewFilter(&suite.state)
|
|
common := common.New(&suite.state, mediaMgr, converter, federator, suite.visFilter, suite.muteFilter)
|
|
suite.polls = polls.New(&common, &suite.state, converter)
|
|
}
|
|
|
|
func (suite *PollTestSuite) TearDownTest() {
|
|
testrig.StopWorkers(&suite.state)
|
|
testrig.StandardDBTeardown(suite.state.DB)
|
|
}
|
|
|
|
func (suite *PollTestSuite) TestPollGet() {
|
|
// Create a new context for this test.
|
|
ctx, cncl := context.WithCancel(suite.T().Context())
|
|
defer cncl()
|
|
|
|
// Perform test for all requester + poll combos.
|
|
for _, account := range suite.testAccounts {
|
|
for _, poll := range suite.testPolls {
|
|
suite.testPollGet(ctx, account, poll)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (suite *PollTestSuite) testPollGet(ctx context.Context, requester *gtsmodel.Account, poll *gtsmodel.Poll) {
|
|
// Ensure poll model is fully populated before anything.
|
|
if err := suite.state.DB.PopulatePoll(ctx, poll); err != nil {
|
|
suite.T().Fatalf("error populating poll: %v", err)
|
|
}
|
|
|
|
var check func(*apimodel.Poll, gtserror.WithCode) bool
|
|
|
|
switch {
|
|
case !pollIsVisible(suite.visFilter, ctx, requester, poll):
|
|
// Poll should not be visible to requester, this should
|
|
// return an error code 404 (to prevent info leak).
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll == nil && err.Code() == http.StatusNotFound
|
|
}
|
|
|
|
default:
|
|
// All other cases should succeed! i.e. no error and poll returned.
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll != nil && err == nil
|
|
}
|
|
}
|
|
|
|
// Perform the poll vote and check the expected response.
|
|
if !check(suite.polls.PollGet(ctx, requester, poll.ID)) {
|
|
suite.T().Errorf("unexpected response for poll get by %s", requester.DisplayName)
|
|
}
|
|
|
|
}
|
|
|
|
func (suite *PollTestSuite) TestPollVote() {
|
|
// Create a new context for this test.
|
|
ctx, cncl := context.WithCancel(suite.T().Context())
|
|
defer cncl()
|
|
|
|
// randomChoices generates random vote choices in poll.
|
|
randomChoices := func(poll *gtsmodel.Poll) []int {
|
|
var max int
|
|
if *poll.Multiple {
|
|
max = len(poll.Options)
|
|
} else {
|
|
max = 1
|
|
}
|
|
count := 1 + rand.Intn(max)
|
|
choices := make([]int, count)
|
|
for i := range choices {
|
|
choices[i] = rand.Intn(len(poll.Options))
|
|
}
|
|
return choices
|
|
}
|
|
|
|
// Perform test for all requester + poll combos.
|
|
for _, account := range suite.testAccounts {
|
|
for _, poll := range suite.testPolls {
|
|
// Generate some valid choices and test.
|
|
choices := randomChoices(poll)
|
|
suite.testPollVote(ctx,
|
|
account,
|
|
poll,
|
|
choices,
|
|
)
|
|
|
|
// Test with empty choices.
|
|
suite.testPollVote(ctx,
|
|
account,
|
|
poll,
|
|
nil,
|
|
)
|
|
|
|
// Test with out of range choice.
|
|
suite.testPollVote(ctx,
|
|
account,
|
|
poll,
|
|
[]int{len(poll.Options)},
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (suite *PollTestSuite) testPollVote(ctx context.Context, requester *gtsmodel.Account, poll *gtsmodel.Poll, choices []int) {
|
|
// Ensure poll model is fully populated before anything.
|
|
if err := suite.state.DB.PopulatePoll(ctx, poll); err != nil {
|
|
suite.T().Fatalf("error populating poll: %v", err)
|
|
}
|
|
|
|
var check func(*apimodel.Poll, gtserror.WithCode) bool
|
|
|
|
switch {
|
|
case !poll.ClosedAt.IsZero():
|
|
// Poll is already closed, i.e. no new votes allowed!
|
|
// This should return an error 422 (unprocessable entity).
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll == nil && err.Code() == http.StatusUnprocessableEntity
|
|
}
|
|
|
|
case !voteChoicesAreValid(poll, choices):
|
|
// These are invalid vote choices, this should return
|
|
// an error code 400 to indicate invalid request data.
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll == nil && err.Code() == http.StatusBadRequest
|
|
}
|
|
|
|
case poll.Status.AccountID == requester.ID:
|
|
// Immediately we know that poll owner cannot vote in
|
|
// their own poll. this should return an error 422.
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll == nil && err.Code() == http.StatusUnprocessableEntity
|
|
}
|
|
|
|
case !pollIsVisible(suite.visFilter, ctx, requester, poll):
|
|
// Poll should not be visible to requester, this should
|
|
// return an error code 404 (to prevent info leak).
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll == nil && err.Code() == http.StatusNotFound
|
|
}
|
|
|
|
default:
|
|
// All other cases should succeed! i.e. no error and poll returned.
|
|
check = func(poll *apimodel.Poll, err gtserror.WithCode) bool {
|
|
return poll != nil && err == nil
|
|
}
|
|
}
|
|
|
|
// Perform the poll vote and check the expected response.
|
|
if !check(suite.polls.PollVote(ctx, requester, poll.ID, choices)) {
|
|
suite.T().Errorf("unexpected response for poll vote by %s with %v", requester.DisplayName, choices)
|
|
}
|
|
}
|
|
|
|
// voteChoicesAreValid is a utility function to check whether choices are valid for poll.
|
|
func voteChoicesAreValid(poll *gtsmodel.Poll, choices []int) bool {
|
|
if len(choices) == 0 || !*poll.Multiple && len(choices) > 1 {
|
|
// Invalid number of vote choices.
|
|
return false
|
|
}
|
|
for _, choice := range choices {
|
|
if choice < 0 || choice >= len(poll.Options) {
|
|
// Choice index out of range.
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// pollIsVisible is a short-hand function to return only a single boolean value for a visibility check on poll source status to account.
|
|
func pollIsVisible(filter *visibility.Filter, ctx context.Context, to *gtsmodel.Account, poll *gtsmodel.Poll) bool {
|
|
visible, _ := filter.StatusVisible(ctx, to, poll.Status)
|
|
return visible
|
|
}
|
|
|
|
func TestPollTestSuite(t *testing.T) {
|
|
suite.Run(t, new(PollTestSuite))
|
|
}
|