From 99d34d6d7b9846cbf219d81a4da7efeb7a74ece9 Mon Sep 17 00:00:00 2001 From: Jim Lambert Date: Thu, 18 Jun 2020 15:25:39 -0400 Subject: [PATCH] add list groups --- internal/iam/repository_group.go | 13 ++++ internal/iam/repository_group_test.go | 102 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/internal/iam/repository_group.go b/internal/iam/repository_group.go index 8b60d7499d..58f4480ff9 100644 --- a/internal/iam/repository_group.go +++ b/internal/iam/repository_group.go @@ -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 +} diff --git a/internal/iam/repository_group_test.go b/internal/iam/repository_group_test.go index bc77bfd67b..75ae58df21 100644 --- a/internal/iam/repository_group_test.go +++ b/internal/iam/repository_group_test.go @@ -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)) + }) + } +}