1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-26 05:27:33 +02:00
authboss/authboss_test.go

225 lines
5.3 KiB
Go
Raw Normal View History

package authboss
import (
2018-03-09 13:11:08 -08:00
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestAuthBossInit(t *testing.T) {
t.Parallel()
ab := New()
err := ab.Init()
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()
2018-03-09 13:11:08 -08:00
ab := New()
ab.Config.Storage.Server = storer
2018-03-09 13:11:08 -08:00
if err := ab.UpdatePassword(context.Background(), user, "hello world"); err != nil {
t.Error(err)
}
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-07-17 15:25:25 -07:00
type testRedirector struct {
Opts RedirectOptions
}
func (r *testRedirector) Redirect(w http.ResponseWriter, req *http.Request, ro RedirectOptions) error {
r.Opts = ro
if len(ro.RedirectPath) == 0 {
panic("no redirect path on redirect call")
}
http.Redirect(w, req, ro.RedirectPath, ro.Code)
return nil
}
func TestAuthbossMiddleware(t *testing.T) {
t.Parallel()
ab := New()
ab.Core.Logger = mockLogger{}
ab.Storage.Server = &mockServerStorer{
Users: map[string]*mockUser{
"test@test.com": &mockUser{},
},
}
2018-08-31 14:57:22 -07:00
setupMore := func(mountPathed, redirect, allowHalfAuth, force2fa bool) (*httptest.ResponseRecorder, bool, bool) {
2018-07-17 15:25:25 -07:00
r := httptest.NewRequest("GET", "/super/secret", nil)
rec := httptest.NewRecorder()
w := ab.NewResponse(rec)
2018-07-17 15:25:25 -07:00
var err error
r, err = ab.LoadClientState(w, r)
if err != nil {
t.Fatal(err)
}
2018-08-31 14:57:22 -07:00
var mid func(http.Handler) http.Handler
if !mountPathed {
mid = Middleware(ab, redirect, allowHalfAuth, force2fa)
} else {
mid = MountedMiddleware(ab, true, redirect, allowHalfAuth, force2fa)
}
2018-07-17 15:25:25 -07:00
var called, hadUser bool
server := mid(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
hadUser = r.Context().Value(CTXKeyUser) != nil
w.WriteHeader(http.StatusOK)
}))
server.ServeHTTP(w, r)
return rec, called, hadUser
}
2018-07-17 15:25:25 -07:00
t.Run("Accept", func(t *testing.T) {
ab.Storage.SessionState = mockClientStateReadWriter{
state: mockClientState{SessionKey: "test@test.com"},
}
2018-08-31 14:57:22 -07:00
_, called, hadUser := setupMore(false, false, false, false)
2018-07-17 15:25:25 -07:00
if !called {
t.Error("should have been called")
}
if !hadUser {
t.Error("should have had user")
}
})
t.Run("AcceptHalfAuth", func(t *testing.T) {
ab.Storage.SessionState = mockClientStateReadWriter{
state: mockClientState{SessionKey: "test@test.com", SessionHalfAuthKey: "true"},
}
2018-08-31 14:57:22 -07:00
_, called, hadUser := setupMore(false, false, false, false)
if !called {
t.Error("should have been called")
}
if !hadUser {
t.Error("should have had user")
}
})
t.Run("Accept2FA", func(t *testing.T) {
ab.Storage.SessionState = mockClientStateReadWriter{
state: mockClientState{SessionKey: "test@test.com", Session2FA: "sms"},
}
_, called, hadUser := setupMore(false, false, false, true)
2018-07-17 15:25:25 -07:00
if !called {
t.Error("should have been called")
}
if !hadUser {
t.Error("should have had user")
}
})
t.Run("Reject404", func(t *testing.T) {
ab.Storage.SessionState = mockClientStateReadWriter{}
2018-08-31 14:57:22 -07:00
rec, called, hadUser := setupMore(false, false, false, false)
2018-07-17 15:25:25 -07:00
if rec.Code != http.StatusNotFound {
t.Error("wrong code:", rec.Code)
}
if called {
t.Error("should not have been called")
}
if hadUser {
t.Error("should not have had user")
}
})
t.Run("RejectRedirect", func(t *testing.T) {
redir := &testRedirector{}
ab.Config.Core.Redirector = redir
ab.Storage.SessionState = mockClientStateReadWriter{}
2018-08-31 14:57:22 -07:00
_, called, hadUser := setupMore(false, true, false, false)
2018-07-17 15:25:25 -07:00
if redir.Opts.Code != http.StatusTemporaryRedirect {
t.Error("code was wrong:", redir.Opts.Code)
}
if redir.Opts.RedirectPath != "/auth/login?redir=%2Fsuper%2Fsecret" {
t.Error("redirect path was wrong:", redir.Opts.RedirectPath)
}
if called {
t.Error("should not have been called")
}
if hadUser {
t.Error("should not have had user")
}
})
2018-08-31 14:57:22 -07:00
t.Run("RejectMountpathedRedirect", func(t *testing.T) {
redir := &testRedirector{}
ab.Config.Core.Redirector = redir
ab.Storage.SessionState = mockClientStateReadWriter{}
_, called, hadUser := setupMore(true, true, false, false)
if redir.Opts.Code != http.StatusTemporaryRedirect {
t.Error("code was wrong:", redir.Opts.Code)
}
if redir.Opts.RedirectPath != "/auth/login?redir=%2Fauth%2Fsuper%2Fsecret" {
t.Error("redirect path was wrong:", redir.Opts.RedirectPath)
}
if called {
t.Error("should not have been called")
}
if hadUser {
t.Error("should not have had user")
}
})
2018-07-17 15:25:25 -07:00
t.Run("RejectHalfAuth", func(t *testing.T) {
ab.Storage.SessionState = mockClientStateReadWriter{
state: mockClientState{SessionKey: "test@test.com", SessionHalfAuthKey: "true"},
}
2018-08-31 14:57:22 -07:00
rec, called, hadUser := setupMore(false, false, true, false)
2018-07-17 15:25:25 -07:00
if rec.Code != http.StatusNotFound {
t.Error("wrong code:", rec.Code)
}
if called {
t.Error("should not have been called")
}
if hadUser {
t.Error("should not have had user")
}
})
2018-08-31 14:57:22 -07:00
t.Run("RejectNo2FA", func(t *testing.T) {
2018-07-17 15:25:25 -07:00
ab.Storage.SessionState = mockClientStateReadWriter{
state: mockClientState{SessionKey: "test@test.com"},
}
2018-08-31 14:57:22 -07:00
rec, called, hadUser := setupMore(false, false, true, true)
2018-07-17 15:25:25 -07:00
2018-08-31 14:57:22 -07:00
if rec.Code != http.StatusNotFound {
t.Error("wrong code:", rec.Code)
2018-07-17 15:25:25 -07:00
}
2018-08-31 14:57:22 -07:00
if called {
t.Error("should not have been called")
2018-07-17 15:25:25 -07:00
}
2018-08-31 14:57:22 -07:00
if hadUser {
t.Error("should not have had user")
2018-07-17 15:25:25 -07:00
}
2018-08-31 14:57:22 -07:00
})
}