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