Skip to content

Commit 0d061fe

Browse files
committed
chore: add tests for setting user roles
1 parent 27f9406 commit 0d061fe

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

cli/sharing_test.go

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ func TestSharingShare(t *testing.T) {
103103
Username: toShareWithUser1.Username,
104104
AvatarURL: toShareWithUser1.AvatarURL,
105105
},
106-
Role: codersdk.WorkspaceRole("use"),
106+
Role: codersdk.WorkspaceRoleUse,
107107
})
108108
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
109109
MinimalUser: codersdk.MinimalUser{
110110
ID: toShareWithUser2.ID,
111111
Username: toShareWithUser2.Username,
112112
AvatarURL: toShareWithUser2.AvatarURL,
113113
},
114-
Role: codersdk.WorkspaceRole("use"),
114+
Role: codersdk.WorkspaceRoleUse,
115115
})
116116

117117
// Test that the users appeart in the output
@@ -126,43 +126,56 @@ func TestSharingShare(t *testing.T) {
126126
}
127127
}
128128

129-
assert.True(t, found, fmt.Sprintf("Expected to find username %s in output", username))
129+
assert.True(t, found, fmt.Sprintf("Expected to find the username %s in the command output: %s", username, out.String()))
130130
}
131131
})
132132

133-
// t.Run("ShareWithUsers_Roles", func(t *testing.T) {
134-
// t.Parallel()
135-
136-
// assert.True(t, false)
137-
// })
138-
139-
// t.Run("ShareWithUsers+AdminRole", func(t *testing.T) {
140-
// t.Parallel()
141-
142-
// assert.True(t, false)
143-
// })
144-
145-
// t.Run("ShareWithGroups+Simple", func(t *testing.T) {
146-
// t.Parallel()
147-
148-
// assert.True(t, false)
149-
// })
150-
151-
// t.Run("ShareWithGroups+Mutliple", func(t *testing.T) {
152-
// t.Parallel()
133+
t.Run("ShareWithUsers_Roles", func(t *testing.T) {
134+
t.Parallel()
153135

154-
// assert.True(t, false)
155-
// })
136+
var (
137+
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{
138+
DeploymentValues: dv,
139+
})
140+
orgOwner = coderdtest.CreateFirstUser(t, client)
141+
workspaceOwnerClient, workspaceOwner = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID))
142+
workspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
143+
OwnerID: workspaceOwner.ID,
144+
OrganizationID: orgOwner.OrganizationID,
145+
}).Do().Workspace
146+
_, toShareWithUser = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID)
147+
)
156148

157-
// t.Run("ShareWithGroups+UseRole", func(t *testing.T) {
158-
// t.Parallel()
149+
ctx := testutil.Context(t, testutil.WaitMedium)
150+
var inv, root = clitest.New(t, "sharing", "share", workspace.Name,
151+
"--org", orgOwner.OrganizationID.String(),
152+
"--user", fmt.Sprintf("%s:admin", toShareWithUser.Username),
153+
)
154+
clitest.SetupConfig(t, workspaceOwnerClient, root)
159155

160-
// assert.True(t, false)
161-
// })
156+
out := bytes.NewBuffer(nil)
157+
inv.Stdout = out
158+
err := inv.WithContext(ctx).Run()
159+
require.NoError(t, err)
162160

163-
// t.Run("ShareWithGroups+AdminRole", func(t *testing.T) {
164-
// t.Parallel()
161+
acl, err := workspaceOwnerClient.WorkspaceACL(inv.Context(), workspace.ID)
162+
assert.NoError(t, err)
163+
assert.Contains(t, acl.Users, codersdk.WorkspaceUser{
164+
MinimalUser: codersdk.MinimalUser{
165+
ID: toShareWithUser.ID,
166+
Username: toShareWithUser.Username,
167+
AvatarURL: toShareWithUser.AvatarURL,
168+
},
169+
Role: codersdk.WorkspaceRoleAdmin,
170+
})
165171

166-
// assert.True(t, false)
167-
// })
172+
found := false
173+
for _, line := range strings.Split(out.String(), "\n") {
174+
if strings.Contains(line, toShareWithUser.Username) && strings.Contains(line, string(codersdk.WorkspaceRoleAdmin)) {
175+
found = true
176+
break
177+
}
178+
}
179+
assert.True(t, found, fmt.Sprintf("expected to find the username %s and role %s in the command: %s", toShareWithUser.Username, codersdk.WorkspaceRoleAdmin, out.String()))
180+
})
168181
}

0 commit comments

Comments
 (0)