2015-01-05 00:18:41 -08:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
2018-03-09 13:11:08 -08:00
|
|
|
"context"
|
2018-04-30 18:17:07 -07:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-01-05 00:18:41 -08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthBossInit(t *testing.T) {
|
2015-03-31 12:34:03 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab := New()
|
|
|
|
err := ab.Init()
|
2015-01-05 00:18:41 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:58:32 -07:00
|
|
|
func TestAuthbossUpdatePassword(t *testing.T) {
|
2018-03-09 13:11:08 -08:00
|
|
|
t.Parallel()
|
2015-03-16 22:58:32 -07:00
|
|
|
|
2018-03-09 13:11:08 -08:00
|
|
|
user := &mockUser{}
|
|
|
|
storer := newMockServerStorer()
|
2017-02-23 16:13:25 -08:00
|
|
|
|
2018-03-09 13:11:08 -08:00
|
|
|
ab := New()
|
|
|
|
ab.Config.Storage.Server = storer
|
2017-02-23 16:13:25 -08:00
|
|
|
|
2018-03-09 13:11:08 -08:00
|
|
|
if err := ab.UpdatePassword(context.Background(), user, "hello world"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-02-23 16:13:25 -08:00
|
|
|
|
2018-03-09 13:11:08 -08:00
|
|
|
if len(user.Password) == 0 {
|
|
|
|
t.Error("password was not updated")
|
|
|
|
}
|
2015-03-16 22:58:32 -07:00
|
|
|
}
|
2018-04-30 18:17:07 -07:00
|
|
|
|
|
|
|
func TestAuthbossMiddleware(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab := New()
|
|
|
|
ab.Core.Logger = mockLogger{}
|
|
|
|
|
|
|
|
mid := Middleware(ab)
|
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
w := ab.NewResponse(rec)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
hadUser := false
|
|
|
|
server := mid(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
called = true
|
|
|
|
hadUser = r.Context().Value(CTXKeyUser) != nil
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r, err = ab.LoadClientState(w, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
server.ServeHTTP(w, r)
|
|
|
|
if called || hadUser {
|
|
|
|
t.Error("should not be called or have a user when no session variables have been provided")
|
|
|
|
}
|
|
|
|
if rec.Code != http.StatusNotFound {
|
|
|
|
t.Error("want a not found code")
|
|
|
|
}
|
|
|
|
|
|
|
|
ab.Storage.SessionState = mockClientStateReadWriter{
|
|
|
|
state: mockClientState{SessionKey: "test@test.com"},
|
|
|
|
}
|
|
|
|
ab.Storage.Server = &mockServerStorer{
|
|
|
|
Users: map[string]*mockUser{
|
|
|
|
"test@test.com": &mockUser{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r = httptest.NewRequest("GET", "/", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
w = ab.NewResponse(rec)
|
|
|
|
|
|
|
|
r, err = ab.LoadClientState(w, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
server.ServeHTTP(w, r)
|
|
|
|
if !called {
|
|
|
|
t.Error("it should have been called")
|
|
|
|
}
|
|
|
|
if !hadUser {
|
|
|
|
t.Error("it should have had a user loaded")
|
|
|
|
}
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Error("want a not found code")
|
|
|
|
}
|
|
|
|
}
|