@@ -497,36 +497,57 @@ func CreateFirstUser(t testing.TB, client *codersdk.Client) codersdk.CreateFirst
497
497
498
498
// CreateAnotherUser creates and authenticates a new user.
499
499
func CreateAnotherUser (t * testing.T , client * codersdk.Client , organizationID uuid.UUID , roles ... string ) (* codersdk.Client , codersdk.User ) {
500
- return createAnotherUserRetry (t , client , organizationID , 5 , roles ... )
500
+ return createAnotherUserRetry (t , client , organizationID , 5 , roles )
501
501
}
502
502
503
- func createAnotherUserRetry (t * testing.T , client * codersdk.Client , organizationID uuid.UUID , retries int , roles ... string ) (* codersdk.Client , codersdk.User ) {
503
+ func CreateAnotherUserMutators (t * testing.T , client * codersdk.Client , organizationID uuid.UUID , roles []string , mutators ... func (r * codersdk.CreateUserRequest )) (* codersdk.Client , codersdk.User ) {
504
+ return createAnotherUserRetry (t , client , organizationID , 5 , roles , mutators ... )
505
+ }
506
+
507
+ func createAnotherUserRetry (t * testing.T , client * codersdk.Client , organizationID uuid.UUID , retries int , roles []string , mutators ... func (r * codersdk.CreateUserRequest )) (* codersdk.Client , codersdk.User ) {
504
508
req := codersdk.CreateUserRequest {
505
509
Email : namesgenerator .GetRandomName (10 ) + "@coder.com" ,
506
510
Username : randomUsername (t ),
507
511
Password : "SomeSecurePassword!" ,
508
512
OrganizationID : organizationID ,
509
513
}
514
+ for _ , m := range mutators {
515
+ m (& req )
516
+ }
510
517
511
518
user , err := client .CreateUser (context .Background (), req )
512
519
var apiError * codersdk.Error
513
520
// If the user already exists by username or email conflict, try again up to "retries" times.
514
521
if err != nil && retries >= 0 && xerrors .As (err , & apiError ) {
515
522
if apiError .StatusCode () == http .StatusConflict {
516
523
retries --
517
- return createAnotherUserRetry (t , client , organizationID , retries , roles ... )
524
+ return createAnotherUserRetry (t , client , organizationID , retries , roles )
518
525
}
519
526
}
520
527
require .NoError (t , err )
521
528
522
- login , err := client .LoginWithPassword (context .Background (), codersdk.LoginWithPasswordRequest {
523
- Email : req .Email ,
524
- Password : req .Password ,
525
- })
526
- require .NoError (t , err )
529
+ var sessionToken string
530
+ if ! req .DisableLogin {
531
+ login , err := client .LoginWithPassword (context .Background (), codersdk.LoginWithPasswordRequest {
532
+ Email : req .Email ,
533
+ Password : req .Password ,
534
+ })
535
+ require .NoError (t , err )
536
+ sessionToken = login .SessionToken
537
+ } else {
538
+ // Cannot log in with a disabled login user. So make it an api key from
539
+ // the client making this user.
540
+ token , err := client .CreateToken (context .Background (), user .ID .String (), codersdk.CreateTokenRequest {
541
+ Lifetime : time .Hour * 24 ,
542
+ Scope : codersdk .APIKeyScopeAll ,
543
+ TokenName : "no-password-user-token" ,
544
+ })
545
+ require .NoError (t , err )
546
+ sessionToken = token .Key
547
+ }
527
548
528
549
other := codersdk .New (client .URL )
529
- other .SetSessionToken (login . SessionToken )
550
+ other .SetSessionToken (sessionToken )
530
551
t .Cleanup (func () {
531
552
other .HTTPClient .CloseIdleConnections ()
532
553
})
0 commit comments