mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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:
+16
-11
@@ -11,6 +11,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/pty/ptytest"
|
||||
)
|
||||
@@ -20,9 +21,10 @@ func TestUserList(t *testing.T) {
|
||||
t.Run("Table", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
|
||||
inv, root := clitest.New(t, "users", "list")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
clitest.SetupConfig(t, userAdmin, root)
|
||||
pty := ptytest.New(t).Attach(inv)
|
||||
errC := make(chan error)
|
||||
go func() {
|
||||
@@ -35,9 +37,10 @@ func TestUserList(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
|
||||
inv, root := clitest.New(t, "users", "list", "-o", "json")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
clitest.SetupConfig(t, userAdmin, root)
|
||||
doneChan := make(chan struct{})
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
@@ -53,7 +56,7 @@ func TestUserList(t *testing.T) {
|
||||
var users []codersdk.User
|
||||
err := json.Unmarshal(buf.Bytes(), &users)
|
||||
require.NoError(t, err, "unmarshal JSON output")
|
||||
require.Len(t, users, 1)
|
||||
require.Len(t, users, 2)
|
||||
require.Contains(t, users[0].Email, "coder.com")
|
||||
})
|
||||
t.Run("NoURLFileErrorHasHelperText", func(t *testing.T) {
|
||||
@@ -84,10 +87,11 @@ func TestUserShow(t *testing.T) {
|
||||
t.Run("Table", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
_, otherUser := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
userAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleUserAdmin())
|
||||
_, otherUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
inv, root := clitest.New(t, "users", "show", otherUser.Username)
|
||||
clitest.SetupConfig(t, client, root)
|
||||
clitest.SetupConfig(t, userAdmin, root)
|
||||
doneChan := make(chan struct{})
|
||||
pty := ptytest.New(t).Attach(inv)
|
||||
go func() {
|
||||
@@ -104,12 +108,13 @@ func TestUserShow(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
other, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
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(ctx, codersdk.Me)
|
||||
require.NoError(t, err, "fetch other user")
|
||||
inv, root := clitest.New(t, "users", "show", otherUser.Username, "-o", "json")
|
||||
clitest.SetupConfig(t, client, root)
|
||||
clitest.SetupConfig(t, userAdmin, root)
|
||||
doneChan := make(chan struct{})
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
Reference in New Issue
Block a user