2015-01-11 08:52:39 +02:00
|
|
|
package remember
|
|
|
|
|
|
|
|
import (
|
2015-01-15 12:56:13 +02:00
|
|
|
"bytes"
|
2015-03-14 01:23:43 +02:00
|
|
|
"fmt"
|
2015-01-15 12:56:13 +02:00
|
|
|
"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-04-01 00:27:47 +02:00
|
|
|
t.Parallel()
|
2015-01-13 00:02:07 +02:00
|
|
|
|
2015-04-01 00:27:47 +02:00
|
|
|
ab := authboss.New()
|
2015-01-13 00:02:07 +02:00
|
|
|
r := &Remember{}
|
2015-04-01 00:27:47 +02:00
|
|
|
err := r.Initialize(ab)
|
2015-01-13 00:02:07 +02:00
|
|
|
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)
|
2015-01-13 00:02:07 +02:00
|
|
|
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)
|
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-04-01 00:27:47 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
r := Remember{authboss.New()}
|
2015-01-24 02:24:34 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-04-01 00:27:47 +02:00
|
|
|
r.Storer = storer
|
2015-02-16 06:07:36 +02:00
|
|
|
|
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")
|
|
|
|
|
2015-04-01 00:27:47 +02:00
|
|
|
ctx, err := r.ContextFromRequest(req)
|
2015-01-15 12:56:13 +02:00
|
|
|
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-04-01 00:27:47 +02:00
|
|
|
ctx.User = authboss.Attributes{r.PrimaryID: "test@email.com"}
|
2015-01-15 12:56:13 +02:00
|
|
|
|
2015-02-27 09:09:37 +02:00
|
|
|
if err := r.afterAuth(ctx); err != nil {
|
2015-02-22 22:55:09 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-01-15 12:56:13 +02:00
|
|
|
|
2015-03-03 08:09:32 +02:00
|
|
|
if _, ok := cookies.Values[authboss.CookieRemember]; !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
|
|
|
}
|
|
|
|
|
2015-03-14 01:23:43 +02:00
|
|
|
func TestAfterOAuth(t *testing.T) {
|
2015-04-01 00:27:47 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
r := Remember{authboss.New()}
|
2015-03-14 01:23:43 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-04-01 00:27:47 +02:00
|
|
|
r.Storer = storer
|
2015-03-14 01:23:43 +02:00
|
|
|
|
|
|
|
cookies := mocks.NewMockClientStorer()
|
2015-03-25 04:39:20 +02:00
|
|
|
session := mocks.NewMockClientStorer(authboss.SessionOAuth2Params, `{"rm":"true"}`)
|
2015-03-14 01:23:43 +02:00
|
|
|
|
2015-03-25 04:39:20 +02:00
|
|
|
uri := fmt.Sprintf("%s?state=%s", "localhost/oauthed", "xsrf")
|
2015-03-14 01:23:43 +02:00
|
|
|
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)
|
2015-03-14 01:23:43 +02:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 06:05:47 +02:00
|
|
|
func TestAfterPasswordReset(t *testing.T) {
|
2015-04-01 00:27:47 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
r := Remember{authboss.New()}
|
2015-03-06 06:05:47 +02:00
|
|
|
|
|
|
|
id := "test@email.com"
|
|
|
|
|
|
|
|
storer := mocks.NewMockStorer()
|
2015-04-01 00:27:47 +02:00
|
|
|
r.Storer = storer
|
2015-03-06 06:05:47 +02:00
|
|
|
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}
|
2015-03-06 06:05:47 +02:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
func TestNew(t *testing.T) {
|
2015-04-01 00:27:47 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
r := &Remember{authboss.New()}
|
2015-01-24 02:24:34 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-04-01 00:27:47 +02:00
|
|
|
r.Storer = storer
|
2015-02-12 02:38:09 +02:00
|
|
|
cookies := mocks.NewMockClientStorer()
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
key := "tester"
|
2015-02-27 09:09:37 +02:00
|
|
|
token, err := r.new(cookies, key)
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
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-03-03 08:09:32 +02:00
|
|
|
if token != cookies.Values[authboss.CookieRemember] {
|
2015-01-13 00:02:07 +02:00
|
|
|
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()}
|
2015-01-24 02:24:34 +02:00
|
|
|
storer := mocks.NewMockStorer()
|
2015-04-01 00:27:47 +02:00
|
|
|
r.Storer = storer
|
2015-02-27 09:09:37 +02:00
|
|
|
|
2015-02-12 02:38:09 +02:00
|
|
|
cookies := mocks.NewMockClientStorer()
|
|
|
|
session := mocks.NewMockClientStorer()
|
2015-04-01 00:27:47 +02:00
|
|
|
ctx := r.NewContext()
|
2015-02-27 09:09:37 +02:00
|
|
|
ctx.CookieStorer = cookies
|
|
|
|
ctx.SessionStorer = session
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
key := "tester"
|
2015-02-27 09:09:37 +02:00
|
|
|
_, err := r.new(cookies, key)
|
2015-01-13 00:02:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2015-03-03 08:09:32 +02:00
|
|
|
cookie, _ := cookies.Get(authboss.CookieRemember)
|
2015-03-02 06:40:09 +02:00
|
|
|
|
2015-02-27 09:09:37 +02:00
|
|
|
interrupt, err := r.auth(ctx)
|
2015-01-13 00:02:07 +02:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2015-03-03 08:09:32 +02:00
|
|
|
if chocolateChip, _ := cookies.Get(authboss.CookieRemember); chocolateChip == cookie {
|
2015-03-02 06:40:09 +02:00
|
|
|
t.Error("Expected cookie to be different")
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:09:37 +02:00
|
|
|
if authboss.InterruptNone != interrupt {
|
|
|
|
t.Error("Keys should have matched:", interrupt)
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
}
|