summaryrefslogtreecommitdiffstats
path: root/utils/password_test.go
blob: 4daf25018f084d1cd1dce080a8863c4be2f3c67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package utils

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/mattermost/mattermost-server/model"
)

func TestIsPasswordValid(t *testing.T) {
	for name, tc := range map[string]struct {
		Password      string
		ExpectedError string
	}{
		"Short": {
			Password: strings.Repeat("x", model.PASSWORD_MINIMUM_LENGTH),
		},
		"Long": {
			Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH),
		},
		"TooShort": {
			Password:      strings.Repeat("x", model.PASSWORD_MINIMUM_LENGTH-1),
			ExpectedError: "model.user.is_valid.pwd.app_error",
		},
		"TooLong": {
			Password:      strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH+1),
			ExpectedError: "model.user.is_valid.pwd.app_error",
		},
	} {
		t.Run(name, func(t *testing.T) {
			if err := IsPasswordValid(tc.Password); tc.ExpectedError == "" {
				assert.Nil(t, err)
			} else {
				assert.Equal(t, tc.ExpectedError, err.Id)
			}
		})
	}
}

func TestIsPasswordValidWithSettings(t *testing.T) {
	for name, tc := range map[string]struct {
		Password      string
		Settings      *model.PasswordSettings
		ExpectedError string
	}{
		"Short": {
			Password: strings.Repeat("x", 3),
			Settings: &model.PasswordSettings{
				MinimumLength: model.NewInt(3),
			},
		},
		"Long": {
			Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH),
			Settings: &model.PasswordSettings{},
		},
		"TooShort": {
			Password: strings.Repeat("x", 2),
			Settings: &model.PasswordSettings{
				MinimumLength: model.NewInt(3),
			},
			ExpectedError: "model.user.is_valid.pwd.app_error",
		},
		"TooLong": {
			Password:      strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH+1),
			Settings:      &model.PasswordSettings{},
			ExpectedError: "model.user.is_valid.pwd.app_error",
		},
		"MissingLower": {
			Password: "ASD123!@#",
			Settings: &model.PasswordSettings{
				Lowercase: model.NewBool(true),
			},
			ExpectedError: "model.user.is_valid.pwd_lowercase.app_error",
		},
		"MissingUpper": {
			Password: "asd123!@#",
			Settings: &model.PasswordSettings{
				Uppercase: model.NewBool(true),
			},
			ExpectedError: "model.user.is_valid.pwd_uppercase.app_error",
		},
		"MissingNumber": {
			Password: "asdASD!@#",
			Settings: &model.PasswordSettings{
				Number: model.NewBool(true),
			},
			ExpectedError: "model.user.is_valid.pwd_number.app_error",
		},
		"MissingSymbol": {
			Password: "asdASD123",
			Settings: &model.PasswordSettings{
				Symbol: model.NewBool(true),
			},
			ExpectedError: "model.user.is_valid.pwd_symbol.app_error",
		},
		"MissingMultiple": {
			Password: "asd",
			Settings: &model.PasswordSettings{
				Lowercase: model.NewBool(true),
				Uppercase: model.NewBool(true),
				Number:    model.NewBool(true),
				Symbol:    model.NewBool(true),
			},
			ExpectedError: "model.user.is_valid.pwd_lowercase_uppercase_number_symbol.app_error",
		},
		"Everything": {
			Password: "asdASD!@#123",
			Settings: &model.PasswordSettings{
				Lowercase: model.NewBool(true),
				Uppercase: model.NewBool(true),
				Number:    model.NewBool(true),
				Symbol:    model.NewBool(true),
			},
		},
	} {
		tc.Settings.SetDefaults()
		t.Run(name, func(t *testing.T) {
			if err := IsPasswordValidWithSettings(tc.Password, tc.Settings); tc.ExpectedError == "" {
				assert.Nil(t, err)
			} else {
				assert.Equal(t, tc.ExpectedError, err.Id)
			}
		})
	}
}