gtsocial-umbx

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

search_test.go (4814B)


      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 search_test
     19 
     20 import (
     21 	"fmt"
     22 	"net/http"
     23 	"net/http/httptest"
     24 
     25 	"github.com/gin-gonic/gin"
     26 	"github.com/stretchr/testify/suite"
     27 	"github.com/superseriousbusiness/gotosocial/internal/api/client/search"
     28 	"github.com/superseriousbusiness/gotosocial/internal/config"
     29 	"github.com/superseriousbusiness/gotosocial/internal/db"
     30 	"github.com/superseriousbusiness/gotosocial/internal/email"
     31 	"github.com/superseriousbusiness/gotosocial/internal/federation"
     32 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     33 	"github.com/superseriousbusiness/gotosocial/internal/media"
     34 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     35 	"github.com/superseriousbusiness/gotosocial/internal/processing"
     36 	"github.com/superseriousbusiness/gotosocial/internal/state"
     37 	"github.com/superseriousbusiness/gotosocial/internal/storage"
     38 	"github.com/superseriousbusiness/gotosocial/internal/visibility"
     39 	"github.com/superseriousbusiness/gotosocial/testrig"
     40 )
     41 
     42 type SearchStandardTestSuite struct {
     43 	// standard suite interfaces
     44 	suite.Suite
     45 	db           db.DB
     46 	storage      *storage.Driver
     47 	mediaManager *media.Manager
     48 	federator    federation.Federator
     49 	processor    *processing.Processor
     50 	emailSender  email.Sender
     51 	sentEmails   map[string]string
     52 	state        state.State
     53 
     54 	// standard suite models
     55 	testTokens       map[string]*gtsmodel.Token
     56 	testClients      map[string]*gtsmodel.Client
     57 	testApplications map[string]*gtsmodel.Application
     58 	testUsers        map[string]*gtsmodel.User
     59 	testAccounts     map[string]*gtsmodel.Account
     60 
     61 	// module being tested
     62 	searchModule *search.Module
     63 }
     64 
     65 func (suite *SearchStandardTestSuite) SetupSuite() {
     66 	suite.testTokens = testrig.NewTestTokens()
     67 	suite.testClients = testrig.NewTestClients()
     68 	suite.testApplications = testrig.NewTestApplications()
     69 	suite.testUsers = testrig.NewTestUsers()
     70 	suite.testAccounts = testrig.NewTestAccounts()
     71 }
     72 
     73 func (suite *SearchStandardTestSuite) SetupTest() {
     74 	suite.state.Caches.Init()
     75 	testrig.StartWorkers(&suite.state)
     76 
     77 	testrig.InitTestConfig()
     78 	testrig.InitTestLog()
     79 
     80 	suite.db = testrig.NewTestDB(&suite.state)
     81 	suite.state.DB = suite.db
     82 	suite.storage = testrig.NewInMemoryStorage()
     83 	suite.state.Storage = suite.storage
     84 
     85 	testrig.StartTimelines(
     86 		&suite.state,
     87 		visibility.NewFilter(&suite.state),
     88 		testrig.NewTestTypeConverter(suite.db),
     89 	)
     90 
     91 	suite.mediaManager = testrig.NewTestMediaManager(&suite.state)
     92 	suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")), suite.mediaManager)
     93 	suite.sentEmails = make(map[string]string)
     94 	suite.emailSender = testrig.NewEmailSender("../../../../web/template/", suite.sentEmails)
     95 	suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)
     96 	suite.searchModule = search.New(suite.processor)
     97 	testrig.StandardDBSetup(suite.db, nil)
     98 	testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media")
     99 }
    100 
    101 func (suite *SearchStandardTestSuite) TearDownTest() {
    102 	testrig.StandardDBTeardown(suite.db)
    103 	testrig.StandardStorageTeardown(suite.storage)
    104 	testrig.StopWorkers(&suite.state)
    105 }
    106 
    107 func (suite *SearchStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, requestPath string) *gin.Context {
    108 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    109 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
    110 	ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"]))
    111 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
    112 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
    113 
    114 	protocol := config.GetProtocol()
    115 	host := config.GetHost()
    116 
    117 	baseURI := fmt.Sprintf("%s://%s", protocol, host)
    118 	requestURI := fmt.Sprintf("%s/%s", baseURI, requestPath)
    119 
    120 	ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) // the endpoint we're hitting
    121 	ctx.Request.Header.Set("accept", "application/json")
    122 	return ctx
    123 }