1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-24 05:17:10 +02:00
authboss/expire_test.go

77 lines
1.6 KiB
Go
Raw Normal View History

2015-03-02 08:04:31 -08:00
package authboss
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// These tests use the global variable nowTime so cannot be parallelized
2015-03-02 08:04:31 -08:00
func TestDudeIsExpired(t *testing.T) {
ab := New()
2015-03-02 08:04:31 -08:00
session := mockClientStore{SessionKey: "username"}
ab.refreshExpiry(session)
2015-03-02 08:04:31 -08:00
nowTime = func() time.Time {
return time.Now().UTC().Add(ab.ExpireAfter * 2)
2015-03-02 08:04:31 -08: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
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)
}
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) {
ab := New()
2015-03-02 08:04:31 -08:00
session := mockClientStore{SessionKey: "username"}
ab.refreshExpiry(session)
2015-03-02 08:04:31 -08:00
nowTime = func() time.Time {
return time.Now().UTC().Add(ab.ExpireAfter / 2)
2015-03-02 08:04:31 -08: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
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)
}
}