Files
coder/cli/userstatus_test.go
T
Nick Vigilante dde500a646 feat: add --yes to coder users suspend and activate (#30155)
`coder users suspend` and `coder users activate` always prompt but,
unlike other commands with a confirmation prompt, had no `-y/--yes`.

Both now accept `-y`.
The suspend help also says that suspending stops the user's running
workspaces and that activating doesn't restart them.

**Behavior change:** Additive.
New `-y` flag; without it, both commands prompt as before.

<details>
<summary>Decisions</summary>

- The workspace note is based on the lifecycle executor, which
auto-stops a suspended user's running workspaces; activation doesn't
restart them.

</details>

Closes [CLI-23](https://linear.app/codercom/issue/CLI-23)

_This PR was generated by Coder Agents on behalf of @nickvigilante._
2026-09-30 12:38:14 -04:00

102 lines
3.4 KiB
Go

package cli_test
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
)
func TestUserStatus(t *testing.T) {
t.Parallel()
t.Run("StatusSelf", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
inv, root := clitest.New(t, "users", "suspend", "me")
clitest.SetupConfig(t, client, root)
// Yes to the prompt
inv.Stdin = bytes.NewReader([]byte("yes\n"))
err := inv.Run()
// Expect an error, as you cannot suspend yourself
require.Error(t, err)
require.ErrorContains(t, err, "cannot suspend yourself")
})
t.Run("StatusOther", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
owner := coderdtest.CreateFirstUser(t, client)
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
other, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
otherUser, err := other.User(context.Background(), codersdk.Me)
require.NoError(t, err, "fetch user")
inv, root := clitest.New(t, "users", "suspend", otherUser.Username)
clitest.SetupConfig(t, userAdmin, root)
// Yes to the prompt
inv.Stdin = bytes.NewReader([]byte("yes\n"))
err = inv.Run()
require.NoError(t, err, "suspend user")
// Check the user status
otherUser, err = client.User(context.Background(), otherUser.Username)
require.NoError(t, err, "fetch suspended user")
require.Equal(t, codersdk.UserStatusSuspended, otherUser.Status, "suspended user")
// Set back to active. Try using a uuid as well
inv, root = clitest.New(t, "users", "activate", otherUser.ID.String())
clitest.SetupConfig(t, userAdmin, root)
// Yes to the prompt
inv.Stdin = bytes.NewReader([]byte("yes\n"))
err = inv.Run()
require.NoError(t, err, "suspend user")
// Check the user status
otherUser, err = client.User(context.Background(), otherUser.ID.String())
require.NoError(t, err, "fetch active user")
require.Equal(t, codersdk.UserStatusActive, otherUser.Status, "active user")
})
t.Run("SkipPromptFlag", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
owner := coderdtest.CreateFirstUser(t, client)
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
other, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
otherUser, err := other.User(context.Background(), codersdk.Me)
require.NoError(t, err, "fetch user")
// No stdin is attached, so the command would block or fail if it tried to
// prompt.
inv, root := clitest.New(t, "users", "suspend", otherUser.Username, "-y")
clitest.SetupConfig(t, userAdmin, root)
err = inv.Run()
require.NoError(t, err, "suspend user")
otherUser, err = client.User(context.Background(), otherUser.Username)
require.NoError(t, err, "fetch suspended user")
require.Equal(t, codersdk.UserStatusSuspended, otherUser.Status, "suspended user")
inv, root = clitest.New(t, "users", "activate", otherUser.Username, "--yes")
clitest.SetupConfig(t, userAdmin, root)
err = inv.Run()
require.NoError(t, err, "activate user")
otherUser, err = client.User(context.Background(), otherUser.Username)
require.NoError(t, err, "fetch active user")
require.Equal(t, codersdk.UserStatusActive, otherUser.Status, "active user")
})
}