chore(cli): add linter to detect potential spurious usage of owner user in cli tests (#10133)

* Detects the following pattern where the CLI is initialized with a client authenticated as the "first user":

    client := coderdtest.New(t, ...)
    [...]
    user := coderdtest.CreateFirstUser(t, client)
    [...]
    clitest.SetupConfig(t, client, root)

* Updates documentation regarding role permissions on workspaces.
This commit is contained in:
Cian Johnston
2023-10-10 11:14:20 +01:00
committed by GitHub
parent 017d7e9dad
commit c83af5e627
28 changed files with 684 additions and 521 deletions
+29 -30
View File
@@ -8,6 +8,7 @@ import (
"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"
"github.com/coder/coder/v2/cryptorand"
"github.com/coder/coder/v2/pty/ptytest"
@@ -19,7 +20,8 @@ func TestUserDelete(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, nil)
aUser := coderdtest.CreateFirstUser(t, client)
owner := coderdtest.CreateFirstUser(t, client)
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
pw, err := cryptorand.String(16)
require.NoError(t, err)
@@ -29,13 +31,13 @@ func TestUserDelete(t *testing.T) {
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: aUser.OrganizationID,
OrganizationID: owner.OrganizationID,
DisableLogin: false,
})
require.NoError(t, err)
inv, root := clitest.New(t, "users", "delete", "coolin")
clitest.SetupConfig(t, client, root)
clitest.SetupConfig(t, userAdmin, root)
pty := ptytest.New(t).Attach(inv)
errC := make(chan error)
go func() {
@@ -49,7 +51,8 @@ func TestUserDelete(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, nil)
aUser := coderdtest.CreateFirstUser(t, client)
owner := coderdtest.CreateFirstUser(t, client)
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
pw, err := cryptorand.String(16)
require.NoError(t, err)
@@ -59,13 +62,13 @@ func TestUserDelete(t *testing.T) {
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: aUser.OrganizationID,
OrganizationID: owner.OrganizationID,
DisableLogin: false,
})
require.NoError(t, err)
inv, root := clitest.New(t, "users", "delete", user.ID.String())
clitest.SetupConfig(t, client, root)
clitest.SetupConfig(t, userAdmin, root)
pty := ptytest.New(t).Attach(inv)
errC := make(chan error)
go func() {
@@ -79,7 +82,8 @@ func TestUserDelete(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, nil)
aUser := coderdtest.CreateFirstUser(t, client)
owner := coderdtest.CreateFirstUser(t, client)
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
pw, err := cryptorand.String(16)
require.NoError(t, err)
@@ -89,13 +93,13 @@ func TestUserDelete(t *testing.T) {
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: aUser.OrganizationID,
OrganizationID: owner.OrganizationID,
DisableLogin: false,
})
require.NoError(t, err)
inv, root := clitest.New(t, "users", "delete", user.ID.String())
clitest.SetupConfig(t, client, root)
clitest.SetupConfig(t, userAdmin, root)
pty := ptytest.New(t).Attach(inv)
errC := make(chan error)
go func() {
@@ -139,27 +143,22 @@ func TestUserDelete(t *testing.T) {
t.Run("DeleteSelf", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, nil)
aUser := coderdtest.CreateFirstUser(t, client)
pw, err := cryptorand.String(16)
require.NoError(t, err)
_, err = client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: aUser.OrganizationID,
DisableLogin: false,
t.Run("Owner", func(t *testing.T) {
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
inv, root := clitest.New(t, "users", "delete", "me")
//nolint:gocritic // The point of the test is to validate that a user cannot delete
// themselves, the owner user is probably the most important user to test this with.
clitest.SetupConfig(t, client, root)
require.ErrorContains(t, inv.Run(), "You cannot delete yourself!")
})
t.Run("UserAdmin", func(t *testing.T) {
client := coderdtest.New(t, nil)
owner := coderdtest.CreateFirstUser(t, client)
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
inv, root := clitest.New(t, "users", "delete", "me")
clitest.SetupConfig(t, userAdmin, root)
require.ErrorContains(t, inv.Run(), "You cannot delete yourself!")
})
require.NoError(t, err)
coderdtest.CreateAnotherUser(t, client, aUser.OrganizationID)
inv, root := clitest.New(t, "users", "delete", "me")
clitest.SetupConfig(t, client, root)
require.ErrorContains(t, inv.Run(), "You cannot delete yourself!")
})
}