/
handle_change-password_test.go
149 lines (124 loc) · 4.16 KB
/
handle_change-password_test.go
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package handlers
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/cortezaproject/corteza-server/auth/request"
"github.com/cortezaproject/corteza-server/auth/settings"
"github.com/cortezaproject/corteza-server/system/service"
"github.com/cortezaproject/corteza-server/system/types"
"github.com/stretchr/testify/require"
)
func Test_changePasswordForm_setValues(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{}
authService *mockAuthService
authHandlers *AuthHandlers
authReq *request.AuthReq
rq = require.New(t)
)
service.CurrentSettings = &types.AppSettings{}
service.CurrentSettings.Auth.Internal.Enabled = true
authSettings := &settings.Settings{}
authService = prepareClientAuthService()
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authReq = prepareClientAuthReq(authHandlers, req, user)
payload := map[string]string{"key": "value"}
authReq.SetKV(payload)
err := authHandlers.changePasswordForm(authReq)
rq.NoError(err)
rq.Equal(TmplChangePassword, authReq.Template)
rq.Equal(payload, authReq.Data["form"])
}
func Test_changePasswordProc(t *testing.T) {
var (
user = makeMockUser()
req = &http.Request{}
authService authService
authHandlers *AuthHandlers
authReq *request.AuthReq
)
service.CurrentSettings = &types.AppSettings{}
tcc := []testingExpect{
{
name: "successful password change",
payload: map[string]string(nil),
alerts: []request.Alert{{Type: "primary", Text: "change-password.alerts.password-change-success", Html: ""}},
link: GetLinks().Profile,
fn: func(_ *settings.Settings) {
authService = &authServiceMocked{
changePassword: func(ctx context.Context, userID uint64, oldPassword, newPassword string) (err error) {
return nil
},
}
},
},
{
name: "provided password is not secure",
payload: map[string]string{"error": "provided password is not secure; use longer password with more special characters"},
link: GetLinks().ChangePassword,
fn: func(_ *settings.Settings) {
authService = &authServiceMocked{
changePassword: func(ctx context.Context, userID uint64, oldPassword, newPassword string) (err error) {
return service.AuthErrPasswordNotSecure()
},
}
},
},
{
name: "internal login is not enabled",
payload: map[string]string{"error": "internal login (username/password) is disabled"},
link: GetLinks().ChangePassword,
fn: func(_ *settings.Settings) {
authService = &authServiceMocked{
changePassword: func(ctx context.Context, userID uint64, oldPassword, newPassword string) (err error) {
return service.AuthErrInternalLoginDisabledByConfig()
},
}
},
},
{
name: "password change failed old password does not match",
payload: map[string]string{"error": "failed to change password, old password does not match"},
link: GetLinks().ChangePassword,
fn: func(_ *settings.Settings) {
authService = &authServiceMocked{
changePassword: func(ctx context.Context, userID uint64, oldPassword, newPassword string) (err error) {
return service.AuthErrPasswodResetFailedOldPasswordCheckFailed()
},
}
},
},
{
name: "password change failed for unknown user",
payload: map[string]string{"error": "failed to change password for the unknown user"},
link: GetLinks().ChangePassword,
fn: func(_ *settings.Settings) {
authService = &authServiceMocked{
changePassword: func(ctx context.Context, userID uint64, oldPassword, newPassword string) (err error) {
return service.AuthErrPasswordChangeFailedForUnknownUser()
},
}
},
},
}
for _, tc := range tcc {
t.Run(tc.name, func(t *testing.T) {
rq := require.New(t)
req.PostForm = url.Values{}
authSettings := &settings.Settings{}
tc.fn(authSettings)
authHandlers = prepareClientAuthHandlers(authService, authSettings)
authReq = prepareClientAuthReq(authHandlers, req, user)
err := authHandlers.changePasswordProc(authReq)
rq.NoError(err)
rq.Equal(tc.payload, authReq.GetKV())
if tc.alerts != nil {
rq.Equal(tc.alerts, authReq.NewAlerts)
}
rq.Equal(tc.link, authReq.RedirectTo)
})
}
}