code.gitea.io/gitea@v1.21.7/routers/web/user/setting/account_test.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/models/unittest"
    11  	"code.gitea.io/gitea/modules/contexttest"
    12  	"code.gitea.io/gitea/modules/setting"
    13  	"code.gitea.io/gitea/modules/web"
    14  	"code.gitea.io/gitea/services/forms"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestChangePassword(t *testing.T) {
    20  	oldPassword := "password"
    21  	setting.MinPasswordLength = 6
    22  	pcALL := []string{"lower", "upper", "digit", "spec"}
    23  	pcLUN := []string{"lower", "upper", "digit"}
    24  	pcLU := []string{"lower", "upper"}
    25  
    26  	for _, req := range []struct {
    27  		OldPassword        string
    28  		NewPassword        string
    29  		Retype             string
    30  		Message            string
    31  		PasswordComplexity []string
    32  	}{
    33  		{
    34  			OldPassword:        oldPassword,
    35  			NewPassword:        "Qwerty123456-",
    36  			Retype:             "Qwerty123456-",
    37  			Message:            "",
    38  			PasswordComplexity: pcALL,
    39  		},
    40  		{
    41  			OldPassword:        oldPassword,
    42  			NewPassword:        "12345",
    43  			Retype:             "12345",
    44  			Message:            "auth.password_too_short",
    45  			PasswordComplexity: pcALL,
    46  		},
    47  		{
    48  			OldPassword:        "12334",
    49  			NewPassword:        "123456",
    50  			Retype:             "123456",
    51  			Message:            "settings.password_incorrect",
    52  			PasswordComplexity: pcALL,
    53  		},
    54  		{
    55  			OldPassword:        oldPassword,
    56  			NewPassword:        "123456",
    57  			Retype:             "12345",
    58  			Message:            "form.password_not_match",
    59  			PasswordComplexity: pcALL,
    60  		},
    61  		{
    62  			OldPassword:        oldPassword,
    63  			NewPassword:        "Qwerty",
    64  			Retype:             "Qwerty",
    65  			Message:            "form.password_complexity",
    66  			PasswordComplexity: pcALL,
    67  		},
    68  		{
    69  			OldPassword:        oldPassword,
    70  			NewPassword:        "Qwerty",
    71  			Retype:             "Qwerty",
    72  			Message:            "form.password_complexity",
    73  			PasswordComplexity: pcLUN,
    74  		},
    75  		{
    76  			OldPassword:        oldPassword,
    77  			NewPassword:        "QWERTY",
    78  			Retype:             "QWERTY",
    79  			Message:            "form.password_complexity",
    80  			PasswordComplexity: pcLU,
    81  		},
    82  	} {
    83  		t.Run(req.OldPassword+"__"+req.NewPassword, func(t *testing.T) {
    84  			unittest.PrepareTestEnv(t)
    85  			setting.PasswordComplexity = req.PasswordComplexity
    86  			ctx, _ := contexttest.MockContext(t, "user/settings/security")
    87  			contexttest.LoadUser(t, ctx, 2)
    88  			contexttest.LoadRepo(t, ctx, 1)
    89  
    90  			web.SetForm(ctx, &forms.ChangePasswordForm{
    91  				OldPassword: req.OldPassword,
    92  				Password:    req.NewPassword,
    93  				Retype:      req.Retype,
    94  			})
    95  			AccountPost(ctx)
    96  
    97  			assert.Contains(t, ctx.Flash.ErrorMsg, req.Message)
    98  			assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
    99  		})
   100  	}
   101  }