Skip to content

Commit 96de7d3

Browse files
committed
test: Check created_at for prepareData to ensure user order
1 parent 3cb9b3d commit 96de7d3

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

cli/root_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"runtime"
1414
"strings"
1515
"testing"
16+
"time"
1617

1718
"github.com/spf13/cobra"
1819
"github.com/stretchr/testify/assert"
@@ -197,13 +198,32 @@ func prepareTestData(t *testing.T) (*codersdk.Client, map[string]string) {
197198
IncludeProvisionerDaemon: true,
198199
})
199200
firstUser := coderdtest.CreateFirstUser(t, rootClient)
200-
secondUser, err := rootClient.CreateUser(ctx, codersdk.CreateUserRequest{
201-
Email: "testuser2@coder.com",
202-
Username: "testuser2",
203-
Password: coderdtest.FirstUserParams.Password,
204-
OrganizationID: firstUser.OrganizationID,
205-
})
201+
202+
firstUserData, err := rootClient.User(ctx, firstUser.UserID.String())
206203
require.NoError(t, err)
204+
205+
var secondUser codersdk.User
206+
for {
207+
secondUser, err = rootClient.CreateUser(ctx, codersdk.CreateUserRequest{
208+
Email: "testuser2@coder.com",
209+
Username: "testuser2",
210+
Password: coderdtest.FirstUserParams.Password,
211+
OrganizationID: firstUser.OrganizationID,
212+
})
213+
require.NoError(t, err)
214+
215+
// If the second user has the same timestamp as the first user, delete it and try again.
216+
// This is because user order is determined by creation time, and we want to ensure
217+
// second user is listed after first user.
218+
// Round to microsecond since that is the precision of the database.
219+
if secondUser.CreatedAt.Round(time.Microsecond).Equal(firstUserData.CreatedAt.Round(time.Microsecond)) {
220+
err = rootClient.DeleteUser(ctx, secondUser.ID)
221+
require.NoError(t, err)
222+
continue
223+
}
224+
break
225+
}
226+
207227
version := coderdtest.CreateTemplateVersion(t, rootClient, firstUser.OrganizationID, nil)
208228
version = coderdtest.AwaitTemplateVersionJob(t, rootClient, version.ID)
209229
template := coderdtest.CreateTemplate(t, rootClient, firstUser.OrganizationID, version.ID, func(req *codersdk.CreateTemplateRequest) {

0 commit comments

Comments
 (0)