@@ -20,6 +20,7 @@ import (
2020"github.com/coder/coder/v2/coderd/coderdtest"
2121"github.com/coder/coder/v2/codersdk"
2222"github.com/coder/coder/v2/pty/ptytest"
23+ "github.com/coder/coder/v2/testutil"
2324)
2425
2526func TestLogin (t * testing.T ) {
@@ -91,10 +92,11 @@ func TestLogin(t *testing.T) {
9192
9293matches := []string {
9394"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
98100"trial" ,"yes" ,
99101}
100102for i := 0 ;i < len (matches );i += 2 {
@@ -105,6 +107,64 @@ func TestLogin(t *testing.T) {
105107}
106108pty .ExpectMatch ("Welcome to Coder" )
107109<- 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 )
108168})
109169
110170t .Run ("InitialUserTTYFlag" ,func (t * testing.T ) {
@@ -121,10 +181,11 @@ func TestLogin(t *testing.T) {
121181pty .ExpectMatch (fmt .Sprintf ("Attempting to authenticate with flag URL: '%s'" ,client .URL .String ()))
122182matches := []string {
123183"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
128189"trial" ,"yes" ,
129190}
130191for i := 0 ;i < len (matches );i += 2 {
@@ -134,20 +195,75 @@ func TestLogin(t *testing.T) {
134195pty .WriteLine (value )
135196}
136197pty .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 )
137210})
138211
139212t .Run ("InitialUserFlags" ,func (t * testing.T ) {
140213t .Parallel ()
141214client := coderdtest .New (t ,nil )
142215inv ,_ := clitest .New (
143216t ,"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" ,
146250)
147251pty := ptytest .New (t ).Attach (inv )
148252w := clitest .StartWithWaiter (t ,inv )
149253pty .ExpectMatch ("Welcome to Coder" )
150254w .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 )
151267})
152268
153269t .Run ("InitialUserTTYConfirmPasswordFailAndReprompt" ,func (t * testing.T ) {
@@ -169,10 +285,11 @@ func TestLogin(t *testing.T) {
169285
170286matches := []string {
171287"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" ,
176293}
177294for i := 0 ;i < len (matches );i += 2 {
178295match := matches [i ]
@@ -185,9 +302,9 @@ func TestLogin(t *testing.T) {
185302pty .ExpectMatch ("Passwords do not match" )
186303pty .ExpectMatch ("Enter a " + pretty .Sprint (cliui .DefaultStyles .Field ,"password" ))
187304
188- pty .WriteLine ("SomeSecurePassword!" )
305+ pty .WriteLine (coderdtest . FirstUserParams . Password )
189306pty .ExpectMatch ("Confirm" )
190- pty .WriteLine ("SomeSecurePassword!" )
307+ pty .WriteLine (coderdtest . FirstUserParams . Password )
191308pty .ExpectMatch ("trial" )
192309pty .WriteLine ("yes" )
193310pty .ExpectMatch ("Welcome to Coder" )