mirror of
https://github.com/volatiletech/authboss.git
synced 2025-01-12 04:23:16 +02:00
a7a67981ce
- Fixed the qq tests - Removed all module singletons
131 lines
2.8 KiB
Go
131 lines
2.8 KiB
Go
package remember
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"gopkg.in/authboss.v0"
|
|
"gopkg.in/authboss.v0/internal/mocks"
|
|
)
|
|
|
|
func TestInitialize(t *testing.T) {
|
|
authboss.NewConfig()
|
|
|
|
r := &Remember{}
|
|
err := r.Initialize()
|
|
if err == nil {
|
|
t.Error("Expected error about token storers.")
|
|
}
|
|
|
|
authboss.Cfg.Storer = mocks.MockFailStorer{}
|
|
err = r.Initialize()
|
|
if err == nil {
|
|
t.Error("Expected error about token storers.")
|
|
}
|
|
|
|
authboss.Cfg.Storer = mocks.NewMockStorer()
|
|
err = r.Initialize()
|
|
if err != nil {
|
|
t.Error("Unexpected error:", err)
|
|
}
|
|
}
|
|
|
|
func TestAfterAuth(t *testing.T) {
|
|
r := Remember{}
|
|
authboss.NewConfig()
|
|
storer := mocks.NewMockStorer()
|
|
authboss.Cfg.Storer = storer
|
|
|
|
cookies := mocks.NewMockClientStorer()
|
|
session := mocks.NewMockClientStorer()
|
|
|
|
req, err := http.NewRequest("POST", "http://localhost", bytes.NewBufferString("rm=true"))
|
|
if err != nil {
|
|
t.Error("Unexpected Error:", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
ctx, err := authboss.ContextFromRequest(req)
|
|
if err != nil {
|
|
t.Error("Unexpected error:", err)
|
|
}
|
|
|
|
ctx.SessionStorer = session
|
|
ctx.CookieStorer = cookies
|
|
ctx.User = authboss.Attributes{authboss.Cfg.PrimaryID: "test@email.com"}
|
|
|
|
if err := r.afterAuth(ctx); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if _, ok := cookies.Values[RememberKey]; !ok {
|
|
t.Error("Expected a cookie to have been set.")
|
|
}
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
r := &Remember{}
|
|
authboss.NewConfig()
|
|
storer := mocks.NewMockStorer()
|
|
authboss.Cfg.Storer = storer
|
|
cookies := mocks.NewMockClientStorer()
|
|
|
|
key := "tester"
|
|
token, err := r.new(cookies, key)
|
|
|
|
if err != nil {
|
|
t.Error("Unexpected error:", err)
|
|
}
|
|
|
|
if len(token) == 0 {
|
|
t.Error("Expected a token.")
|
|
}
|
|
|
|
if tok, ok := storer.Tokens[key]; !ok {
|
|
t.Error("Expected it to store against the key:", key)
|
|
} else if len(tok) != 1 || len(tok[0]) == 0 {
|
|
t.Error("Expected a token to be saved.")
|
|
}
|
|
|
|
if token != cookies.Values[RememberKey] {
|
|
t.Error("Expected a cookie set with the token.")
|
|
}
|
|
}
|
|
|
|
func TestAuth(t *testing.T) {
|
|
r := &Remember{}
|
|
authboss.NewConfig()
|
|
storer := mocks.NewMockStorer()
|
|
authboss.Cfg.Storer = storer
|
|
|
|
cookies := mocks.NewMockClientStorer()
|
|
session := mocks.NewMockClientStorer()
|
|
ctx := authboss.NewContext()
|
|
ctx.CookieStorer = cookies
|
|
ctx.SessionStorer = session
|
|
|
|
key := "tester"
|
|
_, err := r.new(cookies, key)
|
|
if err != nil {
|
|
t.Error("Unexpected error:", err)
|
|
}
|
|
|
|
interrupt, err := r.auth(ctx)
|
|
if err != nil {
|
|
t.Error("Unexpected error:", err)
|
|
}
|
|
|
|
if session.Values[authboss.SessionHalfAuthKey] != "true" {
|
|
t.Error("The user should have been half-authed.")
|
|
}
|
|
|
|
if session.Values[authboss.SessionKey] != key {
|
|
t.Error("The user should have been logged in.")
|
|
}
|
|
|
|
if authboss.InterruptNone != interrupt {
|
|
t.Error("Keys should have matched:", interrupt)
|
|
}
|
|
}
|