2015-01-11 08:52:39 +02:00
|
|
|
package remember
|
|
|
|
|
|
|
|
import (
|
2015-01-15 12:56:13 +02:00
|
|
|
"bytes"
|
|
|
|
"net/http"
|
2015-01-11 08:52:39 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gopkg.in/authboss.v0"
|
2015-01-24 02:24:34 +02:00
|
|
|
"gopkg.in/authboss.v0/internal/mocks"
|
2015-01-11 08:52:39 +02:00
|
|
|
)
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
func TestInitialize(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.NewConfig()
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
r := &Remember{}
|
2015-02-16 06:07:36 +02:00
|
|
|
err := r.Initialize()
|
2015-01-13 00:02:07 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error about token storers.")
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.Cfg.Storer = mocks.MockFailStorer{}
|
|
|
|
err = r.Initialize()
|
2015-01-13 00:02:07 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error about token storers.")
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.Cfg.Storer = mocks.NewMockStorer()
|
|
|
|
err = r.Initialize()
|
2015-01-11 08:52:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
2015-01-13 00:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAfterAuth(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.NewConfig()
|
2015-01-24 02:24:34 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.Cfg.Storer = storer
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
cookies := mocks.NewMockClientStorer()
|
|
|
|
session := mocks.NewMockClientStorer()
|
2015-01-15 12:56:13 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
ctx.SessionStorer = session
|
2015-01-15 12:56:13 +02:00
|
|
|
ctx.CookieStorer = cookies
|
2015-02-22 23:16:11 +02:00
|
|
|
ctx.User = authboss.Attributes{authboss.Cfg.PrimaryID: "test@email.com"}
|
2015-01-15 12:56:13 +02:00
|
|
|
|
2015-02-22 22:55:09 +02:00
|
|
|
if err := R.AfterAuth(ctx); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-01-15 12:56:13 +02:00
|
|
|
|
2015-02-22 22:55:09 +02:00
|
|
|
if _, ok := cookies.Values[RememberKey]; !ok {
|
2015-01-15 12:56:13 +02:00
|
|
|
t.Error("Expected a cookie to have been set.")
|
|
|
|
}
|
2015-01-13 00:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.NewConfig()
|
2015-01-24 02:24:34 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.Cfg.Storer = storer
|
2015-02-12 02:38:09 +02:00
|
|
|
cookies := mocks.NewMockClientStorer()
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:24:34 +02:00
|
|
|
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.")
|
2015-01-13 00:02:07 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 22:55:09 +02:00
|
|
|
if token != cookies.Values[RememberKey] {
|
2015-01-13 00:02:07 +02:00
|
|
|
t.Error("Expected a cookie set with the token.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuth(t *testing.T) {
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.NewConfig()
|
2015-01-24 02:24:34 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.Cfg.Storer = storer
|
2015-02-12 02:38:09 +02:00
|
|
|
cookies := mocks.NewMockClientStorer()
|
|
|
|
session := mocks.NewMockClientStorer()
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
key := "tester"
|
|
|
|
token, err := R.New(cookies, key)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outKey, err := R.Auth(cookies, session, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2015-02-22 23:16:11 +02:00
|
|
|
if session.Values[authboss.SessionHalfAuthKey] != "true" {
|
2015-01-13 00:02:07 +02:00
|
|
|
t.Error("The user should have been half-authed.")
|
|
|
|
}
|
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
if session.Values[authboss.SessionKey] != key {
|
2015-01-13 00:02:07 +02:00
|
|
|
t.Error("The user should have been logged in.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if key != outKey {
|
|
|
|
t.Error("Keys should have matched:", outKey)
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
}
|