add support for WithSessionIds in ListSesions (#395)

This commit is contained in:
Jim authored and GitHub committed 2020-09-22 10:41:42 -04:00
1 parent 262ff06042
commit cfc76eb88f
4 files changed
+44 -1

No files matched your search

+9
View File
@@ -25,6 +25,7 @@ type options struct {
withExpirationTime *timestamp.Timestamp
withTestTofu []byte
withListingConvert bool
withSessionIds []string
}
func getDefaultOptions() options {
@@ -75,6 +76,14 @@ func WithTestTofu(tofu []byte) Option {
}
}
// WithSessionIds allows the specification of the session ids to use for the
// operation.
func WithSessionIds(ids ...string) Option {
return func(o *options) {
o.withSessionIds = ids
}
}
func withListingConvert(withListingConvert bool) Option {
return func(o *options) {
o.withListingConvert = withListingConvert
+7
View File
@@ -70,4 +70,11 @@ func Test_GetOpts(t *testing.T) {
copy(testOpts.withTestTofu, tofu)
assert.Equal(opts, testOpts)
})
t.Run("WithSessionIds", func(t *testing.T) {
assert := assert.New(t)
opts := getOpts(WithSessionIds("s_1", "s_2", "s_3"))
testOpts := getDefaultOptions()
testOpts.withSessionIds = []string{"s_1", "s_2", "s_3"}
assert.Equal(opts, testOpts)
})
}
+10 -1
View File
@@ -162,7 +162,7 @@ func (r *Repository) LookupSession(ctx context.Context, sessionId string, opt ..
return &session, authzSummary, nil
}
// ListSessions will sessions. Supports the WithLimit, and WithScopeId options.
// ListSessions will sessions. Supports the WithLimit, WithScopeId and WithSessionIds options.
func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Session, error) {
opts := getOpts(opt...)
var where []string
@@ -177,6 +177,15 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
inClauseCnt += 1
where, args = append(where, fmt.Sprintf("user_id = $%d", inClauseCnt)), append(args, opts.withUserId)
}
if len(opts.withSessionIds) > 0 {
idsInClause := make([]string, 0, len(opts.withSessionIds))
for _, id := range opts.withSessionIds {
inClauseCnt += 1
idsInClause, args = append(idsInClause, fmt.Sprintf("$%d", inClauseCnt)), append(args, id)
}
where = append(where, fmt.Sprintf("s.public_id in(%s)", strings.Join(idsInClause, ",")))
}
var limit string
switch {
case opts.withLimit < 0: // any negative number signals unlimited results
@@ -138,6 +138,24 @@ func TestRepository_ListSession(t *testing.T) {
assert.Equal(1, len(got))
assert.Equal(got[0].UserId, s.UserId)
})
t.Run("WithSessionIds", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
testSessions := []*Session{}
for i := 0; i < 10; i++ {
s := TestSession(t, conn, wrapper, composedOf)
_ = TestState(t, conn, s.PublicId, StatusActive)
testSessions = append(testSessions, s)
}
assert.Equal(10, len(testSessions))
withIds := []string{testSessions[0].PublicId, testSessions[1].PublicId}
conn.LogMode(true)
got, err := repo.ListSessions(context.Background(), WithSessionIds(withIds...), WithOrder("create_time asc"))
require.NoError(err)
assert.Equal(2, len(got))
assert.Equal(StatusActive, got[0].States[0].Status)
assert.Equal(StatusPending, got[0].States[1].Status)
})
}
func TestRepository_CreateSession(t *testing.T) {