Skip to content

Commit

Permalink
Add test cases for ChangeUserPassword, DeleteUser
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Jul 21, 2024
1 parent c99d8e2 commit 8168ab6
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/rpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ func TestAdminRPCServerBackend(t *testing.T) {
testcases.RunAdminLoginTest(t, testAdminClient)
})

t.Run("admin delete user test", func(t *testing.T) {
testcases.RunAdminDeleteUserTest(t, testAdminClient)
})

t.Run("admin change password test", func(t *testing.T) {
testcases.RunAdminChangePasswordTest(t, testAdminClient)
})

t.Run("admin create project test", func(t *testing.T) {
testcases.RunAdminCreateProjectTest(t, testAdminClient, testAdminAuthInterceptor)
})
Expand Down
107 changes: 107 additions & 0 deletions server/rpc/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,113 @@ func RunAdminLoginTest(
assert.Equal(t, connecthelper.CodeOf(database.ErrMismatchedPassword), converter.ErrorCodeOf(err))
}

// RunAdminDeleteUserTest runs the admin delete user test.
func RunAdminDeleteUserTest(
t *testing.T,
testAdminClient v1connect.AdminServiceClient,
) {
adminUser := helper.TestSlugName(t)
adminPassword := helper.AdminPassword + "123!"

_, err := testAdminClient.CreateUser(
context.Background(),
connect.NewRequest(&api.CreateUserRequest{
Username: adminUser,
Password: adminPassword,
},
))
assert.NoError(t, err)

_, err = testAdminClient.DeleteUser(
context.Background(),
connect.NewRequest(&api.DeleteUserRequest{
Username: adminUser,
Password: adminPassword,
},
))
assert.NoError(t, err)

// try to delete user with not existing username
_, err = testAdminClient.DeleteUser(
context.Background(),
connect.NewRequest(&api.DeleteUserRequest{
Username: adminUser,
Password: adminPassword,
},
))
assert.Equal(t, connect.CodeNotFound, connect.CodeOf(err))
assert.Equal(t, connecthelper.CodeOf(database.ErrUserNotFound), converter.ErrorCodeOf(err))
}

// RunAdminChangePasswordTest runs the admin change password user test.
func RunAdminChangePasswordTest(
t *testing.T,
testAdminClient v1connect.AdminServiceClient,
) {
adminUser := helper.TestSlugName(t)
adminPassword := helper.AdminPassword + "123!"

_, err := testAdminClient.CreateUser(
context.Background(),
connect.NewRequest(&api.CreateUserRequest{
Username: adminUser,
Password: adminPassword,
},
))
assert.NoError(t, err)

_, err = testAdminClient.LogIn(
context.Background(),
connect.NewRequest(&api.LogInRequest{
Username: adminUser,
Password: adminPassword,
},
))
assert.NoError(t, err)

newAdminPassword := helper.AdminPassword + "12345!"
_, err = testAdminClient.ChangeUserPassword(
context.Background(),
connect.NewRequest(&api.ChangeUserPasswordRequest{
Username: adminUser,
CurrentPassword: adminPassword,
NewPassword: newAdminPassword,
},
))
assert.NoError(t, err)

// log in fail when try to log in with old password
_, err = testAdminClient.LogIn(
context.Background(),
connect.NewRequest(&api.LogInRequest{
Username: adminUser,
Password: adminPassword,
},
))
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))
assert.Equal(t, connecthelper.CodeOf(database.ErrMismatchedPassword), converter.ErrorCodeOf(err))

_, err = testAdminClient.LogIn(
context.Background(),
connect.NewRequest(&api.LogInRequest{
Username: adminUser,
Password: newAdminPassword,
},
))
assert.NoError(t, err)

// try to change password with invalid password
_, err = testAdminClient.ChangeUserPassword(
context.Background(),
connect.NewRequest(&api.ChangeUserPasswordRequest{
Username: adminUser,
CurrentPassword: adminPassword,
NewPassword: invalidSlugName,
},
))
assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
}

// RunAdminCreateProjectTest runs the CreateProject test in admin.
func RunAdminCreateProjectTest(
t *testing.T,
Expand Down

0 comments on commit 8168ab6

Please sign in to comment.