@@ -2,6 +2,9 @@ package cli_test
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
6
+ "fmt"
7
+ "slices"
5
8
"testing"
6
9
7
10
"github.com/coder/coder/v2/cli/clitest"
@@ -13,6 +16,7 @@ import (
13
16
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
14
17
"github.com/coder/coder/v2/enterprise/coderd/license"
15
18
"github.com/coder/coder/v2/testutil"
19
+ "github.com/google/uuid"
16
20
"github.com/stretchr/testify/assert"
17
21
"github.com/stretchr/testify/require"
18
22
)
@@ -47,17 +51,10 @@ func TestSharingShareEnterprise(t *testing.T) {
47
51
48
52
ctx := testutil .Context (t , testutil .WaitMedium )
49
53
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 })
58
55
require .NoError (t , err )
59
56
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 )
61
58
clitest .SetupConfig (t , workspaceOwnerClient , root )
62
59
63
60
out := bytes .NewBuffer (nil )
@@ -70,5 +67,86 @@ func TestSharingShareEnterprise(t *testing.T) {
70
67
assert .Len (t , acl .Groups , 1 )
71
68
assert .Equal (t , acl .Groups [0 ].Group .ID , group .ID )
72
69
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 ,
73
151
})
74
152
}
0 commit comments