1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-24 08:42:17 +02:00
authboss/context_test.go
Aaron L c89ca29827 Remove context and errors from get/set
I have a feeling that I wrote all this fanciness in when the
user was still able to fetch himself from the database. But since
that's been dropped I don't think any of this stuff is necessary.

In terms of setting without an error, we should do validation before
an attempt to save, not every time we set a field. This will just end
up being much nicer error handling, and the database is going to do
it's own validation and we can handle that error in the same way.
2018-02-16 11:27:22 -08:00

246 lines
4.5 KiB
Go

package authboss
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func loadClientStateP(ab *Authboss, w http.ResponseWriter, r *http.Request) *http.Request {
r, err := ab.LoadClientState(w, r)
if err != nil {
panic(err)
}
return r
}
func testSetupContext() (*Authboss, *http.Request) {
ab := New()
ab.Storage.SessionState = newMockClientStateRW(SessionKey, "george-pid")
ab.Storage.Server = mockServerStorer{
"george-pid": mockUser{Email: "george-pid", Password: "unreadable"},
}
r := loadClientStateP(ab, nil, httptest.NewRequest("GET", "/", nil))
return ab, r
}
func testSetupContextCached() (*Authboss, mockUser, *http.Request) {
ab := New()
wantUser := mockUser{Email: "george-pid", Password: "unreadable"}
req := httptest.NewRequest("GET", "/", nil)
ctx := context.WithValue(req.Context(), CTXKeyPID, "george-pid")
ctx = context.WithValue(ctx, CTXKeyUser, wantUser)
req = req.WithContext(ctx)
return ab, wantUser, req
}
func testSetupContextPanic() *Authboss {
ab := New()
ab.Storage.SessionState = newMockClientStateRW(SessionKey, "george-pid")
ab.Storage.Server = mockServerStorer{}
return ab
}
func TestCurrentUserID(t *testing.T) {
t.Parallel()
ab, r := testSetupContext()
id, err := ab.CurrentUserID(nil, r)
if err != nil {
t.Error(err)
}
if id != "george-pid" {
t.Error("got:", id)
}
}
func TestCurrentUserIDContext(t *testing.T) {
t.Parallel()
ab, r := testSetupContext()
id, err := ab.CurrentUserID(nil, r)
if err != nil {
t.Error(err)
}
if id != "george-pid" {
t.Error("got:", id)
}
}
func TestCurrentUserIDP(t *testing.T) {
t.Parallel()
ab := testSetupContextPanic()
// Overwrite the setup functions state storer
ab.Storage.SessionState = newMockClientStateRW()
defer func() {
if recover().(error) != ErrUserNotFound {
t.Failed()
}
}()
_ = ab.CurrentUserIDP(nil, httptest.NewRequest("GET", "/", nil))
}
func TestCurrentUser(t *testing.T) {
t.Parallel()
ab, r := testSetupContext()
user, err := ab.CurrentUser(nil, r)
if err != nil {
t.Error(err)
}
if got := user.GetPID(); got != "george-pid" {
t.Error("got:", got)
}
}
func TestCurrentUserContext(t *testing.T) {
t.Parallel()
ab, _, r := testSetupContextCached()
user, err := ab.CurrentUser(nil, r)
if err != nil {
t.Error(err)
}
if got := user.GetPID(); got != "george-pid" {
t.Error("got:", got)
}
}
func TestCurrentUserP(t *testing.T) {
t.Parallel()
ab := testSetupContextPanic()
defer func() {
if recover().(error) != ErrUserNotFound {
t.Failed()
}
}()
_ = ab.CurrentUserP(nil, httptest.NewRequest("GET", "/", nil))
}
func TestLoadCurrentUserID(t *testing.T) {
t.Parallel()
ab, r := testSetupContext()
id, err := ab.LoadCurrentUserID(nil, &r)
if err != nil {
t.Error(err)
}
if id != "george-pid" {
t.Error("got:", id)
}
if r.Context().Value(CTXKeyPID).(string) != "george-pid" {
t.Error("context was not updated in local request")
}
}
func TestLoadCurrentUserIDContext(t *testing.T) {
t.Parallel()
ab, _, r := testSetupContextCached()
pid, err := ab.LoadCurrentUserID(nil, &r)
if err != nil {
t.Error(err)
}
if pid != "george-pid" {
t.Error("got:", pid)
}
}
func TestLoadCurrentUserIDP(t *testing.T) {
t.Parallel()
ab := testSetupContextPanic()
defer func() {
if recover().(error) != ErrUserNotFound {
t.Failed()
}
}()
r := httptest.NewRequest("GET", "/", nil)
_ = ab.LoadCurrentUserIDP(nil, &r)
}
func TestLoadCurrentUser(t *testing.T) {
t.Parallel()
ab, r := testSetupContext()
user, err := ab.LoadCurrentUser(nil, &r)
if err != nil {
t.Error(err)
}
if got := user.GetPID(); got != "george-pid" {
t.Error("got:", got)
}
want := user.(mockUser)
got := r.Context().Value(CTXKeyUser).(mockUser)
if got != want {
t.Errorf("users mismatched:\nwant: %#v\ngot: %#v", want, got)
}
}
func TestLoadCurrentUserContext(t *testing.T) {
t.Parallel()
ab, wantUser, r := testSetupContextCached()
user, err := ab.LoadCurrentUser(nil, &r)
if err != nil {
t.Error(err)
}
got := user.(mockUser)
if got != wantUser {
t.Errorf("users mismatched:\nwant: %#v\ngot: %#v", wantUser, got)
}
}
func TestLoadCurrentUserP(t *testing.T) {
t.Parallel()
ab := testSetupContextPanic()
defer func() {
if recover().(error) != ErrUserNotFound {
t.Failed()
}
}()
r := httptest.NewRequest("GET", "/", nil)
_ = ab.LoadCurrentUserP(nil, &r)
}
func TestCTXKeyString(t *testing.T) {
t.Parallel()
if got := CTXKeyPID.String(); got != "authboss ctx key pid" {
t.Error(got)
}
}