-
Notifications
You must be signed in to change notification settings - Fork 208
/
events_test.go
150 lines (125 loc) · 3.29 KB
/
events_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package authboss
import (
"net/http"
"testing"
"github.com/friendsofgo/errors"
)
func TestEvents(t *testing.T) {
t.Parallel()
ab := New()
afterCalled := false
beforeCalled := false
ab.Events.Before(EventRegister, func(http.ResponseWriter, *http.Request, bool) (bool, error) {
beforeCalled = true
return false, nil
})
ab.Events.After(EventRegister, func(http.ResponseWriter, *http.Request, bool) (bool, error) {
afterCalled = true
return false, nil
})
if beforeCalled || afterCalled {
t.Error("Neither should be called.")
}
handled, err := ab.Events.FireBefore(EventRegister, nil, nil)
if err != nil {
t.Error("Unexpected error:", err)
}
if handled {
t.Error("It should not have been handled.")
}
if !beforeCalled {
t.Error("Expected before to have been called.")
}
if afterCalled {
t.Error("Expected after not to be called.")
}
ab.Events.FireAfter(EventRegister, nil, nil)
if !afterCalled {
t.Error("Expected after to be called.")
}
}
func TestEventsHandled(t *testing.T) {
t.Parallel()
ab := New()
firstCalled := false
secondCalled := false
firstHandled := false
secondHandled := false
ab.Events.Before(EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
firstCalled = true
firstHandled = handled
return true, nil
})
ab.Events.Before(EventRegister, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
secondCalled = true
secondHandled = handled
return false, nil
})
handled, err := ab.Events.FireBefore(EventRegister, nil, nil)
if err != nil {
t.Error("Unexpected error:", err)
}
if !handled {
t.Error("it should have been handled")
}
if !firstCalled {
t.Error("expected first to have been called")
}
if !secondCalled {
t.Error("expected second to have been called")
}
if firstHandled {
t.Error("first should not see the event as being handled")
}
if !secondHandled {
t.Error("second should see the event as being handled")
}
}
func TestEventsErrors(t *testing.T) {
t.Parallel()
ab := New()
firstCalled := false
secondCalled := false
expect := errors.New("error")
ab.Events.Before(EventRegister, func(http.ResponseWriter, *http.Request, bool) (bool, error) {
firstCalled = true
return false, expect
})
ab.Events.Before(EventRegister, func(http.ResponseWriter, *http.Request, bool) (bool, error) {
secondCalled = true
return false, nil
})
_, err := ab.Events.FireBefore(EventRegister, nil, nil)
if err != expect {
t.Error("got the wrong error back:", err)
}
if !firstCalled {
t.Error("expected first to have been called")
}
if secondCalled {
t.Error("expected second to not have been called")
}
}
func TestEventString(t *testing.T) {
t.Parallel()
tests := []struct {
ev Event
str string
}{
{EventRegister, "EventRegister"},
{EventAuth, "EventAuth"},
{EventOAuth2, "EventOAuth2"},
{EventAuthFail, "EventAuthFail"},
{EventOAuth2Fail, "EventOAuth2Fail"},
{EventRecoverStart, "EventRecoverStart"},
{EventRecoverEnd, "EventRecoverEnd"},
{EventGetUser, "EventGetUser"},
{EventGetUserSession, "EventGetUserSession"},
{EventPasswordReset, "EventPasswordReset"},
}
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)
}
}
}