followrequest_test.go (5158B)
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 followrequests_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/followrequests" 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 FollowRequestStandardTestSuite struct { 43 suite.Suite 44 db db.DB 45 storage *storage.Driver 46 mediaManager *media.Manager 47 federator federation.Federator 48 processor *processing.Processor 49 emailSender email.Sender 50 state state.State 51 52 // standard suite models 53 testTokens map[string]*gtsmodel.Token 54 testClients map[string]*gtsmodel.Client 55 testApplications map[string]*gtsmodel.Application 56 testUsers map[string]*gtsmodel.User 57 testAccounts map[string]*gtsmodel.Account 58 testAttachments map[string]*gtsmodel.MediaAttachment 59 testStatuses map[string]*gtsmodel.Status 60 61 // module being tested 62 followRequestModule *followrequests.Module 63 } 64 65 func (suite *FollowRequestStandardTestSuite) 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 suite.testAttachments = testrig.NewTestAttachments() 72 suite.testStatuses = testrig.NewTestStatuses() 73 } 74 75 func (suite *FollowRequestStandardTestSuite) SetupTest() { 76 suite.state.Caches.Init() 77 testrig.StartWorkers(&suite.state) 78 79 testrig.InitTestConfig() 80 testrig.InitTestLog() 81 82 suite.db = testrig.NewTestDB(&suite.state) 83 suite.state.DB = suite.db 84 suite.storage = testrig.NewInMemoryStorage() 85 suite.state.Storage = suite.storage 86 87 testrig.StartTimelines( 88 &suite.state, 89 visibility.NewFilter(&suite.state), 90 testrig.NewTestTypeConverter(suite.db), 91 ) 92 93 suite.mediaManager = testrig.NewTestMediaManager(&suite.state) 94 suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")), suite.mediaManager) 95 suite.emailSender = testrig.NewEmailSender("../../../../web/template/", nil) 96 suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager) 97 suite.followRequestModule = followrequests.New(suite.processor) 98 testrig.StandardDBSetup(suite.db, nil) 99 testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media") 100 } 101 102 func (suite *FollowRequestStandardTestSuite) TearDownTest() { 103 testrig.StandardDBTeardown(suite.db) 104 testrig.StandardStorageTeardown(suite.storage) 105 testrig.StopWorkers(&suite.state) 106 } 107 108 func (suite *FollowRequestStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, requestMethod string, requestBody []byte, requestPath string, bodyContentType string) *gin.Context { 109 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 110 111 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) 112 ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"])) 113 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) 114 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) 115 116 protocol := config.GetProtocol() 117 host := config.GetHost() 118 119 baseURI := fmt.Sprintf("%s://%s", protocol, host) 120 requestURI := fmt.Sprintf("%s/%s", baseURI, requestPath) 121 122 ctx.Request = httptest.NewRequest(requestMethod, requestURI, bytes.NewReader(requestBody)) // the endpoint we're hitting 123 124 if bodyContentType != "" { 125 ctx.Request.Header.Set("Content-Type", bodyContentType) 126 } 127 ctx.Request.Header.Set("accept", "application/json") 128 129 return ctx 130 }