github.com/volatiletech/authboss@v2.4.1+incompatible/module_test.go (about) 1 package authboss 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 ) 8 9 const ( 10 testModName = "testmodule" 11 ) 12 13 var ( 14 testMod = &testModule{} 15 ) 16 17 func init() { 18 RegisterModule(testModName, testMod) 19 } 20 21 type testModule struct{} 22 23 func (t *testModule) Init(a *Authboss) error { return nil } 24 25 func TestRegister(t *testing.T) { 26 t.Parallel() 27 28 // RegisterModule called by init() 29 if _, ok := registeredModules[testModName]; !ok { 30 t.Error("Expected module to be saved.") 31 } 32 } 33 34 func TestLoadedModules(t *testing.T) { 35 t.Parallel() 36 37 // RegisterModule called by init() 38 registered := RegisteredModules() 39 if len(registered) != 1 { 40 t.Error("Expected only a single module to be loaded.") 41 } else { 42 found := false 43 for _, name := range registered { 44 if name == testModName { 45 found = true 46 break 47 } 48 } 49 if !found { 50 t.Error("It should have found the module:", registered) 51 } 52 } 53 } 54 55 func TestIsLoaded(t *testing.T) { 56 t.Parallel() 57 58 ab := New() 59 if err := ab.Init(); err != nil { 60 t.Error(err) 61 } 62 63 if loaded := ab.LoadedModules(); len(loaded) == 0 || loaded[0] != testModName { 64 t.Error("Loaded modules wrong:", loaded) 65 } 66 } 67 68 func TestModuleLoadedMiddleware(t *testing.T) { 69 t.Parallel() 70 71 ab := New() 72 73 ab.loadedModules = map[string]Moduler{ 74 "recover": nil, 75 "auth": nil, 76 "oauth2": nil, 77 } 78 ab.Config.Modules.OAuth2Providers = map[string]OAuth2Provider{ 79 "google": {}, 80 } 81 82 var mods map[string]bool 83 server := ModuleListMiddleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 84 data := r.Context().Value(CTXKeyData).(HTMLData) 85 mods = data[DataModules].(map[string]bool) 86 })) 87 88 server.ServeHTTP(nil, httptest.NewRequest("GET", "/", nil)) 89 90 if len(mods) != 4 { 91 t.Error("want 4 modules, got:", len(mods)) 92 } 93 94 if _, ok := mods["auth"]; !ok { 95 t.Error("auth should be loaded") 96 } 97 if _, ok := mods["recover"]; !ok { 98 t.Error("recover should be loaded") 99 } 100 if _, ok := mods["oauth2"]; !ok { 101 t.Error("modules should include oauth2.google") 102 } 103 if _, ok := mods["oauth2.google"]; !ok { 104 t.Error("modules should include oauth2.google") 105 } 106 }