forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_test.go
166 lines (132 loc) · 4.2 KB
/
register_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package register
import (
"bytes"
"html/template"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"gopkg.in/authboss.v0"
"gopkg.in/authboss.v0/internal/mocks"
)
func setup() *Register {
authboss.Cfg = authboss.NewConfig()
authboss.a.RegisterOKPath = "/regsuccess"
authboss.a.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
authboss.a.XSRFName = "xsrf"
authboss.a.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string {
return "xsrfvalue"
}
authboss.a.ConfirmFields = []string{"password", "confirm_password"}
authboss.a.Storer = mocks.NewMockStorer()
reg := Register{}
if err := reg.Initialize(); err != nil {
panic(err)
}
return ®
}
func TestRegister(t *testing.T) {
authboss.Cfg = authboss.NewConfig()
authboss.a.Storer = mocks.NewMockStorer()
r := Register{}
if err := r.Initialize(); err != nil {
t.Error(err)
}
if r.Routes()["/register"] == nil {
t.Error("Expected a register handler at /register.")
}
sto := r.Storage()
if sto[authboss.a.PrimaryID] != authboss.String {
t.Error("Wanted primary ID to be a string.")
}
if sto[authboss.StorePassword] != authboss.String {
t.Error("Wanted password to be a string.")
}
}
func TestRegisterGet(t *testing.T) {
reg := setup()
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/register", nil)
ctx, _ := authboss.ContextFromRequest(r)
ctx.SessionStorer = mocks.NewMockClientStorer()
if err := reg.registerHandler(ctx, w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusOK {
t.Error("It should have written a 200:", w.Code)
}
if w.Body.Len() == 0 {
t.Error("It should have wrote a response.")
}
if str := w.Body.String(); !strings.Contains(str, "<form") {
t.Error("It should have rendered a nice form:", str)
} else if !strings.Contains(str, `name="`+authboss.a.PrimaryID) {
t.Error("Form should contain the primary ID:", str)
}
}
func TestRegisterPostValidationErrs(t *testing.T) {
reg := setup()
w := httptest.NewRecorder()
vals := url.Values{}
email := "[email protected]"
vals.Set(authboss.a.PrimaryID, email)
vals.Set(authboss.StorePassword, "pass")
vals.Set(authboss.ConfirmPrefix+authboss.StorePassword, "pass2")
r, _ := http.NewRequest("POST", "/register", bytes.NewBufferString(vals.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx, _ := authboss.ContextFromRequest(r)
ctx.SessionStorer = mocks.NewMockClientStorer()
if err := reg.registerHandler(ctx, w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusOK {
t.Error("It should have written a 200:", w.Code)
}
if w.Body.Len() == 0 {
t.Error("It should have wrote a response.")
}
if str := w.Body.String(); !strings.Contains(str, "Does not match password") {
t.Error("Confirm password should have an error:", str)
}
if _, err := authboss.a.Storer.Get(email); err != authboss.ErrUserNotFound {
t.Error("The user should not have been saved.")
}
}
func TestRegisterPostSuccess(t *testing.T) {
reg := setup()
w := httptest.NewRecorder()
vals := url.Values{}
email := "[email protected]"
vals.Set(authboss.a.PrimaryID, email)
vals.Set(authboss.StorePassword, "pass")
vals.Set(authboss.ConfirmPrefix+authboss.StorePassword, "pass")
r, _ := http.NewRequest("POST", "/register", bytes.NewBufferString(vals.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx, _ := authboss.ContextFromRequest(r)
ctx.SessionStorer = mocks.NewMockClientStorer()
if err := reg.registerHandler(ctx, w, r); err != nil {
t.Error(err)
}
if w.Code != http.StatusFound {
t.Error("It should have written a redirect:", w.Code)
}
if loc := w.Header().Get("Location"); loc != authboss.a.RegisterOKPath {
t.Error("Redirected to the wrong location", loc)
}
user, err := authboss.a.Storer.Get(email)
if err == authboss.ErrUserNotFound {
t.Error("The user have been saved.")
}
attrs := authboss.Unbind(user)
if e, err := attrs.StringErr(authboss.a.PrimaryID); err != nil {
t.Error(err)
} else if e != email {
t.Errorf("Email was not set properly, want: %s, got: %s", email, e)
}
if p, err := attrs.StringErr(authboss.StorePassword); err != nil {
t.Error(err)
} else if p == "pass" {
t.Error("Password was not hashed.")
}
}