From fb77bd3511fcab886340e850ef7db7d35f9790f3 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 20:35:03 +0300 Subject: [PATCH] [#302] pkg/session: Document default values set in `NewToken` Document field values of instance constructed via `NewToken`. Assert the values in corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/session/session.go | 9 +++++++++ pkg/session/session_test.go | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pkg/session/session.go b/pkg/session/session.go index 3f58bfff..6235636e 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -24,6 +24,15 @@ func NewTokenFromV2(tV2 *session.SessionToken) *Token { } // NewToken creates and returns blank Token. +// +// Defaults: +// - body: nil; +// - id: nil; +// - ownerId: nil; +// - sessionKey: nil; +// - exp: 0; +// - iat: 0; +// - nbf: 0; func NewToken() *Token { return NewTokenFromV2(new(session.SessionToken)) } diff --git a/pkg/session/session_test.go b/pkg/session/session_test.go index 1ce07b3f..3835f38e 100644 --- a/pkg/session/session_test.go +++ b/pkg/session/session_test.go @@ -179,3 +179,24 @@ func TestToken_ToV2(t *testing.T) { require.Nil(t, x.ToV2()) }) } + +func TestNewToken(t *testing.T) { + t.Run("default values", func(t *testing.T) { + token := session.NewToken() + + // check initial values + require.Nil(t, token.Signature()) + require.Nil(t, token.OwnerID()) + require.Nil(t, token.SessionKey()) + require.Nil(t, token.ID()) + require.Zero(t, token.Exp()) + require.Zero(t, token.Iat()) + require.Zero(t, token.Nbf()) + + // convert to v2 message + tokenV2 := token.ToV2() + + require.Nil(t, tokenV2.GetSignature()) + require.Nil(t, tokenV2.GetBody()) + }) +}