From 8703933df4e3c4c0c778c4953b1f013d6811aa31 Mon Sep 17 00:00:00 2001 From: Christoph Voigt Date: Tue, 13 Dec 2022 12:33:49 +0100 Subject: [PATCH] [bugfix] fix unordered favorites (#1245) * [bugfix] fix unordered favorites * add test for favouritesget * add license to new test files --- .../api/client/favourites/favourites_test.go | 100 ++++++++++++++++++ .../client/favourites/favouritesget_test.go | 77 ++++++++++++++ internal/db/bundb/timeline.go | 2 +- testrig/testmodels.go | 24 +++++ 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 internal/api/client/favourites/favourites_test.go create mode 100644 internal/api/client/favourites/favouritesget_test.go diff --git a/internal/api/client/favourites/favourites_test.go b/internal/api/client/favourites/favourites_test.go new file mode 100644 index 000000000..c84da6b32 --- /dev/null +++ b/internal/api/client/favourites/favourites_test.go @@ -0,0 +1,100 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + 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 . +*/ + +package favourites_test + +import ( + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/api/client/favourites" + "github.com/superseriousbusiness/gotosocial/internal/concurrency" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/email" + "github.com/superseriousbusiness/gotosocial/internal/federation" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/media" + "github.com/superseriousbusiness/gotosocial/internal/messages" + "github.com/superseriousbusiness/gotosocial/internal/processing" + "github.com/superseriousbusiness/gotosocial/internal/storage" + "github.com/superseriousbusiness/gotosocial/internal/typeutils" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type FavouritesStandardTestSuite struct { + // standard suite interfaces + suite.Suite + db db.DB + tc typeutils.TypeConverter + mediaManager media.Manager + federator federation.Federator + emailSender email.Sender + processor processing.Processor + storage *storage.Driver + + // standard suite models + testTokens map[string]*gtsmodel.Token + testClients map[string]*gtsmodel.Client + testApplications map[string]*gtsmodel.Application + testUsers map[string]*gtsmodel.User + testAccounts map[string]*gtsmodel.Account + testAttachments map[string]*gtsmodel.MediaAttachment + testStatuses map[string]*gtsmodel.Status + testFollows map[string]*gtsmodel.Follow + + // module being tested + favModule *favourites.Module +} + +func (suite *FavouritesStandardTestSuite) SetupSuite() { + suite.testTokens = testrig.NewTestTokens() + suite.testClients = testrig.NewTestClients() + suite.testApplications = testrig.NewTestApplications() + suite.testUsers = testrig.NewTestUsers() + suite.testAccounts = testrig.NewTestAccounts() + suite.testAttachments = testrig.NewTestAttachments() + suite.testStatuses = testrig.NewTestStatuses() + suite.testFollows = testrig.NewTestFollows() +} + +func (suite *FavouritesStandardTestSuite) SetupTest() { + testrig.InitTestConfig() + testrig.InitTestLog() + + suite.db = testrig.NewTestDB() + suite.tc = testrig.NewTestTypeConverter(suite.db) + suite.storage = testrig.NewInMemoryStorage() + testrig.StandardDBSetup(suite.db, nil) + testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media") + + fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) + clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) + + suite.mediaManager = testrig.NewTestMediaManager(suite.db, suite.storage) + suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker), suite.storage, suite.mediaManager, fedWorker) + suite.emailSender = testrig.NewEmailSender("../../../../web/template/", nil) + suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator, suite.emailSender, suite.mediaManager, clientWorker, fedWorker) + suite.favModule = favourites.New(suite.processor).(*favourites.Module) + + suite.NoError(suite.processor.Start()) +} + +func (suite *FavouritesStandardTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.db) + testrig.StandardStorageTeardown(suite.storage) +} + +func (suite *FavouritesStandardTestSuite) TestProcessFave() {} diff --git a/internal/api/client/favourites/favouritesget_test.go b/internal/api/client/favourites/favouritesget_test.go new file mode 100644 index 000000000..71562cad2 --- /dev/null +++ b/internal/api/client/favourites/favouritesget_test.go @@ -0,0 +1,77 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + 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 . +*/ + +package favourites_test + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/api/client/favourites" + "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/oauth" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type FavouritesTestSuite struct { + FavouritesStandardTestSuite +} + +func (suite *FavouritesTestSuite) TestGetFavourites() { + t := suite.testTokens["local_account_1"] + oauthToken := oauth.DBTokenToToken(t) + + // setup + recorder := httptest.NewRecorder() + ctx, _ := testrig.CreateGinTestContext(recorder, nil) + ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_2"]) + ctx.Set(oauth.SessionAuthorizedToken, oauthToken) + ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) + ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) + ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s?limit=80", favourites.BasePath), nil) + ctx.Request.Header.Set("accept", "application/json") + + suite.favModule.FavouritesGETHandler(ctx) + + // check response + suite.EqualValues(http.StatusOK, recorder.Code) + + result := recorder.Result() + defer result.Body.Close() + b, err := ioutil.ReadAll(result.Body) + assert.NoError(suite.T(), err) + + favs := []model.Status{} + err = json.Unmarshal(b, &favs) + + assert.NoError(suite.T(), err) + + assert.Len(suite.T(), favs, 4) + assert.Equal(suite.T(), "01F8MHCP5P2NWYQ416SBA0XSEV", favs[0].ID) + assert.Equal(suite.T(), "01F8MH75CBF9JFX4ZAD54N0W0R", favs[len(favs)-1].ID) +} + +func TestStatusGetTestSuite(t *testing.T) { + suite.Run(t, new(FavouritesTestSuite)) +} diff --git a/internal/db/bundb/timeline.go b/internal/db/bundb/timeline.go index b52b68459..5ebfd6f76 100644 --- a/internal/db/bundb/timeline.go +++ b/internal/db/bundb/timeline.go @@ -233,7 +233,7 @@ func (t *timelineDB) GetFavedTimeline(ctx context.Context, accountID string, max // Sort by favourite ID rather than status ID slices.SortFunc(faves, func(a, b *gtsmodel.StatusFave) bool { - return b.CreatedAt.Before(a.CreatedAt) + return a.ID > b.ID }) statuses := make([]*gtsmodel.Status, 0, len(faves)) diff --git a/testrig/testmodels.go b/testrig/testmodels.go index b3850dcad..1f61d0b81 100644 --- a/testrig/testmodels.go +++ b/testrig/testmodels.go @@ -1798,6 +1798,30 @@ func NewTestFaves() map[string]*gtsmodel.StatusFave { StatusID: "01F8MH75CBF9JFX4ZAD54N0W0R", // admin account status 1 URI: "http://localhost:8080/users/the_mighty_zork/liked/01F8MHD2QCZSZ6WQS2ATVPEYJ9", }, + "local_account_1_admin_account_status_3": { + ID: "01GM435XERVPXXRK6NBAHK5HCZ", + CreatedAt: TimeMustParse("2022-12-12T20:17:56+02:00"), + AccountID: "01F8MH1H7YV1Z7D2C8K2730QBF", // local account 1 + TargetAccountID: "01F8MH17FWEB39HZJ76B6VXSKF", // admin account + StatusID: "01F8MHAAY43M6RJ473VQFCVH37", // admin account status 1 + URI: "http://localhost:8080/users/the_mighty_zork/liked/01GM435XERVPXXRK6NBAHK5HCZ", + }, + "local_account_1_local_account_2_status_1": { + ID: "01GM43AKBMN4YNXQ1HZHVC1SGB", + CreatedAt: TimeMustParse("2022-12-12T20:19:49+02:00"), + AccountID: "01F8MH1H7YV1Z7D2C8K2730QBF", // local account 1 + TargetAccountID: "01F8MH5NBDF2MV7CTC4Q5128HF", // admin account + StatusID: "01F8MHBQCBTDKN6X5VHGMMN4MA", // admin account status 1 + URI: "http://localhost:8080/users/the_mighty_zork/liked/01GM43AKBMN4YNXQ1HZHVC1SGB", + }, + "local_account_1_local_account_2_status_4": { + ID: "01GM43CC47DRPNZZ7BD04BS1YZ", + CreatedAt: TimeMustParse("2022-12-12T20:20:47+02:00"), + AccountID: "01F8MH1H7YV1Z7D2C8K2730QBF", // local account 1 + TargetAccountID: "01F8MH5NBDF2MV7CTC4Q5128HF", // admin account + StatusID: "01F8MHCP5P2NWYQ416SBA0XSEV", // admin account status 1 + URI: "http://localhost:8080/users/the_mighty_zork/liked/01GM43CC47DRPNZZ7BD04BS1YZ", + }, "admin_account_local_account_1_status_1": { ID: "01F8Q0486ANTDWKG02A7DS1Q24", CreatedAt: TimeMustParse("2022-05-14T13:21:09+02:00"),