From e42a2d127dacccd2cc13aad6de506b82fcdcb87a Mon Sep 17 00:00:00 2001 From: Jim Lambert Date: Sun, 13 Sep 2020 12:22:06 -0400 Subject: [PATCH] add WithExpirationTime option with unit test --- internal/session/options.go | 20 ++++++++++++++++---- internal/session/options_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/session/options.go b/internal/session/options.go index f5a2146324..15fe56a64c 100644 --- a/internal/session/options.go +++ b/internal/session/options.go @@ -1,5 +1,9 @@ package session +import ( + "github.com/hashicorp/boundary/internal/db/timestamp" +) + // getOpts - iterate the inbound Options and return a struct func getOpts(opt ...Option) options { opts := getDefaultOptions() @@ -14,10 +18,11 @@ type Option func(*options) // options = how options are represented type options struct { - withLimit int - withOrder string - withScopeId string - withUserId string + withLimit int + withOrder string + withScopeId string + withUserId string + withExpirationTime *timestamp.Timestamp } func getDefaultOptions() options { @@ -53,3 +58,10 @@ func WithUserId(userId string) Option { o.withUserId = userId } } + +// WithExpirationTime allows specifying an expiration time for the session +func WithExpirationTime(exp *timestamp.Timestamp) Option { + return func(o *options) { + o.withExpirationTime = exp + } +} diff --git a/internal/session/options_test.go b/internal/session/options_test.go index 885db1b012..d8cd8cecf4 100644 --- a/internal/session/options_test.go +++ b/internal/session/options_test.go @@ -2,8 +2,12 @@ package session import ( "testing" + "time" + "github.com/golang/protobuf/ptypes" + "github.com/hashicorp/boundary/internal/db/timestamp" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // Test_GetOpts provides unit tests for GetOpts and all the options @@ -48,4 +52,13 @@ func Test_GetOpts(t *testing.T) { testOpts.withUserId = "u_1234" assert.Equal(opts, testOpts) }) + t.Run("WithExpirationTime", func(t *testing.T) { + assert, require := assert.New(t), require.New(t) + now, err := ptypes.TimestampProto(time.Now()) + require.NoError(err) + opts := getOpts(WithExpirationTime(×tamp.Timestamp{Timestamp: now})) + testOpts := getDefaultOptions() + testOpts.withExpirationTime = ×tamp.Timestamp{Timestamp: now} + assert.Equal(opts, testOpts) + }) }