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:
+22
-22
@@ -2111,15 +2111,15 @@ func TestTemplateInsights_RBAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
var templateIDs []uuid.UUID
|
||||
if tt.withTemplate {
|
||||
version := coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
templateIDs = append(templateIDs, template.ID)
|
||||
}
|
||||
|
||||
@@ -2135,17 +2135,17 @@ func TestTemplateInsights_RBAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleTemplateAdmin())
|
||||
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
var templateIDs []uuid.UUID
|
||||
if tt.withTemplate {
|
||||
version := coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
templateIDs = append(templateIDs, template.ID)
|
||||
}
|
||||
|
||||
@@ -2161,17 +2161,17 @@ func TestTemplateInsights_RBAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
regular, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
regular, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
var templateIDs []uuid.UUID
|
||||
if tt.withTemplate {
|
||||
version := coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
templateIDs = append(templateIDs, template.ID)
|
||||
}
|
||||
|
||||
@@ -2240,15 +2240,15 @@ func TestGenericInsights_RBAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
var templateIDs []uuid.UUID
|
||||
if tt.withTemplate {
|
||||
version := coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
templateIDs = append(templateIDs, template.ID)
|
||||
}
|
||||
|
||||
@@ -2262,17 +2262,17 @@ func TestGenericInsights_RBAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleTemplateAdmin())
|
||||
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
var templateIDs []uuid.UUID
|
||||
if tt.withTemplate {
|
||||
version := coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
templateIDs = append(templateIDs, template.ID)
|
||||
}
|
||||
|
||||
@@ -2286,17 +2286,17 @@ func TestGenericInsights_RBAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
regular, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
regular, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
||||
defer cancel()
|
||||
|
||||
var templateIDs []uuid.UUID
|
||||
if tt.withTemplate {
|
||||
version := coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
templateIDs = append(templateIDs, template.ID)
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -17,10 +17,10 @@ func TestListRoles(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
// Create admin, member, and org admin
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
orgAdmin, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleOrgAdmin(admin.OrganizationID))
|
||||
// Create owner, member, and org admin
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
orgAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleOrgAdmin(owner.OrganizationID))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
t.Cleanup(cancel)
|
||||
@@ -55,10 +55,10 @@ func TestListRoles(t *testing.T) {
|
||||
{
|
||||
Name: "OrgMemberListOrg",
|
||||
APICall: func(ctx context.Context) ([]codersdk.AssignableRoles, error) {
|
||||
return member.ListOrganizationRoles(ctx, admin.OrganizationID)
|
||||
return member.ListOrganizationRoles(ctx, owner.OrganizationID)
|
||||
},
|
||||
ExpectedRoles: convertRoles(map[string]bool{
|
||||
rbac.RoleOrgAdmin(admin.OrganizationID): false,
|
||||
rbac.RoleOrgAdmin(owner.OrganizationID): false,
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -84,10 +84,10 @@ func TestListRoles(t *testing.T) {
|
||||
{
|
||||
Name: "OrgAdminListOrg",
|
||||
APICall: func(ctx context.Context) ([]codersdk.AssignableRoles, error) {
|
||||
return orgAdmin.ListOrganizationRoles(ctx, admin.OrganizationID)
|
||||
return orgAdmin.ListOrganizationRoles(ctx, owner.OrganizationID)
|
||||
},
|
||||
ExpectedRoles: convertRoles(map[string]bool{
|
||||
rbac.RoleOrgAdmin(admin.OrganizationID): true,
|
||||
rbac.RoleOrgAdmin(owner.OrganizationID): true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -113,10 +113,10 @@ func TestListRoles(t *testing.T) {
|
||||
{
|
||||
Name: "AdminListOrg",
|
||||
APICall: func(ctx context.Context) ([]codersdk.AssignableRoles, error) {
|
||||
return client.ListOrganizationRoles(ctx, admin.OrganizationID)
|
||||
return client.ListOrganizationRoles(ctx, owner.OrganizationID)
|
||||
},
|
||||
ExpectedRoles: convertRoles(map[string]bool{
|
||||
rbac.RoleOrgAdmin(admin.OrganizationID): true,
|
||||
rbac.RoleOrgAdmin(owner.OrganizationID): true,
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
+15
-15
@@ -262,13 +262,13 @@ func TestPostLogin(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
split := strings.Split(client.SessionToken(), "-")
|
||||
key, err := client.APIKeyByID(ctx, admin.UserID.String(), split[0])
|
||||
key, err := client.APIKeyByID(ctx, owner.UserID.String(), split[0])
|
||||
require.NoError(t, err, "fetch login key")
|
||||
require.Equal(t, int64(86400), key.LifetimeSeconds, "default should be 86400")
|
||||
|
||||
@@ -276,7 +276,7 @@ func TestPostLogin(t *testing.T) {
|
||||
token, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{})
|
||||
require.NoError(t, err, "make new token api key")
|
||||
split = strings.Split(token.Key, "-")
|
||||
apiKey, err := client.APIKeyByID(ctx, admin.UserID.String(), split[0])
|
||||
apiKey, err := client.APIKeyByID(ctx, owner.UserID.String(), split[0])
|
||||
require.NoError(t, err, "fetch api key")
|
||||
|
||||
require.True(t, apiKey.ExpiresAt.After(time.Now().Add(time.Hour*24*29)), "default tokens lasts more than 29 days")
|
||||
@@ -364,14 +364,14 @@ func TestPostLogout(t *testing.T) {
|
||||
client := coderdtest.New(t, &coderdtest.Options{Auditor: auditor})
|
||||
numLogs := len(auditor.AuditLogs())
|
||||
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
numLogs++ // add an audit log for login
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
keyID := strings.Split(client.SessionToken(), "-")[0]
|
||||
apiKey, err := client.APIKeyByID(ctx, admin.UserID.String(), keyID)
|
||||
apiKey, err := client.APIKeyByID(ctx, owner.UserID.String(), keyID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, keyID, apiKey.ID, "API key should exist in the database")
|
||||
|
||||
@@ -400,7 +400,7 @@ func TestPostLogout(t *testing.T) {
|
||||
}
|
||||
require.True(t, found, "auth cookie should be returned")
|
||||
|
||||
_, err = client.APIKeyByID(ctx, admin.UserID.String(), keyID)
|
||||
_, err = client.APIKeyByID(ctx, owner.UserID.String(), keyID)
|
||||
sdkErr := &codersdk.Error{}
|
||||
require.ErrorAs(t, err, &sdkErr)
|
||||
require.Equal(t, http.StatusUnauthorized, sdkErr.StatusCode(), "Expecting 401")
|
||||
@@ -707,13 +707,13 @@ func TestUpdateUserPassword(t *testing.T) {
|
||||
t.Run("MemberCantUpdateAdminPassword", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
err := member.UpdateUserPassword(ctx, admin.UserID.String(), codersdk.UpdateUserPasswordRequest{
|
||||
err := member.UpdateUserPassword(ctx, owner.UserID.String(), codersdk.UpdateUserPasswordRequest{
|
||||
Password: "newpassword",
|
||||
})
|
||||
require.Error(t, err, "member should not be able to update admin password")
|
||||
@@ -722,7 +722,7 @@ func TestUpdateUserPassword(t *testing.T) {
|
||||
t.Run("AdminCanUpdateMemberPassword", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
@@ -731,7 +731,7 @@ func TestUpdateUserPassword(t *testing.T) {
|
||||
Email: "coder@coder.com",
|
||||
Username: "coder",
|
||||
Password: "SomeStrongPassword!",
|
||||
OrganizationID: admin.OrganizationID,
|
||||
OrganizationID: owner.OrganizationID,
|
||||
})
|
||||
require.NoError(t, err, "create member")
|
||||
err = client.UpdateUserPassword(ctx, member.ID.String(), codersdk.UpdateUserPasswordRequest{
|
||||
@@ -751,11 +751,11 @@ func TestUpdateUserPassword(t *testing.T) {
|
||||
client := coderdtest.New(t, &coderdtest.Options{Auditor: auditor})
|
||||
numLogs := len(auditor.AuditLogs())
|
||||
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
numLogs++ // add an audit log for user create
|
||||
numLogs++ // add an audit log for login
|
||||
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
numLogs++ // add an audit log for user create
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
@@ -775,8 +775,8 @@ func TestUpdateUserPassword(t *testing.T) {
|
||||
t.Run("MemberCantUpdateOwnPasswordWithoutOldPassword", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
admin := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
@@ -729,16 +729,16 @@ func TestWorkspaceBuildDebugMode(t *testing.T) {
|
||||
deploymentValues.EnableTerraformDebugMode = true
|
||||
|
||||
adminClient := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, DeploymentValues: deploymentValues})
|
||||
admin := coderdtest.CreateFirstUser(t, adminClient)
|
||||
templateAuthorClient, _ := coderdtest.CreateAnotherUser(t, adminClient, admin.OrganizationID, rbac.RoleTemplateAdmin())
|
||||
owner := coderdtest.CreateFirstUser(t, adminClient)
|
||||
templateAuthorClient, _ := coderdtest.CreateAnotherUser(t, adminClient, owner.OrganizationID, rbac.RoleTemplateAdmin())
|
||||
|
||||
// Template author: create a template
|
||||
version := coderdtest.CreateTemplateVersion(t, templateAuthorClient, admin.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, templateAuthorClient, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, templateAuthorClient, owner.OrganizationID, nil)
|
||||
template := coderdtest.CreateTemplate(t, templateAuthorClient, owner.OrganizationID, version.ID)
|
||||
coderdtest.AwaitTemplateVersionJobCompleted(t, templateAuthorClient, version.ID)
|
||||
|
||||
// Template author: create a workspace
|
||||
workspace := coderdtest.CreateWorkspace(t, templateAuthorClient, admin.OrganizationID, template.ID)
|
||||
workspace := coderdtest.CreateWorkspace(t, templateAuthorClient, owner.OrganizationID, template.ID)
|
||||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, templateAuthorClient, workspace.LatestBuild.ID)
|
||||
|
||||
// Template author: try to start a workspace build in debug mode
|
||||
@@ -766,7 +766,7 @@ func TestWorkspaceBuildDebugMode(t *testing.T) {
|
||||
deploymentValues.EnableTerraformDebugMode = true
|
||||
|
||||
adminClient := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, DeploymentValues: deploymentValues})
|
||||
admin := coderdtest.CreateFirstUser(t, adminClient)
|
||||
owner := coderdtest.CreateFirstUser(t, adminClient)
|
||||
|
||||
// Interact as template admin
|
||||
echoResponses := &echo.Responses{
|
||||
@@ -799,12 +799,12 @@ func TestWorkspaceBuildDebugMode(t *testing.T) {
|
||||
},
|
||||
}},
|
||||
}
|
||||
version := coderdtest.CreateTemplateVersion(t, adminClient, admin.OrganizationID, echoResponses)
|
||||
template := coderdtest.CreateTemplate(t, adminClient, admin.OrganizationID, version.ID)
|
||||
version := coderdtest.CreateTemplateVersion(t, adminClient, owner.OrganizationID, echoResponses)
|
||||
template := coderdtest.CreateTemplate(t, adminClient, owner.OrganizationID, version.ID)
|
||||
coderdtest.AwaitTemplateVersionJobCompleted(t, adminClient, version.ID)
|
||||
|
||||
// Create workspace
|
||||
workspace := coderdtest.CreateWorkspace(t, adminClient, admin.OrganizationID, template.ID)
|
||||
workspace := coderdtest.CreateWorkspace(t, adminClient, owner.OrganizationID, template.ID)
|
||||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, adminClient, workspace.LatestBuild.ID)
|
||||
|
||||
// Create workspace build
|
||||
|
||||
Reference in New Issue
Block a user