@@ -23,7 +23,7 @@ import (
23
23
"github.com/coder/coder/v2/testutil"
24
24
)
25
25
26
- func TestSharingShareEnterprise (t * testing.T ) {
26
+ func TestSharingShare (t * testing.T ) {
27
27
t .Parallel ()
28
28
29
29
dv := coderdtest .DeploymentValues (t )
@@ -245,6 +245,157 @@ func TestSharingStatus(t *testing.T) {
245
245
})
246
246
}
247
247
248
+ func TestSharingRemove (t * testing.T ) {
249
+ t .Parallel ()
250
+
251
+ dv := coderdtest .DeploymentValues (t )
252
+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceSharing )}
253
+
254
+ t .Run ("RemoveSharedGroup_Single" ,func (t * testing.T ) {
255
+ t .Parallel ()
256
+
257
+ var (
258
+ client ,db ,orgOwner = coderdenttest .NewWithDatabase (t ,& coderdenttest.Options {
259
+ Options :& coderdtest.Options {
260
+ DeploymentValues :dv ,
261
+ },
262
+ LicenseOptions :& coderdenttest.LicenseOptions {
263
+ Features : license.Features {
264
+ codersdk .FeatureTemplateRBAC :1 ,
265
+ },
266
+ },
267
+ })
268
+ workspaceOwnerClient ,workspaceOwner = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID ,rbac .ScopedRoleOrgAuditor (orgOwner .OrganizationID ))
269
+ workspace = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
270
+ OwnerID :workspaceOwner .ID ,
271
+ OrganizationID :orgOwner .OrganizationID ,
272
+ }).Do ().Workspace
273
+ _ ,groupUser1 = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID )
274
+ _ ,groupUser2 = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID )
275
+ )
276
+
277
+ ctx := testutil .Context (t ,testutil .WaitMedium )
278
+
279
+ group1 ,err := createGroupWithMembers (ctx ,client ,orgOwner .OrganizationID ,"group-1" , []uuid.UUID {groupUser1 .ID ,groupUser2 .ID })
280
+ require .NoError (t ,err )
281
+
282
+ group2 ,err := createGroupWithMembers (ctx ,client ,orgOwner .OrganizationID ,"group-2" , []uuid.UUID {groupUser1 .ID ,groupUser2 .ID })
283
+ require .NoError (t ,err )
284
+
285
+ // Share the workspace with a user to later remove
286
+ err = client .UpdateWorkspaceACL (ctx ,workspace .ID , codersdk.UpdateWorkspaceACL {
287
+ GroupRoles :map [string ]codersdk.WorkspaceRole {
288
+ group1 .ID .String ():codersdk .WorkspaceRoleUse ,
289
+ group2 .ID .String ():codersdk .WorkspaceRoleUse ,
290
+ },
291
+ })
292
+ require .NoError (t ,err )
293
+
294
+ inv ,root := clitest .New (t ,
295
+ "sharing" ,
296
+ "remove" ,
297
+ workspace .Name ,
298
+ "--org" ,orgOwner .OrganizationID .String (),
299
+ "--group" ,group1 .Name ,
300
+ )
301
+ clitest .SetupConfig (t ,workspaceOwnerClient ,root )
302
+
303
+ err = inv .WithContext (ctx ).Run ()
304
+ require .NoError (t ,err )
305
+
306
+ acl ,err := workspaceOwnerClient .WorkspaceACL (inv .Context (),workspace .ID )
307
+ require .NoError (t ,err )
308
+
309
+ removedGroup1 := true
310
+ removedGroup2 := true
311
+ for _ ,group := range acl .Groups {
312
+ if group .ID == group1 .ID {
313
+ removedGroup1 = false
314
+ continue
315
+ }
316
+
317
+ if group .ID == group2 .ID {
318
+ removedGroup2 = false
319
+ continue
320
+ }
321
+ }
322
+ assert .True (t ,removedGroup1 )
323
+ assert .False (t ,removedGroup2 )
324
+ })
325
+
326
+ t .Run ("RemoveSharedGroup_Multiple" ,func (t * testing.T ) {
327
+ t .Parallel ()
328
+
329
+ var (
330
+ client ,db ,orgOwner = coderdenttest .NewWithDatabase (t ,& coderdenttest.Options {
331
+ Options :& coderdtest.Options {
332
+ DeploymentValues :dv ,
333
+ },
334
+ LicenseOptions :& coderdenttest.LicenseOptions {
335
+ Features : license.Features {
336
+ codersdk .FeatureTemplateRBAC :1 ,
337
+ },
338
+ },
339
+ })
340
+ workspaceOwnerClient ,workspaceOwner = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID ,rbac .ScopedRoleOrgAuditor (orgOwner .OrganizationID ))
341
+ workspace = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
342
+ OwnerID :workspaceOwner .ID ,
343
+ OrganizationID :orgOwner .OrganizationID ,
344
+ }).Do ().Workspace
345
+ _ ,groupUser1 = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID )
346
+ _ ,groupUser2 = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID )
347
+ )
348
+
349
+ ctx := testutil .Context (t ,testutil .WaitMedium )
350
+
351
+ group1 ,err := createGroupWithMembers (ctx ,client ,orgOwner .OrganizationID ,"group-1" , []uuid.UUID {groupUser1 .ID ,groupUser2 .ID })
352
+ require .NoError (t ,err )
353
+
354
+ group2 ,err := createGroupWithMembers (ctx ,client ,orgOwner .OrganizationID ,"group-2" , []uuid.UUID {groupUser1 .ID ,groupUser2 .ID })
355
+ require .NoError (t ,err )
356
+
357
+ // Share the workspace with a user to later remove
358
+ err = client .UpdateWorkspaceACL (ctx ,workspace .ID , codersdk.UpdateWorkspaceACL {
359
+ GroupRoles :map [string ]codersdk.WorkspaceRole {
360
+ group1 .ID .String ():codersdk .WorkspaceRoleUse ,
361
+ group2 .ID .String ():codersdk .WorkspaceRoleUse ,
362
+ },
363
+ })
364
+ require .NoError (t ,err )
365
+
366
+ inv ,root := clitest .New (t ,
367
+ "sharing" ,
368
+ "remove" ,
369
+ workspace .Name ,
370
+ "--org" ,orgOwner .OrganizationID .String (),
371
+ fmt .Sprintf ("--group=%s,%s" ,group1 .Name ,group2 .Name ),
372
+ )
373
+ clitest .SetupConfig (t ,workspaceOwnerClient ,root )
374
+
375
+ err = inv .WithContext (ctx ).Run ()
376
+ require .NoError (t ,err )
377
+
378
+ acl ,err := workspaceOwnerClient .WorkspaceACL (inv .Context (),workspace .ID )
379
+ require .NoError (t ,err )
380
+
381
+ removedGroup1 := true
382
+ removedGroup2 := true
383
+ for _ ,group := range acl .Groups {
384
+ if group .ID == group1 .ID {
385
+ removedGroup1 = false
386
+ continue
387
+ }
388
+
389
+ if group .ID == group2 .ID {
390
+ removedGroup2 = false
391
+ continue
392
+ }
393
+ }
394
+ assert .True (t ,removedGroup1 )
395
+ assert .True (t ,removedGroup2 )
396
+ })
397
+ }
398
+
248
399
func createGroupWithMembers (ctx context.Context ,client * codersdk.Client ,orgID uuid.UUID ,name string ,memberIDs []uuid.UUID ) (codersdk.Group ,error ) {
249
400
group ,err := client .CreateGroup (ctx ,orgID , codersdk.CreateGroupRequest {
250
401
Name :name ,