forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_test.go
166 lines (133 loc) · 4.05 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"
"github.com/go-authboss/authboss"
"github.com/go-authboss/authboss/internal/mocks"
)
func setup() *Register {
ab := authboss.New()
ab.RegisterOKPath = "/regsuccess"
ab.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
ab.XSRFName = "xsrf"
ab.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string {
return "xsrfvalue"
}
ab.ConfirmFields = []string{"password", "confirm_password"}
ab.Storer = mocks.NewMockStorer()
reg := Register{}
if err := reg.Initialize(ab); err != nil {
panic(err)
}
return ®
}
func TestRegister(t *testing.T) {
ab := authboss.New()
ab.Storer = mocks.NewMockStorer()
r := Register{}
if err := r.Initialize(ab); err != nil {
t.Error(err)
}
if r.Routes()["/register"] == nil {
t.Error("Expected a register handler at /register.")
}
sto := r.Storage()
if sto[r.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 := reg.NewContext()
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="`+reg.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(reg.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 := reg.NewContext()
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 := reg.Storer.Get(email); err != authboss.ErrUserNotFound {
t.Error("The user should not have been saved.")
}
}
func TestRegisterPostSuccess(t *testing.T) {
reg := setup()
reg.Policies = nil
w := httptest.NewRecorder()
vals := url.Values{}
email := "[email protected]"
vals.Set(reg.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 := reg.NewContext()
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 != reg.RegisterOKPath {
t.Error("Redirected to the wrong location", loc)
}
user, err := reg.Storer.Get(email)
if err == authboss.ErrUserNotFound {
t.Error("The user have been saved.")
}
attrs := authboss.Unbind(user)
if e, err := attrs.StringErr(reg.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.")
}
}