1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-24 08:42:17 +02:00
authboss/callbacks_test.go

192 lines
4.1 KiB
Go
Raw Normal View History

package authboss
import (
2015-02-21 09:01:45 +02:00
"bytes"
"errors"
2015-02-21 09:01:45 +02:00
"strings"
"testing"
)
func TestCallbacks(t *testing.T) {
afterCalled := false
beforeCalled := false
c := NewCallbacks()
2015-02-22 07:03:03 +02:00
c.Before(EventRegister, func(ctx *Context) (Interrupt, error) {
beforeCalled = true
2015-02-22 07:03:03 +02:00
return InterruptNone, nil
})
2015-02-20 14:03:22 +02:00
c.After(EventRegister, func(ctx *Context) error {
afterCalled = true
2015-02-20 14:03:22 +02:00
return nil
})
if beforeCalled || afterCalled {
t.Error("Neither should be called.")
}
2015-02-22 07:03:03 +02:00
interrupt, err := c.FireBefore(EventRegister, NewContext())
if err != nil {
t.Error("Unexpected error:", err)
}
2015-02-22 07:03:03 +02:00
if interrupt != InterruptNone {
2015-02-20 14:03:22 +02:00
t.Error("It should not have been stopped.")
}
if !beforeCalled {
t.Error("Expected before to have been called.")
}
if afterCalled {
t.Error("Expected after not to be called.")
}
c.FireAfter(EventRegister, NewContext())
if !afterCalled {
t.Error("Expected after to be called.")
}
}
func TestCallbacksInterrupt(t *testing.T) {
before1 := false
before2 := false
c := NewCallbacks()
2015-02-22 07:03:03 +02:00
c.Before(EventRegister, func(ctx *Context) (Interrupt, error) {
2015-02-20 14:03:22 +02:00
before1 = true
2015-02-22 07:03:03 +02:00
return InterruptAccountLocked, nil
2015-02-20 14:03:22 +02:00
})
2015-02-22 07:03:03 +02:00
c.Before(EventRegister, func(ctx *Context) (Interrupt, error) {
2015-02-20 14:03:22 +02:00
before2 = true
2015-02-22 07:03:03 +02:00
return InterruptNone, nil
2015-02-20 14:03:22 +02:00
})
2015-02-22 07:03:03 +02:00
interrupt, err := c.FireBefore(EventRegister, NewContext())
2015-02-20 14:03:22 +02:00
if err != nil {
t.Error(err)
}
2015-02-22 07:03:03 +02:00
if interrupt != InterruptAccountLocked {
t.Error("The interrupt signal was not account locked:", interrupt)
2015-02-20 14:03:22 +02:00
}
if !before1 {
t.Error("Before1 should have been called.")
}
if before2 {
t.Error("Before2 should not have been called.")
}
}
2015-02-21 09:01:45 +02:00
func TestCallbacksBeforeErrors(t *testing.T) {
log := &bytes.Buffer{}
Cfg = &Config{
LogWriter: log,
}
2015-02-20 14:03:22 +02:00
before1 := false
before2 := false
c := NewCallbacks()
2015-02-21 09:01:45 +02:00
errValue := errors.New("Problem occured")
2015-02-22 07:03:03 +02:00
c.Before(EventRegister, func(ctx *Context) (Interrupt, error) {
before1 = true
2015-02-22 07:03:03 +02:00
return InterruptNone, errValue
})
2015-02-22 07:03:03 +02:00
c.Before(EventRegister, func(ctx *Context) (Interrupt, error) {
before2 = true
2015-02-22 07:03:03 +02:00
return InterruptNone, nil
})
2015-02-22 07:03:03 +02:00
interrupt, err := c.FireBefore(EventRegister, NewContext())
if err != errValue {
t.Error("Expected an error to come back.")
}
2015-02-22 07:03:03 +02:00
if interrupt != InterruptNone {
2015-02-21 09:10:18 +02:00
t.Error("It should not have been stopped.")
2015-02-20 14:03:22 +02:00
}
if !before1 {
t.Error("Before1 should have been called.")
}
if before2 {
t.Error("Before2 should not have been called.")
}
2015-02-21 09:01:45 +02:00
if estr := log.String(); !strings.Contains(estr, errValue.Error()) {
t.Error("Error string wrong:", estr)
}
}
func TestCallbacksAfterErrors(t *testing.T) {
log := &bytes.Buffer{}
Cfg = &Config{
LogWriter: log,
}
after1 := false
after2 := false
c := NewCallbacks()
errValue := errors.New("Problem occured")
c.After(EventRegister, func(ctx *Context) error {
after1 = true
return errValue
})
c.After(EventRegister, func(ctx *Context) error {
after2 = true
return nil
})
err := c.FireAfter(EventRegister, NewContext())
if err != errValue {
t.Error("Expected an error to come back.")
}
if !after1 {
t.Error("After1 should have been called.")
}
if after2 {
t.Error("After2 should not have been called.")
}
if estr := log.String(); !strings.Contains(estr, errValue.Error()) {
t.Error("Error string wrong:", estr)
}
}
2015-02-22 07:03:03 +02:00
func TestEventString(t *testing.T) {
tests := []struct {
ev Event
str string
}{
{EventRegister, "EventRegister"},
{EventAuth, "EventAuth"},
{EventAuthFail, "EventAuthFail"},
{EventRecoverStart, "EventRecoverStart"},
{EventRecoverEnd, "EventRecoverEnd"},
{EventGet, "EventGet"},
}
for i, test := range tests {
if got := test.ev.String(); got != test.str {
t.Errorf("%d) Wrong string for Event(%d) expected: %v got: %s", i, test.ev, test.str, got)
}
}
}
func TestInterruptString(t *testing.T) {
tests := []struct {
in Interrupt
str string
}{
{InterruptNone, "InterruptNone"},
{InterruptAccountLocked, "InterruptAccountLocked"},
{InterruptAccountNotConfirmed, "InterruptAccountNotConfirmed"},
}
for i, test := range tests {
if got := test.in.String(); got != test.str {
t.Errorf("%d) Wrong string for Event(%d) expected: %v got: %s", i, test.in, test.str, got)
}
}
}