(t *testing.T)
| 431 | } |
| 432 | |
| 433 | func TestDeleteUser(t *testing.T) { |
| 434 | t.Parallel() |
| 435 | t.Run("Works", func(t *testing.T) { |
| 436 | t.Parallel() |
| 437 | client, _, api := coderdtest.NewWithAPI(t, nil) |
| 438 | user := coderdtest.CreateFirstUser(t, client) |
| 439 | authz := coderdtest.AssertRBAC(t, api, client) |
| 440 | |
| 441 | anotherClient, another := coderdtest.CreateAnotherUser(t, client, user.OrganizationID) |
| 442 | err := client.DeleteUser(context.Background(), another.ID) |
| 443 | require.NoError(t, err) |
| 444 | // Attempt to create a user with the same email and username, and delete them again. |
| 445 | another, err = client.CreateUserWithOrgs(context.Background(), codersdk.CreateUserRequestWithOrgs{ |
| 446 | Email: another.Email, |
| 447 | Username: another.Username, |
| 448 | Password: "SomeSecurePassword!", |
| 449 | OrganizationIDs: []uuid.UUID{user.OrganizationID}, |
| 450 | }) |
| 451 | require.NoError(t, err) |
| 452 | err = client.DeleteUser(context.Background(), another.ID) |
| 453 | require.NoError(t, err) |
| 454 | |
| 455 | // IMPORTANT: assert that the deleted user's session is no longer valid. |
| 456 | _, err = anotherClient.User(context.Background(), codersdk.Me) |
| 457 | require.Error(t, err) |
| 458 | var apiErr *codersdk.Error |
| 459 | require.ErrorAs(t, err, &apiErr) |
| 460 | require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode()) |
| 461 | |
| 462 | // RBAC checks |
| 463 | authz.AssertChecked(t, policy.ActionCreate, rbac.ResourceUser) |
| 464 | authz.AssertChecked(t, policy.ActionDelete, another) |
| 465 | }) |
| 466 | t.Run("NoPermission", func(t *testing.T) { |
| 467 | t.Parallel() |
| 468 | api := coderdtest.New(t, nil) |
| 469 | firstUser := coderdtest.CreateFirstUser(t, api) |
| 470 | client, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID) |
| 471 | err := client.DeleteUser(context.Background(), firstUser.UserID) |
| 472 | var apiErr *codersdk.Error |
| 473 | require.ErrorAs(t, err, &apiErr) |
| 474 | require.Equal(t, http.StatusNotFound, apiErr.StatusCode()) |
| 475 | }) |
| 476 | t.Run("HasWorkspaces", func(t *testing.T) { |
| 477 | t.Parallel() |
| 478 | client, _ := coderdtest.NewWithProvisionerCloser(t, nil) |
| 479 | user := coderdtest.CreateFirstUser(t, client) |
| 480 | anotherClient, another := coderdtest.CreateAnotherUser(t, client, user.OrganizationID) |
| 481 | version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) |
| 482 | coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) |
| 483 | template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) |
| 484 | coderdtest.CreateWorkspace(t, anotherClient, template.ID) |
| 485 | err := client.DeleteUser(context.Background(), another.ID) |
| 486 | var apiErr *codersdk.Error |
| 487 | require.ErrorAs(t, err, &apiErr) |
| 488 | require.Equal(t, http.StatusExpectationFailed, apiErr.StatusCode()) |
| 489 | }) |
| 490 | t.Run("Self", func(t *testing.T) { |
nothing calls this directly
no test coverage detected