mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-19 17:10:56 +02:00
add coverage for must change password
This commit is contained in:
parent
13b9d6e4db
commit
02497c0153
@ -16,6 +16,6 @@ var subcmdUser = &cli.Command{
|
|||||||
microcmdUserChangePassword(),
|
microcmdUserChangePassword(),
|
||||||
microcmdUserDelete(),
|
microcmdUserDelete(),
|
||||||
microcmdUserGenerateAccessToken,
|
microcmdUserGenerateAccessToken,
|
||||||
microcmdUserMustChangePassword,
|
microcmdUserMustChangePassword(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -9,30 +9,33 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var microcmdUserMustChangePassword = &cli.Command{
|
func microcmdUserMustChangePassword() *cli.Command {
|
||||||
Name: "must-change-password",
|
return &cli.Command{
|
||||||
Usage: "Set the must change password flag for the provided users or all users",
|
Name: "must-change-password",
|
||||||
Action: runMustChangePassword,
|
Usage: "Set the must change password flag for the provided users or all users",
|
||||||
Flags: []cli.Flag{
|
Action: runMustChangePassword,
|
||||||
&cli.BoolFlag{
|
Flags: []cli.Flag{
|
||||||
Name: "all",
|
&cli.BoolFlag{
|
||||||
Aliases: []string{"A"},
|
Name: "all",
|
||||||
Usage: "All users must change password, except those explicitly excluded with --exclude",
|
Aliases: []string{"A"},
|
||||||
|
Usage: "All users must change password, except those explicitly excluded with --exclude",
|
||||||
|
},
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "exclude",
|
||||||
|
Aliases: []string{"e"},
|
||||||
|
Usage: "Do not change the must-change-password flag for these users",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "unset",
|
||||||
|
Usage: "Instead of setting the must-change-password flag, unset it",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
}
|
||||||
Name: "exclude",
|
|
||||||
Aliases: []string{"e"},
|
|
||||||
Usage: "Do not change the must-change-password flag for these users",
|
|
||||||
},
|
|
||||||
&cli.BoolFlag{
|
|
||||||
Name: "unset",
|
|
||||||
Usage: "Instead of setting the must-change-password flag, unset it",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMustChangePassword(ctx context.Context, c *cli.Command) error {
|
func runMustChangePassword(ctx context.Context, c *cli.Command) error {
|
||||||
@ -44,8 +47,10 @@ func runMustChangePassword(ctx context.Context, c *cli.Command) error {
|
|||||||
all := c.Bool("all")
|
all := c.Bool("all")
|
||||||
exclude := c.StringSlice("exclude")
|
exclude := c.StringSlice("exclude")
|
||||||
|
|
||||||
if err := initDB(ctx); err != nil {
|
if !setting.IsInTesting {
|
||||||
return err
|
if err := initDB(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args().Slice(), exclude)
|
n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args().Slice(), exclude)
|
||||||
|
77
cmd/admin_user_must_change_password_test.go
Normal file
77
cmd/admin_user_must_change_password_test.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
// 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 TestMustChangePassword(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{}))
|
||||||
|
}()
|
||||||
|
err := microcmdUserCreate().Run(t.Context(), []string{"create", "--username", "testuser", "--email", "testuser@gitea.local", "--random-password"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = microcmdUserCreate().Run(t.Context(), []string{"create", "--username", "testuserexclude", "--email", "testuserexclude@gitea.local", "--random-password"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
// Reset password change flag
|
||||||
|
err = microcmdUserMustChangePassword().Run(t.Context(), []string{"change-test", "--all", "--unset"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
|
||||||
|
assert.False(t, testUser.MustChangePassword)
|
||||||
|
testUserExclude := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuserexclude"})
|
||||||
|
assert.False(t, testUserExclude.MustChangePassword)
|
||||||
|
|
||||||
|
// Make all users change password
|
||||||
|
err = microcmdUserMustChangePassword().Run(t.Context(), []string{"change-test", "--all"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
|
||||||
|
assert.True(t, testUser.MustChangePassword)
|
||||||
|
testUserExclude = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuserexclude"})
|
||||||
|
assert.True(t, testUserExclude.MustChangePassword)
|
||||||
|
|
||||||
|
// Reset password change flag but exclude all tested users
|
||||||
|
err = microcmdUserMustChangePassword().Run(t.Context(), []string{"change-test", "--all", "--unset", "--exclude", "testuser,testuserexclude"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
|
||||||
|
assert.True(t, testUser.MustChangePassword)
|
||||||
|
testUserExclude = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuserexclude"})
|
||||||
|
assert.True(t, testUserExclude.MustChangePassword)
|
||||||
|
|
||||||
|
// Reset password change flag by listing multiple users
|
||||||
|
err = microcmdUserMustChangePassword().Run(t.Context(), []string{"change-test", "--unset", "testuser", "testuserexclude"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
|
||||||
|
assert.False(t, testUser.MustChangePassword)
|
||||||
|
testUserExclude = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuserexclude"})
|
||||||
|
assert.False(t, testUserExclude.MustChangePassword)
|
||||||
|
|
||||||
|
// Exclude a user from all user
|
||||||
|
err = microcmdUserMustChangePassword().Run(t.Context(), []string{"change-test", "--all", "--exclude", "testuserexclude"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
|
||||||
|
assert.True(t, testUser.MustChangePassword)
|
||||||
|
testUserExclude = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuserexclude"})
|
||||||
|
assert.False(t, testUserExclude.MustChangePassword)
|
||||||
|
|
||||||
|
// Unset a flag for single user
|
||||||
|
err = microcmdUserMustChangePassword().Run(t.Context(), []string{"change-test", "--unset", "testuser"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuser"})
|
||||||
|
assert.False(t, testUser.MustChangePassword)
|
||||||
|
testUserExclude = unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "testuserexclude"})
|
||||||
|
assert.False(t, testUserExclude.MustChangePassword)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user