@@ -5240,6 +5240,79 @@ func TestDeleteWorkspaceACL(t *testing.T) {
52405240 })
52415241}
52425242
5243+ // nolint:tparallel,paralleltest // Subtests modify package global.
5244+ func TestWorkspaceSharingDisabled (t * testing.T ) {
5245+ t .Run ("CanAccessWhenEnabled" , func (t * testing.T ) {
5246+ var (
5247+ client , db = coderdtest .NewWithDatabase (t , & coderdtest.Options {
5248+ DeploymentValues : coderdtest .DeploymentValues (t , func (dv * codersdk.DeploymentValues ) {
5249+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceSharing )}
5250+ // DisableWorkspaceSharing is false (default)
5251+ }),
5252+ })
5253+ admin = coderdtest .CreateFirstUser (t , client )
5254+ _ , wsOwner = coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
5255+ userClient , user = coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
5256+ )
5257+
5258+ ctx := testutil .Context (t , testutil .WaitMedium )
5259+
5260+ // Create workspace with ACL granting access to user
5261+ ws := dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
5262+ OwnerID : wsOwner .ID ,
5263+ OrganizationID : admin .OrganizationID ,
5264+ UserACL : database.WorkspaceACL {
5265+ user .ID .String (): database.WorkspaceACLEntry {
5266+ Permissions : []policy.Action {
5267+ policy .ActionRead , policy .ActionSSH , policy .ActionApplicationConnect ,
5268+ },
5269+ },
5270+ },
5271+ }).Do ().Workspace
5272+
5273+ // User SHOULD be able to access workspace when sharing is enabled
5274+ fetchedWs , err := userClient .Workspace (ctx , ws .ID )
5275+ require .NoError (t , err )
5276+ require .Equal (t , ws .ID , fetchedWs .ID )
5277+ })
5278+
5279+ t .Run ("NoAccessWhenDisabled" , func (t * testing.T ) {
5280+ var (
5281+ client , db = coderdtest .NewWithDatabase (t , & coderdtest.Options {
5282+ DeploymentValues : coderdtest .DeploymentValues (t , func (dv * codersdk.DeploymentValues ) {
5283+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceSharing )}
5284+ dv .DisableWorkspaceSharing = true
5285+ }),
5286+ })
5287+ admin = coderdtest .CreateFirstUser (t , client )
5288+ _ , wsOwner = coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
5289+ userClient , user = coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
5290+ )
5291+
5292+ ctx := testutil .Context (t , testutil .WaitMedium )
5293+
5294+ // Create workspace with ACL granting access to user directly in DB
5295+ ws := dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
5296+ OwnerID : wsOwner .ID ,
5297+ OrganizationID : admin .OrganizationID ,
5298+ UserACL : database.WorkspaceACL {
5299+ user .ID .String (): database.WorkspaceACLEntry {
5300+ Permissions : []policy.Action {
5301+ policy .ActionRead , policy .ActionSSH , policy .ActionApplicationConnect ,
5302+ },
5303+ },
5304+ },
5305+ }).Do ().Workspace
5306+
5307+ // User should NOT be able to access workspace when sharing is disabled
5308+ _ , err := userClient .Workspace (ctx , ws .ID )
5309+ require .Error (t , err )
5310+ var sdkErr * codersdk.Error
5311+ require .ErrorAs (t , err , & sdkErr )
5312+ require .Equal (t , http .StatusNotFound , sdkErr .StatusCode ())
5313+ })
5314+ }
5315+
52435316func TestWorkspaceCreateWithImplicitPreset (t * testing.T ) {
52445317 t .Parallel ()
52455318
0 commit comments