add listing for groups and roles (#130)
This commit is contained in:
4 files changed
+230
No files matched your search
@@ -114,3 +114,16 @@ func (r *Repository) DeleteGroup(ctx context.Context, withPublicId string, opt .
|
||||
}
|
||||
return rowsDeleted, nil
|
||||
}
|
||||
|
||||
// ListGroups in a scope and supports WithLimit option.
|
||||
func (r *Repository) ListGroups(ctx context.Context, withScopeId string, opt ...Option) ([]*Group, error) {
|
||||
if withScopeId == "" {
|
||||
return nil, fmt.Errorf("list groups: missing scope id %w", db.ErrInvalidParameter)
|
||||
}
|
||||
var grps []*Group
|
||||
err := r.list(ctx, &grps, "scope_id = ?", []interface{}{withScopeId}, opt...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list groups: %w", err)
|
||||
}
|
||||
return grps, nil
|
||||
}
|
||||
@@ -540,3 +540,105 @@ func TestRepository_DeleteGroup(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListGroups(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
||||
defer func() {
|
||||
err := cleanup()
|
||||
assert.NoError(t, err)
|
||||
err = conn.Close()
|
||||
assert.NoError(t, err)
|
||||
}()
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
|
||||
type args struct {
|
||||
withScopeId string
|
||||
opt []Option
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
createCnt int
|
||||
createScopeId string
|
||||
args args
|
||||
wantCnt int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "no-limit",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: org.PublicId,
|
||||
opt: []Option{WithLimit(-1)},
|
||||
},
|
||||
wantCnt: repo.defaultLimit + 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "no-limit-proj-group",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: proj.PublicId,
|
||||
args: args{
|
||||
withScopeId: proj.PublicId,
|
||||
opt: []Option{WithLimit(-1)},
|
||||
},
|
||||
wantCnt: repo.defaultLimit + 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "default-limit",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: org.PublicId,
|
||||
},
|
||||
wantCnt: repo.defaultLimit,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "custom-limit",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: org.PublicId,
|
||||
opt: []Option{WithLimit(3)},
|
||||
},
|
||||
wantCnt: 3,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "bad-org",
|
||||
createCnt: 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: "bad-id",
|
||||
},
|
||||
wantCnt: 0,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(allocGroup()).Error)
|
||||
testGroups := []*Group{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
testGroups = append(testGroups, TestGroup(t, conn, tt.createScopeId))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(testGroups))
|
||||
got, err := repo.ListGroups(context.Background(), tt.args.withScopeId, tt.args.opt...)
|
||||
if tt.wantErr {
|
||||
require.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tt.wantCnt, len(got))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -114,3 +114,16 @@ func (r *Repository) DeleteRole(ctx context.Context, withPublicId string, opt ..
|
||||
}
|
||||
return rowsDeleted, nil
|
||||
}
|
||||
|
||||
// ListRoles in a scope and supports WithLimit option.
|
||||
func (r *Repository) ListRoles(ctx context.Context, withScopeId string, opt ...Option) ([]*Role, error) {
|
||||
if withScopeId == "" {
|
||||
return nil, fmt.Errorf("list roles: missing scope id %w", db.ErrInvalidParameter)
|
||||
}
|
||||
var roles []*Role
|
||||
err := r.list(ctx, &roles, "scope_id = ?", []interface{}{withScopeId}, opt...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list roles: %w", err)
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
@@ -539,3 +539,105 @@ func TestRepository_DeleteRole(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListRoles(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
||||
defer func() {
|
||||
err := cleanup()
|
||||
assert.NoError(t, err)
|
||||
err = conn.Close()
|
||||
assert.NoError(t, err)
|
||||
}()
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
|
||||
type args struct {
|
||||
withScopeId string
|
||||
opt []Option
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
createCnt int
|
||||
createScopeId string
|
||||
args args
|
||||
wantCnt int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "no-limit",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: org.PublicId,
|
||||
opt: []Option{WithLimit(-1)},
|
||||
},
|
||||
wantCnt: repo.defaultLimit + 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "no-limit-proj-group",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: proj.PublicId,
|
||||
args: args{
|
||||
withScopeId: proj.PublicId,
|
||||
opt: []Option{WithLimit(-1)},
|
||||
},
|
||||
wantCnt: repo.defaultLimit + 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "default-limit",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: org.PublicId,
|
||||
},
|
||||
wantCnt: repo.defaultLimit,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "custom-limit",
|
||||
createCnt: repo.defaultLimit + 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: org.PublicId,
|
||||
opt: []Option{WithLimit(3)},
|
||||
},
|
||||
wantCnt: 3,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "bad-org",
|
||||
createCnt: 1,
|
||||
createScopeId: org.PublicId,
|
||||
args: args{
|
||||
withScopeId: "bad-id",
|
||||
},
|
||||
wantCnt: 0,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(allocGroup()).Error)
|
||||
testRoles := []*Role{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
testRoles = append(testRoles, TestRole(t, conn, tt.createScopeId))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(testRoles))
|
||||
got, err := repo.ListRoles(context.Background(), tt.args.withScopeId, tt.args.opt...)
|
||||
if tt.wantErr {
|
||||
require.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tt.wantCnt, len(got))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user