Skip to content

Commit ce8aa66

Browse files
committed
chore: add test for adding multiple groups
1 parent 2b5d7d8 commit ce8aa66

File tree

2 files changed

+93
-15
lines changed

2 files changed

+93
-15
lines changed

cli/sharing_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestSharingShare(t *testing.T) {
4040
)
4141

4242
ctx := testutil.Context(t, testutil.WaitMedium)
43-
var inv, root = clitest.New(t, "sharing", "share", workspace.Name, "--org", orgOwner.OrganizationID.String(), "--user", toShareWithUser.Username)
43+
inv, root := clitest.New(t, "sharing", "share", workspace.Name, "--org", orgOwner.OrganizationID.String(), "--user", toShareWithUser.Username)
4444
clitest.SetupConfig(t, workspaceOwnerClient, root)
4545

4646
out := bytes.NewBuffer(nil)
@@ -49,7 +49,7 @@ func TestSharingShare(t *testing.T) {
4949
require.NoError(t, err)
5050

5151
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
52-
assert.NoError(t, err)
52+
require.NoError(t, err)
5353
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
5454
MinimalUser: codersdk.MinimalUser{
5555
ID: toShareWithUser.ID,
@@ -83,7 +83,7 @@ func TestSharingShare(t *testing.T) {
8383
)
8484

8585
ctx := testutil.Context(t, testutil.WaitMedium)
86-
var inv, root = clitest.New(t,
86+
inv, root := clitest.New(t,
8787
"sharing",
8888
"share", workspace.Name, "--org", orgOwner.OrganizationID.String(),
8989
fmt.Sprintf("--user=%s,%s", toShareWithUser1.Username, toShareWithUser2.Username),
@@ -96,7 +96,7 @@ func TestSharingShare(t *testing.T) {
9696
require.NoError(t, err)
9797

9898
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
99-
assert.NoError(t, err)
99+
require.NoError(t, err)
100100
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
101101
MinimalUser: codersdk.MinimalUser{
102102
ID: toShareWithUser1.ID,
@@ -147,7 +147,7 @@ func TestSharingShare(t *testing.T) {
147147
)
148148

149149
ctx := testutil.Context(t, testutil.WaitMedium)
150-
var inv, root = clitest.New(t, "sharing", "share", workspace.Name,
150+
inv, root := clitest.New(t, "sharing", "share", workspace.Name,
151151
"--org", orgOwner.OrganizationID.String(),
152152
"--user", fmt.Sprintf("%s:admin", toShareWithUser.Username),
153153
)
@@ -159,7 +159,7 @@ func TestSharingShare(t *testing.T) {
159159
require.NoError(t, err)
160160

161161
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
162-
assert.NoError(t, err)
162+
require.NoError(t, err)
163163
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
164164
MinimalUser: codersdk.MinimalUser{
165165
ID: toShareWithUser.ID,

enterprise/cli/sharing_test.go

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package cli_test
22

33
import (
44
"bytes"
5+
"context"
6+
"fmt"
7+
"slices"
58
"testing"
69

710
"github.com/coder/coder/v2/cli/clitest"
@@ -13,6 +16,7 @@ import (
1316
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1417
"github.com/coder/coder/v2/enterprise/coderd/license"
1518
"github.com/coder/coder/v2/testutil"
19+
"github.com/google/uuid"
1620
"github.com/stretchr/testify/assert"
1721
"github.com/stretchr/testify/require"
1822
)
@@ -47,17 +51,10 @@ func TestSharingShareEnterprise(t *testing.T) {
4751

4852
ctx := testutil.Context(t, testutil.WaitMedium)
4953

50-
group, err := client.CreateGroup(ctx, orgOwner.OrganizationID, codersdk.CreateGroupRequest{
51-
Name: "new-group",
52-
DisplayName: "new-group",
53-
})
54-
require.NoError(t, err)
55-
group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
56-
AddUsers: []string{orgMember.ID.String()},
57-
})
54+
group, err := createGroupWithMembers(client, ctx, orgOwner.OrganizationID, "new-group", []uuid.UUID{orgMember.ID})
5855
require.NoError(t, err)
5956

60-
var inv, root = clitest.New(t, "sharing", "share", workspace.Name, "--org", orgOwner.OrganizationID.String(), "--group", group.Name)
57+
inv, root := clitest.New(t, "sharing", "share", workspace.Name, "--org", orgOwner.OrganizationID.String(), "--group", group.Name)
6158
clitest.SetupConfig(t, workspaceOwnerClient, root)
6259

6360
out := bytes.NewBuffer(nil)
@@ -70,5 +67,86 @@ func TestSharingShareEnterprise(t *testing.T) {
7067
assert.Len(t, acl.Groups, 1)
7168
assert.Equal(t, acl.Groups[0].Group.ID, group.ID)
7269
assert.Equal(t, acl.Groups[0].Role, codersdk.WorkspaceRoleUse)
70+
assert.Contains(t, acl.Groups, codersdk.WorkspaceGroup{
71+
Role: codersdk.WorkspaceRoleUse,
72+
Group: group,
73+
})
74+
75+
assert.Contains(t, out.String(), group.Name)
76+
})
77+
78+
t.Run("ShareWithGroups_Multiple", func(t *testing.T) {
79+
t.Parallel()
80+
81+
var (
82+
client, db, orgOwner = coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
83+
Options: &coderdtest.Options{
84+
DeploymentValues: dv,
85+
},
86+
LicenseOptions: &coderdenttest.LicenseOptions{
87+
Features: license.Features{
88+
codersdk.FeatureTemplateRBAC: 1,
89+
},
90+
},
91+
})
92+
93+
workspaceOwnerClient, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID))
94+
workspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
95+
OwnerID: workspaceOwner.ID,
96+
OrganizationID: orgOwner.OrganizationID,
97+
}).Do().Workspace
98+
99+
_, wibbleMember = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID)
100+
_, wobbleMember = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID)
101+
)
102+
103+
ctx := testutil.Context(t, testutil.WaitMedium)
104+
105+
wibbleGroup, err := createGroupWithMembers(client, ctx, orgOwner.OrganizationID, "wibble", []uuid.UUID{wibbleMember.ID})
106+
require.NoError(t, err)
107+
108+
wobbleGroup, err := createGroupWithMembers(client, ctx, orgOwner.OrganizationID, "wobble", []uuid.UUID{wobbleMember.ID})
109+
require.NoError(t, err)
110+
111+
inv, root := clitest.New(t, "sharing", "share", workspace.Name, "--org", orgOwner.OrganizationID.String(),
112+
fmt.Sprintf("--group=%s,%s", wibbleGroup.Name, wobbleGroup.Name))
113+
clitest.SetupConfig(t, workspaceOwnerClient, root)
114+
115+
out := bytes.NewBuffer(nil)
116+
inv.Stdout = out
117+
err = inv.WithContext(ctx).Run()
118+
require.NoError(t, err)
119+
120+
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
121+
require.NoError(t, err)
122+
assert.Len(t, acl.Groups, 2)
123+
124+
type workspaceGroup []codersdk.WorkspaceGroup
125+
assert.NotEqual(t, -1, slices.IndexFunc(workspaceGroup(acl.Groups), func(g codersdk.WorkspaceGroup) bool {
126+
return g.Group.ID == wibbleGroup.ID
127+
}))
128+
assert.NotEqual(t, -1, slices.IndexFunc(workspaceGroup(acl.Groups), func(g codersdk.WorkspaceGroup) bool {
129+
return g.Group.ID == wobbleGroup.ID
130+
}))
131+
132+
})
133+
}
134+
135+
func createGroupWithMembers(client *codersdk.Client, ctx context.Context, orgId uuid.UUID, name string, memberIds []uuid.UUID) (codersdk.Group, error) {
136+
group, err := client.CreateGroup(ctx, orgId, codersdk.CreateGroupRequest{
137+
Name: name,
138+
DisplayName: name,
139+
})
140+
if err != nil {
141+
return codersdk.Group{}, err
142+
}
143+
144+
ids := make([]string, len(memberIds))
145+
for i, id := range memberIds {
146+
ids[i] = id.String()
147+
}
148+
149+
return client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
150+
AddUsers: ids,
73151
})
74152
}

0 commit comments

Comments
 (0)