- Notifications
You must be signed in to change notification settings - Fork1k
feat: add shared filter to workspaces query#19807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
8b0972f
060560d
61c3aee
dd758d3
127e7e5
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1812,6 +1812,82 @@ func TestWorkspaceFilter(t *testing.T) { | ||
require.ElementsMatch(t, exp, workspaces, "expected workspaces returned") | ||
}) | ||
} | ||
t.Run("SharedWithUser", func(t *testing.T) { | ||
t.Parallel() | ||
dv := coderdtest.DeploymentValues(t) | ||
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)} | ||
var ( | ||
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{ | ||
DeploymentValues: dv, | ||
}) | ||
orgOwner = coderdtest.CreateFirstUser(t, client) | ||
_, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID)) | ||
sharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
_ = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
_, toShareWithUser = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID) | ||
ctx = testutil.Context(t, testutil.WaitMedium) | ||
) | ||
client.UpdateWorkspaceACL(ctx, sharedWorkspace.ID, codersdk.UpdateWorkspaceACL{ | ||
UserRoles: map[string]codersdk.WorkspaceRole{ | ||
toShareWithUser.ID.String(): codersdk.WorkspaceRoleUse, | ||
}, | ||
}) | ||
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{ | ||
Shared: ptr.Ref(true), | ||
}) | ||
require.NoError(t, err, "fetch workspaces") | ||
require.Equal(t, 1, workspaces.Count, "expected only one workspace") | ||
Comment on lines +1846 to +1850 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. since there's only one workspace here, could we also run this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I am creating two workspaces if you look at the setup | ||
require.Equal(t, workspaces.Workspaces[0].ID, sharedWorkspace.ID) | ||
}) | ||
t.Run("NotSharedWithUser", func(t *testing.T) { | ||
t.Parallel() | ||
dv := coderdtest.DeploymentValues(t) | ||
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)} | ||
var ( | ||
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{ | ||
DeploymentValues: dv, | ||
}) | ||
orgOwner = coderdtest.CreateFirstUser(t, client) | ||
_, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID)) | ||
sharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
notSharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
_, toShareWithUser = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID) | ||
ctx = testutil.Context(t, testutil.WaitMedium) | ||
) | ||
client.UpdateWorkspaceACL(ctx, sharedWorkspace.ID, codersdk.UpdateWorkspaceACL{ | ||
UserRoles: map[string]codersdk.WorkspaceRole{ | ||
toShareWithUser.ID.String(): codersdk.WorkspaceRoleUse, | ||
}, | ||
}) | ||
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{ | ||
Shared: ptr.Ref(false), | ||
}) | ||
require.NoError(t, err, "fetch workspaces") | ||
require.Equal(t, 1, workspaces.Count, "expected only one workspace") | ||
require.Equal(t, workspaces.Workspaces[0].ID, notSharedWorkspace.ID) | ||
}) | ||
} | ||
// TestWorkspaceFilterManual runs some specific setups with basic checks. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -516,6 +516,8 @@ type WorkspaceFilter struct { | ||
Offset int `json:"offset,omitempty" typescript:"-"` | ||
// Limit is a limit on the number of workspaces returned. | ||
Limit int `json:"limit,omitempty" typescript:"-"` | ||
// Shared is a whether the workspace is shared with any users or groups | ||
Shared *bool `json:"shared,omitempty" typescript:"-"` | ||
// FilterQuery supports a raw filter query string | ||
FilterQuery string `json:"q,omitempty"` | ||
Comment on lines 521 to 522 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. nit: I think this being last was intentional since it doesn't quite "fit in", and that should be preserved | ||
} | ||
@@ -539,6 +541,9 @@ func (f WorkspaceFilter) asRequestOption() RequestOption { | ||
if f.Status != "" { | ||
params = append(params, fmt.Sprintf("status:%q", f.Status)) | ||
} | ||
if f.Shared != nil { | ||
params = append(params, fmt.Sprintf("shared:%v", *f.Shared)) | ||
} | ||
if f.FilterQuery != "" { | ||
// If custom stuff is added, just add it on here. | ||
params = append(params, f.FilterQuery) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3575,6 +3575,154 @@ func TestWorkspacesFiltering(t *testing.T) { | ||
} | ||
} | ||
}) | ||
t.Run("SharedWithGroup", func(t *testing.T) { | ||
t.Parallel() | ||
dv := coderdtest.DeploymentValues(t) | ||
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)} | ||
var ( | ||
client, db, orgOwner = coderdenttest.NewWithDatabase(t, &coderdenttest.Options{ | ||
Options: &coderdtest.Options{ | ||
DeploymentValues: dv, | ||
}, | ||
LicenseOptions: &coderdenttest.LicenseOptions{ | ||
Features: license.Features{ | ||
codersdk.FeatureTemplateRBAC: 1, | ||
}, | ||
}, | ||
}) | ||
_, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID)) | ||
sharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
_ = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
ctx = testutil.Context(t, testutil.WaitMedium) | ||
) | ||
group, err := client.CreateGroup(ctx, orgOwner.OrganizationID, codersdk.CreateGroupRequest{ | ||
Name: "wibble", | ||
}) | ||
require.NoError(t, err, "create group") | ||
client.UpdateWorkspaceACL(ctx, sharedWorkspace.ID, codersdk.UpdateWorkspaceACL{ | ||
GroupRoles: map[string]codersdk.WorkspaceRole{ | ||
group.ID.String(): codersdk.WorkspaceRoleUse, | ||
}, | ||
}) | ||
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{ | ||
Shared: ptr.Ref(true), | ||
}) | ||
require.NoError(t, err, "fetch workspaces") | ||
require.Equal(t, 1, workspaces.Count, "expected only one workspace") | ||
require.Equal(t, workspaces.Workspaces[0].ID, sharedWorkspace.ID) | ||
Comment on lines +3619 to +3624 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. and perhaps the same thing here for the groups case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Same as above, a second workspace is being created but not shared | ||
}) | ||
t.Run("SharedWithUserAndGroup", func(t *testing.T) { | ||
t.Parallel() | ||
dv := coderdtest.DeploymentValues(t) | ||
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)} | ||
var ( | ||
client, db, orgOwner = coderdenttest.NewWithDatabase(t, &coderdenttest.Options{ | ||
Options: &coderdtest.Options{ | ||
DeploymentValues: dv, | ||
}, | ||
LicenseOptions: &coderdenttest.LicenseOptions{ | ||
Features: license.Features{ | ||
codersdk.FeatureTemplateRBAC: 1, | ||
}, | ||
}, | ||
}) | ||
_, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID)) | ||
sharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
_ = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
_, toShareWithUser = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID) | ||
ctx = testutil.Context(t, testutil.WaitMedium) | ||
) | ||
group, err := client.CreateGroup(ctx, orgOwner.OrganizationID, codersdk.CreateGroupRequest{ | ||
Name: "wibble", | ||
}) | ||
require.NoError(t, err, "create group") | ||
client.UpdateWorkspaceACL(ctx, sharedWorkspace.ID, codersdk.UpdateWorkspaceACL{ | ||
UserRoles: map[string]codersdk.WorkspaceRole{ | ||
toShareWithUser.ID.String(): codersdk.WorkspaceRoleUse, | ||
}, | ||
GroupRoles: map[string]codersdk.WorkspaceRole{ | ||
group.ID.String(): codersdk.WorkspaceRoleUse, | ||
}, | ||
}) | ||
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{ | ||
Shared: ptr.Ref(true), | ||
}) | ||
require.NoError(t, err, "fetch workspaces") | ||
require.Equal(t, 1, workspaces.Count, "expected only one workspace") | ||
require.Equal(t, workspaces.Workspaces[0].ID, sharedWorkspace.ID) | ||
}) | ||
t.Run("NotSharedWithGroup", func(t *testing.T) { | ||
t.Parallel() | ||
dv := coderdtest.DeploymentValues(t) | ||
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)} | ||
var ( | ||
client, db, orgOwner = coderdenttest.NewWithDatabase(t, &coderdenttest.Options{ | ||
Options: &coderdtest.Options{ | ||
DeploymentValues: dv, | ||
}, | ||
LicenseOptions: &coderdenttest.LicenseOptions{ | ||
Features: license.Features{ | ||
codersdk.FeatureTemplateRBAC: 1, | ||
}, | ||
}, | ||
}) | ||
_, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID)) | ||
sharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
notSharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ | ||
OwnerID: workspaceOwner.ID, | ||
OrganizationID: orgOwner.OrganizationID, | ||
}).Do().Workspace | ||
ctx = testutil.Context(t, testutil.WaitMedium) | ||
) | ||
group, err := client.CreateGroup(ctx, orgOwner.OrganizationID, codersdk.CreateGroupRequest{ | ||
Name: "wibble", | ||
}) | ||
require.NoError(t, err, "create group") | ||
client.UpdateWorkspaceACL(ctx, sharedWorkspace.ID, codersdk.UpdateWorkspaceACL{ | ||
GroupRoles: map[string]codersdk.WorkspaceRole{ | ||
group.ID.String(): codersdk.WorkspaceRoleUse, | ||
}, | ||
}) | ||
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{ | ||
Shared: ptr.Ref(false), | ||
}) | ||
require.NoError(t, err, "fetch workspaces") | ||
require.Equal(t, 1, workspaces.Count, "expected only one workspace") | ||
require.Equal(t, workspaces.Workspaces[0].ID, notSharedWorkspace.ID) | ||
}) | ||
} | ||
// TestWorkspacesWithoutTemplatePerms creates a workspace for a user, then drops | ||
Uh oh!
There was an error while loading.Please reload this page.