gtsocial-umbx

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

admin_test.go (5521B)


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