mirror of
https://github.com/volatiletech/authboss.git
synced 2025-07-15 01:24:33 +02:00
@ -1,6 +1,8 @@
|
||||
package authboss
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@ -15,7 +17,7 @@ func TestMain(main *testing.M) {
|
||||
}
|
||||
|
||||
func TestAuthBossInit(t *testing.T) {
|
||||
NewConfig()
|
||||
Cfg = NewConfig()
|
||||
err := Init()
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
@ -23,7 +25,7 @@ func TestAuthBossInit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuthBossCurrentUser(t *testing.T) {
|
||||
NewConfig()
|
||||
Cfg = NewConfig()
|
||||
Cfg.Storer = mockStorer{"joe": Attributes{"email": "john@john.com", "password": "lies"}}
|
||||
Cfg.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
||||
return mockClientStore{SessionKey: "joe"}
|
||||
@ -46,3 +48,82 @@ func TestAuthBossCurrentUser(t *testing.T) {
|
||||
t.Error("Wrong user found!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthbossUpdatePassword(t *testing.T) {
|
||||
Cfg = NewConfig()
|
||||
session := mockClientStore{}
|
||||
cookies := mockClientStore{}
|
||||
Cfg.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
||||
return session
|
||||
}
|
||||
Cfg.CookieStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
||||
return cookies
|
||||
}
|
||||
|
||||
called := false
|
||||
Cfg.Callbacks.After(EventPasswordReset, func(ctx *Context) error {
|
||||
called = true
|
||||
return nil
|
||||
})
|
||||
|
||||
user1 := struct {
|
||||
Password string
|
||||
}{}
|
||||
user2 := struct {
|
||||
Password sql.NullString
|
||||
}{}
|
||||
|
||||
r, _ := http.NewRequest("GET", "http://localhost", nil)
|
||||
|
||||
called = false
|
||||
err := UpdatePassword(nil, r, "newpassword", &user1, func() error { return nil })
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(user1.Password) == 0 {
|
||||
t.Error("Password not updated")
|
||||
}
|
||||
if !called {
|
||||
t.Error("Callbacks should have been called.")
|
||||
}
|
||||
|
||||
called = false
|
||||
err = UpdatePassword(nil, r, "newpassword", &user2, func() error { return nil })
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !user2.Password.Valid || len(user2.Password.String) == 0 {
|
||||
t.Error("Password not updated")
|
||||
}
|
||||
if !called {
|
||||
t.Error("Callbacks should have been called.")
|
||||
}
|
||||
|
||||
called = false
|
||||
oldPassword := user1.Password
|
||||
err = UpdatePassword(nil, r, "", &user1, func() error { return nil })
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if user1.Password != oldPassword {
|
||||
t.Error("Password not updated")
|
||||
}
|
||||
if called {
|
||||
t.Error("Callbacks should not have been called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthbossUpdatePasswordFail(t *testing.T) {
|
||||
user1 := struct {
|
||||
Password string
|
||||
}{}
|
||||
|
||||
anErr := errors.New("AnError")
|
||||
err := UpdatePassword(nil, nil, "update", &user1, func() error { return anErr })
|
||||
if err != anErr {
|
||||
t.Error("Expected an specific error:", err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user