mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 02:32:25 -05:00 
			
		
		
		
	[bugfix] Fix POST to create account endpoint (#3767)
This commit is contained in:
		
					parent
					
						
							
								128fcc871c
							
						
					
				
			
			
				commit
				
					
						ce7ba8f498
					
				
			
		
					 3 changed files with 83 additions and 2 deletions
				
			
		
							
								
								
									
										74
									
								
								internal/processing/user/create_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								internal/processing/user/create_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,74 @@ | ||||||
|  | // 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/>. | ||||||
|  | 
 | ||||||
|  | package user_test | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"net" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/stretchr/testify/suite" | ||||||
|  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type CreateTestSuite struct { | ||||||
|  | 	UserStandardTestSuite | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (suite *CreateTestSuite) TestCreateOK() { | ||||||
|  | 	var ( | ||||||
|  | 		ctx      = context.Background() | ||||||
|  | 		app      = suite.testApps["application_1"] | ||||||
|  | 		appToken = suite.testTokens["local_account_1_client_application_token"] | ||||||
|  | 		form     = &apimodel.AccountCreateRequest{ | ||||||
|  | 			Reason:    "a long enough explanation of why I am doing api calls", | ||||||
|  | 			Username:  "someone_new", | ||||||
|  | 			Email:     "someone_new@example.org", | ||||||
|  | 			Password:  "a long enough password for this endpoint", | ||||||
|  | 			Agreement: true, | ||||||
|  | 			Locale:    "en-us", | ||||||
|  | 			IP:        net.ParseIP("192.0.2.128"), | ||||||
|  | 		} | ||||||
|  | 	) | ||||||
|  | 
 | ||||||
|  | 	// Create user via the API endpoint. | ||||||
|  | 	user, errWithCode := suite.user.Create(ctx, app, form) | ||||||
|  | 	if errWithCode != nil { | ||||||
|  | 		suite.FailNow(errWithCode.Error()) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Load the app-level access token that was just used. | ||||||
|  | 	appAccessToken, err := suite.oauthServer.LoadAccessToken(ctx, appToken.Access) | ||||||
|  | 	if err != nil { | ||||||
|  | 		suite.FailNow(err.Error()) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Create a user-level access token for the new user. | ||||||
|  | 	userAccessToken, err := suite.user.TokenForNewUser(ctx, appAccessToken, app, user) | ||||||
|  | 	if err != nil { | ||||||
|  | 		suite.FailNow(err.Error()) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Check returned user-level access token. | ||||||
|  | 	suite.NotEmpty(userAccessToken.AccessToken) | ||||||
|  | 	suite.Equal("Bearer", userAccessToken.TokenType) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestCreateTestSuite(t *testing.T) { | ||||||
|  | 	suite.Run(t, &CreateTestSuite{}) | ||||||
|  | } | ||||||
|  | @ -41,6 +41,7 @@ func New( | ||||||
| 	return Processor{ | 	return Processor{ | ||||||
| 		state:       state, | 		state:       state, | ||||||
| 		converter:   converter, | 		converter:   converter, | ||||||
|  | 		oauthServer: oauthServer, | ||||||
| 		emailSender: emailSender, | 		emailSender: emailSender, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -23,6 +23,7 @@ import ( | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/db" | 	"github.com/superseriousbusiness/gotosocial/internal/db" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/email" | 	"github.com/superseriousbusiness/gotosocial/internal/email" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | ||||||
|  | 	"github.com/superseriousbusiness/gotosocial/internal/oauth" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/processing/user" | 	"github.com/superseriousbusiness/gotosocial/internal/processing/user" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/state" | 	"github.com/superseriousbusiness/gotosocial/internal/state" | ||||||
| 	"github.com/superseriousbusiness/gotosocial/internal/typeutils" | 	"github.com/superseriousbusiness/gotosocial/internal/typeutils" | ||||||
|  | @ -34,9 +35,11 @@ type UserStandardTestSuite struct { | ||||||
| 	emailSender email.Sender | 	emailSender email.Sender | ||||||
| 	db          db.DB | 	db          db.DB | ||||||
| 	state       state.State | 	state       state.State | ||||||
|  | 	oauthServer oauth.Server | ||||||
| 
 | 
 | ||||||
| 	testUsers map[string]*gtsmodel.User | 	testApps   map[string]*gtsmodel.Application | ||||||
| 
 | 	testTokens map[string]*gtsmodel.Token | ||||||
|  | 	testUsers  map[string]*gtsmodel.User | ||||||
| 	sentEmails map[string]string | 	sentEmails map[string]string | ||||||
| 
 | 
 | ||||||
| 	user user.Processor | 	user user.Processor | ||||||
|  | @ -51,9 +54,12 @@ func (suite *UserStandardTestSuite) SetupTest() { | ||||||
| 	suite.db = testrig.NewTestDB(&suite.state) | 	suite.db = testrig.NewTestDB(&suite.state) | ||||||
| 	suite.state.DB = suite.db | 	suite.state.DB = suite.db | ||||||
| 	suite.state.AdminActions = admin.New(suite.state.DB, &suite.state.Workers) | 	suite.state.AdminActions = admin.New(suite.state.DB, &suite.state.Workers) | ||||||
|  | 	suite.oauthServer = testrig.NewTestOauthServer(suite.state.DB) | ||||||
| 
 | 
 | ||||||
| 	suite.sentEmails = make(map[string]string) | 	suite.sentEmails = make(map[string]string) | ||||||
| 	suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails) | 	suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails) | ||||||
|  | 	suite.testApps = testrig.NewTestApplications() | ||||||
|  | 	suite.testTokens = testrig.NewTestTokens() | ||||||
| 	suite.testUsers = testrig.NewTestUsers() | 	suite.testUsers = testrig.NewTestUsers() | ||||||
| 
 | 
 | ||||||
| 	suite.user = user.New(&suite.state, typeutils.NewConverter(&suite.state), testrig.NewTestOauthServer(suite.db), suite.emailSender) | 	suite.user = user.New(&suite.state, typeutils.NewConverter(&suite.state), testrig.NewTestOauthServer(suite.db), suite.emailSender) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue