From 679798564f8c1a69b6b1e624a5702e5c3a388f70 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 18 Feb 2015 08:57:50 -0800 Subject: [PATCH] Fix test breakages from new saving stuff. --- confirm/confirm.go | 9 ++------- confirm/confirm_test.go | 9 +-------- lock/lock.go | 21 ++------------------- lock/lock_test.go | 28 ++-------------------------- recover/complete.go | 2 +- recover/init.go | 2 +- 6 files changed, 9 insertions(+), 62 deletions(-) diff --git a/confirm/confirm.go b/confirm/confirm.go index 6762573..414818a 100644 --- a/confirm/confirm.go +++ b/confirm/confirm.go @@ -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 } diff --git a/confirm/confirm_test.go b/confirm/confirm_test.go index af07d4f..3bfece5 100644 --- a/confirm/confirm_test.go +++ b/confirm/confirm_test.go @@ -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") { diff --git a/lock/lock.go b/lock/lock.go index cfbdb89..5aeab4f 100644 --- a/lock/lock.go +++ b/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) } } diff --git a/lock/lock_test.go b/lock/lock_test.go index f16a7c8..062f173 100644 --- a/lock/lock_test.go +++ b/lock/lock_test.go @@ -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) { diff --git a/recover/complete.go b/recover/complete.go index 93a0081..587e547 100644 --- a/recover/complete.go +++ b/recover/complete.go @@ -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 } diff --git a/recover/init.go b/recover/init.go index fdf164f..4bc0c74 100644 --- a/recover/init.go +++ b/recover/init.go @@ -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 }