add WithExpirationTime option with unit test

jimlambrt-session-basics
Jim Lambert 6 years ago
parent 47e7b3cf54
commit e42a2d127d

@ -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
}
}

@ -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(&timestamp.Timestamp{Timestamp: now}))
testOpts := getDefaultOptions()
testOpts.withExpirationTime = &timestamp.Timestamp{Timestamp: now}
assert.Equal(opts, testOpts)
})
}

Loading…
Cancel
Save