mirror of
https://github.com/volatiletech/authboss.git
synced 2025-01-06 03:54:17 +02:00
Fix test breakages from new saving stuff.
This commit is contained in:
parent
9f4cde2934
commit
679798564f
@ -100,12 +100,7 @@ func (c *Confirm) AfterRegister(ctx *authboss.Context) {
|
||||
|
||||
ctx.User[StoreConfirmToken] = base64.StdEncoding.EncodeToString(sum[:])
|
||||
|
||||
username, ok := ctx.User.String(authboss.StoreUsername)
|
||||
if !ok {
|
||||
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to save confirm token, username doesn't exist")
|
||||
}
|
||||
|
||||
if err := ctx.SaveUser(username, authboss.Cfg.Storer); err != nil {
|
||||
if err := ctx.SaveUser(); err != nil {
|
||||
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to save user's token:", err)
|
||||
return
|
||||
}
|
||||
@ -186,7 +181,7 @@ func (c *Confirm) confirmHandler(ctx *authboss.Context, w http.ResponseWriter, r
|
||||
ctx.SessionStorer.Put(authboss.SessionKey, key)
|
||||
ctx.SessionStorer.Put(authboss.FlashSuccessKey, "Successfully confirmed your account.")
|
||||
|
||||
if err := ctx.SaveUser(key, authboss.Cfg.Storer); err != nil {
|
||||
if err := ctx.SaveUser(); err != nil {
|
||||
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to clear the user's token:", err)
|
||||
return
|
||||
}
|
||||
|
@ -107,14 +107,7 @@ func TestConfirm_AfterRegister(t *testing.T) {
|
||||
t.Error("Expected it to die with loading error:", str)
|
||||
}
|
||||
|
||||
ctx.User = authboss.Attributes{}
|
||||
log.Reset()
|
||||
c.AfterRegister(ctx)
|
||||
if str := log.String(); !strings.Contains(str, "username doesn't exist") {
|
||||
t.Error("Expected it to die with username error:", str)
|
||||
}
|
||||
|
||||
ctx.User[authboss.StoreUsername] = "uname"
|
||||
ctx.User = authboss.Attributes{authboss.StoreUsername: "uname"}
|
||||
log.Reset()
|
||||
c.AfterRegister(ctx)
|
||||
if str := log.String(); !strings.Contains(str, "no e-mail address to send to") {
|
||||
|
21
lock/lock.go
21
lock/lock.go
@ -76,21 +76,13 @@ func (l *Lock) BeforeAuth(ctx *authboss.Context) error {
|
||||
func (l *Lock) AfterAuth(ctx *authboss.Context) {
|
||||
if ctx.User == nil {
|
||||
fmt.Fprintln(authboss.Cfg.LogWriter, "lock: user not loaded in after auth callback")
|
||||
}
|
||||
|
||||
var username string
|
||||
if intf, ok := ctx.User["username"]; !ok {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, "lock: username not present")
|
||||
return
|
||||
} else if username, ok = intf.(string); !ok {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, "lock: username wrong type")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.User[StoreAttemptNumber] = 0
|
||||
ctx.User[StoreAttemptTime] = time.Now().UTC()
|
||||
|
||||
if err := ctx.SaveUser(username, authboss.Cfg.Storer); err != nil {
|
||||
if err := ctx.SaveUser(); err != nil {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, "lock: saving user failed %v", err)
|
||||
}
|
||||
}
|
||||
@ -101,15 +93,6 @@ func (l *Lock) AfterAuthFail(ctx *authboss.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var username string
|
||||
if intf, ok := ctx.User["username"]; !ok {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, "lock: username not present")
|
||||
return
|
||||
} else if username, ok = intf.(string); !ok {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, "lock: username wrong type")
|
||||
return
|
||||
}
|
||||
|
||||
lastAttempt := time.Now().UTC()
|
||||
if attemptTimeIntf, ok := ctx.User[StoreAttemptTime]; ok {
|
||||
if attemptTime, ok := attemptTimeIntf.(time.Time); ok {
|
||||
@ -137,7 +120,7 @@ func (l *Lock) AfterAuthFail(ctx *authboss.Context) {
|
||||
}
|
||||
ctx.User[StoreAttemptTime] = time.Now().UTC()
|
||||
|
||||
if err := ctx.SaveUser(username, authboss.Cfg.Storer); err != nil {
|
||||
if err := ctx.SaveUser(); err != nil {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, "lock: saving user failed %v", err)
|
||||
}
|
||||
}
|
||||
|
@ -44,23 +44,11 @@ func TestAfterAuth(t *testing.T) {
|
||||
t.Error("Expected nothing to be set, missing user.")
|
||||
}
|
||||
|
||||
ctx.User = map[string]interface{}{"otherattribute": "somevalue"}
|
||||
lock.AfterAuth(ctx)
|
||||
if _, ok := ctx.User[StoreAttemptNumber]; ok {
|
||||
t.Error("Expected username not present to stop this assignment.")
|
||||
}
|
||||
|
||||
ctx.User["username"] = 5
|
||||
lock.AfterAuth(ctx)
|
||||
if _, ok := ctx.User[StoreAttemptNumber]; ok {
|
||||
t.Error("Expected username wrong type stop this assignment.")
|
||||
}
|
||||
|
||||
storer := mocks.NewMockStorer()
|
||||
authboss.Cfg.Storer = storer
|
||||
ctx.User["username"] = "username"
|
||||
lock.AfterAuth(ctx)
|
||||
ctx.User = authboss.Attributes{"username": "username"}
|
||||
|
||||
lock.AfterAuth(ctx)
|
||||
if storer.Users["username"][StoreAttemptNumber].(int) != 0 {
|
||||
t.Error("StoreAttemptNumber set incorrectly.")
|
||||
}
|
||||
@ -153,18 +141,6 @@ func TestAfterAuthFail_Errors(t *testing.T) {
|
||||
if _, ok := ctx.User[StoreAttemptNumber]; ok {
|
||||
t.Error("Expected nothing to be set, missing user.")
|
||||
}
|
||||
|
||||
ctx.User = map[string]interface{}{"otherattribute": "somevalue"}
|
||||
lock.AfterAuthFail(ctx)
|
||||
if _, ok := ctx.User[StoreAttemptNumber]; ok {
|
||||
t.Error("Expected username not present to stop this assignment.")
|
||||
}
|
||||
|
||||
ctx.User["username"] = 5
|
||||
lock.AfterAuthFail(ctx)
|
||||
if _, ok := ctx.User[StoreAttemptNumber]; ok {
|
||||
t.Error("Expected username wrong type stop this assignment.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLock(t *testing.T) {
|
||||
|
@ -133,7 +133,7 @@ func (m *RecoverModule) recoverComplete(ctx *authboss.Context, xsrfName, xsrfTok
|
||||
ctx.User[attrRecoverTokenExpiry] = nullTime
|
||||
|
||||
username, _ := ctx.User.String(attrUsername)
|
||||
if err := ctx.SaveUser(username, authboss.Cfg.Storer); err != nil {
|
||||
if err := ctx.SaveUser(); err != nil {
|
||||
fmt.Fprintf(authboss.Cfg.LogWriter, errFormat, "failed to save user", err)
|
||||
return defaultErrPage
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ func (m *RecoverModule) makeAndSendToken(ctx *authboss.Context, username string)
|
||||
ctx.User[attrRecoverToken] = base64.StdEncoding.EncodeToString(sum[:])
|
||||
ctx.User[attrRecoverTokenExpiry] = time.Now().Add(authboss.Cfg.RecoverTokenDuration)
|
||||
|
||||
if err = ctx.SaveUser(username, authboss.Cfg.Storer); err != nil {
|
||||
if err = ctx.SaveUser(); err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user