mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore] Deinterface processor and subprocessors (#1501)
* [chore] Deinterface processor and subprocessors * expose subprocessors via function calls * missing license header
This commit is contained in:
@@ -35,7 +35,8 @@ import (
|
||||
|
||||
var oneWeek = 168 * time.Hour
|
||||
|
||||
func (p *processor) SendConfirmEmail(ctx context.Context, user *gtsmodel.User, username string) error {
|
||||
// EmailSendConfirmation sends an email address confirmation request email to the given user.
|
||||
func (p *Processor) EmailSendConfirmation(ctx context.Context, user *gtsmodel.User, username string) error {
|
||||
if user.UnconfirmedEmail == "" || user.UnconfirmedEmail == user.Email {
|
||||
// user has already confirmed this email address, so there's nothing to do
|
||||
return nil
|
||||
@@ -84,7 +85,9 @@ func (p *processor) SendConfirmEmail(ctx context.Context, user *gtsmodel.User, u
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *processor) ConfirmEmail(ctx context.Context, token string) (*gtsmodel.User, gtserror.WithCode) {
|
||||
// EmailConfirm processes an email confirmation request, usually initiated as a result of clicking on a link
|
||||
// in a 'confirm your email address' type email.
|
||||
func (p *Processor) EmailConfirm(ctx context.Context, token string) (*gtsmodel.User, gtserror.WithCode) {
|
||||
if token == "" {
|
||||
return nil, gtserror.NewErrorNotFound(errors.New("no token provided"))
|
||||
}
|
@@ -41,7 +41,7 @@ func (suite *EmailConfirmTestSuite) TestSendConfirmEmail() {
|
||||
user.ConfirmationSentAt = time.Time{}
|
||||
user.ConfirmationToken = ""
|
||||
|
||||
err := suite.user.SendConfirmEmail(context.Background(), user, "the_mighty_zork")
|
||||
err := suite.user.EmailSendConfirmation(context.Background(), user, "the_mighty_zork")
|
||||
suite.NoError(err)
|
||||
|
||||
// zork should have an email now
|
||||
@@ -78,7 +78,7 @@ func (suite *EmailConfirmTestSuite) TestConfirmEmail() {
|
||||
suite.NoError(err)
|
||||
|
||||
// confirm with the token set above
|
||||
updatedUser, errWithCode := suite.user.ConfirmEmail(ctx, "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6")
|
||||
updatedUser, errWithCode := suite.user.EmailConfirm(ctx, "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6")
|
||||
suite.NoError(errWithCode)
|
||||
|
||||
// email should now be confirmed and token cleared
|
||||
@@ -106,7 +106,7 @@ func (suite *EmailConfirmTestSuite) TestConfirmEmailOldToken() {
|
||||
suite.NoError(err)
|
||||
|
||||
// confirm with the token set above
|
||||
updatedUser, errWithCode := suite.user.ConfirmEmail(ctx, "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6")
|
||||
updatedUser, errWithCode := suite.user.EmailConfirm(ctx, "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6")
|
||||
suite.Nil(updatedUser)
|
||||
suite.EqualError(errWithCode, "ConfirmEmail: confirmation token expired")
|
||||
}
|
@@ -27,7 +27,8 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func (p *processor) ChangePassword(ctx context.Context, user *gtsmodel.User, oldPassword string, newPassword string) gtserror.WithCode {
|
||||
// PasswordChange processes a password change request for the given user.
|
||||
func (p *Processor) PasswordChange(ctx context.Context, user *gtsmodel.User, oldPassword string, newPassword string) gtserror.WithCode {
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.EncryptedPassword), []byte(oldPassword)); err != nil {
|
||||
return gtserror.NewErrorUnauthorized(err, "old password was incorrect")
|
||||
}
|
@@ -35,7 +35,7 @@ type ChangePasswordTestSuite struct {
|
||||
func (suite *ChangePasswordTestSuite) TestChangePasswordOK() {
|
||||
user := suite.testUsers["local_account_1"]
|
||||
|
||||
errWithCode := suite.user.ChangePassword(context.Background(), user, "password", "verygoodnewpassword")
|
||||
errWithCode := suite.user.PasswordChange(context.Background(), user, "password", "verygoodnewpassword")
|
||||
suite.NoError(errWithCode)
|
||||
|
||||
err := bcrypt.CompareHashAndPassword([]byte(user.EncryptedPassword), []byte("verygoodnewpassword"))
|
||||
@@ -54,7 +54,7 @@ func (suite *ChangePasswordTestSuite) TestChangePasswordOK() {
|
||||
func (suite *ChangePasswordTestSuite) TestChangePasswordIncorrectOld() {
|
||||
user := suite.testUsers["local_account_1"]
|
||||
|
||||
errWithCode := suite.user.ChangePassword(context.Background(), user, "ooooopsydoooopsy", "verygoodnewpassword")
|
||||
errWithCode := suite.user.PasswordChange(context.Background(), user, "ooooopsydoooopsy", "verygoodnewpassword")
|
||||
suite.EqualError(errWithCode, "crypto/bcrypt: hashedPassword is not the hash of the given password")
|
||||
suite.Equal(http.StatusUnauthorized, errWithCode.Code())
|
||||
suite.Equal("Unauthorized: old password was incorrect", errWithCode.Safe())
|
||||
@@ -72,7 +72,7 @@ func (suite *ChangePasswordTestSuite) TestChangePasswordIncorrectOld() {
|
||||
func (suite *ChangePasswordTestSuite) TestChangePasswordWeakNew() {
|
||||
user := suite.testUsers["local_account_1"]
|
||||
|
||||
errWithCode := suite.user.ChangePassword(context.Background(), user, "password", "1234")
|
||||
errWithCode := suite.user.PasswordChange(context.Background(), user, "password", "1234")
|
||||
suite.EqualError(errWithCode, "password is only 11% strength, try including more special characters, using lowercase letters, using uppercase letters or using a longer password")
|
||||
suite.Equal(http.StatusBadRequest, errWithCode.Code())
|
||||
suite.Equal("Bad Request: password is only 11% strength, try including more special characters, using lowercase letters, using uppercase letters or using a longer password", errWithCode.Safe())
|
@@ -19,33 +19,18 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/email"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
// Processor wraps a bunch of functions for processing user-level actions.
|
||||
type Processor interface {
|
||||
// ChangePassword changes the specified user's password from old => new,
|
||||
// or returns an error if the new password is too weak, or the old password is incorrect.
|
||||
ChangePassword(ctx context.Context, user *gtsmodel.User, oldPassword string, newPassword string) gtserror.WithCode
|
||||
// SendConfirmEmail sends a 'confirm-your-email-address' type email to a user.
|
||||
SendConfirmEmail(ctx context.Context, user *gtsmodel.User, username string) error
|
||||
// ConfirmEmail confirms an email address using the given token.
|
||||
ConfirmEmail(ctx context.Context, token string) (*gtsmodel.User, gtserror.WithCode)
|
||||
}
|
||||
|
||||
type processor struct {
|
||||
type Processor struct {
|
||||
emailSender email.Sender
|
||||
db db.DB
|
||||
}
|
||||
|
||||
// New returns a new user processor
|
||||
func New(db db.DB, emailSender email.Sender) Processor {
|
||||
return &processor{
|
||||
return Processor{
|
||||
emailSender: emailSender,
|
||||
db: db,
|
||||
}
|
||||
|
Reference in New Issue
Block a user