2015-01-14 19:18:45 -08:00
|
|
|
package authboss
|
|
|
|
|
2015-08-02 11:51:35 -07:00
|
|
|
import "testing"
|
2015-01-16 21:30:04 -08:00
|
|
|
|
|
|
|
func TestContext_SaveUser(t *testing.T) {
|
2015-03-31 12:34:03 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab := New()
|
|
|
|
ctx := ab.NewContext()
|
2015-01-16 21:30:04 -08:00
|
|
|
storer := mockStorer{}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.Storer = storer
|
2015-02-22 13:16:11 -08:00
|
|
|
ctx.User = Attributes{StoreUsername: "joe", StoreEmail: "hello@joe.com", StorePassword: "mysticalhash"}
|
2015-01-16 21:30:04 -08:00
|
|
|
|
2015-02-18 08:45:27 -08:00
|
|
|
err := ctx.SaveUser()
|
2015-01-16 21:30:04 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2015-02-22 13:16:11 -08:00
|
|
|
attr, ok := storer["hello@joe.com"]
|
2015-01-16 21:30:04 -08:00
|
|
|
if !ok {
|
|
|
|
t.Error("Could not find joe!")
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range ctx.User {
|
|
|
|
if v != attr[k] {
|
|
|
|
t.Error(v, "not equal to", ctx.User[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_LoadUser(t *testing.T) {
|
2015-03-31 12:34:03 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab := New()
|
|
|
|
ctx := ab.NewContext()
|
2015-03-13 16:23:43 -07:00
|
|
|
|
|
|
|
attr := Attributes{
|
|
|
|
"email": "hello@joe.com",
|
|
|
|
"password": "mysticalhash",
|
|
|
|
"uid": "what",
|
|
|
|
"provider": "google",
|
|
|
|
}
|
|
|
|
|
2015-01-16 21:30:04 -08:00
|
|
|
storer := mockStorer{
|
2015-03-13 16:23:43 -07:00
|
|
|
"joe": attr,
|
|
|
|
"whatgoogle": attr,
|
2015-01-16 21:30:04 -08:00
|
|
|
}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.Storer = storer
|
|
|
|
ab.OAuth2Storer = storer
|
2015-01-16 21:30:04 -08:00
|
|
|
|
2015-03-13 16:23:43 -07:00
|
|
|
ctx.User = nil
|
|
|
|
if err := ctx.LoadUser("joe"); err != nil {
|
2015-02-18 08:45:27 -08:00
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:23:43 -07:00
|
|
|
if email, err := ctx.User.StringErr("email"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if email != attr["email"] {
|
|
|
|
t.Error("Email wrong:", email)
|
|
|
|
}
|
|
|
|
if password, err := ctx.User.StringErr("password"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if password != attr["password"] {
|
|
|
|
t.Error("Password wrong:", password)
|
|
|
|
}
|
2015-02-18 08:45:27 -08:00
|
|
|
|
2015-03-13 16:23:43 -07:00
|
|
|
ctx.User = nil
|
|
|
|
if err := ctx.LoadUser("what;google"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if email, err := ctx.User.StringErr("email"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if email != attr["email"] {
|
|
|
|
t.Error("Email wrong:", email)
|
|
|
|
}
|
|
|
|
if password, err := ctx.User.StringErr("password"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if password != attr["password"] {
|
|
|
|
t.Error("Password wrong:", password)
|
2015-02-18 08:45:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_LoadSessionUser(t *testing.T) {
|
2015-03-31 12:34:03 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab := New()
|
|
|
|
ctx := ab.NewContext()
|
2015-02-18 08:45:27 -08:00
|
|
|
storer := mockStorer{
|
|
|
|
"joe": Attributes{"email": "hello@joe.com", "password": "mysticalhash"},
|
|
|
|
}
|
2015-03-31 12:34:03 -07:00
|
|
|
ab.Storer = storer
|
2015-02-18 08:45:27 -08:00
|
|
|
ctx.SessionStorer = mockClientStore{
|
|
|
|
SessionKey: "joe",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ctx.LoadSessionUser()
|
2015-01-16 21:30:04 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := storer["joe"]
|
|
|
|
|
|
|
|
for k, v := range attr {
|
|
|
|
if v != ctx.User[k] {
|
|
|
|
t.Error(v, "not equal to", ctx.User[k])
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 19:18:45 -08:00
|
|
|
}
|