Skip to content

Commit 27f9406

Browse files
committed
chore: add tests for multi-user sharing
1 parent a8565fc commit 27f9406

File tree

2 files changed

+93
-28
lines changed

2 files changed

+93
-28
lines changed

cli/sharing.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
3737
var (
3838
// Username regex taken from codersdk/name.go
3939
userAndGroupRegex = regexp.MustCompile(`(^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)+(?::([A-Za-z0-9-]+))?`)
40-
41-
client = new(codersdk.Client)
42-
users []string
43-
groups []string
44-
formatter = cliui.NewOutputFormatter(
40+
client = new(codersdk.Client)
41+
users []string
42+
groups []string
43+
formatter = cliui.NewOutputFormatter(
4544
cliui.TableFormat(
4645
[]workspaceShareRow{}, []string{"User", "Group", "Role"}),
4746
cliui.JSONFormat(),
@@ -178,7 +177,6 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
178177
})
179178
}
180179
}
181-
182180
out, err := formatter.Format(inv.Context(), outputRows)
183181
if err != nil {
184182
return err

cli/sharing_test.go

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package cli_test
22

33
import (
4+
"bytes"
5+
"fmt"
6+
"strings"
47
"testing"
58

69
"github.com/coder/coder/v2/cli/clitest"
710
"github.com/coder/coder/v2/coderd/coderdtest"
811
"github.com/coder/coder/v2/coderd/database"
912
"github.com/coder/coder/v2/coderd/database/dbfake"
13+
"github.com/coder/coder/v2/coderd/rbac"
1014
"github.com/coder/coder/v2/codersdk"
1115
"github.com/coder/coder/v2/testutil"
1216
"github.com/stretchr/testify/assert"
@@ -16,13 +20,18 @@ import (
1620
func TestSharingShare(t *testing.T) {
1721
t.Parallel()
1822

19-
t.Run("ShareWithUsers+Simple", func(t *testing.T) {
23+
dv := coderdtest.DeploymentValues(t)
24+
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)}
25+
26+
t.Run("ShareWithUsers_Simple", func(t *testing.T) {
2027
t.Parallel()
2128

2229
var (
23-
client, db = coderdtest.NewWithDatabase(t, nil)
30+
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{
31+
DeploymentValues: dv,
32+
})
2433
orgOwner = coderdtest.CreateFirstUser(t, client)
25-
workspaceOwnerClient, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID)
34+
workspaceOwnerClient, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID))
2635
workspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
2736
OwnerID: workspaceOwner.ID,
2837
OrganizationID: orgOwner.OrganizationID,
@@ -34,36 +43,94 @@ func TestSharingShare(t *testing.T) {
3443
var inv, root = clitest.New(t, "sharing", "share", workspace.Name, "--org", orgOwner.OrganizationID.String(), "--user", toShareWithUser.Username)
3544
clitest.SetupConfig(t, workspaceOwnerClient, root)
3645

46+
out := bytes.NewBuffer(nil)
47+
inv.Stdout = out
3748
err := inv.WithContext(ctx).Run()
3849
require.NoError(t, err)
3950

40-
// TODO: Test output of the command
41-
4251
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
4352
assert.NoError(t, err)
44-
assert.Equal(t,
45-
codersdk.WorkspaceACL{
46-
Users: []codersdk.WorkspaceUser{{
47-
MinimalUser: codersdk.MinimalUser{
48-
ID: toShareWithUser.ID,
49-
Username: toShareWithUser.Username,
50-
AvatarURL: toShareWithUser.AvatarURL,
51-
},
52-
Role: codersdk.WorkspaceRole("use"),
53-
}},
53+
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
54+
MinimalUser: codersdk.MinimalUser{
55+
ID: toShareWithUser.ID,
56+
Username: toShareWithUser.Username,
57+
AvatarURL: toShareWithUser.AvatarURL,
5458
},
55-
acl)
59+
Role: codersdk.WorkspaceRole("use"),
60+
})
5661

57-
assert.True(t, false)
62+
assert.Contains(t, out.String(), toShareWithUser.Username)
63+
assert.Contains(t, out.String(), codersdk.WorkspaceRoleUse)
5864
})
5965

60-
// t.Run("ShareWithUsers+Multiple", func(t *testing.T) {
61-
// t.Parallel()
66+
t.Run("ShareWithUsers_Multiple", func(t *testing.T) {
67+
t.Parallel()
6268

63-
// assert.True(t, false)
64-
// })
69+
var (
70+
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{
71+
DeploymentValues: dv,
72+
})
73+
orgOwner = coderdtest.CreateFirstUser(t, client)
74+
75+
workspaceOwnerClient, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID))
76+
workspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
77+
OwnerID: workspaceOwner.ID,
78+
OrganizationID: orgOwner.OrganizationID,
79+
}).Do().Workspace
80+
81+
_, toShareWithUser1 = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID)
82+
_, toShareWithUser2 = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID)
83+
)
84+
85+
ctx := testutil.Context(t, testutil.WaitMedium)
86+
var inv, root = clitest.New(t,
87+
"sharing",
88+
"share", workspace.Name, "--org", orgOwner.OrganizationID.String(),
89+
fmt.Sprintf("--user=%s,%s", toShareWithUser1.Username, toShareWithUser2.Username),
90+
)
91+
clitest.SetupConfig(t, workspaceOwnerClient, root)
92+
93+
out := bytes.NewBuffer(nil)
94+
inv.Stdout = out
95+
err := inv.WithContext(ctx).Run()
96+
require.NoError(t, err)
97+
98+
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
99+
assert.NoError(t, err)
100+
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
101+
MinimalUser: codersdk.MinimalUser{
102+
ID: toShareWithUser1.ID,
103+
Username: toShareWithUser1.Username,
104+
AvatarURL: toShareWithUser1.AvatarURL,
105+
},
106+
Role: codersdk.WorkspaceRole("use"),
107+
})
108+
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
109+
MinimalUser: codersdk.MinimalUser{
110+
ID: toShareWithUser2.ID,
111+
Username: toShareWithUser2.Username,
112+
AvatarURL: toShareWithUser2.AvatarURL,
113+
},
114+
Role: codersdk.WorkspaceRole("use"),
115+
})
116+
117+
// Test that the users appeart in the output
118+
outputLines := strings.Split(out.String(), "\n")
119+
userNames := []string{toShareWithUser1.Username, toShareWithUser2.Username}
120+
for _, username := range userNames {
121+
found := false
122+
for _, line := range outputLines {
123+
if strings.Contains(line, username) {
124+
found = true
125+
break
126+
}
127+
}
128+
129+
assert.True(t, found, fmt.Sprintf("Expected to find username %s in output", username))
130+
}
131+
})
65132

66-
// t.Run("ShareWithUsers+UseRole", func(t *testing.T) {
133+
// t.Run("ShareWithUsers_Roles", func(t *testing.T) {
67134
// t.Parallel()
68135

69136
// assert.True(t, false)

0 commit comments

Comments
 (0)