@@ -33,6 +33,7 @@ import (
33
33
"github.com/spf13/afero"
34
34
"github.com/stretchr/testify/assert"
35
35
"github.com/stretchr/testify/require"
36
+ "golang.org/x/xerrors"
36
37
"google.golang.org/api/idtoken"
37
38
"google.golang.org/api/option"
38
39
@@ -265,13 +266,26 @@ func CreateFirstUser(t *testing.T, client *codersdk.Client) codersdk.CreateFirst
265
266
266
267
// CreateAnotherUser creates and authenticates a new user.
267
268
func CreateAnotherUser (t * testing.T , client * codersdk.Client , organizationID uuid.UUID , roles ... string ) * codersdk.Client {
269
+ return createAnotherUserRetry (t , client , organizationID , 5 , roles ... )
270
+ }
271
+
272
+ func createAnotherUserRetry (t * testing.T , client * codersdk.Client , organizationID uuid.UUID , retries int , roles ... string ) * codersdk.Client {
268
273
req := codersdk.CreateUserRequest {
269
274
Email : namesgenerator .GetRandomName (1 ) + "@coder.com" ,
270
275
Username : randomUsername (),
271
276
Password : "testpass" ,
272
277
OrganizationID : organizationID ,
273
278
}
279
+
274
280
user , err := client .CreateUser (context .Background (), req )
281
+ var apiError * codersdk.Error
282
+ // If the user already exists by username or email conflict, try again up to "retries" times.
283
+ if err != nil && retries >= 0 && xerrors .As (err , & apiError ) {
284
+ if apiError .StatusCode () == http .StatusConflict {
285
+ retries --
286
+ return createAnotherUserRetry (t , client , organizationID , retries , roles ... )
287
+ }
288
+ }
275
289
require .NoError (t , err )
276
290
277
291
login , err := client .LoginWithPassword (context .Background (), codersdk.LoginWithPasswordRequest {
0 commit comments