-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
password_test.go
106 lines (95 loc) · 3.35 KB
/
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
package users
import (
"testing"
"time"
)
func TestForgottenPasswordPageLoggedIn(t *testing.T) {
computed, _ := runRequest(&MyUser{email: "X", state: StateVerified}, nil, nil, ForgottenPassword)
assertString("redirect", computed, t)
}
func TestForgottenPasswordPageLoggedOut(t *testing.T) {
computed, _ := runRequest(nil, nil, nil, ForgottenPassword)
assertString("HOFPF", computed, t)
}
func TestForgottenPasswordExistingUser(t *testing.T) {
user := &MyUser{email: "@", state: StateVerified}
Config.LoadUserByEmail = func(email string) (User, error) {
return user, nil
}
html, mail := runRequest(nil, nil, map[string]string{
"email": "@",
}, ForgottenPassword)
assertString("HOLS@F", html, t)
assertString("RE", mail, t)
if user.passwordToken == "" {
t.Error("No password token set")
}
}
func TestForgottenPasswordUnknownUser(t *testing.T) {
Config.LoadUserByEmail = func(email string) (User, error) {
return nil, nil
}
html, mail := runRequest(nil, nil, map[string]string{
"email": "@",
}, ForgottenPassword)
assertString("HOLS@F", html, t)
assertString("RU", mail, t)
}
func TestResetPasswordUnknownUser(t *testing.T) {
Config.LoadUserByPasswordToken = func(token string) (User, error) {
return nil, nil
}
computed, _ := runRequest(nil, nil, nil, ResetPassword)
assertString("HOFP!TNF!F", computed, t)
}
func TestResetPasswordExpiredToken(t *testing.T) {
Config.LoadUserByPasswordToken = func(token string) (User, error) {
return &MyUser{passwordToken: "12345", tokenCreated: time.Now().Add(-48 * time.Hour), state: StateVerified}, nil
}
computed, _ := runRequest(nil, map[string]string{"token": "12345"}, nil, ResetPassword)
assertString("HOFP!TE!F", computed, t)
}
func TestResetPasswordPage(t *testing.T) {
Config.LoadUserByPasswordToken = func(token string) (User, error) {
return &MyUser{passwordToken: "12345", tokenCreated: time.Now(), state: StateVerified}, nil
}
computed, _ := runRequest(nil, map[string]string{"token": "12345"}, nil, ResetPassword)
assertString("HORP12345F", computed, t)
}
func TestResetPasswordNoMatchingPasswords(t *testing.T) {
Config.LoadUserByPasswordToken = func(token string) (User, error) {
return &MyUser{passwordToken: "12345", tokenCreated: time.Now(), state: StateVerified}, nil
}
computed, _ := runRequest(nil, nil, map[string]string{
"token": "12345",
"password": "abcd",
"passwordconfirm": "abc",
}, ResetPassword)
assertString("HORP12345!2!F", computed, t)
}
func TestResetPasswordInvalidPassword(t *testing.T) {
Config.LoadUserByPasswordToken = func(token string) (User, error) {
return &MyUser{passwordToken: "12345", tokenCreated: time.Now(), state: StateVerified}, nil
}
computed, _ := runRequest(nil, nil, map[string]string{
"token": "12345",
"password": "abc",
"passwordconfirm": "abc",
}, ResetPassword)
assertString("HORP12345!a3!F", computed, t)
}
func TestResetPassword(t *testing.T) {
user := &MyUser{passwordToken: "12345", tokenCreated: time.Now(), state: StateVerified}
Config.LoadUserByPasswordToken = func(token string) (User, error) {
return user, nil
}
computed, _ := runRequest(nil, nil, map[string]string{
"token": "12345",
"password": "kjhvasfiuwbucj",
"passwordconfirm": "kjhvasfiuwbucj",
}, ResetPassword)
assertString("HOPRF", computed, t)
if len(user.passwordHash) == 0 {
t.Error("No password was set")
}
}