2023-03-12 15:00:57 +00: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 12:26:04 +00:00
package user_test
import (
"context"
2024-06-06 13:43:25 +00:00
"io"
2021-10-14 12:26:04 +00:00
"net/http"
"testing"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/api/client/user"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"golang.org/x/crypto/bcrypt"
)
type PasswordChangeTestSuite struct {
UserStandardTestSuite
}
func ( suite * PasswordChangeTestSuite ) TestPasswordChangePOST ( ) {
2024-06-06 13:43:25 +00:00
response , code := suite . POST ( user . PasswordChangePath , map [ string ] [ ] string {
2021-10-14 12:26:04 +00:00
"old_password" : { "password" } ,
"new_password" : { "peepeepoopoopassword" } ,
2024-06-06 13:43:25 +00:00
} , suite . userModule . PasswordChangePOSTHandler )
defer response . Body . Close ( )
2021-10-14 12:26:04 +00:00
2024-06-06 13:43:25 +00:00
// Check response
suite . EqualValues ( http . StatusOK , code )
2021-10-14 12:26:04 +00:00
dbUser := & gtsmodel . User { }
err := suite . db . GetByID ( context . Background ( ) , suite . testUsers [ "local_account_1" ] . ID , dbUser )
2024-06-06 13:43:25 +00:00
if err != nil {
suite . FailNow ( err . Error ( ) )
}
2021-10-14 12:26:04 +00:00
// new password should pass
err = bcrypt . CompareHashAndPassword ( [ ] byte ( dbUser . EncryptedPassword ) , [ ] byte ( "peepeepoopoopassword" ) )
suite . NoError ( err )
// old password should fail
err = bcrypt . CompareHashAndPassword ( [ ] byte ( dbUser . EncryptedPassword ) , [ ] byte ( "password" ) )
suite . EqualError ( err , "crypto/bcrypt: hashedPassword is not the hash of the given password" )
}
func ( suite * PasswordChangeTestSuite ) TestPasswordMissingOldPassword ( ) {
2024-06-06 13:43:25 +00:00
response , code := suite . POST ( user . PasswordChangePath , map [ string ] [ ] string {
2021-10-14 12:26:04 +00:00
"new_password" : { "peepeepoopoopassword" } ,
2024-06-06 13:43:25 +00:00
} , suite . userModule . PasswordChangePOSTHandler )
defer response . Body . Close ( )
// Check response
suite . EqualValues ( http . StatusBadRequest , code )
b , err := io . ReadAll ( response . Body )
if err != nil {
suite . FailNow ( err . Error ( ) )
2021-10-14 12:26:04 +00:00
}
2022-06-08 18:38:03 +00:00
suite . Equal ( ` { "error":"Bad Request: password change request missing field old_password"} ` , string ( b ) )
2021-10-14 12:26:04 +00:00
}
func ( suite * PasswordChangeTestSuite ) TestPasswordIncorrectOldPassword ( ) {
2024-06-06 13:43:25 +00:00
response , code := suite . POST ( user . PasswordChangePath , map [ string ] [ ] string {
2021-10-14 12:26:04 +00:00
"old_password" : { "notright" } ,
"new_password" : { "peepeepoopoopassword" } ,
2024-06-06 13:43:25 +00:00
} , suite . userModule . PasswordChangePOSTHandler )
defer response . Body . Close ( )
// Check response
suite . EqualValues ( http . StatusUnauthorized , code )
b , err := io . ReadAll ( response . Body )
if err != nil {
suite . FailNow ( err . Error ( ) )
2021-10-14 12:26:04 +00:00
}
2022-08-08 08:40:51 +00:00
suite . Equal ( ` { "error":"Unauthorized: old password was incorrect"} ` , string ( b ) )
2021-10-14 12:26:04 +00:00
}
func ( suite * PasswordChangeTestSuite ) TestPasswordWeakNewPassword ( ) {
2024-06-06 13:43:25 +00:00
response , code := suite . POST ( user . PasswordChangePath , map [ string ] [ ] string {
2021-10-14 12:26:04 +00:00
"old_password" : { "password" } ,
"new_password" : { "peepeepoopoo" } ,
2024-06-06 13:43:25 +00:00
} , suite . userModule . PasswordChangePOSTHandler )
defer response . Body . Close ( )
// Check response
suite . EqualValues ( http . StatusBadRequest , code )
b , err := io . ReadAll ( response . Body )
if err != nil {
suite . FailNow ( err . Error ( ) )
2021-10-14 12:26:04 +00:00
}
2022-08-08 08:40:51 +00:00
suite . Equal ( ` { "error":"Bad Request: password is only 94% strength, try including more special characters, using uppercase letters, using numbers or using a longer password"} ` , string ( b ) )
2021-10-14 12:26:04 +00:00
}
func TestPasswordChangeTestSuite ( t * testing . T ) {
suite . Run ( t , & PasswordChangeTestSuite { } )
}