-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
change_test.go
141 lines (129 loc) · 3.91 KB
/
change_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
package users
import (
"strings"
"testing"
)
func TestChangePageLoggedOut(t *testing.T) {
computed, _ := runRequest(nil, nil, nil, Change)
expected := "PEThis page may only be accessed when you are logged in"
if !strings.HasPrefix(computed, expected) {
t.Errorf(`Expected prefix "%s" but string is "%s"`, expected, computed)
}
}
func TestChangePageLoggedIn(t *testing.T) {
computed, _ := runRequest(&MyUser{email: "x", state: StateVerified}, nil, nil, Change)
assertString("HICExF", computed, t)
}
func TestChangeNoData(t *testing.T) {
computed, _ := runRequest(&MyUser{email: "x", state: StateVerified}, nil, map[string]string{
"email": "x",
}, Change)
assertString("HICExF", computed, t)
}
func TestChangeNoCurrentPassword(t *testing.T) {
computed, _ := runRequest(&MyUser{
email: "x",
state: StateVerified,
}, nil, map[string]string{
"email": "y",
}, Change)
assertString("HIC!NCP!EyF", computed, t)
}
func TestChangeWrongCurrentPassword(t *testing.T) {
computed, _ := runRequest(&MyUser{
email: "x",
state: StateVerified,
passwordHash: []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC"),
}, nil, map[string]string{
"email": "y",
"currentpassword": "12345?",
}, Change)
assertString("HIC!WCP!EyF", computed, t)
}
func TestChangePasswordsDontMatch(t *testing.T) {
computed, _ := runRequest(&MyUser{
email: "x",
state: StateVerified,
passwordHash: []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC"),
}, nil, map[string]string{
"email": "x",
"currentpassword": "12345",
"password": "abc",
"passwordconfirm": "abcd",
}, Change)
assertString("HIC!2!ExF", computed, t)
}
func TestChangeInvalidPassword(t *testing.T) {
computed, _ := runRequest(&MyUser{
email: "x",
state: StateVerified,
passwordHash: []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC"),
}, nil, map[string]string{
"email": "x",
"currentpassword": "12345",
"password": "abc",
"passwordconfirm": "abc",
}, Change)
assertString("HIC!a3!ExF", computed, t)
}
func TestChangePasswordChange(t *testing.T) {
hash := []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC")
user := &MyUser{
email: "x",
state: StateVerified,
passwordHash: hash,
}
computed, _ := runRequest(user, nil, map[string]string{
"email": "x",
"currentpassword": "12345",
"password": "lkasflkhasf",
"passwordconfirm": "lkasflkhasf",
}, Change)
assertString("HIICF", computed, t)
if string(hash) == string(user.passwordHash) {
t.Error("Password hashes are still the same")
}
}
func TestChangeInvalidEmail(t *testing.T) {
computed, _ := runRequest(&MyUser{
email: "x",
state: StateVerified,
passwordHash: []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC"),
}, nil, map[string]string{
"email": "y",
"currentpassword": "12345",
}, Change)
assertString("HIC!01!EyF", computed, t)
}
func TestChangeEmailExists(t *testing.T) {
user := &MyUser{
email: "x",
state: StateVerified,
passwordHash: []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC"),
}
Config.LoadUserByEmail = func(email string) (User, error) {
return user, nil
}
html, mail := runRequest(user, nil, map[string]string{
"email": "@",
"currentpassword": "12345",
}, Change)
assertString("HOICF", html, t)
assertString("VE", mail, t)
}
func TestChangeEmail(t *testing.T) {
user := &MyUser{
email: "x",
state: StateVerified,
passwordHash: []byte("$2a$10$bRkkfyQZRP3eQkgkRvoktuvt6.ebieDKr/hZY4zWHg98HHEhbTHCC"),
}
Config.LoadUserByEmail = func(email string) (User, error) {
return nil, nil
}
html, mail := runRequest(user, nil, map[string]string{
"email": "@",
"currentpassword": "12345",
}, Change)
assertString("HOICF", html, t)
assertString("VC", mail, t)
}