@@ -20,6 +20,7 @@ import (
20
20
"github.com/coder/coder/v2/coderd/coderdtest"
21
21
"github.com/coder/coder/v2/codersdk"
22
22
"github.com/coder/coder/v2/pty/ptytest"
23
+ "github.com/coder/coder/v2/testutil"
23
24
)
24
25
25
26
func TestLogin (t * testing.T ) {
@@ -91,10 +92,11 @@ func TestLogin(t *testing.T) {
91
92
92
93
matches := []string {
93
94
"first user?" , "yes" ,
94
- "username" , "testuser" ,
95
- "email" , "user@coder.com" ,
96
- "password" , "SomeSecurePassword!" ,
97
- "password" , "SomeSecurePassword!" , // Confirm.
95
+ "username" , coderdtest .FirstUserParams .Username ,
96
+ "name" , coderdtest .FirstUserParams .Name ,
97
+ "email" , coderdtest .FirstUserParams .Email ,
98
+ "password" , coderdtest .FirstUserParams .Password ,
99
+ "password" , coderdtest .FirstUserParams .Password , // confirm
98
100
"trial" , "yes" ,
99
101
}
100
102
for i := 0 ; i < len (matches ); i += 2 {
@@ -105,6 +107,64 @@ func TestLogin(t *testing.T) {
105
107
}
106
108
pty .ExpectMatch ("Welcome to Coder" )
107
109
<- doneChan
110
+ ctx := testutil .Context (t , testutil .WaitShort )
111
+ resp , err := client .LoginWithPassword (ctx , codersdk.LoginWithPasswordRequest {
112
+ Email : coderdtest .FirstUserParams .Email ,
113
+ Password : coderdtest .FirstUserParams .Password ,
114
+ })
115
+ require .NoError (t , err )
116
+ client .SetSessionToken (resp .SessionToken )
117
+ me , err := client .User (ctx , codersdk .Me )
118
+ require .NoError (t , err )
119
+ assert .Equal (t , coderdtest .FirstUserParams .Username , me .Username )
120
+ assert .Equal (t , coderdtest .FirstUserParams .Name , me .Name )
121
+ assert .Equal (t , coderdtest .FirstUserParams .Email , me .Email )
122
+ })
123
+
124
+ t .Run ("InitialUserTTYNameOptional" , func (t * testing.T ) {
125
+ t .Parallel ()
126
+ client := coderdtest .New (t , nil )
127
+ // The --force-tty flag is required on Windows, because the `isatty` library does not
128
+ // accurately detect Windows ptys when they are not attached to a process:
129
+ // https://github.com/mattn/go-isatty/issues/59
130
+ doneChan := make (chan struct {})
131
+ root , _ := clitest .New (t , "login" , "--force-tty" , client .URL .String ())
132
+ pty := ptytest .New (t ).Attach (root )
133
+ go func () {
134
+ defer close (doneChan )
135
+ err := root .Run ()
136
+ assert .NoError (t , err )
137
+ }()
138
+
139
+ matches := []string {
140
+ "first user?" , "yes" ,
141
+ "username" , coderdtest .FirstUserParams .Username ,
142
+ "name" , "" ,
143
+ "email" , coderdtest .FirstUserParams .Email ,
144
+ "password" , coderdtest .FirstUserParams .Password ,
145
+ "password" , coderdtest .FirstUserParams .Password , // confirm
146
+ "trial" , "yes" ,
147
+ }
148
+ for i := 0 ; i < len (matches ); i += 2 {
149
+ match := matches [i ]
150
+ value := matches [i + 1 ]
151
+ pty .ExpectMatch (match )
152
+ pty .WriteLine (value )
153
+ }
154
+ pty .ExpectMatch ("Welcome to Coder" )
155
+ <- doneChan
156
+ ctx := testutil .Context (t , testutil .WaitShort )
157
+ resp , err := client .LoginWithPassword (ctx , codersdk.LoginWithPasswordRequest {
158
+ Email : coderdtest .FirstUserParams .Email ,
159
+ Password : coderdtest .FirstUserParams .Password ,
160
+ })
161
+ require .NoError (t , err )
162
+ client .SetSessionToken (resp .SessionToken )
163
+ me , err := client .User (ctx , codersdk .Me )
164
+ require .NoError (t , err )
165
+ assert .Equal (t , coderdtest .FirstUserParams .Username , me .Username )
166
+ assert .Equal (t , coderdtest .FirstUserParams .Email , me .Email )
167
+ assert .Empty (t , me .Name )
108
168
})
109
169
110
170
t .Run ("InitialUserTTYFlag" , func (t * testing.T ) {
@@ -121,10 +181,11 @@ func TestLogin(t *testing.T) {
121
181
pty .ExpectMatch (fmt .Sprintf ("Attempting to authenticate with flag URL: '%s'" , client .URL .String ()))
122
182
matches := []string {
123
183
"first user?" , "yes" ,
124
- "username" , "testuser" ,
125
- "email" , "user@coder.com" ,
126
- "password" , "SomeSecurePassword!" ,
127
- "password" , "SomeSecurePassword!" , // Confirm.
184
+ "username" , coderdtest .FirstUserParams .Username ,
185
+ "name" , coderdtest .FirstUserParams .Name ,
186
+ "email" , coderdtest .FirstUserParams .Email ,
187
+ "password" , coderdtest .FirstUserParams .Password ,
188
+ "password" , coderdtest .FirstUserParams .Password , // confirm
128
189
"trial" , "yes" ,
129
190
}
130
191
for i := 0 ; i < len (matches ); i += 2 {
@@ -134,20 +195,75 @@ func TestLogin(t *testing.T) {
134
195
pty .WriteLine (value )
135
196
}
136
197
pty .ExpectMatch ("Welcome to Coder" )
198
+ ctx := testutil .Context (t , testutil .WaitShort )
199
+ resp , err := client .LoginWithPassword (ctx , codersdk.LoginWithPasswordRequest {
200
+ Email : coderdtest .FirstUserParams .Email ,
201
+ Password : coderdtest .FirstUserParams .Password ,
202
+ })
203
+ require .NoError (t , err )
204
+ client .SetSessionToken (resp .SessionToken )
205
+ me , err := client .User (ctx , codersdk .Me )
206
+ require .NoError (t , err )
207
+ assert .Equal (t , coderdtest .FirstUserParams .Username , me .Username )
208
+ assert .Equal (t , coderdtest .FirstUserParams .Name , me .Name )
209
+ assert .Equal (t , coderdtest .FirstUserParams .Email , me .Email )
137
210
})
138
211
139
212
t .Run ("InitialUserFlags" , func (t * testing.T ) {
140
213
t .Parallel ()
141
214
client := coderdtest .New (t , nil )
142
215
inv , _ := clitest .New (
143
216
t , "login" , client .URL .String (),
144
- "--first-user-username" , "testuser" , "--first-user-email" , "user@coder.com" ,
145
- "--first-user-password" , "SomeSecurePassword!" , "--first-user-trial" ,
217
+ "--first-user-username" , coderdtest .FirstUserParams .Username ,
218
+ "--first-user-full-name" , coderdtest .FirstUserParams .Name ,
219
+ "--first-user-email" , coderdtest .FirstUserParams .Email ,
220
+ "--first-user-password" , coderdtest .FirstUserParams .Password ,
221
+ "--first-user-trial" ,
222
+ )
223
+ pty := ptytest .New (t ).Attach (inv )
224
+ w := clitest .StartWithWaiter (t , inv )
225
+ pty .ExpectMatch ("Welcome to Coder" )
226
+ w .RequireSuccess ()
227
+ ctx := testutil .Context (t , testutil .WaitShort )
228
+ resp , err := client .LoginWithPassword (ctx , codersdk.LoginWithPasswordRequest {
229
+ Email : coderdtest .FirstUserParams .Email ,
230
+ Password : coderdtest .FirstUserParams .Password ,
231
+ })
232
+ require .NoError (t , err )
233
+ client .SetSessionToken (resp .SessionToken )
234
+ me , err := client .User (ctx , codersdk .Me )
235
+ require .NoError (t , err )
236
+ assert .Equal (t , coderdtest .FirstUserParams .Username , me .Username )
237
+ assert .Equal (t , coderdtest .FirstUserParams .Name , me .Name )
238
+ assert .Equal (t , coderdtest .FirstUserParams .Email , me .Email )
239
+ })
240
+
241
+ t .Run ("InitialUserFlagsNameOptional" , func (t * testing.T ) {
242
+ t .Parallel ()
243
+ client := coderdtest .New (t , nil )
244
+ inv , _ := clitest .New (
245
+ t , "login" , client .URL .String (),
246
+ "--first-user-username" , coderdtest .FirstUserParams .Username ,
247
+ "--first-user-email" , coderdtest .FirstUserParams .Email ,
248
+ "--first-user-password" , coderdtest .FirstUserParams .Password ,
249
+ "--first-user-trial" ,
146
250
)
147
251
pty := ptytest .New (t ).Attach (inv )
148
252
w := clitest .StartWithWaiter (t , inv )
149
253
pty .ExpectMatch ("Welcome to Coder" )
150
254
w .RequireSuccess ()
255
+ ctx := testutil .Context (t , testutil .WaitShort )
256
+ resp , err := client .LoginWithPassword (ctx , codersdk.LoginWithPasswordRequest {
257
+ Email : coderdtest .FirstUserParams .Email ,
258
+ Password : coderdtest .FirstUserParams .Password ,
259
+ })
260
+ require .NoError (t , err )
261
+ client .SetSessionToken (resp .SessionToken )
262
+ me , err := client .User (ctx , codersdk .Me )
263
+ require .NoError (t , err )
264
+ assert .Equal (t , coderdtest .FirstUserParams .Username , me .Username )
265
+ assert .Equal (t , coderdtest .FirstUserParams .Email , me .Email )
266
+ assert .Empty (t , me .Name )
151
267
})
152
268
153
269
t .Run ("InitialUserTTYConfirmPasswordFailAndReprompt" , func (t * testing.T ) {
@@ -169,10 +285,11 @@ func TestLogin(t *testing.T) {
169
285
170
286
matches := []string {
171
287
"first user?" , "yes" ,
172
- "username" , "testuser" ,
173
- "email" , "user@coder.com" ,
174
- "password" , "MyFirstSecurePassword!" ,
175
- "password" , "MyNonMatchingSecurePassword!" , // Confirm.
288
+ "username" , coderdtest .FirstUserParams .Username ,
289
+ "name" , coderdtest .FirstUserParams .Name ,
290
+ "email" , coderdtest .FirstUserParams .Email ,
291
+ "password" , coderdtest .FirstUserParams .Password ,
292
+ "password" , "something completely different" ,
176
293
}
177
294
for i := 0 ; i < len (matches ); i += 2 {
178
295
match := matches [i ]
@@ -185,9 +302,9 @@ func TestLogin(t *testing.T) {
185
302
pty .ExpectMatch ("Passwords do not match" )
186
303
pty .ExpectMatch ("Enter a " + pretty .Sprint (cliui .DefaultStyles .Field , "password" ))
187
304
188
- pty .WriteLine ("SomeSecurePassword!" )
305
+ pty .WriteLine (coderdtest . FirstUserParams . Password )
189
306
pty .ExpectMatch ("Confirm" )
190
- pty .WriteLine ("SomeSecurePassword!" )
307
+ pty .WriteLine (coderdtest . FirstUserParams . Password )
191
308
pty .ExpectMatch ("trial" )
192
309
pty .WriteLine ("yes" )
193
310
pty .ExpectMatch ("Welcome to Coder" )
0 commit comments