Skip to content

Commit 26740cf

Browse files
authored
chore(scripts/rules.go): broaden scope of testingWithOwnerUser linter (coder#10548)
* Updated testingWithOwnerUser ruleguard rule to detect: a) Passing client from coderdenttest.New() to clitest.SetupConfig() similar to what already exists for AGPL code b) Usage of any method of the owner client from coderdenttest.New() - all usages of the owner client must be justified with a `//nolint:gocritic` comment. * Fixed resulting linter complaints. * Added new coderdtest helpers CreateGroup and UpdateTemplateMeta. * Modified check_enterprise_import.sh to ignore scripts/rules.go.
1 parent 057b43a commit 26740cf

27 files changed

+472
-330
lines changed

coderd/coderdtest/coderdtest.go

+27
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,25 @@ func CreateTemplate(t testing.TB, client *codersdk.Client, organization uuid.UUI
762762
return template
763763
}
764764

765+
// CreateGroup creates a group with the given name and members.
766+
func CreateGroup(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, name string, members ...codersdk.User) codersdk.Group {
767+
t.Helper()
768+
group, err := client.CreateGroup(context.Background(), organizationID, codersdk.CreateGroupRequest{
769+
Name: name,
770+
})
771+
require.NoError(t, err, "failed to create group")
772+
memberIDs := make([]string, 0)
773+
for _, member := range members {
774+
memberIDs = append(memberIDs, member.ID.String())
775+
}
776+
group, err = client.PatchGroup(context.Background(), group.ID, codersdk.PatchGroupRequest{
777+
AddUsers: memberIDs,
778+
})
779+
780+
require.NoError(t, err, "failed to add members to group")
781+
return group
782+
}
783+
765784
// UpdateTemplateVersion creates a new template version with the "echo" provisioner
766785
// and associates it with the given templateID.
767786
func UpdateTemplateVersion(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, res *echo.Responses, templateID uuid.UUID) codersdk.TemplateVersion {
@@ -787,6 +806,14 @@ func UpdateActiveTemplateVersion(t testing.TB, client *codersdk.Client, template
787806
require.NoError(t, err)
788807
}
789808

809+
// UpdateTemplateMeta updates the template meta for the given template.
810+
func UpdateTemplateMeta(t testing.TB, client *codersdk.Client, templateID uuid.UUID, meta codersdk.UpdateTemplateMeta) codersdk.Template {
811+
t.Helper()
812+
updated, err := client.UpdateTemplateMeta(context.Background(), templateID, meta)
813+
require.NoError(t, err)
814+
return updated
815+
}
816+
790817
// AwaitTemplateVersionJobRunning waits for the build to be picked up by a provisioner.
791818
func AwaitTemplateVersionJobRunning(t testing.TB, client *codersdk.Client, version uuid.UUID) codersdk.TemplateVersion {
792819
t.Helper()

enterprise/cli/features_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
"github.com/coder/coder/v2/cli/clitest"
12+
"github.com/coder/coder/v2/coderd/coderdtest"
1213
"github.com/coder/coder/v2/codersdk"
1314
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1415
"github.com/coder/coder/v2/pty/ptytest"
@@ -18,9 +19,10 @@ func TestFeaturesList(t *testing.T) {
1819
t.Parallel()
1920
t.Run("Table", func(t *testing.T) {
2021
t.Parallel()
21-
client, _ := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
22+
client, admin := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
23+
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
2224
inv, conf := newCLI(t, "features", "list")
23-
clitest.SetupConfig(t, client, conf)
25+
clitest.SetupConfig(t, anotherClient, conf)
2426
pty := ptytest.New(t).Attach(inv)
2527
clitest.Start(t, inv)
2628
pty.ExpectMatch("user_limit")
@@ -29,9 +31,10 @@ func TestFeaturesList(t *testing.T) {
2931
t.Run("JSON", func(t *testing.T) {
3032
t.Parallel()
3133

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

3740
buf := bytes.NewBuffer(nil)

enterprise/cli/groupcreate_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"github.com/coder/coder/v2/cli/clitest"
1212
"github.com/coder/coder/v2/cli/cliui"
13+
"github.com/coder/coder/v2/coderd/coderdtest"
14+
"github.com/coder/coder/v2/coderd/rbac"
1315
"github.com/coder/coder/v2/codersdk"
1416
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1517
"github.com/coder/coder/v2/enterprise/coderd/license"
@@ -22,11 +24,12 @@ func TestCreateGroup(t *testing.T) {
2224
t.Run("OK", func(t *testing.T) {
2325
t.Parallel()
2426

25-
client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
27+
client, admin := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
2628
Features: license.Features{
2729
codersdk.FeatureTemplateRBAC: 1,
2830
},
2931
}})
32+
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())
3033

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

4144
pty := ptytest.New(t)
4245
inv.Stdout = pty.Output()
43-
clitest.SetupConfig(t, client, conf)
46+
clitest.SetupConfig(t, anotherClient, conf)
4447

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

enterprise/cli/groupdelete_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010

1111
"github.com/coder/coder/v2/cli/clitest"
1212
"github.com/coder/coder/v2/cli/cliui"
13+
"github.com/coder/coder/v2/coderd/coderdtest"
14+
"github.com/coder/coder/v2/coderd/rbac"
1315
"github.com/coder/coder/v2/codersdk"
1416
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1517
"github.com/coder/coder/v2/enterprise/coderd/license"
1618
"github.com/coder/coder/v2/pty/ptytest"
17-
"github.com/coder/coder/v2/testutil"
1819
)
1920

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

32-
ctx := testutil.Context(t, testutil.WaitLong)
33-
group, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
34-
Name: "alpha",
35-
})
36-
require.NoError(t, err)
34+
group := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha")
3735

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

4442
inv.Stdout = pty.Output()
45-
clitest.SetupConfig(t, client, conf)
43+
clitest.SetupConfig(t, anotherClient, conf)
4644

47-
err = inv.Run()
45+
err := inv.Run()
4846
require.NoError(t, err)
4947

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

56-
client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
54+
client, admin := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
5755
Features: license.Features{
5856
codersdk.FeatureTemplateRBAC: 1,
5957
},
6058
}})
59+
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())
6160

6261
inv, conf := newCLI(
6362
t,
6463
"groups", "delete",
6564
)
6665

67-
clitest.SetupConfig(t, client, conf)
66+
clitest.SetupConfig(t, anotherClient, conf)
6867

6968
err := inv.Run()
7069
require.Error(t, err)

enterprise/cli/groupedit_test.go

+14-28
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"github.com/coder/coder/v2/cli/clitest"
1212
"github.com/coder/coder/v2/cli/cliui"
1313
"github.com/coder/coder/v2/coderd/coderdtest"
14+
"github.com/coder/coder/v2/coderd/rbac"
1415
"github.com/coder/coder/v2/codersdk"
1516
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1617
"github.com/coder/coder/v2/enterprise/coderd/license"
1718
"github.com/coder/coder/v2/pty/ptytest"
18-
"github.com/coder/coder/v2/testutil"
1919
)
2020

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

33-
ctx := testutil.Context(t, testutil.WaitLong)
3434
_, user1 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
3535
_, user2 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
3636
_, user3 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
3737

38-
group, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
39-
Name: "alpha",
40-
})
41-
require.NoError(t, err)
42-
43-
// We use the sdk here as opposed to the CLI since adding this user
44-
// is considered setup. They will be removed in the proper CLI test.
45-
group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
46-
AddUsers: []string{user3.ID.String()},
47-
})
48-
require.NoError(t, err)
38+
group := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha", user3)
4939

5040
expectedName := "beta"
5141

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

6454
inv.Stdout = pty.Output()
65-
clitest.SetupConfig(t, client, conf)
55+
clitest.SetupConfig(t, anotherClient, conf)
6656

67-
err = inv.Run()
57+
err := inv.Run()
6858
require.NoError(t, err)
6959

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

82-
ctx := testutil.Context(t, testutil.WaitLong)
83-
84-
group, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
85-
Name: "alpha",
86-
})
87-
require.NoError(t, err)
72+
// Create a group with no members.
73+
group := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha")
8874

8975
inv, conf := newCLI(
9076
t,
9177
"groups", "edit", group.Name,
9278
"-a", "foo",
9379
)
9480

95-
clitest.SetupConfig(t, client, conf)
81+
clitest.SetupConfig(t, client, conf) //nolint:gocritic // intentional usage of owner
9682

97-
err = inv.Run()
98-
require.Error(t, err)
99-
require.Contains(t, err.Error(), "must be a valid UUID or email address")
83+
err := inv.Run()
84+
require.ErrorContains(t, err, "must be a valid UUID or email address")
10085
})
10186

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

105-
client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
90+
client, user := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
10691
Features: license.Features{
10792
codersdk.FeatureTemplateRBAC: 1,
10893
},
10994
}})
95+
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleUserAdmin())
11096

11197
inv, conf := newCLI(t, "groups", "edit")
112-
clitest.SetupConfig(t, client, conf)
98+
clitest.SetupConfig(t, anotherClient, conf)
11399

114100
err := inv.Run()
115-
require.Error(t, err)
101+
require.ErrorContains(t, err, "wanted 1 args but got 0")
116102
})
117103
}

enterprise/cli/grouplist_test.go

+10-26
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77

88
"github.com/coder/coder/v2/cli/clitest"
99
"github.com/coder/coder/v2/coderd/coderdtest"
10+
"github.com/coder/coder/v2/coderd/rbac"
1011
"github.com/coder/coder/v2/codersdk"
1112
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1213
"github.com/coder/coder/v2/enterprise/coderd/license"
1314
"github.com/coder/coder/v2/pty/ptytest"
14-
"github.com/coder/coder/v2/testutil"
1515
)
1616

1717
func TestGroupList(t *testing.T) {
@@ -25,42 +25,25 @@ func TestGroupList(t *testing.T) {
2525
codersdk.FeatureTemplateRBAC: 1,
2626
},
2727
}})
28+
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())
2829

29-
ctx := testutil.Context(t, testutil.WaitLong)
3030
_, user1 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
3131
_, user2 := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
3232

3333
// We intentionally create the first group as beta so that we
3434
// can assert that things are being sorted by name intentionally
3535
// and not by chance (or some other parameter like created_at).
36-
group1, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
37-
Name: "beta",
38-
})
39-
require.NoError(t, err)
40-
41-
group2, err := client.CreateGroup(ctx, admin.OrganizationID, codersdk.CreateGroupRequest{
42-
Name: "alpha",
43-
})
44-
require.NoError(t, err)
45-
46-
_, err = client.PatchGroup(ctx, group1.ID, codersdk.PatchGroupRequest{
47-
AddUsers: []string{user1.ID.String()},
48-
})
49-
require.NoError(t, err)
50-
51-
_, err = client.PatchGroup(ctx, group2.ID, codersdk.PatchGroupRequest{
52-
AddUsers: []string{user2.ID.String()},
53-
})
54-
require.NoError(t, err)
36+
group1 := coderdtest.CreateGroup(t, client, admin.OrganizationID, "beta", user1)
37+
group2 := coderdtest.CreateGroup(t, client, admin.OrganizationID, "alpha", user2)
5538

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

5841
pty := ptytest.New(t)
5942

6043
inv.Stdout = pty.Output()
61-
clitest.SetupConfig(t, client, conf)
44+
clitest.SetupConfig(t, anotherClient, conf)
6245

63-
err = inv.Run()
46+
err := inv.Run()
6447
require.NoError(t, err)
6548

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

80-
client, user := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
63+
client, admin := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
8164
Features: license.Features{
8265
codersdk.FeatureTemplateRBAC: 1,
8366
},
8467
}})
68+
anotherClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleUserAdmin())
8569

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

8872
pty := ptytest.New(t)
8973

9074
inv.Stdout = pty.Output()
91-
clitest.SetupConfig(t, client, conf)
75+
clitest.SetupConfig(t, anotherClient, conf)
9276

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

9680
matches := []string{
9781
"NAME", "ORGANIZATION ID", "MEMBERS", " AVATAR URL",
98-
"Everyone", user.OrganizationID.String(), coderdtest.FirstUserParams.Email, "",
82+
"Everyone", admin.OrganizationID.String(), coderdtest.FirstUserParams.Email, "",
9983
}
10084

10185
for _, match := range matches {

enterprise/cli/licenses_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestLicensesAddReal(t *testing.T) {
122122
t,
123123
"licenses", "add", "-l", fakeLicenseJWT,
124124
)
125-
clitest.SetupConfig(t, client, conf)
125+
clitest.SetupConfig(t, client, conf) //nolint:gocritic // requires owner
126126

127127
waiter := clitest.StartWithWaiter(t, inv)
128128
var coderError *codersdk.Error
@@ -180,7 +180,7 @@ func TestLicensesListReal(t *testing.T) {
180180
inv.Stdout = stdout
181181
stderr := new(bytes.Buffer)
182182
inv.Stderr = stderr
183-
clitest.SetupConfig(t, client, conf)
183+
clitest.SetupConfig(t, client, conf) //nolint:gocritic // requires owner
184184
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
185185
defer cancel()
186186
errC := make(chan error)
@@ -216,7 +216,7 @@ func TestLicensesDeleteReal(t *testing.T) {
216216
inv, conf := newCLI(
217217
t,
218218
"licenses", "delete", "1")
219-
clitest.SetupConfig(t, client, conf)
219+
clitest.SetupConfig(t, client, conf) //nolint:gocritic // requires owner
220220

221221
var coderError *codersdk.Error
222222
clitest.StartWithWaiter(t, inv).RequireAs(&coderError)

0 commit comments

Comments
 (0)