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  (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"context" 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"io" 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"net/http" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"testing" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-07 16:14:41 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"codeberg.org/gruf/go-byteutil" 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"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 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									response ,  code  :=  suite . POST ( user . PasswordChangePath ,  map [ string ] [ ] string { 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										"old_password" :  { "password" } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"new_password" :  { "peepeepoopoopassword" } , 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} ,  suite . userModule . PasswordChangePOSTHandler ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  response . Body . Close ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Check response 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									suite . EqualValues ( http . StatusOK ,  code ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									dbUser  :=  & gtsmodel . User { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									err  :=  suite . db . GetByID ( context . Background ( ) ,  suite . testUsers [ "local_account_1" ] . ID ,  dbUser ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										suite . FailNow ( err . Error ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// new password should pass 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-07 16:14:41 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  =  bcrypt . CompareHashAndPassword ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										byteutil . S2B ( dbUser . EncryptedPassword ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										byteutil . S2B ( "peepeepoopoopassword" ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									suite . NoError ( err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// old password should fail 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-07 16:14:41 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  =  bcrypt . CompareHashAndPassword ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										byteutil . S2B ( dbUser . EncryptedPassword ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										byteutil . S2B ( "password" ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									suite . EqualError ( err ,  "crypto/bcrypt: hashedPassword is not the hash of the given password" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( suite  * PasswordChangeTestSuite )  TestPasswordMissingOldPassword ( )  {  
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									response ,  code  :=  suite . POST ( user . PasswordChangePath ,  map [ string ] [ ] string { 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										"new_password" :  { "peepeepoopoopassword" } , 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02: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 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2022-06-08 20:38:03 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									suite . Equal ( ` { "error":"Bad Request: password change request missing field old_password"} ` ,  string ( b ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( suite  * PasswordChangeTestSuite )  TestPasswordIncorrectOldPassword ( )  {  
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									response ,  code  :=  suite . POST ( user . PasswordChangePath ,  map [ string ] [ ] string { 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										"old_password" :  { "notright" } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"new_password" :  { "peepeepoopoopassword" } , 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02: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 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-08 10:40:51 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									suite . Equal ( ` { "error":"Unauthorized: old password was incorrect"} ` ,  string ( b ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( suite  * PasswordChangeTestSuite )  TestPasswordWeakNewPassword ( )  {  
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									response ,  code  :=  suite . POST ( user . PasswordChangePath ,  map [ string ] [ ] string { 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										"old_password" :  { "password" } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"new_password" :  { "peepeepoopoo" } , 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-06 15:43:25 +02: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 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-08 10:40:51 +02: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 14:26:04 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  TestPasswordChangeTestSuite ( t  * testing . T )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									suite . Run ( t ,  & PasswordChangeTestSuite { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}