Add support for recursive listing (#885)
This commit is contained in:
1 parent
1f9fcad1cd
commit
6cd97a4a6e
77 files changed
+2244
-1448
No files matched your search
@@ -20,7 +20,7 @@ type Option func(*options)
|
||||
type options struct {
|
||||
withLimit int
|
||||
withOrder string
|
||||
withScopeId string
|
||||
withScopeIds []string
|
||||
withUserId string
|
||||
withExpirationTime *timestamp.Timestamp
|
||||
withTestTofu []byte
|
||||
@@ -49,9 +49,9 @@ func WithOrder(order string) Option {
|
||||
}
|
||||
|
||||
// WithScopeId allows specifying a scope ID criteria for the function.
|
||||
func WithScopeId(scopeId string) Option {
|
||||
func WithScopeIds(scopeIds []string) Option {
|
||||
return func(o *options) {
|
||||
o.withScopeId = scopeId
|
||||
o.withScopeIds = scopeIds
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,9 +33,9 @@ func Test_GetOpts(t *testing.T) {
|
||||
})
|
||||
t.Run("WithScopeId", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
opts := getOpts(WithScopeId("o_1234"))
|
||||
opts := getOpts(WithScopeIds([]string{"o_1234"}))
|
||||
testOpts := getDefaultOptions()
|
||||
testOpts.withScopeId = "o_1234"
|
||||
testOpts.withScopeIds = []string{"o_1234"}
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
t.Run("WithOrder", func(t *testing.T) {
|
||||
|
||||
@@ -171,9 +171,19 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
|
||||
|
||||
inClauseCnt := 0
|
||||
switch {
|
||||
case opts.withScopeId != "":
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("scope_id = $%d", inClauseCnt)), append(args, opts.withScopeId)
|
||||
case len(opts.withScopeIds) != 0:
|
||||
switch len(opts.withScopeIds) {
|
||||
case 1:
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("scope_id = $%d", inClauseCnt)), append(args, opts.withScopeIds[0])
|
||||
default:
|
||||
idsInClause := make([]string, 0, len(opts.withScopeIds))
|
||||
for _, id := range opts.withScopeIds {
|
||||
inClauseCnt += 1
|
||||
idsInClause, args = append(idsInClause, fmt.Sprintf("$%d", inClauseCnt)), append(args, id)
|
||||
}
|
||||
where = append(where, fmt.Sprintf("scope_id in (%s)", strings.Join(idsInClause, ",")))
|
||||
}
|
||||
case opts.withUserId != "":
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("user_id = $%d", inClauseCnt)), append(args, opts.withUserId)
|
||||
@@ -184,7 +194,7 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
|
||||
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, ",")))
|
||||
where = append(where, fmt.Sprintf("s.public_id in (%s)", strings.Join(idsInClause, ",")))
|
||||
}
|
||||
|
||||
var limit string
|
||||
|
||||
@@ -78,7 +78,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
name: "withScopeId",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
args: args{
|
||||
opt: []Option{WithScopeId(composedOf.ScopeId)},
|
||||
opt: []Option{WithScopeIds([]string{composedOf.ScopeId})},
|
||||
},
|
||||
wantCnt: repo.defaultLimit,
|
||||
wantErr: false,
|
||||
@@ -87,7 +87,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
name: "bad-withScopeId",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
args: args{
|
||||
opt: []Option{WithScopeId("o_thisIsNotValid")},
|
||||
opt: []Option{WithScopeIds([]string{"o_thisIsNotValid"})},
|
||||
},
|
||||
wantCnt: 0,
|
||||
wantErr: false,
|
||||
@@ -168,6 +168,32 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_ListSessions_Multiple_Scopes(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
rw := db.New(conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
|
||||
const numPerScope = 10
|
||||
var projs []string
|
||||
for i := 0; i < numPerScope; i++ {
|
||||
composedOf := TestSessionParams(t, conn, wrapper, iamRepo)
|
||||
projs = append(projs, composedOf.ScopeId)
|
||||
s := TestSession(t, conn, wrapper, composedOf)
|
||||
_ = TestState(t, conn, s.PublicId, StatusActive)
|
||||
}
|
||||
|
||||
got, err := repo.ListSessions(context.Background(), WithScopeIds(projs))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(projs), len(got))
|
||||
}
|
||||
|
||||
func TestRepository_CreateSession(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
|
||||
Reference in new issue
Block a user