@@ -102,11 +102,13 @@ func TestGroupSyncTable(t *testing.T) {
102102ids .ID ("sg-bar" ):false ,
103103ids .ID ("sg-baz" ):false ,
104104},
105- ExpectedGroups : []uuid.UUID {
106- ids .ID ("sg-foo" ),
107- ids .ID ("sg-foo-2" ),
108- ids .ID ("sg-bar" ),
109- ids .ID ("sg-baz" ),
105+ assertGroups :& orgGroupAssert {
106+ ExpectedGroups : []uuid.UUID {
107+ ids .ID ("sg-foo" ),
108+ ids .ID ("sg-foo-2" ),
109+ ids .ID ("sg-bar" ),
110+ ids .ID ("sg-baz" ),
111+ },
110112},
111113},
112114{
@@ -125,8 +127,10 @@ func TestGroupSyncTable(t *testing.T) {
125127ids .ID ("gg-foo" ):true ,
126128ids .ID ("gg-bar" ):false ,
127129},
128- ExpectedGroups : []uuid.UUID {
129- ids .ID ("gg-foo" ),
130+ assertGroups :& orgGroupAssert {
131+ ExpectedGroups : []uuid.UUID {
132+ ids .ID ("gg-foo" ),
133+ },
130134},
131135},
132136{
@@ -145,11 +149,13 @@ func TestGroupSyncTable(t *testing.T) {
145149ids .ID ("ng-bar-2" ):false ,
146150ids .ID ("ng-baz" ):false ,
147151},
148- ExpectedGroups : []uuid.UUID {
149- ids .ID ("ng-foo" ),
150- ids .ID ("ng-bar" ),
151- ids .ID ("ng-bar-2" ),
152- ids .ID ("ng-baz" ),
152+ assertGroups :& orgGroupAssert {
153+ ExpectedGroups : []uuid.UUID {
154+ ids .ID ("ng-foo" ),
155+ ids .ID ("ng-bar" ),
156+ ids .ID ("ng-bar-2" ),
157+ ids .ID ("ng-baz" ),
158+ },
153159},
154160},
155161{
@@ -160,9 +166,11 @@ func TestGroupSyncTable(t *testing.T) {
160166AutoCreateMissing :true ,
161167},
162168Groups :map [uuid.UUID ]bool {},
163- ExpectedGroupNames : []string {
164- "create-bar" ,
165- "create-baz" ,
169+ assertGroups :& orgGroupAssert {
170+ ExpectedGroupNames : []string {
171+ "create-bar" ,
172+ "create-baz" ,
173+ },
166174},
167175},
168176{
@@ -177,9 +185,11 @@ func TestGroupSyncTable(t *testing.T) {
177185"bar" :false ,
178186"goob" :true ,
179187},
180- ExpectedGroupNames : []string {
181- "foo" ,
182- "bar" ,
188+ assertGroups :& orgGroupAssert {
189+ ExpectedGroupNames : []string {
190+ "foo" ,
191+ "bar" ,
192+ },
183193},
184194},
185195{
@@ -200,9 +210,9 @@ func TestGroupSyncTable(t *testing.T) {
200210},
201211},
202212{
203- Name :"NoSettingsNoUser" ,
204- Settings :nil ,
205- Groups :map [uuid.UUID ]bool {},
213+ Name :"NoSettingsNoUser" ,
214+ GroupSettings :nil ,
215+ Groups :map [uuid.UUID ]bool {},
206216},
207217{
208218Name :"LegacyMapping" ,
@@ -224,9 +234,11 @@ func TestGroupSyncTable(t *testing.T) {
224234"extra" :true ,
225235"legacy-bop" :true ,
226236},
227- ExpectedGroupNames : []string {
228- "legacy-bar" ,
229- "legacy-foo" ,
237+ assertGroups :& orgGroupAssert {
238+ ExpectedGroupNames : []string {
239+ "legacy-bar" ,
240+ "legacy-foo" ,
241+ },
230242},
231243},
232244}
@@ -311,9 +323,10 @@ func TestGroupSyncTable(t *testing.T) {
311323"random" :true ,
312324},
313325// No settings, because they come from the deployment values
314- Settings :nil ,
315- ExpectedGroups :nil ,
316- ExpectedGroupNames : []string {"legacy-foo" ,"legacy-baz" ,"legacy-bar" },
326+ GroupSettings :nil ,
327+ assertGroups :& orgGroupAssert {
328+ ExpectedGroupNames : []string {"legacy-foo" ,"legacy-baz" ,"legacy-bar" },
329+ },
317330}
318331
319332//nolint:gocritic // testing
@@ -392,9 +405,11 @@ func TestSyncDisabled(t *testing.T) {
392405"baz" : {ids .ID ("baz" )},
393406},
394407},
395- ExpectedGroups : []uuid.UUID {
396- ids .ID ("foo" ),
397- ids .ID ("bar" ),
408+ assertGroups :& orgGroupAssert {
409+ ExpectedGroups : []uuid.UUID {
410+ ids .ID ("foo" ),
411+ ids .ID ("bar" ),
412+ },
398413},
399414}
400415
@@ -728,18 +743,27 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
728743}
729744
730745manager := runtimeconfig .NewManager ()
731- if def .Settings != nil {
732- orgResolver := manager .OrganizationResolver (db ,org .ID )
733- err = s .Group .SetRuntimeValue (context .Background (),orgResolver , (* idpsync .GroupSyncSettings )(def .Settings ))
734- require .NoError (t ,err )
735- }
746+ orgResolver := manager .OrganizationResolver (db ,org .ID )
747+ err = s .Group .SetRuntimeValue (context .Background (),orgResolver ,def .GroupSettings )
748+ require .NoError (t ,err )
749+
750+ err = s .Role .SetRuntimeValue (context .Background (),orgResolver ,def .RoleSettings )
751+ require .NoError (t ,err )
736752
737753if ! def .NotMember {
738754dbgen .OrganizationMember (t ,db , database.OrganizationMember {
739755UserID :user .ID ,
740756OrganizationID :org .ID ,
741757})
742758}
759+ if len (def .OrganizationRoles )> 0 {
760+ _ ,err := db .UpdateMemberRoles (context .Background (), database.UpdateMemberRolesParams {
761+ GrantedRoles :def .OrganizationRoles ,
762+ UserID :user .ID ,
763+ OrgID :org .ID ,
764+ })
765+ require .NoError (t ,err )
766+ }
743767for groupID ,in := range def .Groups {
744768dbgen .Group (t ,db , database.Group {
745769ID :groupID ,
@@ -771,9 +795,23 @@ type orgSetupDefinition struct {
771795// True if the user is a member of the group
772796Groups map [uuid.UUID ]bool
773797GroupNames map [string ]bool
774- NotMember bool
775798
776- Settings * codersdk.GroupSyncSettings
799+ OrganizationRoles []string
800+ // NotMember if true will ensure the user is not a member of the organization.
801+ NotMember bool
802+
803+ GroupSettings * idpsync.GroupSyncSettings
804+ RoleSettings * idpsync.RoleSyncSettings
805+
806+ assertGroups * orgGroupAssert
807+ assertRoles * orgRoleAssert
808+ }
809+
810+ type orgRoleAssert struct {
811+ ExpectedOrgRoles []string
812+ }
813+
814+ type orgGroupAssert struct {
777815ExpectedGroups []uuid.UUID
778816ExpectedGroupNames []string
779817}
@@ -794,6 +832,24 @@ func (o orgSetupDefinition) Assert(t *testing.T, orgID uuid.UUID, db database.St
794832require .Len (t ,members ,1 ,"should be a member" )
795833}
796834
835+ if o .assertGroups != nil {
836+ o .assertGroups .Assert (t ,orgID ,db ,user )
837+ }
838+ if o .assertRoles != nil {
839+ o .assertRoles .Assert (t ,orgID ,db ,o .NotMember ,user )
840+ }
841+
842+ if o .assertGroups == nil && o .assertRoles == nil {
843+ t .Errorf ("no group or role asserts present, must have at least one" )
844+ t .FailNow ()
845+ }
846+ }
847+
848+ func (o orgGroupAssert )Assert (t * testing.T ,orgID uuid.UUID ,db database.Store ,user database.User ) {
849+ t .Helper ()
850+
851+ ctx := context .Background ()
852+
797853userGroups ,err := db .GetGroups (ctx , database.GetGroupsParams {
798854OrganizationID :orgID ,
799855HasMemberID :user .ID ,
@@ -826,3 +882,22 @@ func (o orgSetupDefinition) Assert(t *testing.T, orgID uuid.UUID, db database.St
826882require .Len (t ,o .ExpectedGroupNames ,0 ,"ExpectedGroupNames should be empty" )
827883}
828884}
885+
886+ func (o orgRoleAssert )Assert (t * testing.T ,orgID uuid.UUID ,db database.Store ,notMember bool ,user database.User ) {
887+ t .Helper ()
888+
889+ ctx := context .Background ()
890+
891+ members ,err := db .OrganizationMembers (ctx , database.OrganizationMembersParams {
892+ OrganizationID :orgID ,
893+ UserID :user .ID ,
894+ })
895+ if notMember {
896+ require .ErrorIs (t ,err ,sql .ErrNoRows )
897+ return
898+ }
899+ require .NoError (t ,err )
900+ require .Len (t ,members ,1 )
901+ member := members [0 ]
902+ require .ElementsMatch (t ,member .OrganizationMember .Roles ,o .ExpectedOrgRoles )
903+ }