From ce7ba8f4986845d39dfae284f156f14af677a7b4 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Sun, 9 Feb 2025 13:52:52 +0100 Subject: [PATCH] [bugfix] Fix POST to create account endpoint (#3767) --- internal/processing/user/create_test.go | 74 +++++++++++++++++++++++++ internal/processing/user/user.go | 1 + internal/processing/user/user_test.go | 10 +++- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 internal/processing/user/create_test.go diff --git a/internal/processing/user/create_test.go b/internal/processing/user/create_test.go new file mode 100644 index 000000000..9781a4214 --- /dev/null +++ b/internal/processing/user/create_test.go @@ -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 . + +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{}) +} diff --git a/internal/processing/user/user.go b/internal/processing/user/user.go index cd8ab9900..5efb89061 100644 --- a/internal/processing/user/user.go +++ b/internal/processing/user/user.go @@ -41,6 +41,7 @@ func New( return Processor{ state: state, converter: converter, + oauthServer: oauthServer, emailSender: emailSender, } } diff --git a/internal/processing/user/user_test.go b/internal/processing/user/user_test.go index 2a9e0a89f..72fd22117 100644 --- a/internal/processing/user/user_test.go +++ b/internal/processing/user/user_test.go @@ -23,6 +23,7 @@ "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/email" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/processing/user" "github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/typeutils" @@ -34,9 +35,11 @@ type UserStandardTestSuite struct { emailSender email.Sender db db.DB 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 user user.Processor @@ -51,9 +54,12 @@ func (suite *UserStandardTestSuite) SetupTest() { suite.db = testrig.NewTestDB(&suite.state) suite.state.DB = suite.db 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.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails) + suite.testApps = testrig.NewTestApplications() + suite.testTokens = testrig.NewTestTokens() suite.testUsers = testrig.NewTestUsers() suite.user = user.New(&suite.state, typeutils.NewConverter(&suite.state), testrig.NewTestOauthServer(suite.db), suite.emailSender)