2015-03-02 08:04:31 -08:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-03-31 12:34:03 -07:00
|
|
|
// These tests use the global variable nowTime so cannot be parallelized
|
|
|
|
|
2015-03-02 08:04:31 -08:00
|
|
|
func TestDudeIsExpired(t *testing.T) {
|
2015-03-31 12:34:03 -07:00
|
|
|
ab := New()
|
2015-03-02 08:04:31 -08:00
|
|
|
|
|
|
|
session := mockClientStore{SessionKey: "username"}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.refreshExpiry(session)
|
2015-03-02 08:04:31 -08:00
|
|
|
nowTime = func() time.Time {
|
2015-03-31 12:34:03 -07:00
|
|
|
return time.Now().UTC().Add(ab.ExpireAfter * 2)
|
2015-03-02 08:04:31 -08:00
|
|
|
}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
2015-03-02 08:04:31 -08:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _ := http.NewRequest("GET", "tra/la/la", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
called := false
|
|
|
|
|
2015-03-31 12:34:03 -07:00
|
|
|
m := ab.ExpireMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-03-02 08:04:31 -08:00
|
|
|
called = true
|
|
|
|
}))
|
|
|
|
|
|
|
|
m.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Error("Expected middleware to call handler")
|
|
|
|
}
|
|
|
|
|
|
|
|
if key, ok := session.Get(SessionKey); ok {
|
|
|
|
t.Error("Unexpcted session key:", key)
|
|
|
|
}
|
2015-03-02 22:09:32 -08:00
|
|
|
|
|
|
|
if key, ok := session.Get(SessionLastAction); ok {
|
|
|
|
t.Error("Unexpcted last action key:", key)
|
|
|
|
}
|
2015-03-02 08:04:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDudeIsNotExpired(t *testing.T) {
|
2015-03-31 12:34:03 -07:00
|
|
|
ab := New()
|
2015-03-02 08:04:31 -08:00
|
|
|
|
|
|
|
session := mockClientStore{SessionKey: "username"}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.refreshExpiry(session)
|
2015-03-02 08:04:31 -08:00
|
|
|
nowTime = func() time.Time {
|
2015-03-31 12:34:03 -07:00
|
|
|
return time.Now().UTC().Add(ab.ExpireAfter / 2)
|
2015-03-02 08:04:31 -08:00
|
|
|
}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
2015-03-02 08:04:31 -08:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _ := http.NewRequest("GET", "tra/la/la", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
called := false
|
|
|
|
|
2015-03-31 12:34:03 -07:00
|
|
|
m := ab.ExpireMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-03-02 08:04:31 -08:00
|
|
|
called = true
|
|
|
|
}))
|
|
|
|
|
|
|
|
m.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Error("Expected middleware to call handler")
|
|
|
|
}
|
|
|
|
|
|
|
|
if key, ok := session.Get(SessionKey); !ok {
|
|
|
|
t.Error("Expected session key:", key)
|
|
|
|
}
|
|
|
|
}
|