mirror of
https://github.com/volatiletech/authboss.git
synced 2025-07-13 01:20:17 +02:00
Introduce new type of client storage
- This addresses the problem of having to update multiple times during one request. It's hard to have a nice interface especially with JWT because you always end up having to decode the request, encode new response, write header, then a second write to it comes, and where do you grab the value from? Often you don't have access to the response as a "read" structure. So we store it as events instead, and play those events against the original data right before the response is written to set the headers.
This commit is contained in:
@ -1,13 +1,8 @@
|
||||
package authboss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func TestAuthBossInit(t *testing.T) {
|
||||
@ -22,74 +17,6 @@ func TestAuthBossInit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthBossCurrentUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ab := New()
|
||||
ab.LogWriter = ioutil.Discard
|
||||
ab.StoreLoader = mockStoreLoader{"joe": mockUser{Email: "john@john.com", Password: "lies"}}
|
||||
ab.ViewLoader = mockRenderLoader{}
|
||||
ab.SessionStoreMaker = newMockClientStoreMaker(mockClientStore{SessionKey: "joe"})
|
||||
ab.CookieStoreMaker = newMockClientStoreMaker(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.(mockStoredUser)
|
||||
|
||||
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.StoreLoader = mockStoreLoader{"joe": mockUser{Email: "john@john.com", Password: "lies"}}
|
||||
ab.ViewLoader = mockRenderLoader{}
|
||||
ab.SessionStoreMaker = newMockClientStoreMaker(mockClientStore{SessionKey: "joe"})
|
||||
ab.CookieStoreMaker = newMockClientStoreMaker(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.Context) error {
|
||||
return afterGetUser
|
||||
})
|
||||
if _, err := ab.CurrentUser(rec, req); err != afterGetUser {
|
||||
t.Error("Want:", afterGetUser, "Got:", err)
|
||||
}
|
||||
|
||||
ab.Callbacks.Before(EventGetUser, func(context.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.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.Skip("TODO(aarondl): Implement")
|
||||
/*
|
||||
|
Reference in New Issue
Block a user