1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-03 13:21:22 +02:00

Add SaveUser for context.

- Add test coverage for context.go
This commit is contained in:
Aaron 2015-01-16 21:30:04 -08:00
parent a0bde30e3d
commit 1073b36cce
2 changed files with 78 additions and 0 deletions

View File

@ -92,3 +92,12 @@ func (c *Context) LoadUser(key string, storer Storer) error {
c.User = Unbind(intf) c.User = Unbind(intf)
return nil return nil
} }
// SaveUser saves the user Attributes.
func (c *Context) SaveUser(key string, storer Storer) error {
if c.User == nil {
return nil
}
return storer.Put(key, c.User)
}

View File

@ -6,6 +6,25 @@ import (
"testing" "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) { func TestContext_PutGet(t *testing.T) {
ctx := NewContext() ctx := NewContext()
@ -34,4 +53,54 @@ func TestContext_Request(t *testing.T) {
if post, ok := ctx.PostFormValue("post"); !ok || post[0] != "form" { if post, ok := ctx.PostFormValue("post"); !ok || post[0] != "form" {
t.Error("Postform value not getting recorded correctly.") t.Error("Postform value not getting recorded correctly.")
} }
if query, ok := ctx.FirstFormValue("query"); !ok || query != "string" {
t.Error("Form value not getting recorded correctly.")
}
if post, ok := ctx.FirstPostFormValue("post"); !ok || post != "form" {
t.Error("Postform value not getting recorded correctly.")
}
}
func TestContext_SaveUser(t *testing.T) {
ctx := NewContext()
storer := mockStorer{}
ctx.User = Attributes{"email": "hello@joe.com", "password": "mysticalhash"}
err := ctx.SaveUser("joe", storer)
if err != nil {
t.Error("Unexpected error:", err)
}
attr, ok := storer["joe"]
if !ok {
t.Error("Could not find joe!")
}
for k, v := range ctx.User {
if v != attr[k] {
t.Error(v, "not equal to", ctx.User[k])
}
}
}
func TestContext_LoadUser(t *testing.T) {
ctx := NewContext()
storer := mockStorer{
"joe": Attributes{"email": "hello@joe.com", "password": "mysticalhash"},
}
err := ctx.LoadUser("joe", storer)
if err != nil {
t.Error("Unexpected error:", err)
}
attr := storer["joe"]
for k, v := range attr {
if v != ctx.User[k] {
t.Error(v, "not equal to", ctx.User[k])
}
}
} }