Skip to content

Commit bfcc771

Browse files
committed
test
1 parent 73a77ed commit bfcc771

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

coderd/users_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,43 @@ func TestDeleteUser(t *testing.T) {
377377
require.ErrorAs(t, err, &apiErr, "should be a coderd error")
378378
require.Equal(t, http.StatusForbidden, apiErr.StatusCode(), "should be forbidden")
379379
})
380+
t.Run("CountCheckIncludesAllWorkspaces", func(t *testing.T) {
381+
t.Parallel()
382+
client, _ := coderdtest.NewWithProvisionerCloser(t, nil)
383+
firstUser := coderdtest.CreateFirstUser(t, client)
384+
385+
// Create a target user who will own a workspace
386+
targetUserClient, targetUser := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID)
387+
388+
// Create a User Admin who should not have permission to see the target user's workspace
389+
userAdminClient, userAdmin := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID)
390+
391+
// Grant User Admin role to the userAdmin
392+
userAdmin, err := client.UpdateUserRoles(context.Background(), userAdmin.ID.String(), codersdk.UpdateRoles{
393+
Roles: []string{rbac.RoleUserAdmin().String()},
394+
})
395+
require.NoError(t, err)
396+
397+
// Create a template and workspace owned by the target user
398+
version := coderdtest.CreateTemplateVersion(t, client, firstUser.OrganizationID, nil)
399+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
400+
template := coderdtest.CreateTemplate(t, client, firstUser.OrganizationID, version.ID)
401+
_ = coderdtest.CreateWorkspace(t, targetUserClient, template.ID)
402+
403+
workspaces, err := userAdminClient.Workspaces(context.Background(), codersdk.WorkspaceFilter{
404+
Owner: targetUser.Username,
405+
})
406+
require.NoError(t, err)
407+
require.Len(t, workspaces.Workspaces, 0)
408+
409+
// Attempt to delete the target user - this should fail because the
410+
// user has a workspace not visible to the deleting user.
411+
err = userAdminClient.DeleteUser(context.Background(), targetUser.ID)
412+
var apiErr *codersdk.Error
413+
require.ErrorAs(t, err, &apiErr)
414+
require.Equal(t, http.StatusExpectationFailed, apiErr.StatusCode())
415+
require.Contains(t, apiErr.Message, "has workspaces")
416+
})
380417
}
381418

382419
func TestNotifyUserStatusChanged(t *testing.T) {

0 commit comments

Comments
 (0)