@@ -17,13 +17,17 @@ import (
17
17
func TestTokens (t * testing.T ) {
18
18
t .Parallel ()
19
19
client := coderdtest .New (t , nil )
20
- _ = coderdtest .CreateFirstUser (t , client )
20
+ adminUser := coderdtest .CreateFirstUser (t , client )
21
+
22
+ secondUserClient , secondUser := coderdtest .CreateAnotherUser (t , client , adminUser .OrganizationID )
23
+ _ , thirdUser := coderdtest .CreateAnotherUser (t , client , adminUser .OrganizationID )
21
24
22
25
ctx , cancelFunc := context .WithTimeout (context .Background (), testutil .WaitLong )
23
26
defer cancelFunc ()
24
27
25
28
// helpful empty response
26
29
inv , root := clitest .New (t , "tokens" , "ls" )
30
+ //nolint:gocritic // This should be run as the owner user.
27
31
clitest .SetupConfig (t , client , root )
28
32
buf := new (bytes.Buffer )
29
33
inv .Stdout = buf
@@ -42,6 +46,19 @@ func TestTokens(t *testing.T) {
42
46
require .NotEmpty (t , res )
43
47
id := res [:10 ]
44
48
49
+ // Test creating a token for second user from first user's (admin) session
50
+ inv , root = clitest .New (t , "tokens" , "create" , "--name" , "token-two" , "--user" , secondUser .ID .String ())
51
+ clitest .SetupConfig (t , client , root )
52
+ buf = new (bytes.Buffer )
53
+ inv .Stdout = buf
54
+ err = inv .WithContext (ctx ).Run ()
55
+ // Test should succeed in creating token for second user
56
+ require .NoError (t , err )
57
+ res = buf .String ()
58
+ require .NotEmpty (t , res )
59
+ secondTokenID := res [:10 ]
60
+
61
+ // Test listing tokens from the first user's (admin) session
45
62
inv , root = clitest .New (t , "tokens" , "ls" )
46
63
clitest .SetupConfig (t , client , root )
47
64
buf = new (bytes.Buffer )
@@ -50,11 +67,39 @@ func TestTokens(t *testing.T) {
50
67
require .NoError (t , err )
51
68
res = buf .String ()
52
69
require .NotEmpty (t , res )
70
+ // Result should only contain the token created for the admin user
53
71
require .Contains (t , res , "ID" )
54
72
require .Contains (t , res , "EXPIRES AT" )
55
73
require .Contains (t , res , "CREATED AT" )
56
74
require .Contains (t , res , "LAST USED" )
57
75
require .Contains (t , res , id )
76
+ // Result should not contain the token created for the second user
77
+ require .NotContains (t , res , secondTokenID )
78
+
79
+ // Test listing tokens from the second user's session
80
+ inv , root = clitest .New (t , "tokens" , "ls" )
81
+ clitest .SetupConfig (t , secondUserClient , root )
82
+ buf = new (bytes.Buffer )
83
+ inv .Stdout = buf
84
+ err = inv .WithContext (ctx ).Run ()
85
+ require .NoError (t , err )
86
+ res = buf .String ()
87
+ require .NotEmpty (t , res )
88
+ require .Contains (t , res , "ID" )
89
+ require .Contains (t , res , "EXPIRES AT" )
90
+ require .Contains (t , res , "CREATED AT" )
91
+ require .Contains (t , res , "LAST USED" )
92
+ // Result should contain the token created for the second user
93
+ require .Contains (t , res , secondTokenID )
94
+
95
+ // Test creating a token for third user from second user's (non-admin) session
96
+ inv , root = clitest .New (t , "tokens" , "create" , "--name" , "token-two" , "--user" , thirdUser .ID .String ())
97
+ clitest .SetupConfig (t , secondUserClient , root )
98
+ buf = new (bytes.Buffer )
99
+ inv .Stdout = buf
100
+ err = inv .WithContext (ctx ).Run ()
101
+ // User (non-admin) should not be able to create a token for another user
102
+ require .Error (t , err )
58
103
59
104
inv , root = clitest .New (t , "tokens" , "ls" , "--output=json" )
60
105
clitest .SetupConfig (t , client , root )
0 commit comments