| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2021-10-14 14:26:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package user_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-06-06 15:43:25 +02:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/admin" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/api/client/user" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/federation" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/media" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/oauth" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/processing" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/state" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/storage" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/typeutils" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/testrig" | 
					
						
							| 
									
										
										
										
											2024-06-06 15:43:25 +02:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2021-10-14 14:26:04 +02:00
										 |  |  | 	"github.com/stretchr/testify/suite" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type UserStandardTestSuite struct { | 
					
						
							|  |  |  | 	suite.Suite | 
					
						
							| 
									
										
										
										
											2022-01-10 18:36:09 +01:00
										 |  |  | 	db           db.DB | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	tc           *typeutils.Converter | 
					
						
							| 
									
										
										
										
											2023-05-28 13:08:35 +01:00
										 |  |  | 	mediaManager *media.Manager | 
					
						
							| 
									
										
										
										
											2023-10-23 10:58:13 +01:00
										 |  |  | 	federator    *federation.Federator | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	processor    *processing.Processor | 
					
						
							| 
									
										
										
										
											2022-11-24 08:35:46 +00:00
										 |  |  | 	storage      *storage.Driver | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	state        state.State | 
					
						
							| 
									
										
										
										
											2021-10-14 14:26:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	testTokens       map[string]*gtsmodel.Token | 
					
						
							|  |  |  | 	testApplications map[string]*gtsmodel.Application | 
					
						
							|  |  |  | 	testUsers        map[string]*gtsmodel.User | 
					
						
							|  |  |  | 	testAccounts     map[string]*gtsmodel.Account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	userModule *user.Module | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *UserStandardTestSuite) SetupTest() { | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	suite.state.Caches.Init() | 
					
						
							| 
									
										
										
										
											2023-11-14 15:57:25 +01:00
										 |  |  | 	testrig.StartNoopWorkers(&suite.state) | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 	testrig.InitTestConfig() | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	testrig.InitTestLog() | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-14 14:26:04 +02:00
										 |  |  | 	suite.testTokens = testrig.NewTestTokens() | 
					
						
							|  |  |  | 	suite.testApplications = testrig.NewTestApplications() | 
					
						
							|  |  |  | 	suite.testUsers = testrig.NewTestUsers() | 
					
						
							|  |  |  | 	suite.testAccounts = testrig.NewTestAccounts() | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	suite.db = testrig.NewTestDB(&suite.state) | 
					
						
							|  |  |  | 	suite.state.DB = suite.db | 
					
						
							| 
									
										
										
										
											2025-01-08 11:29:40 +01:00
										 |  |  | 	suite.state.AdminActions = admin.New(suite.state.DB, &suite.state.Workers) | 
					
						
							| 
									
										
										
										
											2022-07-03 12:08:30 +02:00
										 |  |  | 	suite.storage = testrig.NewInMemoryStorage() | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	suite.state.Storage = suite.storage | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	suite.tc = typeutils.NewConverter(&suite.state) | 
					
						
							| 
									
										
										
										
											2023-05-25 10:37:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	suite.mediaManager = testrig.NewTestMediaManager(&suite.state) | 
					
						
							| 
									
										
										
										
											2025-01-23 16:47:30 -08:00
										 |  |  | 	suite.federator = testrig.NewTestFederator( | 
					
						
							|  |  |  | 		&suite.state, | 
					
						
							|  |  |  | 		testrig.NewTestTransportController( | 
					
						
							|  |  |  | 			&suite.state, | 
					
						
							|  |  |  | 			testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		suite.mediaManager, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	suite.processor = testrig.NewTestProcessor( | 
					
						
							|  |  |  | 		&suite.state, | 
					
						
							|  |  |  | 		suite.federator, | 
					
						
							|  |  |  | 		testrig.NewEmailSender("../../../../web/template/", nil), | 
					
						
							|  |  |  | 		testrig.NewNoopWebPushSender(), | 
					
						
							|  |  |  | 		suite.mediaManager, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	suite.userModule = user.New(suite.processor) | 
					
						
							| 
									
										
										
										
											2021-10-14 14:26:04 +02:00
										 |  |  | 	testrig.StandardDBSetup(suite.db, suite.testAccounts) | 
					
						
							|  |  |  | 	testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *UserStandardTestSuite) TearDownTest() { | 
					
						
							|  |  |  | 	testrig.StandardDBTeardown(suite.db) | 
					
						
							|  |  |  | 	testrig.StandardStorageTeardown(suite.storage) | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	testrig.StopWorkers(&suite.state) | 
					
						
							| 
									
										
										
										
											2021-10-14 14:26:04 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-06 15:43:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (suite *UserStandardTestSuite) POST(path string, formValues map[string][]string, handler gin.HandlerFunc) (*http.Response, int) { | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		oauthToken = oauth.DBTokenToToken(suite.testTokens["local_account_1"]) | 
					
						
							|  |  |  | 		app        = suite.testApplications["application_1"] | 
					
						
							|  |  |  | 		user       = suite.testUsers["local_account_1"] | 
					
						
							|  |  |  | 		account    = suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 		target     = "http://localhost:8080" + path | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Prepare context. | 
					
						
							|  |  |  | 	recorder := httptest.NewRecorder() | 
					
						
							|  |  |  | 	ctx, _ := testrig.CreateGinTestContext(recorder, nil) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedApplication, app) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedUser, user) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedAccount, account) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Prepare request. | 
					
						
							|  |  |  | 	ctx.Request = httptest.NewRequest(http.MethodPost, target, nil) | 
					
						
							|  |  |  | 	ctx.Request.Header.Set("accept", "application/json") | 
					
						
							|  |  |  | 	ctx.Request.Form = url.Values(formValues) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Call the handler. | 
					
						
							|  |  |  | 	handler(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return response. | 
					
						
							|  |  |  | 	return recorder.Result(), recorder.Code | 
					
						
							|  |  |  | } |