Skip to content

chore(scripts/rules.go): broaden scope of testingWithOwnerUser linter #10548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,25 @@ func CreateTemplate(t testing.TB, client *codersdk.Client, organization uuid.UUI
return template
}

// CreateGroup creates a group with the given name and members.
func CreateGroup(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, name string, members ...codersdk.User) codersdk.Group {
t.Helper()
group, err := client.CreateGroup(context.Background(), organizationID, codersdk.CreateGroupRequest{
Name: name,
})
require.NoError(t, err, "failed to create group")
memberIDs := make([]string, 0)
for _, member := range members {
memberIDs = append(memberIDs, member.ID.String())
}
group, err = client.PatchGroup(context.Background(), group.ID, codersdk.PatchGroupRequest{
AddUsers: memberIDs,
})

require.NoError(t, err, "failed to add members to group")
return group
}

// UpdateTemplateVersion creates a new template version with the "echo" provisioner
// and associates it with the given templateID.
func UpdateTemplateVersion(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, res *echo.Responses, templateID uuid.UUID) codersdk.TemplateVersion {
Expand All @@ -787,6 +806,14 @@ func UpdateActiveTemplateVersion(t testing.TB, client *codersdk.Client, template
require.NoError(t, err)
}

// UpdateTemplateMeta updates the template meta for the given template.
func UpdateTemplateMeta(t testing.TB, client *codersdk.Client, templateID uuid.UUID, meta codersdk.UpdateTemplateMeta) codersdk.Template {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a helper like this, would it make sense to ensure that all methods of codersdk.Client are implemented here instead of just select ones? This essentially turns 2 lines into 1 which arguably doesn't add a whole lot of value if you still don't know when to use client directly, and when to use coderdtest.

Copy link
Member Author

@johnstcn johnstcn Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter won't complain about passing the owner client into a coderdtest method, which I view as both a feature and a semantic difference:

  • coderdtest.X: this thing is part of test setup.
  • client.X: this is the thing we are actually testing where ensuring it works with the least-privileged RBAC role is crucial.

I don't think a 1:1 mapping of client methods -> coderdtest methods is warranted, especially as we have some coderdtest methods that call multiple client methods.

t.Helper()
updated, err := client.UpdateTemplateMeta(context.Background(), templateID, meta)
require.NoError(t, err)
return updated
}

// AwaitTemplateVersionJobRunning waits for the build to be picked up by a provisioner.
func AwaitTemplateVersionJobRunning(t testing.TB, client *codersdk.Client, version uuid.UUID) codersdk.TemplateVersion {
t.Helper()
Expand Down
11 changes: 7 additions & 4 deletions enterprise/cli/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/pty/ptytest"
Expand All @@ -18,9 +19,10 @@ func TestFeaturesList(t *testing.T) {
t.Parallel()
t.Run("Table", func(t *testing.T) {
t.Parallel()
client, _ := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
client, admin := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
inv, conf := newCLI(t, "features", "list")
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)
pty := ptytest.New(t).Attach(inv)
clitest.Start(t, inv)
pty.ExpectMatch("user_limit")
Expand All @@ -29,9 +31,10 @@ func TestFeaturesList(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
t.Parallel()

client, _ := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
client, admin := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
inv, conf := newCLI(t, "features", "list", "-o", "json")
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)
doneChan := make(chan struct{})

buf := bytes.NewBuffer(nil)
Expand Down
7 changes: 5 additions & 2 deletions enterprise/cli/groupcreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/cliui"
"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/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
Expand All @@ -22,11 +24,12 @@ func TestCreateGroup(t *testing.T) {
t.Run("OK", func(t *testing.T) {
t.Parallel()

client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
client, admin := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())

var (
groupName = "test"
Expand All @@ -40,7 +43,7 @@ func TestCreateGroup(t *testing.T) {

pty := ptytest.New(t)
inv.Stdout = pty.Output()
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err := inv.Run()
require.NoError(t, err)
Expand Down
19 changes: 9 additions & 10 deletions enterprise/cli/groupdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/cliui"
"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/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
)

func TestGroupDelete(t *testing.T) {
Expand All @@ -28,12 +29,9 @@ func TestGroupDelete(t *testing.T) {
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())

ctx := testutil.Context(t, testutil.WaitLong)
group, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
Name: "alpha",
})
require.NoError(t, err)
group := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha")

inv, conf := newCLI(t,
"groups", "delete", group.Name,
Expand All @@ -42,9 +40,9 @@ func TestGroupDelete(t *testing.T) {
pty := ptytest.New(t)

inv.Stdout = pty.Output()
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err = inv.Run()
err := inv.Run()
require.NoError(t, err)

pty.ExpectMatch(fmt.Sprintf("Successfully deleted group %s", pretty.Sprint(cliui.DefaultStyles.Keyword, group.Name)))
Expand All @@ -53,18 +51,19 @@ func TestGroupDelete(t *testing.T) {
t.Run("NoArg", func(t *testing.T) {
t.Parallel()

client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
client, admin := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())

inv, conf := newCLI(
t,
"groups", "delete",
)

clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err := inv.Run()
require.Error(t, err)
Expand Down
42 changes: 14 additions & 28 deletions enterprise/cli/groupedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/cliui"
"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/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
)

func TestGroupEdit(t *testing.T) {
Expand All @@ -29,23 +29,13 @@ func TestGroupEdit(t *testing.T) {
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())

ctx := testutil.Context(t, testutil.WaitLong)
_, user1 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
_, user2 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
_, user3 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)

group, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
Name: "alpha",
})
require.NoError(t, err)

// We use the sdk here as opposed to the CLI since adding this user
// is considered setup. They will be removed in the proper CLI test.
group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
AddUsers: []string{user3.ID.String()},
})
require.NoError(t, err)
group := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha", user3)

expectedName := "beta"

Expand All @@ -62,9 +52,9 @@ func TestGroupEdit(t *testing.T) {
pty := ptytest.New(t)

inv.Stdout = pty.Output()
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err = inv.Run()
err := inv.Run()
require.NoError(t, err)

pty.ExpectMatch(fmt.Sprintf("Successfully patched group %s", pretty.Sprint(cliui.DefaultStyles.Keyword, expectedName)))
Expand All @@ -79,39 +69,35 @@ func TestGroupEdit(t *testing.T) {
},
}})

ctx := testutil.Context(t, testutil.WaitLong)

group, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
Name: "alpha",
})
require.NoError(t, err)
// Create a group with no members.
group := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha")

inv, conf := newCLI(
t,
"groups", "edit", group.Name,
"-a", "foo",
)

clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, client, conf) //nolint:gocritic // intentional usage of owner

err = inv.Run()
require.Error(t, err)
require.Contains(t, err.Error(), "must be a valid UUID or email address")
err := inv.Run()
require.ErrorContains(t, err, "must be a valid UUID or email address")
})

t.Run("NoArg", func(t *testing.T) {
t.Parallel()

client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
client, user := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleUserAdmin())

inv, conf := newCLI(t, "groups", "edit")
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err := inv.Run()
require.Error(t, err)
require.ErrorContains(t, err, "wanted 1 args but got 0")
})
}
36 changes: 10 additions & 26 deletions enterprise/cli/grouplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ 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/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
)

func TestGroupList(t *testing.T) {
Expand All @@ -25,42 +25,25 @@ func TestGroupList(t *testing.T) {
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())

ctx := testutil.Context(t, testutil.WaitLong)
_, user1 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
_, user2 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)

// We intentionally create the first group as beta so that we
// can assert that things are being sorted by name intentionally
// and not by chance (or some other parameter like created_at).
group1, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
Name: "beta",
})
require.NoError(t, err)

group2, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
Name: "alpha",
})
require.NoError(t, err)

_, err = client.PatchGroup(ctx, group1.ID, codersdk.PatchGroupRequest{
AddUsers: []string{user1.ID.String()},
})
require.NoError(t, err)

_, err = client.PatchGroup(ctx, group2.ID, codersdk.PatchGroupRequest{
AddUsers: []string{user2.ID.String()},
})
require.NoError(t, err)
group1 := coderdtest.CreateGroup(t, client, admin.OrganizationID, "beta", user1)
group2 := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha", user2)

inv, conf := newCLI(t, "groups", "list")

pty := ptytest.New(t)

inv.Stdout = pty.Output()
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err = inv.Run()
err := inv.Run()
require.NoError(t, err)

matches := []string{
Expand All @@ -77,25 +60,26 @@ func TestGroupList(t *testing.T) {
t.Run("Everyone", func(t *testing.T) {
t.Parallel()

client, user := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
client, admin := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())

inv, conf := newCLI(t, "groups", "list")

pty := ptytest.New(t)

inv.Stdout = pty.Output()
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, anotherClient, conf)

err := inv.Run()
require.NoError(t, err)

matches := []string{
"NAME", "ORGANIZATION ID", "MEMBERS", " AVATAR URL",
"Everyone", user.OrganizationID.String(), coderdtest.FirstUserParams.Email, "",
"Everyone", admin.OrganizationID.String(), coderdtest.FirstUserParams.Email, "",
}

for _, match := range matches {
Expand Down
6 changes: 3 additions & 3 deletions enterprise/cli/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestLicensesAddReal(t *testing.T) {
t,
"licenses", "add", "-l", fakeLicenseJWT,
)
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, client, conf) //nolint:gocritic // requires owner

waiter := clitest.StartWithWaiter(t, inv)
var coderError *codersdk.Error
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestLicensesListReal(t *testing.T) {
inv.Stdout = stdout
stderr := new(bytes.Buffer)
inv.Stderr = stderr
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, client, conf) //nolint:gocritic // requires owner
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
errC := make(chan error)
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestLicensesDeleteReal(t *testing.T) {
inv, conf := newCLI(
t,
"licenses", "delete", "1")
clitest.SetupConfig(t, client, conf)
clitest.SetupConfig(t, client, conf) //nolint:gocritic // requires owner

var coderError *codersdk.Error
clitest.StartWithWaiter(t, inv).RequireAs(&coderError)
Expand Down
Loading