Exemplo n.º 1
0
func TestUserSession(t *testing.T) {
	userStore := store{kUserId}
	sessionStore := newSessionStoreWithUserId(kSessionId, kUserId)
	r := requestWithCookie(kSessionCookieName, kSessionId)
	us, err := session_util.NewUserSession(
		sessionStore,
		r,
		kSessionCookieName,
		func(s *sessions.Session) session_util.UserSession {
			return newUserSession(s)
		},
		userStore,
		errNoSuchId)
	if err != nil {
		t.Fatalf("An error happened getting userSession: %v", err)
	}
	defer context.Clear(r)
	myUserSession := us.(*userSession)
	if output := myUserSession.User; *output != kUserId {
		t.Errorf("Expected %v, got %v", kUserId, *output)
	}
	if myUserSession != session_util.GetUserSession(r) {
		t.Error("User session not stored with request.")
	}
}
Exemplo n.º 2
0
func TestUserSessionError(t *testing.T) {
	userStore := errorStore{}
	sessionStore := newSessionStoreWithUserId(kSessionId, kUserId)
	r := requestWithCookie(kSessionCookieName, kSessionId)
	_, err := session_util.NewUserSession(
		sessionStore,
		r,
		kSessionCookieName,
		func(s *sessions.Session) session_util.UserSession {
			return newUserSession(s)
		},
		userStore,
		errNoSuchId)
	if err != errDb {
		t.Error("Expected to get an error getting user session.")
	}
}
Exemplo n.º 3
0
// NewUserSession creates a UserSession and associates it with the request
// instance. If user not logged in, the User field in returned UserSession
// is nil. Caller must call  must call context.Clear(r) from the
// github.com/gorilla/context package.
// It is up to the caller to ensure that the Id of the key in the returned
// session matches the result of GetOwner() on the user instance in the same
// session.
func NewUserSession(
	userStore vsafedb.UserByIdRunner,
	sessionStore sessions.Store,
	r *http.Request) (*UserSession, error) {
	us, err := session_util.NewUserSession(
		sessionStore,
		r,
		kCookieName,
		func(s *sessions.Session) session_util.UserSession {
			return CreateUserSession(s)
		},
		userGetter{userStore},
		vsafedb.ErrNoSuchId)
	if err != nil {
		return nil, err
	}
	return us.(*UserSession), nil
}
Exemplo n.º 4
0
func TestUserSessionNewSession(t *testing.T) {
	userStore := errorStore{}
	sessionStore := ramstore.NewRAMStore(900)
	r := &http.Request{}
	us, err := session_util.NewUserSession(
		sessionStore,
		r,
		kSessionCookieName,
		func(s *sessions.Session) session_util.UserSession {
			return newUserSession(s)
		},
		userStore,
		errNoSuchId)
	if err != nil {
		t.Fatalf("An error happened getting userSession: %v", err)
	}
	defer context.Clear(r)
	myUserSession := us.(*userSession)
	if output := myUserSession.User; output != nil {
		t.Error("Should not have user instance in user session.")
	}
}
Exemplo n.º 5
0
func TestUserSessionNoSuchId(t *testing.T) {
	userStore := store{kUserId + 1}
	sessionStore := newSessionStoreWithUserId(kSessionId, kUserId)
	r := requestWithCookie(kSessionCookieName, kSessionId)
	us, err := session_util.NewUserSession(
		sessionStore,
		r,
		kSessionCookieName,
		func(s *sessions.Session) session_util.UserSession {
			return newUserSession(s)
		},
		userStore,
		errNoSuchId)
	if err != nil {
		t.Fatalf("An error happened getting userSession: %v", err)
	}
	defer context.Clear(r)
	myUserSession := us.(*userSession)
	if output := myUserSession.User; output != nil {
		t.Error("Should not have user instance in user session.")
	}
}