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