1
1
package cli_test
2
2
3
3
import (
4
+ "bytes"
5
+ "fmt"
6
+ "strings"
4
7
"testing"
5
8
6
9
"github.com/coder/coder/v2/cli/clitest"
7
10
"github.com/coder/coder/v2/coderd/coderdtest"
8
11
"github.com/coder/coder/v2/coderd/database"
9
12
"github.com/coder/coder/v2/coderd/database/dbfake"
13
+ "github.com/coder/coder/v2/coderd/rbac"
10
14
"github.com/coder/coder/v2/codersdk"
11
15
"github.com/coder/coder/v2/testutil"
12
16
"github.com/stretchr/testify/assert"
@@ -16,13 +20,18 @@ import (
16
20
func TestSharingShare (t * testing.T ) {
17
21
t .Parallel ()
18
22
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 ) {
20
27
t .Parallel ()
21
28
22
29
var (
23
- client , db = coderdtest .NewWithDatabase (t , nil )
30
+ client , db = coderdtest .NewWithDatabase (t , & coderdtest.Options {
31
+ DeploymentValues : dv ,
32
+ })
24
33
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 ) )
26
35
workspace = dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
27
36
OwnerID : workspaceOwner .ID ,
28
37
OrganizationID : orgOwner .OrganizationID ,
@@ -34,36 +43,94 @@ func TestSharingShare(t *testing.T) {
34
43
var inv , root = clitest .New (t , "sharing" , "share" , workspace .Name , "--org" , orgOwner .OrganizationID .String (), "--user" , toShareWithUser .Username )
35
44
clitest .SetupConfig (t , workspaceOwnerClient , root )
36
45
46
+ out := bytes .NewBuffer (nil )
47
+ inv .Stdout = out
37
48
err := inv .WithContext (ctx ).Run ()
38
49
require .NoError (t , err )
39
50
40
- // TODO: Test output of the command
41
-
42
51
acl , err := workspaceOwnerClient .WorkspaceACL (inv .Context (), workspace .ID )
43
52
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 ,
54
58
},
55
- acl )
59
+ Role : codersdk .WorkspaceRole ("use" ),
60
+ })
56
61
57
- assert .True (t , false )
62
+ assert .Contains (t , out .String (), toShareWithUser .Username )
63
+ assert .Contains (t , out .String (), codersdk .WorkspaceRoleUse )
58
64
})
59
65
60
- // t.Run("ShareWithUsers+Multiple ", func(t *testing.T) {
61
- // t.Parallel()
66
+ t .Run ("ShareWithUsers_Multiple " , func (t * testing.T ) {
67
+ t .Parallel ()
62
68
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
+ })
65
132
66
- // t.Run("ShareWithUsers+UseRole ", func(t *testing.T) {
133
+ // t.Run("ShareWithUsers_Roles ", func(t *testing.T) {
67
134
// t.Parallel()
68
135
69
136
// assert.True(t, false)
0 commit comments