-
Notifications
You must be signed in to change notification settings - Fork 208
/
module_test.go
106 lines (86 loc) · 2.04 KB
/
module_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
package authboss
import (
"net/http"
"net/http/httptest"
"testing"
)
const (
testModName = "testmodule"
)
var (
testMod = &testModule{}
)
func init() {
RegisterModule(testModName, testMod)
}
type testModule struct{}
func (t *testModule) Init(a *Authboss) error { return nil }
func TestRegister(t *testing.T) {
t.Parallel()
// RegisterModule called by init()
if _, ok := registeredModules[testModName]; !ok {
t.Error("Expected module to be saved.")
}
}
func TestLoadedModules(t *testing.T) {
t.Parallel()
// RegisterModule called by init()
registered := RegisteredModules()
if len(registered) != 1 {
t.Error("Expected only a single module to be loaded.")
} else {
found := false
for _, name := range registered {
if name == testModName {
found = true
break
}
}
if !found {
t.Error("It should have found the module:", registered)
}
}
}
func TestIsLoaded(t *testing.T) {
t.Parallel()
ab := New()
if err := ab.Init(); err != nil {
t.Error(err)
}
if loaded := ab.LoadedModules(); len(loaded) == 0 || loaded[0] != testModName {
t.Error("Loaded modules wrong:", loaded)
}
}
func TestModuleLoadedMiddleware(t *testing.T) {
t.Parallel()
ab := New()
ab.loadedModules = map[string]Moduler{
"recover": nil,
"auth": nil,
"oauth2": nil,
}
ab.Config.Modules.OAuth2Providers = map[string]OAuth2Provider{
"google": {},
}
var mods map[string]bool
server := ModuleListMiddleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := r.Context().Value(CTXKeyData).(HTMLData)
mods = data[DataModules].(map[string]bool)
}))
server.ServeHTTP(nil, httptest.NewRequest("GET", "/", nil))
if len(mods) != 4 {
t.Error("want 4 modules, got:", len(mods))
}
if _, ok := mods["auth"]; !ok {
t.Error("auth should be loaded")
}
if _, ok := mods["recover"]; !ok {
t.Error("recover should be loaded")
}
if _, ok := mods["oauth2"]; !ok {
t.Error("modules should include oauth2.google")
}
if _, ok := mods["oauth2.google"]; !ok {
t.Error("modules should include oauth2.google")
}
}