0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-21 18:54:39 +02:00

add coverage for change password

This commit is contained in:
TheFox0x7 2025-05-25 20:10:01 +02:00
parent 173ad6c9da
commit 13b9d6e4db
No known key found for this signature in database
GPG Key ID: 6CA33903484AF7C2
3 changed files with 119 additions and 25 deletions

View File

@ -13,7 +13,7 @@ var subcmdUser = &cli.Command{
Commands: []*cli.Command{ Commands: []*cli.Command{
microcmdUserCreate(), microcmdUserCreate(),
microcmdUserList, microcmdUserList,
microcmdUserChangePassword, microcmdUserChangePassword(),
microcmdUserDelete(), microcmdUserDelete(),
microcmdUserGenerateAccessToken, microcmdUserGenerateAccessToken,
microcmdUserMustChangePassword, microcmdUserMustChangePassword,

View File

@ -17,7 +17,8 @@ import (
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
var microcmdUserChangePassword = &cli.Command{ func microcmdUserChangePassword() *cli.Command {
return &cli.Command{
Name: "change-password", Name: "change-password",
Usage: "Change a user's password", Usage: "Change a user's password",
Action: runChangePassword, Action: runChangePassword,
@ -41,15 +42,18 @@ var microcmdUserChangePassword = &cli.Command{
}, },
}, },
} }
}
func runChangePassword(ctx context.Context, c *cli.Command) error { func runChangePassword(ctx context.Context, c *cli.Command) error {
if err := argsSet(c, "username", "password"); err != nil { if err := argsSet(c, "username", "password"); err != nil {
return err return err
} }
if !setting.IsInTesting {
if err := initDB(ctx); err != nil { if err := initDB(ctx); err != nil {
return err return err
} }
}
user, err := user_model.GetUserByName(ctx, c.String("username")) user, err := user_model.GetUserByName(ctx, c.String("username"))
if err != nil { if err != nil {

View File

@ -0,0 +1,90 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package cmd
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestChangePasswordCommand(t *testing.T) {
ctx := t.Context()
defer func() {
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{}))
}()
t.Run("change password successfully", func(t *testing.T) {
// defer func() {
// require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{}))
// }()
// Prepare test user
unittest.AssertNotExistsBean(t, &user_model.User{LowerName: "testuser"})
err := microcmdUserCreate().Run(ctx, []string{"create", "--username", "testuser", "--email", "testuser@gitea.local", "--random-password"})
require.NoError(t, err)
// load test user
userBase := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
// Change the password
err = microcmdUserChangePassword().Run(ctx, []string{"change-password", "--username", "testuser", "--password", "newpassword"})
require.NoError(t, err)
// Verify the password has been changed
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
assert.NotEqual(t, userBase.Passwd, user.Passwd)
assert.NotEqual(t, userBase.Salt, user.Salt)
// Additional check for must-change-password flag
require.NoError(t, microcmdUserChangePassword().Run(ctx, []string{"change-password", "--username", "testuser", "--password", "anotherpassword", "--must-change-password=false"}))
user = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
assert.False(t, user.MustChangePassword)
require.NoError(t, microcmdUserChangePassword().Run(ctx, []string{"change-password", "--username", "testuser", "--password", "yetanotherpassword", "--must-change-password"}))
user = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
assert.True(t, user.MustChangePassword)
})
t.Run("failure cases", func(t *testing.T) {
testCases := []struct {
name string
args []string
expectedErr string
}{
{
name: "user does not exist",
args: []string{"change-password", "--username", "nonexistentuser", "--password", "newpassword"},
expectedErr: "user does not exist",
},
{
name: "missing username",
args: []string{"change-password", "--password", "newpassword"},
expectedErr: "username is not set",
},
{
name: "missing password",
args: []string{"change-password", "--username", "testuser"},
expectedErr: "password is not set",
},
{
name: "too short password",
args: []string{"change-password", "--username", "testuser", "--password", "1"},
expectedErr: "password is not long enough",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := microcmdUserChangePassword().Run(ctx, tc.args)
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErr)
})
}
})
}