gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

federatingdb_test.go (3858B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package federatingdb_test
     19 
     20 import (
     21 	"context"
     22 
     23 	"github.com/stretchr/testify/suite"
     24 	"github.com/superseriousbusiness/gotosocial/internal/db"
     25 	"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
     26 	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     28 	"github.com/superseriousbusiness/gotosocial/internal/messages"
     29 	"github.com/superseriousbusiness/gotosocial/internal/state"
     30 	"github.com/superseriousbusiness/gotosocial/internal/typeutils"
     31 	"github.com/superseriousbusiness/gotosocial/internal/visibility"
     32 	"github.com/superseriousbusiness/gotosocial/testrig"
     33 )
     34 
     35 type FederatingDBTestSuite struct {
     36 	suite.Suite
     37 	db            db.DB
     38 	tc            typeutils.TypeConverter
     39 	fromFederator chan messages.FromFederator
     40 	federatingDB  federatingdb.DB
     41 	state         state.State
     42 
     43 	testTokens       map[string]*gtsmodel.Token
     44 	testClients      map[string]*gtsmodel.Client
     45 	testApplications map[string]*gtsmodel.Application
     46 	testUsers        map[string]*gtsmodel.User
     47 	testAccounts     map[string]*gtsmodel.Account
     48 	testAttachments  map[string]*gtsmodel.MediaAttachment
     49 	testStatuses     map[string]*gtsmodel.Status
     50 	testBlocks       map[string]*gtsmodel.Block
     51 	testActivities   map[string]testrig.ActivityWithSignature
     52 }
     53 
     54 func (suite *FederatingDBTestSuite) SetupSuite() {
     55 	suite.testTokens = testrig.NewTestTokens()
     56 	suite.testClients = testrig.NewTestClients()
     57 	suite.testApplications = testrig.NewTestApplications()
     58 	suite.testUsers = testrig.NewTestUsers()
     59 	suite.testAccounts = testrig.NewTestAccounts()
     60 	suite.testAttachments = testrig.NewTestAttachments()
     61 	suite.testStatuses = testrig.NewTestStatuses()
     62 	suite.testBlocks = testrig.NewTestBlocks()
     63 }
     64 
     65 func (suite *FederatingDBTestSuite) SetupTest() {
     66 	testrig.InitTestConfig()
     67 	testrig.InitTestLog()
     68 
     69 	suite.state.Caches.Init()
     70 	testrig.StartWorkers(&suite.state)
     71 
     72 	suite.fromFederator = make(chan messages.FromFederator, 10)
     73 	suite.state.Workers.EnqueueFederator = func(ctx context.Context, msgs ...messages.FromFederator) {
     74 		for _, msg := range msgs {
     75 			suite.fromFederator <- msg
     76 		}
     77 	}
     78 
     79 	suite.db = testrig.NewTestDB(&suite.state)
     80 
     81 	suite.testActivities = testrig.NewTestActivities(suite.testAccounts)
     82 	suite.tc = testrig.NewTestTypeConverter(suite.db)
     83 
     84 	testrig.StartTimelines(
     85 		&suite.state,
     86 		visibility.NewFilter(&suite.state),
     87 		suite.tc,
     88 	)
     89 
     90 	suite.federatingDB = testrig.NewTestFederatingDB(&suite.state)
     91 	testrig.StandardDBSetup(suite.db, suite.testAccounts)
     92 
     93 	suite.state.DB = suite.db
     94 }
     95 
     96 func (suite *FederatingDBTestSuite) TearDownTest() {
     97 	testrig.StandardDBTeardown(suite.db)
     98 	testrig.StopWorkers(&suite.state)
     99 	for suite.fromFederator != nil {
    100 		select {
    101 		case <-suite.fromFederator:
    102 		default:
    103 			return
    104 		}
    105 	}
    106 }
    107 
    108 func createTestContext(receivingAccount *gtsmodel.Account, requestingAccount *gtsmodel.Account) context.Context {
    109 	ctx := context.Background()
    110 	ctx = gtscontext.SetReceivingAccount(ctx, receivingAccount)
    111 	ctx = gtscontext.SetRequestingAccount(ctx, requestingAccount)
    112 	return ctx
    113 }