Skip to content

Commit 18cb55c

Browse files
author
TJ Zhang
committed
adding tests
Signed-off-by: TJ Zhang <[email protected]>
1 parent 65ba898 commit 18cb55c

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

go/api/generic_base_commands.go

+4
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ type GenericBaseCommands interface {
8181
Copy(source string, destination string) (bool, error)
8282

8383
CopyWithOptions(source string, destination string, option options.CopyOptions) (bool, error)
84+
85+
UpdateConnectionPassword(password string, immediateAuth bool) (Result[string], error)
86+
87+
ResetConnectionPassword(immediateAuth bool) (Result[string], error)
8488
}

go/integTest/cluster_commands_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package integTest
44

55
import (
6+
"math/rand"
67
"strings"
78

89
"github.com/google/uuid"
@@ -971,3 +972,130 @@ func (suite *GlideTestSuite) TestFlushDBWithOptions_AsyncMode() {
971972
assert.NoError(suite.T(), err)
972973
assert.Empty(suite.T(), val.Value())
973974
}
975+
976+
func (suite *GlideTestSuite) TestUpdateConnectionPassword() {
977+
// Create admin client
978+
adminClient := suite.defaultClusterClient()
979+
defer adminClient.Close()
980+
981+
// Create test client
982+
testClient := suite.defaultClusterClient()
983+
defer testClient.Close()
984+
985+
// Generate random password
986+
pwd := uuid.NewString()
987+
988+
// Validate that we can use the test client
989+
_, err := testClient.Info()
990+
assert.NoError(suite.T(), err)
991+
992+
// Update password without re-authentication
993+
_, err = testClient.UpdateConnectionPassword(pwd, false)
994+
assert.NoError(suite.T(), err)
995+
996+
// Verify client still works with old auth
997+
_, err = testClient.Info()
998+
assert.NoError(suite.T(), err)
999+
1000+
// Update server password and kill all other clients to force reconnection
1001+
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", pwd})
1002+
assert.NoError(suite.T(), err)
1003+
1004+
_, err = adminClient.CustomCommand([]string{"CLIENT", "KILL", "TYPE", "NORMAL"})
1005+
assert.NoError(suite.T(), err)
1006+
1007+
// Verify client auto-reconnects with new password
1008+
_, err = testClient.Info()
1009+
assert.NoError(suite.T(), err)
1010+
1011+
// Cleanup: Reset password
1012+
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", ""})
1013+
assert.NoError(suite.T(), err)
1014+
}
1015+
1016+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_InvalidParameters() {
1017+
// Create test client
1018+
testClient := suite.defaultClient()
1019+
defer testClient.Close()
1020+
1021+
// Test empty password
1022+
_, err := testClient.UpdateConnectionPassword("", true)
1023+
assert.NotNil(suite.T(), err)
1024+
assert.IsType(suite.T(), &errors.RequestError{}, err)
1025+
1026+
// Test no password (assuming the method exists with this signature)
1027+
_, err = testClient.UpdateConnectionPassword("", true)
1028+
assert.NotNil(suite.T(), err)
1029+
assert.IsType(suite.T(), &errors.RequestError{}, err)
1030+
}
1031+
1032+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_NoServerAuth() {
1033+
// Create test client
1034+
testClient := suite.defaultClient()
1035+
defer testClient.Close()
1036+
1037+
// Validate that we can use the client
1038+
_, err := testClient.Info()
1039+
assert.Nil(suite.T(), err)
1040+
1041+
// Test immediate re-authentication fails when no server password is set
1042+
pwd := uuid.NewString()
1043+
_, err = testClient.UpdateConnectionPassword(pwd, true)
1044+
assert.NotNil(suite.T(), err)
1045+
assert.IsType(suite.T(), &errors.RequestError{}, err)
1046+
}
1047+
1048+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_LongPassword() {
1049+
// Create test client
1050+
testClient := suite.defaultClient()
1051+
defer testClient.Close()
1052+
1053+
// Generate long random password (1000 chars)
1054+
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
1055+
pwd := make([]byte, 1000)
1056+
for i := range pwd {
1057+
pwd[i] = letters[rand.Intn(len(letters))]
1058+
}
1059+
1060+
// Validate that we can use the client
1061+
_, err := testClient.Info()
1062+
assert.Nil(suite.T(), err)
1063+
1064+
// Test replacing connection password with a long password string
1065+
_, err = testClient.UpdateConnectionPassword(string(pwd), false)
1066+
assert.Nil(suite.T(), err)
1067+
}
1068+
1069+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_ImmediateAuthWrongPassword() {
1070+
// Create admin client
1071+
adminClient := suite.defaultClient()
1072+
defer adminClient.Close()
1073+
1074+
// Create test client
1075+
testClient := suite.defaultClient()
1076+
defer testClient.Close()
1077+
1078+
pwd := uuid.NewString()
1079+
notThePwd := uuid.NewString()
1080+
1081+
// Validate that we can use the client
1082+
_, err := testClient.Info()
1083+
assert.Nil(suite.T(), err)
1084+
1085+
// Set the password to something else
1086+
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", notThePwd})
1087+
assert.Nil(suite.T(), err)
1088+
1089+
// Test that re-authentication fails when using wrong password
1090+
_, err = testClient.UpdateConnectionPassword(pwd, true)
1091+
assert.NotNil(suite.T(), err)
1092+
assert.IsType(suite.T(), &errors.RequestError{}, err)
1093+
1094+
// But using correct password returns OK
1095+
_, err = testClient.UpdateConnectionPassword(notThePwd, true)
1096+
assert.Nil(suite.T(), err)
1097+
1098+
// Cleanup: Reset password
1099+
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", ""})
1100+
assert.Nil(suite.T(), err)
1101+
}

0 commit comments

Comments
 (0)