2018-05-16 23:05:00 -05:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-12-02 12:11:22 -05:00
|
|
|
|
2018-05-16 23:05:00 -05:00
|
|
|
package setting
|
2017-12-02 12:11:22 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2017-12-02 12:11:22 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 10:36:53 -05:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 02:12:22 -05:00
|
|
|
"code.gitea.io/gitea/services/contexttest"
|
2021-04-06 14:44:05 -05:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2017-12-02 12:11:22 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestChangePassword(t *testing.T) {
|
|
|
|
oldPassword := "password"
|
|
|
|
setting.MinPasswordLength = 6
|
2022-01-20 12:46:10 -05:00
|
|
|
pcALL := []string{"lower", "upper", "digit", "spec"}
|
|
|
|
pcLUN := []string{"lower", "upper", "digit"}
|
|
|
|
pcLU := []string{"lower", "upper"}
|
2017-12-02 12:11:22 -05:00
|
|
|
|
|
|
|
for _, req := range []struct {
|
2019-10-14 10:24:26 -05:00
|
|
|
OldPassword string
|
|
|
|
NewPassword string
|
|
|
|
Retype string
|
|
|
|
Message string
|
2019-10-15 22:09:58 -05:00
|
|
|
PasswordComplexity []string
|
2017-12-02 12:11:22 -05:00
|
|
|
}{
|
|
|
|
{
|
2019-10-14 10:24:26 -05:00
|
|
|
OldPassword: oldPassword,
|
|
|
|
NewPassword: "Qwerty123456-",
|
|
|
|
Retype: "Qwerty123456-",
|
|
|
|
Message: "",
|
2019-10-15 22:09:58 -05:00
|
|
|
PasswordComplexity: pcALL,
|
2019-10-14 10:24:26 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
OldPassword: oldPassword,
|
|
|
|
NewPassword: "12345",
|
|
|
|
Retype: "12345",
|
|
|
|
Message: "auth.password_too_short",
|
2019-10-15 22:09:58 -05:00
|
|
|
PasswordComplexity: pcALL,
|
2019-10-14 10:24:26 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
OldPassword: "12334",
|
|
|
|
NewPassword: "123456",
|
|
|
|
Retype: "123456",
|
|
|
|
Message: "settings.password_incorrect",
|
2019-10-15 22:09:58 -05:00
|
|
|
PasswordComplexity: pcALL,
|
2019-10-14 10:24:26 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
OldPassword: oldPassword,
|
|
|
|
NewPassword: "123456",
|
|
|
|
Retype: "12345",
|
|
|
|
Message: "form.password_not_match",
|
2019-10-15 22:09:58 -05:00
|
|
|
PasswordComplexity: pcALL,
|
2017-12-02 12:11:22 -05:00
|
|
|
},
|
|
|
|
{
|
2019-10-14 10:24:26 -05:00
|
|
|
OldPassword: oldPassword,
|
|
|
|
NewPassword: "Qwerty",
|
|
|
|
Retype: "Qwerty",
|
2019-10-15 22:09:58 -05:00
|
|
|
Message: "form.password_complexity",
|
|
|
|
PasswordComplexity: pcALL,
|
2017-12-02 12:11:22 -05:00
|
|
|
},
|
|
|
|
{
|
2019-10-14 10:24:26 -05:00
|
|
|
OldPassword: oldPassword,
|
|
|
|
NewPassword: "Qwerty",
|
|
|
|
Retype: "Qwerty",
|
2019-10-15 22:09:58 -05:00
|
|
|
Message: "form.password_complexity",
|
2019-10-14 10:24:26 -05:00
|
|
|
PasswordComplexity: pcLUN,
|
2017-12-02 12:11:22 -05:00
|
|
|
},
|
|
|
|
{
|
2019-10-14 10:24:26 -05:00
|
|
|
OldPassword: oldPassword,
|
|
|
|
NewPassword: "QWERTY",
|
|
|
|
Retype: "QWERTY",
|
2019-10-15 22:09:58 -05:00
|
|
|
Message: "form.password_complexity",
|
2019-10-14 10:24:26 -05:00
|
|
|
PasswordComplexity: pcLU,
|
2017-12-02 12:11:22 -05:00
|
|
|
},
|
|
|
|
} {
|
2023-05-03 22:55:35 -05:00
|
|
|
t.Run(req.OldPassword+"__"+req.NewPassword, func(t *testing.T) {
|
|
|
|
unittest.PrepareTestEnv(t)
|
|
|
|
setting.PasswordComplexity = req.PasswordComplexity
|
2023-09-01 06:26:07 -05:00
|
|
|
ctx, _ := contexttest.MockContext(t, "user/settings/security")
|
|
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
contexttest.LoadRepo(t, ctx, 1)
|
2017-12-02 12:11:22 -05:00
|
|
|
|
2023-05-03 22:55:35 -05:00
|
|
|
web.SetForm(ctx, &forms.ChangePasswordForm{
|
|
|
|
OldPassword: req.OldPassword,
|
|
|
|
Password: req.NewPassword,
|
|
|
|
Retype: req.Retype,
|
|
|
|
})
|
|
|
|
AccountPost(ctx)
|
2017-12-02 12:11:22 -05:00
|
|
|
|
2023-05-03 22:55:35 -05:00
|
|
|
assert.Contains(t, ctx.Flash.ErrorMsg, req.Message)
|
|
|
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
|
|
|
})
|
2017-12-02 12:11:22 -05:00
|
|
|
}
|
|
|
|
}
|