MCPcopy Index your code
hub / github.com/coder/coder / TestPaginatedUsers

Function TestPaginatedUsers

coderd/users_test.go:2950–3041  ·  view source on GitHub ↗

TestPaginatedUsers creates a list of users, then tries to paginate through them using different page sizes.

(t *testing.T)

Source from the content-addressed store, hash-verified

2948// TestPaginatedUsers creates a list of users, then tries to paginate through
2949// them using different page sizes.
2950func TestPaginatedUsers(t *testing.T) {
2951 t.Parallel()
2952 client, db := coderdtest.NewWithDatabase(t, nil)
2953 coderdtest.CreateFirstUser(t, client)
2954
2955 // This test takes longer than a long time.
2956 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong*4)
2957 t.Cleanup(cancel)
2958
2959 me, err := client.User(ctx, codersdk.Me)
2960 require.NoError(t, err)
2961
2962 // When 50 users exist
2963 total := 50
2964 allUsers := make([]database.User, total+1)
2965 allUsers[0] = database.User{
2966 Email: me.Email,
2967 Username: me.Username,
2968 }
2969 specialUsers := make([]database.User, total/2)
2970
2971 eg, _ := errgroup.WithContext(ctx)
2972 // Create users
2973 for i := 0; i < total; i++ {
2974 eg.Go(func() error {
2975 email := fmt.Sprintf("%d@coder.com", i)
2976 username := fmt.Sprintf("user%d", i)
2977 if i%2 == 0 {
2978 email = fmt.Sprintf("%d@gmail.com", i)
2979 username = fmt.Sprintf("specialuser%d", i)
2980 }
2981 if i%3 == 0 {
2982 username = strings.ToUpper(username)
2983 }
2984
2985 // We used to use the API to ceate users, but that is slow.
2986 // Instead, we create them directly in the database now
2987 // to prevent timeout flakes.
2988 newUser := dbgen.User(t, db, database.User{
2989 Email: email,
2990 Username: username,
2991 })
2992 allUsers[i+1] = newUser
2993 if i%2 == 0 {
2994 specialUsers[i/2] = newUser
2995 }
2996
2997 return nil
2998 })
2999 }
3000 err = eg.Wait()
3001 require.NoError(t, err, "create users failed")
3002
3003 // Sorting the users will sort by username.
3004 sortDatabaseUsers(allUsers)
3005 sortDatabaseUsers(specialUsers)
3006
3007 gmailSearch := func(request codersdk.UsersRequest) codersdk.UsersRequest {

Callers

nothing calls this directly

Calls 11

NewWithDatabaseFunction · 0.92
CreateFirstUserFunction · 0.92
UserFunction · 0.92
sortDatabaseUsersFunction · 0.85
assertPaginationFunction · 0.85
WithContextMethod · 0.80
GoMethod · 0.80
CleanupMethod · 0.65
UserMethod · 0.65
WaitMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected