From 585f842a5c3ee53e1f2f063255662f3af826c91a Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 20 Feb 2015 04:03:22 -0800 Subject: [PATCH] Get authboss tests passing. --- authboss.go | 2 +- callbacks.go | 2 ++ callbacks_test.go | 63 +++++++++++++++++++++++++++++++-------- context_test.go | 9 ------ errors.go | 4 +-- internal/render/render.go | 55 ++++++++++++++++++++++++++++++++++ internal/views/views.go | 46 ---------------------------- module_test.go | 3 +- router.go | 2 +- storer.go | 6 ++-- views.go | 5 ++++ 11 files changed, 121 insertions(+), 76 deletions(-) create mode 100644 internal/render/render.go diff --git a/authboss.go b/authboss.go index e7b988f..27d3c6d 100644 --- a/authboss.go +++ b/authboss.go @@ -42,7 +42,7 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) { return nil, err } - err = Cfg.Callbacks.FireBefore(EventGet, ctx) + _, err = Cfg.Callbacks.FireBefore(EventGet, ctx) if err != nil { return nil, err } diff --git a/callbacks.go b/callbacks.go index 88bb3c6..3deb4d3 100644 --- a/callbacks.go +++ b/callbacks.go @@ -72,4 +72,6 @@ func (c *Callbacks) FireAfter(e Event, ctx *Context) (err error) { return err } } + + return nil } diff --git a/callbacks_test.go b/callbacks_test.go index 2a87d39..19164be 100644 --- a/callbacks_test.go +++ b/callbacks_test.go @@ -10,22 +10,26 @@ func TestCallbacks(t *testing.T) { beforeCalled := false c := NewCallbacks() - c.Before(EventRegister, func(ctx *Context) error { + c.Before(EventRegister, func(ctx *Context) (bool, error) { beforeCalled = true - return nil + return false, nil }) - c.After(EventRegister, func(ctx *Context) { + c.After(EventRegister, func(ctx *Context) error { afterCalled = true + return nil }) if beforeCalled || afterCalled { t.Error("Neither should be called.") } - err := c.FireBefore(EventRegister, NewContext()) + stopped, err := c.FireBefore(EventRegister, NewContext()) if err != nil { t.Error("Unexpected error:", err) } + if stopped { + t.Error("It should not have been stopped.") + } if !beforeCalled { t.Error("Expected before to have been called.") @@ -45,20 +49,53 @@ func TestCallbacksInterrupt(t *testing.T) { before2 := false c := NewCallbacks() - errValue := errors.New("Problem occured.") - - c.Before(EventRegister, func(ctx *Context) error { + c.Before(EventRegister, func(ctx *Context) (bool, error) { before1 = true - return errValue + return true, nil }) - c.Before(EventRegister, func(ctx *Context) error { + c.Before(EventRegister, func(ctx *Context) (bool, error) { before2 = true - return nil + return false, nil }) - err := c.FireBefore(EventRegister, NewContext()) - if err != errValue { - t.Error("Expected an error to come back.") + stopped, err := c.FireBefore(EventRegister, NewContext()) + if err != nil { + t.Error(err) + } + if !stopped { + t.Error("It was not stopped.") + } + + if !before1 { + t.Error("Before1 should have been called.") + } + if before2 { + t.Error("Before2 should not have been called.") + } +} + +func TestCallbacksErrors(t *testing.T) { + before1 := false + before2 := false + c := NewCallbacks() + + errValue := errors.New("Problem occured.") + + c.Before(EventRegister, func(ctx *Context) (bool, error) { + before1 = true + return false, errValue + }) + c.Before(EventRegister, func(ctx *Context) (bool, error) { + before2 = true + return false, nil + }) + + stopped, err := c.FireBefore(EventRegister, NewContext()) + if err != errValue { + t.Error("Expected an error to come back.") + } + if stopped { + t.Error("It should not have been stopped.") } if !before1 { diff --git a/context_test.go b/context_test.go index 5796b9f..5f14267 100644 --- a/context_test.go +++ b/context_test.go @@ -7,15 +7,6 @@ import ( "time" ) -func TestContext_PutGet(t *testing.T) { - ctx := NewContext() - - ctx.Put("key", "value") - if v, has := ctx.Get("key"); !has || v.(string) != "value" { - t.Error("Not retrieving key values correctly.") - } -} - func TestContext_Request(t *testing.T) { req, err := http.NewRequest("POST", "http://localhost?query=string", bytes.NewBufferString("post=form")) if err != nil { diff --git a/errors.go b/errors.go index 779fdfb..d797e10 100644 --- a/errors.go +++ b/errors.go @@ -10,7 +10,7 @@ type AttributeErr struct { GotKind string } -func MakeAttributeErr(name, kind DataType, val interface{}) AttributeErr { +func MakeAttributeErr(name string, kind DataType, val interface{}) AttributeErr { return AttributeErr{ Name: name, WantKind: kind, @@ -19,7 +19,7 @@ func MakeAttributeErr(name, kind DataType, val interface{}) AttributeErr { } func (a AttributeErr) Error() string { - if len(a.KindIssue) == 0 { + if len(a.GotKind) == 0 { return fmt.Sprintf("Failed to retrieve database attribute: %s", a.Name) } diff --git a/internal/render/render.go b/internal/render/render.go new file mode 100644 index 0000000..d7dc784 --- /dev/null +++ b/internal/render/render.go @@ -0,0 +1,55 @@ +package render + +import ( + "bytes" + "io" + "net/http" + + "gopkg.in/authboss.v0" + "gopkg.in/authboss.v0/internal/views" +) + +// View renders a view with xsrf and flash attributes. +func View(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, t views.Templates, name string, data authboss.HTMLData) error { + tpl, ok := t[name] + if !ok { + return authboss.RenderErr{tpl.Name(), data, ErrTemplateNotFound} + } + + data.Merge("xsrfName", authboss.Cfg.XSRFName, "xsrfToken", authboss.Cfg.XSRFMaker(w, r)) + + if flash, ok := ctx.CookieStorer.Get(authboss.FlashSuccessKey); ok { + ctx.CookieStorer.Del(authboss.FlashSuccessKey) + data.Merge(authboss.FlashSuccessKey, flash) + } + if flash, ok := ctx.CookieStorer.Get(authboss.FlashErrorKey); ok { + ctx.CookieStorer.Del(authboss.FlashErrorKey) + data.Merge(authboss.FlashErrorKey, flash) + } + + if authboss.Cfg.LayoutDataMaker != nil { + data.Merge(authboss.Cfg.LayoutDataMaker(w, r)) + } + + buffer = &bytes.Buffer{} + err = tpl.ExecuteTemplate(buffer, tpl.Name(), data) + if err != nil { + return authboss.RenderErr{tpl.Name(), data, err} + } + + err = io.Copy(w, buffer) + if err != nil { + return authboss.RenderErr{tpl.Name(), data, err} + } +} + +// Redirect sets any flash messages given and redirects the user. +func Redirect(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, path, flashSuccess, flashError string) { + if len(flashSuccess) > 0 { + ctx.CookieStorer.Put(authboss.FlashSuccessKey, flashSuccess) + } + if len(flashError) > 0 { + ctx.CookieStorer.Put(authboss.FlashErrorKey, flashError) + } + http.Redirect(w, r, path, http.StatusTemporaryRedirect) +} diff --git a/internal/views/views.go b/internal/views/views.go index b869f85..285d59d 100644 --- a/internal/views/views.go +++ b/internal/views/views.go @@ -6,16 +6,11 @@ package views //go:generate go-bindata -pkg=views -prefix=templates templates import ( - "bytes" "errors" "html/template" - "io" "io/ioutil" - "net/http" "os" "path/filepath" - - "gopkg.in/authboss.v0" ) var ( @@ -26,47 +21,6 @@ var ( // Templates is a map depicting the forms a template needs wrapped within the specified layout type Templates map[string]*template.Template -// Render renders a view with xsrf and flash attributes. -func (t Templates) Render(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, name string, data authboss.HTMLData) error { - tpl, ok := t[name] - if !ok { - return authboss.RenderErr{tpl.Name(), data, ErrTemplateNotFound} - } - - data.Merge("xsrfName", authboss.Cfg.XSRFName, "xsrfToken", authboss.Cfg.XSRFMaker(w, r)) - - if flash, ok := ctx.CookieStorer.Get(authboss.FlashSuccessKey); ok { - ctx.CookieStorer.Del(authboss.FlashSuccessKey) - data.Merge(authboss.FlashSuccessKey, flash) - } - if flash, ok := ctx.CookieStorer.Get(authboss.FlashErrorKey); ok { - ctx.CookieStorer.Del(authboss.FlashErrorKey) - data.Merge(authboss.FlashErrorKey, flash) - } - - buffer = &bytes.Buffer{} - err = tpl.ExecuteTemplate(buffer, tpl.Name(), data) - if err != nil { - return authboss.RenderErr{tpl.Name(), data, err} - } - - err = io.Copy(w, buffer) - if err != nil { - return authboss.RenderErr{tpl.Name(), data, err} - } -} - -// FlashRedirect sets any flash messages given and redirects the user. -func FlashRedirect(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, path, flashSuccess, flashError string) { - if len(flashSuccess) > 0 { - ctx.CookieStorer.Put(authboss.FlashSuccessKey, flashSuccess) - } - if len(flashError) > 0 { - ctx.CookieStorer.Put(authboss.FlashErrorKey, flashError) - } - http.Redirect(w, r, path, http.StatusTemporaryRedirect) -} - // Get parses all specified files located in path. Each template is wrapped // in a unique clone of layout. All templates are expecting {{authboss}} handlebars // for parsing. diff --git a/module_test.go b/module_test.go index a391a46..0138285 100644 --- a/module_test.go +++ b/module_test.go @@ -18,8 +18,9 @@ var testMod = &testModule{ }, } -func testHandler(ctx *Context, w http.ResponseWriter, r *http.Request) { +func testHandler(ctx *Context, w http.ResponseWriter, r *http.Request) error { w.Header().Set("testhandler", "test") + return nil } func (t *testModule) Initialize() error { diff --git a/router.go b/router.go index 07115f1..83f73f51 100644 --- a/router.go +++ b/router.go @@ -51,7 +51,7 @@ func (c contextRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) case ClientDataErr: w.WriteHeader(http.StatusBadRequest) - case RedirectErr: + case ErrAndRedirect: if len(e.FlashSuccess) > 0 { ctx.CookieStorer.Put(FlashSuccessKey, e.FlashSuccess) } diff --git a/storer.go b/storer.go index ee65f5e..f08274d 100644 --- a/storer.go +++ b/storer.go @@ -194,7 +194,7 @@ func (a Attributes) StringErr(key string) (val string, err error) { func (a Attributes) IntErr(key string) (val int, err error) { inter, ok := a[key] if !ok { - return "", AttributeErr{Name: key} + return val, AttributeErr{Name: key} } val, ok = inter.(int) if !ok { @@ -207,9 +207,9 @@ func (a Attributes) IntErr(key string) (val int, err error) { func (a Attributes) BoolErr(key string) (val bool, err error) { inter, ok := a[key] if !ok { - return "", AttributeErr{Name: key} + return val, AttributeErr{Name: key} } - val, ok = inter.(int) + val, ok = inter.(bool) if !ok { return val, MakeAttributeErr(key, Integer, inter) } diff --git a/views.go b/views.go index c38b4c2..a7634b5 100644 --- a/views.go +++ b/views.go @@ -1,5 +1,10 @@ package authboss +import "net/http" + +// ViewDataMaker asks for an HTMLData object to assist with rendering. +type ViewDataMaker func(http.ResponseWriter, *http.Request) HTMLData + // HTMLData is used to render templates with. type HTMLData map[string]interface{}