@@ -3575,6 +3575,154 @@ func TestWorkspacesFiltering(t *testing.T) {
3575
3575
}
3576
3576
}
3577
3577
})
3578
+
3579
+ t .Run ("SharedWithGroup" ,func (t * testing.T ) {
3580
+ t .Parallel ()
3581
+
3582
+ dv := coderdtest .DeploymentValues (t )
3583
+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceSharing )}
3584
+
3585
+ var (
3586
+ client ,db ,orgOwner = coderdenttest .NewWithDatabase (t ,& coderdenttest.Options {
3587
+ Options :& coderdtest.Options {
3588
+ DeploymentValues :dv ,
3589
+ },
3590
+ LicenseOptions :& coderdenttest.LicenseOptions {
3591
+ Features : license.Features {
3592
+ codersdk .FeatureTemplateRBAC :1 ,
3593
+ },
3594
+ },
3595
+ })
3596
+ _ ,workspaceOwner = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID ,rbac .ScopedRoleOrgAuditor (orgOwner .OrganizationID ))
3597
+ sharedWorkspace = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
3598
+ OwnerID :workspaceOwner .ID ,
3599
+ OrganizationID :orgOwner .OrganizationID ,
3600
+ }).Do ().Workspace
3601
+ _ = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
3602
+ OwnerID :workspaceOwner .ID ,
3603
+ OrganizationID :orgOwner .OrganizationID ,
3604
+ }).Do ().Workspace
3605
+ ctx = testutil .Context (t ,testutil .WaitMedium )
3606
+ )
3607
+
3608
+ group ,err := client .CreateGroup (ctx ,orgOwner .OrganizationID , codersdk.CreateGroupRequest {
3609
+ Name :"wibble" ,
3610
+ })
3611
+ require .NoError (t ,err ,"create group" )
3612
+
3613
+ client .UpdateWorkspaceACL (ctx ,sharedWorkspace .ID , codersdk.UpdateWorkspaceACL {
3614
+ GroupRoles :map [string ]codersdk.WorkspaceRole {
3615
+ group .ID .String ():codersdk .WorkspaceRoleUse ,
3616
+ },
3617
+ })
3618
+
3619
+ workspaces ,err := client .Workspaces (ctx , codersdk.WorkspaceFilter {
3620
+ Shared :true ,
3621
+ })
3622
+ require .NoError (t ,err ,"fetch workspaces" )
3623
+ require .Equal (t ,workspaces .Count ,1 ,"expected only one workspace" )
3624
+ require .Equal (t ,workspaces .Workspaces [0 ].ID ,sharedWorkspace .ID )
3625
+ })
3626
+
3627
+ t .Run ("SharedWithUserAndGroup" ,func (t * testing.T ) {
3628
+ t .Parallel ()
3629
+
3630
+ dv := coderdtest .DeploymentValues (t )
3631
+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceSharing )}
3632
+
3633
+ var (
3634
+ client ,db ,orgOwner = coderdenttest .NewWithDatabase (t ,& coderdenttest.Options {
3635
+ Options :& coderdtest.Options {
3636
+ DeploymentValues :dv ,
3637
+ },
3638
+ LicenseOptions :& coderdenttest.LicenseOptions {
3639
+ Features : license.Features {
3640
+ codersdk .FeatureTemplateRBAC :1 ,
3641
+ },
3642
+ },
3643
+ })
3644
+ _ ,workspaceOwner = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID ,rbac .ScopedRoleOrgAuditor (orgOwner .OrganizationID ))
3645
+ sharedWorkspace = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
3646
+ OwnerID :workspaceOwner .ID ,
3647
+ OrganizationID :orgOwner .OrganizationID ,
3648
+ }).Do ().Workspace
3649
+ _ = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
3650
+ OwnerID :workspaceOwner .ID ,
3651
+ OrganizationID :orgOwner .OrganizationID ,
3652
+ }).Do ().Workspace
3653
+ _ ,toShareWithUser = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID )
3654
+ ctx = testutil .Context (t ,testutil .WaitMedium )
3655
+ )
3656
+
3657
+ group ,err := client .CreateGroup (ctx ,orgOwner .OrganizationID , codersdk.CreateGroupRequest {
3658
+ Name :"wibble" ,
3659
+ })
3660
+ require .NoError (t ,err ,"create group" )
3661
+
3662
+ client .UpdateWorkspaceACL (ctx ,sharedWorkspace .ID , codersdk.UpdateWorkspaceACL {
3663
+ UserRoles :map [string ]codersdk.WorkspaceRole {
3664
+ toShareWithUser .ID .String ():codersdk .WorkspaceRoleUse ,
3665
+ },
3666
+ GroupRoles :map [string ]codersdk.WorkspaceRole {
3667
+ group .ID .String ():codersdk .WorkspaceRoleUse ,
3668
+ },
3669
+ })
3670
+
3671
+ workspaces ,err := client .Workspaces (ctx , codersdk.WorkspaceFilter {
3672
+ Shared :true ,
3673
+ })
3674
+ require .NoError (t ,err ,"fetch workspaces" )
3675
+ require .Equal (t ,workspaces .Count ,1 ,"expected only one workspace" )
3676
+ require .Equal (t ,workspaces .Workspaces [0 ].ID ,sharedWorkspace .ID )
3677
+ })
3678
+
3679
+ t .Run ("NotSharedWithGroup" ,func (t * testing.T ) {
3680
+ t .Parallel ()
3681
+
3682
+ dv := coderdtest .DeploymentValues (t )
3683
+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceSharing )}
3684
+
3685
+ var (
3686
+ client ,db ,orgOwner = coderdenttest .NewWithDatabase (t ,& coderdenttest.Options {
3687
+ Options :& coderdtest.Options {
3688
+ DeploymentValues :dv ,
3689
+ },
3690
+ LicenseOptions :& coderdenttest.LicenseOptions {
3691
+ Features : license.Features {
3692
+ codersdk .FeatureTemplateRBAC :1 ,
3693
+ },
3694
+ },
3695
+ })
3696
+ _ ,workspaceOwner = coderdtest .CreateAnotherUser (t ,client ,orgOwner .OrganizationID ,rbac .ScopedRoleOrgAuditor (orgOwner .OrganizationID ))
3697
+ sharedWorkspace = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
3698
+ OwnerID :workspaceOwner .ID ,
3699
+ OrganizationID :orgOwner .OrganizationID ,
3700
+ }).Do ().Workspace
3701
+ notSharedWorkspace = dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
3702
+ OwnerID :workspaceOwner .ID ,
3703
+ OrganizationID :orgOwner .OrganizationID ,
3704
+ }).Do ().Workspace
3705
+ ctx = testutil .Context (t ,testutil .WaitMedium )
3706
+ )
3707
+
3708
+ group ,err := client .CreateGroup (ctx ,orgOwner .OrganizationID , codersdk.CreateGroupRequest {
3709
+ Name :"wibble" ,
3710
+ })
3711
+ require .NoError (t ,err ,"create group" )
3712
+
3713
+ client .UpdateWorkspaceACL (ctx ,sharedWorkspace .ID , codersdk.UpdateWorkspaceACL {
3714
+ GroupRoles :map [string ]codersdk.WorkspaceRole {
3715
+ group .ID .String ():codersdk .WorkspaceRoleUse ,
3716
+ },
3717
+ })
3718
+
3719
+ workspaces ,err := client .Workspaces (ctx , codersdk.WorkspaceFilter {
3720
+ Shared :false ,
3721
+ })
3722
+ require .NoError (t ,err ,"fetch workspaces" )
3723
+ require .Equal (t ,workspaces .Count ,1 ,"expected only one workspace" )
3724
+ require .Equal (t ,workspaces .Workspaces [0 ].ID ,notSharedWorkspace .ID )
3725
+ })
3578
3726
}
3579
3727
3580
3728
// TestWorkspacesWithoutTemplatePerms creates a workspace for a user, then drops