gtsocial-umbx

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

instance_test.go (5165B)


      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 instance_test
     19 
     20 import (
     21 	"bytes"
     22 	"fmt"
     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/instance"
     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 InstanceStandardTestSuite 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 	testAttachments  map[string]*gtsmodel.MediaAttachment
     61 	testStatuses     map[string]*gtsmodel.Status
     62 
     63 	// module being tested
     64 	instanceModule *instance.Module
     65 }
     66 
     67 func (suite *InstanceStandardTestSuite) SetupSuite() {
     68 	suite.testTokens = testrig.NewTestTokens()
     69 	suite.testClients = testrig.NewTestClients()
     70 	suite.testApplications = testrig.NewTestApplications()
     71 	suite.testUsers = testrig.NewTestUsers()
     72 	suite.testAccounts = testrig.NewTestAccounts()
     73 	suite.testAttachments = testrig.NewTestAttachments()
     74 	suite.testStatuses = testrig.NewTestStatuses()
     75 }
     76 
     77 func (suite *InstanceStandardTestSuite) SetupTest() {
     78 	suite.state.Caches.Init()
     79 	testrig.StartWorkers(&suite.state)
     80 
     81 	testrig.InitTestConfig()
     82 	testrig.InitTestLog()
     83 
     84 	suite.db = testrig.NewTestDB(&suite.state)
     85 	suite.state.DB = suite.db
     86 	suite.storage = testrig.NewInMemoryStorage()
     87 	suite.state.Storage = suite.storage
     88 
     89 	testrig.StartTimelines(
     90 		&suite.state,
     91 		visibility.NewFilter(&suite.state),
     92 		testrig.NewTestTypeConverter(suite.db),
     93 	)
     94 
     95 	suite.mediaManager = testrig.NewTestMediaManager(&suite.state)
     96 	suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")), suite.mediaManager)
     97 	suite.sentEmails = make(map[string]string)
     98 	suite.emailSender = testrig.NewEmailSender("../../../../web/template/", suite.sentEmails)
     99 	suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)
    100 	suite.instanceModule = instance.New(suite.processor)
    101 	testrig.StandardDBSetup(suite.db, nil)
    102 	testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media")
    103 }
    104 
    105 func (suite *InstanceStandardTestSuite) TearDownTest() {
    106 	testrig.StandardDBTeardown(suite.db)
    107 	testrig.StandardStorageTeardown(suite.storage)
    108 	testrig.StopWorkers(&suite.state)
    109 }
    110 
    111 func (suite *InstanceStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, method string, path string, body []byte, contentType string, auth bool) *gin.Context {
    112 	protocol := config.GetProtocol()
    113 	host := config.GetHost()
    114 
    115 	baseURI := fmt.Sprintf("%s://%s", protocol, host)
    116 	requestURI := fmt.Sprintf("%s/%s", baseURI, path)
    117 
    118 	req := httptest.NewRequest(method, requestURI, bytes.NewReader(body)) // the endpoint we're hitting
    119 
    120 	if contentType != "" {
    121 		req.Header.Set("Content-Type", contentType)
    122 	}
    123 
    124 	req.Header.Set("accept", "application/json")
    125 
    126 	ctx, _ := testrig.CreateGinTestContext(recorder, req)
    127 
    128 	if auth {
    129 		ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["admin_account"])
    130 		ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["admin_account"]))
    131 		ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["admin_account"])
    132 		ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["admin_account"])
    133 	}
    134 
    135 	return ctx
    136 }