1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-12-10 10:40:07 +02:00
authboss/remember/remember_test.go

211 lines
4.5 KiB
Go
Raw Normal View History

package remember
import (
"bytes"
"fmt"
"net/http"
"testing"
"gopkg.in/authboss.v0"
"gopkg.in/authboss.v0/internal/mocks"
)
func TestInitialize(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
2015-04-01 00:27:47 +02:00
ab := authboss.New()
r := &Remember{}
2015-04-01 00:27:47 +02:00
err := r.Initialize(ab)
if err == nil {
t.Error("Expected error about token storers.")
}
2015-04-01 00:27:47 +02:00
ab.Storer = mocks.MockFailStorer{}
err = r.Initialize(ab)
if err == nil {
t.Error("Expected error about token storers.")
}
2015-04-01 00:27:47 +02:00
ab.Storer = mocks.NewMockStorer()
err = r.Initialize(ab)
if err != nil {
t.Error("Unexpected error:", err)
}
}
func TestAfterAuth(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
r := Remember{authboss.New()}
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
r.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")
2015-04-01 00:27:47 +02:00
ctx, err := r.ContextFromRequest(req)
if err != nil {
t.Error("Unexpected error:", err)
}
ctx.SessionStorer = session
ctx.CookieStorer = cookies
2015-04-01 00:27:47 +02:00
ctx.User = authboss.Attributes{r.PrimaryID: "test@email.com"}
if err := r.afterAuth(ctx); err != nil {
2015-02-22 22:55:09 +02:00
t.Error(err)
}
if _, ok := cookies.Values[authboss.CookieRemember]; !ok {
t.Error("Expected a cookie to have been set.")
}
}
func TestAfterOAuth(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
r := Remember{authboss.New()}
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
r.Storer = storer
cookies := mocks.NewMockClientStorer()
session := mocks.NewMockClientStorer(authboss.SessionOAuth2Params, `{"rm":"true"}`)
uri := fmt.Sprintf("%s?state=%s", "localhost/oauthed", "xsrf")
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
t.Error("Unexpected Error:", err)
}
2015-04-01 00:27:47 +02:00
ctx, err := r.ContextFromRequest(req)
if err != nil {
t.Error("Unexpected error:", err)
}
ctx.SessionStorer = session
ctx.CookieStorer = cookies
ctx.User = authboss.Attributes{
authboss.StoreOAuth2UID: "uid",
authboss.StoreOAuth2Provider: "google",
}
if err := r.afterOAuth(ctx); err != nil {
t.Error(err)
}
if _, ok := cookies.Values[authboss.CookieRemember]; !ok {
t.Error("Expected a cookie to have been set.")
}
}
func TestAfterPasswordReset(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
r := Remember{authboss.New()}
id := "test@email.com"
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
r.Storer = storer
session := mocks.NewMockClientStorer()
cookies := mocks.NewMockClientStorer()
storer.Tokens[id] = []string{"one", "two"}
cookies.Values[authboss.CookieRemember] = "token"
2015-04-01 00:27:47 +02:00
ctx := r.NewContext()
ctx.User = authboss.Attributes{r.PrimaryID: id}
ctx.SessionStorer = session
ctx.CookieStorer = cookies
if err := r.afterPassword(ctx); err != nil {
t.Error(err)
}
if _, ok := cookies.Values[authboss.CookieRemember]; ok {
t.Error("Expected the remember cookie to be deleted.")
}
if len(storer.Tokens) != 0 {
t.Error("Should have wiped out all tokens.")
}
}
func TestNew(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
r := &Remember{authboss.New()}
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
r.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[authboss.CookieRemember] {
t.Error("Expected a cookie set with the token.")
}
}
func TestAuth(t *testing.T) {
2015-04-01 00:27:47 +02:00
t.Parallel()
r := &Remember{authboss.New()}
storer := mocks.NewMockStorer()
2015-04-01 00:27:47 +02:00
r.Storer = storer
cookies := mocks.NewMockClientStorer()
session := mocks.NewMockClientStorer()
2015-04-01 00:27:47 +02:00
ctx := r.NewContext()
ctx.CookieStorer = cookies
ctx.SessionStorer = session
key := "tester"
_, err := r.new(cookies, key)
if err != nil {
t.Error("Unexpected error:", err)
}
cookie, _ := cookies.Get(authboss.CookieRemember)
interrupt, err := r.auth(ctx)
if err != nil {
t.Error("Unexpected error:", err)
}
2015-02-22 23:16:11 +02:00
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 chocolateChip, _ := cookies.Get(authboss.CookieRemember); chocolateChip == cookie {
t.Error("Expected cookie to be different")
}
if authboss.InterruptNone != interrupt {
t.Error("Keys should have matched:", interrupt)
}
}