forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authboss_test.go
executable file
·180 lines (148 loc) · 4.19 KB
/
authboss_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package authboss
import (
"database/sql"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestAuthBossInit(t *testing.T) {
t.Parallel()
ab := New()
ab.LogWriter = ioutil.Discard
err := ab.Init()
if err != nil {
t.Error("Unexpected error:", err)
}
}
func TestAuthBossCurrentUser(t *testing.T) {
t.Parallel()
ab := New()
ab.LogWriter = ioutil.Discard
ab.Storer = mockStorer{"joe": Attributes{"email": "john@john.com", "password": "lies"}}
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return mockClientStore{SessionKey: "joe"}
}
ab.CookieStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return mockClientStore{}
}
if err := ab.Init(); err != nil {
t.Error("Unexpected error:", err)
}
rec := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "localhost", nil)
userStruct := ab.CurrentUserP(rec, req)
us := userStruct.(*mockUser)
if us.Email != "john@john.com" || us.Password != "lies" {
t.Error("Wrong user found!")
}
}
func TestAuthBossCurrentUserCallbacks(t *testing.T) {
t.Parallel()
ab := New()
ab.LogWriter = ioutil.Discard
ab.Storer = mockStorer{"joe": Attributes{"email": "john@john.com", "password": "lies"}}
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return mockClientStore{SessionKey: "joe"}
}
ab.CookieStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return mockClientStore{}
}
if err := ab.Init(); err != nil {
t.Error("Unexpected error:", err)
}
rec := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "localhost", nil)
afterGetUser := errors.New("afterGetUser")
beforeGetUser := errors.New("beforeGetUser")
beforeGetUserSession := errors.New("beforeGetUserSession")
ab.Callbacks.After(EventGetUser, func(*Context) error {
return afterGetUser
})
if _, err := ab.CurrentUser(rec, req); err != afterGetUser {
t.Error("Want:", afterGetUser, "Got:", err)
}
ab.Callbacks.Before(EventGetUser, func(*Context) (Interrupt, error) {
return InterruptNone, beforeGetUser
})
if _, err := ab.CurrentUser(rec, req); err != beforeGetUser {
t.Error("Want:", beforeGetUser, "Got:", err)
}
ab.Callbacks.Before(EventGetUserSession, func(*Context) (Interrupt, error) {
return InterruptNone, beforeGetUserSession
})
if _, err := ab.CurrentUser(rec, req); err != beforeGetUserSession {
t.Error("Want:", beforeGetUserSession, "Got:", err)
}
}
func TestAuthbossUpdatePassword(t *testing.T) {
t.Parallel()
ab := New()
session := mockClientStore{}
cookies := mockClientStore{}
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return session
}
ab.CookieStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return cookies
}
called := false
ab.Callbacks.After(EventPasswordReset, func(ctx *Context) error {
called = true
return nil
})
user1 := struct {
Password string
}{}
user2 := struct {
Password sql.NullString
}{}
r, _ := http.NewRequest("GET", "http://localhost", nil)
called = false
err := ab.UpdatePassword(nil, r, "newpassword", &user1, func() error { return nil })
if err != nil {
t.Error(err)
}
if len(user1.Password) == 0 {
t.Error("Password not updated")
}
if !called {
t.Error("Callbacks should have been called.")
}
called = false
err = ab.UpdatePassword(nil, r, "newpassword", &user2, func() error { return nil })
if err != nil {
t.Error(err)
}
if !user2.Password.Valid || len(user2.Password.String) == 0 {
t.Error("Password not updated")
}
if !called {
t.Error("Callbacks should have been called.")
}
called = false
oldPassword := user1.Password
err = ab.UpdatePassword(nil, r, "", &user1, func() error { return nil })
if err != nil {
t.Error(err)
}
if user1.Password != oldPassword {
t.Error("Password not updated")
}
if called {
t.Error("Callbacks should not have been called")
}
}
func TestAuthbossUpdatePasswordFail(t *testing.T) {
t.Parallel()
ab := New()
user1 := struct {
Password string
}{}
anErr := errors.New("AnError")
err := ab.UpdatePassword(nil, nil, "update", &user1, func() error { return anErr })
if err != anErr {
t.Error("Expected an specific error:", err)
}
}