1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-07 13:41:55 +02:00

Get authboss tests passing.

This commit is contained in:
Aaron 2015-02-20 04:03:22 -08:00
parent 13a38828bd
commit 585f842a5c
11 changed files with 121 additions and 76 deletions

View File

@ -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
}

View File

@ -72,4 +72,6 @@ func (c *Callbacks) FireAfter(e Event, ctx *Context) (err error) {
return err
}
}
return nil
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}

55
internal/render/render.go Normal file
View File

@ -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)
}

View File

@ -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.

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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{}