2017-02-24 02:13:25 +02:00
|
|
|
package authboss
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func loadClientStateP(ab *Authboss, w http.ResponseWriter, r *http.Request) *http.Request {
|
|
|
|
r, err := ab.LoadClientState(w, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-08 02:21:37 +02:00
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetupContext() (*Authboss, *http.Request) {
|
|
|
|
ab := New()
|
2018-02-02 01:42:48 +02:00
|
|
|
ab.Storage.SessionState = newMockClientStateRW(SessionKey, "george-pid")
|
|
|
|
ab.Storage.Server = mockServerStorer{
|
2017-03-05 20:01:46 +02:00
|
|
|
"george-pid": mockUser{Email: "george-pid", Password: "unreadable"},
|
|
|
|
}
|
2018-03-08 02:21:37 +02:00
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
w := ab.NewResponse(httptest.NewRecorder())
|
|
|
|
r = loadClientStateP(ab, w, r)
|
2017-03-05 20:01:46 +02:00
|
|
|
|
|
|
|
return ab, r
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetupContextCached() (*Authboss, mockUser, *http.Request) {
|
|
|
|
ab := New()
|
|
|
|
wantUser := mockUser{Email: "george-pid", Password: "unreadable"}
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
2018-02-15 01:16:44 +02:00
|
|
|
ctx := context.WithValue(req.Context(), CTXKeyPID, "george-pid")
|
|
|
|
ctx = context.WithValue(ctx, CTXKeyUser, wantUser)
|
2017-03-05 20:01:46 +02:00
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
return ab, wantUser, req
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetupContextPanic() *Authboss {
|
|
|
|
ab := New()
|
2018-02-02 01:42:48 +02:00
|
|
|
ab.Storage.SessionState = newMockClientStateRW(SessionKey, "george-pid")
|
|
|
|
ab.Storage.Server = mockServerStorer{}
|
2017-03-05 20:01:46 +02:00
|
|
|
|
|
|
|
return ab
|
|
|
|
}
|
2017-02-24 02:13:25 +02:00
|
|
|
|
|
|
|
func TestCurrentUserID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, r := testSetupContext()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
id, err := ab.CurrentUserID(r)
|
2017-02-24 02:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != "george-pid" {
|
|
|
|
t.Error("got:", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:29:58 +02:00
|
|
|
func TestCurrentUserIDContext(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, r := testSetupContext()
|
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
id, err := ab.CurrentUserID(r)
|
2017-02-24 02:29:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != "george-pid" {
|
|
|
|
t.Error("got:", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
func TestCurrentUserIDP(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab := testSetupContextPanic()
|
|
|
|
// Overwrite the setup functions state storer
|
2018-02-02 01:42:48 +02:00
|
|
|
ab.Storage.SessionState = newMockClientStateRW()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if recover().(error) != ErrUserNotFound {
|
|
|
|
t.Failed()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
_ = ab.CurrentUserIDP(httptest.NewRequest("GET", "/", nil))
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCurrentUser(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, r := testSetupContext()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
user, err := ab.CurrentUser(r)
|
2017-02-24 02:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:31:55 +02:00
|
|
|
if got := user.GetPID(); got != "george-pid" {
|
2017-02-24 02:13:25 +02:00
|
|
|
t.Error("got:", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:29:58 +02:00
|
|
|
func TestCurrentUserContext(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, _, r := testSetupContextCached()
|
2017-02-24 02:29:58 +02:00
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
user, err := ab.CurrentUser(r)
|
2017-02-24 02:29:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:31:55 +02:00
|
|
|
if got := user.GetPID(); got != "george-pid" {
|
2017-02-24 02:29:58 +02:00
|
|
|
t.Error("got:", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
func TestCurrentUserP(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab := testSetupContextPanic()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if recover().(error) != ErrUserNotFound {
|
|
|
|
t.Failed()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
_ = ab.CurrentUserP(httptest.NewRequest("GET", "/", nil))
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadCurrentUserID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, r := testSetupContext()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
id, err := ab.LoadCurrentUserID(&r)
|
2017-02-24 02:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != "george-pid" {
|
|
|
|
t.Error("got:", id)
|
|
|
|
}
|
|
|
|
|
2018-02-15 01:16:44 +02:00
|
|
|
if r.Context().Value(CTXKeyPID).(string) != "george-pid" {
|
2017-02-24 02:13:25 +02:00
|
|
|
t.Error("context was not updated in local request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
func TestLoadCurrentUserIDContext(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ab, _, r := testSetupContextCached()
|
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
pid, err := ab.LoadCurrentUserID(&r)
|
2017-03-05 20:01:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pid != "george-pid" {
|
|
|
|
t.Error("got:", pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
func TestLoadCurrentUserIDP(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab := testSetupContextPanic()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if recover().(error) != ErrUserNotFound {
|
|
|
|
t.Failed()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
2018-03-08 02:21:37 +02:00
|
|
|
_ = ab.LoadCurrentUserIDP(&r)
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadCurrentUser(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, r := testSetupContext()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
user, err := ab.LoadCurrentUser(&r)
|
2017-02-24 02:13:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:31:55 +02:00
|
|
|
if got := user.GetPID(); got != "george-pid" {
|
2017-02-24 02:13:25 +02:00
|
|
|
t.Error("got:", got)
|
|
|
|
}
|
|
|
|
|
2018-02-01 21:51:43 +02:00
|
|
|
want := user.(mockUser)
|
2018-02-15 01:16:44 +02:00
|
|
|
got := r.Context().Value(CTXKeyUser).(mockUser)
|
2017-02-24 02:13:25 +02:00
|
|
|
if got != want {
|
2018-02-01 21:51:43 +02:00
|
|
|
t.Errorf("users mismatched:\nwant: %#v\ngot: %#v", want, got)
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:29:58 +02:00
|
|
|
func TestLoadCurrentUserContext(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab, wantUser, r := testSetupContextCached()
|
2017-02-24 02:29:58 +02:00
|
|
|
|
2018-03-08 02:21:37 +02:00
|
|
|
user, err := ab.LoadCurrentUser(&r)
|
2017-02-24 02:29:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2018-02-01 21:51:43 +02:00
|
|
|
got := user.(mockUser)
|
2017-03-05 20:01:46 +02:00
|
|
|
if got != wantUser {
|
2018-02-01 21:51:43 +02:00
|
|
|
t.Errorf("users mismatched:\nwant: %#v\ngot: %#v", wantUser, got)
|
2017-02-24 02:29:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
func TestLoadCurrentUserP(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
ab := testSetupContextPanic()
|
2017-02-24 02:13:25 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if recover().(error) != ErrUserNotFound {
|
|
|
|
t.Failed()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-03-05 20:01:46 +02:00
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
2018-03-08 02:21:37 +02:00
|
|
|
_ = ab.LoadCurrentUserP(&r)
|
2017-03-05 20:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 01:16:44 +02:00
|
|
|
func TestCTXKeyString(t *testing.T) {
|
2017-03-05 20:01:46 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-15 01:16:44 +02:00
|
|
|
if got := CTXKeyPID.String(); got != "authboss ctx key pid" {
|
2017-03-05 20:01:46 +02:00
|
|
|
t.Error(got)
|
|
|
|
}
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|