Files
team/server/channels/app/users/password_test.go
Jesse HallamandGitHub edf2075381 MM-68149: Upgrade to Go 1.26.2 (#36418)
* MM-68149: upgrade to Go 1.26.2

Update go directive in go.mod and .go-version.

* MM-68149: replace pointer helpers with Go 1.26 new()

Go 1.26 extends the built-in new() to accept an initial value expression,
making typed-pointer helpers like model.NewPointer(x), bToP(x), and boolPtr(x)
redundant. Replace every call site with new(x) and remove the now-unused
helper functions and their //go:fix inline directives.

* MM-68149: apply go fix for reflect API and format-string changes

- reflect.Ptr → reflect.Pointer (renamed in Go 1.18, deprecated alias removed in 1.26)
- reflect range-over-struct: for i := 0; i < t.NumField(); i++ → for field := range t.Fields()
  and the equivalent for Methods() and interface types
- Fix format-string concatenation and variadic-arg mismatches flagged by go vet

* MM-68149: update JPEG fixtures and test infrastructure for Go 1.26 encoder

Go 1.26 ships a new image/jpeg encoder that produces slightly different output.
Regenerate all JPEG fixture files and switch the comparison helpers from
byte-equality to pixel-level comparison with a small per-channel tolerance,
so minor encoder drift across patch versions is handled automatically.

Add -update-fixtures flag to make it easy to regenerate fixtures after future
major Go upgrades. Document the update procedure in tests/README.md.

* MM-68149: CI check that go fix ./... produces no changes

* Fix real bugs flagged by CodeRabbit review

- group.go: set newGroup.MemberCount not group.MemberCount (member count
  was populated on the wrong variable and lost before publish/return)
- file_test.go: guard compareImage(GetFilePreview) on the preview slice
  length, not the thumbnail slice length (copy-paste error)
- config_test.go: remove duplicate MinimumLength assignment

* fixup! Fix real bugs flagged by CodeRabbit review
2026-05-12 15:59:12 +00:00

134 lines
3.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package users
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
func TestIsPasswordValidWithSettings(t *testing.T) {
for name, tc := range map[string]struct {
Password string
Settings *model.PasswordSettings
ExpectedError string
}{
"Short": {
Password: strings.Repeat("x", model.PasswordFIPSMinimumLength),
Settings: &model.PasswordSettings{
MinimumLength: model.NewPointer(model.PasswordFIPSMinimumLength),
Lowercase: new(false),
Uppercase: new(false),
Number: new(false),
Symbol: new(false),
},
},
"Long": {
Password: strings.Repeat("x", model.PasswordMaximumLength),
Settings: &model.PasswordSettings{
Lowercase: new(false),
Uppercase: new(false),
Number: new(false),
Symbol: new(false),
},
},
"TooShort": {
Password: strings.Repeat("x", 2),
Settings: &model.PasswordSettings{
MinimumLength: model.NewPointer(model.PasswordFIPSMinimumLength),
Lowercase: new(false),
Uppercase: new(false),
Number: new(false),
Symbol: new(false),
},
ExpectedError: "model.user.is_valid.pwd_min_length.app_error",
},
"TooLong": {
Password: strings.Repeat("x", model.PasswordMaximumLength+1),
Settings: &model.PasswordSettings{
Lowercase: new(false),
Uppercase: new(false),
Number: new(false),
Symbol: new(false),
},
ExpectedError: "model.user.is_valid.pwd_max_length.app_error",
},
"MissingLower": {
Password: "AAAAAAAAAAASD123!@#",
Settings: &model.PasswordSettings{
Lowercase: new(true),
Uppercase: new(false),
Number: new(false),
Symbol: new(false),
},
ExpectedError: "model.user.is_valid.pwd_lowercase.app_error",
},
"MissingUpper": {
Password: "aaaaaaaaaaaaasd123!@#",
Settings: &model.PasswordSettings{
Uppercase: new(true),
Lowercase: new(false),
Number: new(false),
Symbol: new(false),
},
ExpectedError: "model.user.is_valid.pwd_uppercase.app_error",
},
"MissingNumber": {
Password: "asasdasdsadASD!@#",
Settings: &model.PasswordSettings{
Number: new(true),
Lowercase: new(false),
Uppercase: new(false),
Symbol: new(false),
},
ExpectedError: "model.user.is_valid.pwd_number.app_error",
},
"MissingSymbol": {
Password: "asdasdasdasdasdASD123",
Settings: &model.PasswordSettings{
Symbol: new(true),
Lowercase: new(false),
Uppercase: new(false),
Number: new(false),
},
ExpectedError: "model.user.is_valid.pwd_symbol.app_error",
},
"MissingMultiple": {
Password: "asdasdasdasdasdasd",
Settings: &model.PasswordSettings{
Lowercase: new(true),
Uppercase: new(true),
Number: new(true),
Symbol: new(true),
},
ExpectedError: "model.user.is_valid.pwd_uppercase_number_symbol.app_error",
},
"Everything": {
Password: "asdASDasd!@#123",
Settings: &model.PasswordSettings{
Lowercase: new(true),
Uppercase: new(true),
Number: new(true),
Symbol: new(true),
},
},
} {
tc.Settings.SetDefaults()
t.Run(name, func(t *testing.T) {
if err := IsPasswordValidWithSettings(tc.Password, tc.Settings); tc.ExpectedError == "" {
assert.NoError(t, err)
} else {
invErr, ok := err.(*ErrInvalidPassword)
require.True(t, ok)
assert.Equal(t, tc.ExpectedError, invErr.Id())
}
})
}
}