Skip to content

Commit ea6d61b

Browse files
committed
WIP unit test for group sync
1 parent 7d4977e commit ea6d61b

File tree

7 files changed

+421
-18
lines changed

7 files changed

+421
-18
lines changed

coderd/coderdtest/uuids.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package coderdtest
2+
3+
import "github.com/google/uuid"
4+
5+
type DeterministicUUIDGenerator struct {
6+
Named map[string]uuid.UUID
7+
}
8+
9+
func NewDeterministicUUIDGenerator() *DeterministicUUIDGenerator {
10+
return &DeterministicUUIDGenerator{
11+
Named: make(map[string]uuid.UUID),
12+
}
13+
}
14+
15+
func (d *DeterministicUUIDGenerator) ID(name string) uuid.UUID {
16+
if v, ok := d.Named[name]; ok {
17+
return v
18+
}
19+
d.Named[name] = uuid.New()
20+
return d.Named[name]
21+
}

coderd/database/dbmem/dbmem.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -7643,7 +7643,22 @@ func (q *FakeQuerier) RemoveUserFromGroups(ctx context.Context, arg database.Rem
76437643
return nil, err
76447644
}
76457645

7646-
panic("not implemented")
7646+
q.mutex.Lock()
7647+
defer q.mutex.Unlock()
7648+
7649+
removed := make([]uuid.UUID, 0)
7650+
q.data.groupMembers = slices.DeleteFunc(q.data.groupMembers, func(groupMember database.GroupMemberTable) bool {
7651+
if groupMember.UserID != arg.UserID {
7652+
return false
7653+
}
7654+
if !slices.Contains(arg.GroupIds, groupMember.GroupID) {
7655+
return false
7656+
}
7657+
removed = append(removed, groupMember.GroupID)
7658+
return true
7659+
})
7660+
7661+
return removed, nil
76477662
}
76487663

76497664
func (q *FakeQuerier) RevokeDBCryptKey(_ context.Context, activeKeyDigest string) error {

coderd/idpsync/group.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package idpsync
22

33
import (
44
"context"
5+
"encoding/json"
56
"regexp"
67

78
"github.com/golang-jwt/jwt/v4"
@@ -12,6 +13,7 @@ import (
1213
"github.com/coder/coder/v2/coderd/database"
1314
"github.com/coder/coder/v2/coderd/database/db2sdk"
1415
"github.com/coder/coder/v2/coderd/database/dbauthz"
16+
"github.com/coder/coder/v2/coderd/runtimeconfig"
1517
"github.com/coder/coder/v2/coderd/util/slice"
1618
)
1719

@@ -32,7 +34,6 @@ func (s AGPLIDPSync) ParseGroupClaims(_ context.Context, _ jwt.MapClaims) (Group
3234
}, nil
3335
}
3436

35-
// TODO: Group allowlist behavior should probably happen at this step.
3637
func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user database.User, params GroupParams) error {
3738
// Nothing happens if sync is not enabled
3839
if !params.SyncEnabled {
@@ -43,6 +44,8 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
4344
ctx = dbauthz.AsSystemRestricted(ctx)
4445

4546
db.InTx(func(tx database.Store) error {
47+
manager := runtimeconfig.NewStoreManager(tx)
48+
4649
userGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{
4750
HasMemberID: user.ID,
4851
})
@@ -60,12 +63,12 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
6063
// For each org, we need to fetch the sync settings
6164
orgSettings := make(map[uuid.UUID]GroupSyncSettings)
6265
for orgID := range userOrgs {
63-
orgResolver := s.Manager.Scoped(orgID.String())
66+
orgResolver := manager.Scoped(orgID.String())
6467
settings, err := s.SyncSettings.Group.Resolve(ctx, orgResolver)
6568
if err != nil {
6669
return xerrors.Errorf("resolve group sync settings: %w", err)
6770
}
68-
orgSettings[orgID] = settings.Value
71+
orgSettings[orgID] = *settings
6972
}
7073

7174
// collect all diffs to do 1 sql update for all orgs
@@ -177,6 +180,20 @@ type GroupSyncSettings struct {
177180
AutoCreateMissingGroups bool `json:"auto_create_missing_groups"`
178181
}
179182

183+
func (s *GroupSyncSettings) Set(v string) error {
184+
return json.Unmarshal([]byte(v), s)
185+
}
186+
func (s *GroupSyncSettings) String() string {
187+
v, err := json.Marshal(s)
188+
if err != nil {
189+
return "decode failed: " + err.Error()
190+
}
191+
return string(v)
192+
}
193+
func (s *GroupSyncSettings) Type() string {
194+
return "GroupSyncSettings"
195+
}
196+
180197
type ExpectedGroup struct {
181198
GroupID *uuid.UUID
182199
GroupName *string

0 commit comments

Comments
 (0)