mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-24 08:42:17 +02:00
Clean up mocks.
- Add CurrentUser method.
This commit is contained in:
parent
7841223f39
commit
89875f7b68
19
authboss.go
19
authboss.go
@ -10,6 +10,7 @@ package authboss // import "gopkg.in/authboss.v0"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -41,3 +42,21 @@ func Init(config *Config) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
sessions := cfg.SessionStoreMaker(w, r)
|
||||
key, ok := sessions.Get(SessionKey)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return cfg.Storer.Get(key, moduleAttrMeta)
|
||||
}
|
||||
|
||||
func CurrentUserP(w http.ResponseWriter, r *http.Request) interface{} {
|
||||
i, err := CurrentUser(w, r)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
@ -8,12 +8,6 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type clientStoreMock struct{}
|
||||
|
||||
func (c clientStoreMock) Get(_ string) (string, bool) { return "", false }
|
||||
func (c clientStoreMock) Put(_, _ string) {}
|
||||
func (c clientStoreMock) Del(_ string) {}
|
||||
|
||||
func TestMain(main *testing.M) {
|
||||
RegisterModule("testmodule", testMod)
|
||||
Init(NewConfig())
|
||||
@ -29,7 +23,7 @@ func TestAuthBossInit(t *testing.T) {
|
||||
t.Error("Expected error about a storer, got:", err)
|
||||
}
|
||||
|
||||
c.Storer = testStorer(0)
|
||||
c.Storer = mockStorer{}
|
||||
err = Init(c)
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
@ -41,9 +35,9 @@ func TestAuthBossInit(t *testing.T) {
|
||||
|
||||
func TestAuthBossRouter(t *testing.T) {
|
||||
c := NewConfig()
|
||||
c.Storer = testStorer(0)
|
||||
c.Storer = mockStorer{}
|
||||
c.CookieStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
||||
return clientStoreMock{}
|
||||
return mockClientStore{}
|
||||
}
|
||||
c.SessionStoreMaker = SessionStoreMaker(c.CookieStoreMaker)
|
||||
c.MountPath = "/candycanes"
|
||||
@ -62,3 +56,25 @@ func TestAuthBossRouter(t *testing.T) {
|
||||
t.Error("Expected a header to have been set.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthBossCurrentUser(t *testing.T) {
|
||||
c := NewConfig()
|
||||
c.Storer = mockStorer{"joe": Attributes{"email": "john@john.com", "password": "lies"}}
|
||||
c.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
||||
return mockClientStore{SessionKey: "joe"}
|
||||
}
|
||||
|
||||
if err := Init(c); err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "localhost", nil)
|
||||
|
||||
userStruct := CurrentUserP(rec, req)
|
||||
us := userStruct.(*mockUser)
|
||||
|
||||
if us.Email != "john@john.com" || us.Password != "lies" {
|
||||
t.Error("Wrong user found!")
|
||||
}
|
||||
}
|
||||
|
@ -6,25 +6,6 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type mockStorer map[string]Attributes
|
||||
|
||||
func (m mockStorer) Create(key string, attr Attributes) error {
|
||||
m[key] = attr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m mockStorer) Put(key string, attr Attributes) error {
|
||||
m[key] = attr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m mockStorer) Get(key string, attrMeta AttributeMeta) (result interface{}, err error) {
|
||||
return &struct {
|
||||
Email string
|
||||
Password string
|
||||
}{m["joe"]["email"].(string), m["joe"]["password"].(string)}, nil
|
||||
}
|
||||
|
||||
func TestContext_PutGet(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
|
||||
|
33
mocks_test.go
Normal file
33
mocks_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package authboss
|
||||
|
||||
type mockUser struct {
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
|
||||
type mockStorer map[string]Attributes
|
||||
|
||||
func (m mockStorer) Create(key string, attr Attributes) error {
|
||||
m[key] = attr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m mockStorer) Put(key string, attr Attributes) error {
|
||||
m[key] = attr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m mockStorer) Get(key string, attrMeta AttributeMeta) (result interface{}, err error) {
|
||||
return &mockUser{
|
||||
m[key]["email"].(string), m[key]["password"].(string),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type mockClientStore map[string]string
|
||||
|
||||
func (m mockClientStore) Get(key string) (string, bool) {
|
||||
v, ok := m[key]
|
||||
return v, ok
|
||||
}
|
||||
func (m mockClientStore) Put(key, val string) { m[key] = val }
|
||||
func (m mockClientStore) Del(key string) { delete(m, key) }
|
@ -6,12 +6,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type testStorer int
|
||||
|
||||
func (t testStorer) Create(key string, attr Attributes) error { return nil }
|
||||
func (t testStorer) Put(key string, attr Attributes) error { return nil }
|
||||
func (t testStorer) Get(key string, attrMeta AttributeMeta) (interface{}, error) { return nil, nil }
|
||||
|
||||
func TestAttributes_Names(t *testing.T) {
|
||||
attr := Attributes{
|
||||
"integer": 5,
|
||||
|
Loading…
Reference in New Issue
Block a user