From 217e5dc244a81d6129daebc3910152bc1e033c93 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Mon, 9 Jan 2023 11:43:08 +0100 Subject: [PATCH] Fix users search command to correctly search through result pages --- docs/auth0_users_search.md | 5 +- internal/cli/users.go | 63 +++++++++++++++++-------- test/integration/scripts/get-user-id.sh | 2 +- test/integration/test-cases.yaml | 11 ++++- 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/docs/auth0_users_search.md b/docs/auth0_users_search.md index 7bb03e348..7c4594f0b 100644 --- a/docs/auth0_users_search.md +++ b/docs/auth0_users_search.md @@ -16,8 +16,8 @@ auth0 users search [flags] auth0 users search auth0 users search --query id auth0 users search -q name --sort "name:1" - auth0 users search -q name -s "name:1" - auth0 users search -q name -s "name:1" --json + auth0 users search -q name -s "name:1" --number 200 + auth0 users search -q name -s "name:1" -n 200 --json ``` @@ -25,6 +25,7 @@ auth0 users search ``` --json Output in json format. + -n, --number int Number of users, that match the search criteria, to retrieve. Maximum result number is 1000. If limit is hit, please refine the search query. (default 50) -q, --query string Query in Lucene query syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details. -s, --sort string Field to sort by. Use 'field:order' where 'order' is '1' for ascending and '-1' for descending. e.g. 'created_at:1'. ``` diff --git a/internal/cli/users.go b/internal/cli/users.go index 326fca6e5..18b2efb61 100644 --- a/internal/cli/users.go +++ b/internal/cli/users.go @@ -68,6 +68,12 @@ var ( ShortForm: "s", Help: "Field to sort by. Use 'field:order' where 'order' is '1' for ascending and '-1' for descending. e.g. 'created_at:1'.", } + userNumber = Flag{ + Name: "Number", + LongForm: "number", + ShortForm: "n", + Help: "Number of users, that match the search criteria, to retrieve. Maximum result number is 1000. If limit is hit, please refine the search query.", + } userImportTemplate = Flag{ Name: "Template", LongForm: "template", @@ -127,8 +133,9 @@ func usersCmd(cli *cli) *cobra.Command { func searchUsersCmd(cli *cli) *cobra.Command { var inputs struct { - query string - sort string + query string + sort string + number int } cmd := &cobra.Command{ @@ -139,35 +146,50 @@ func searchUsersCmd(cli *cli) *cobra.Command { Example: `auth0 users search auth0 users search --query id auth0 users search -q name --sort "name:1" - auth0 users search -q name -s "name:1" - auth0 users search -q name -s "name:1" --json`, + auth0 users search -q name -s "name:1" --number 200 + auth0 users search -q name -s "name:1" -n 200 --json`, RunE: func(cmd *cobra.Command, args []string) error { if err := userQuery.Ask(cmd, &inputs.query, nil); err != nil { return err } - search := &management.UserList{} + queryParams := []management.RequestOption{ + management.Query(inputs.query), + } + if inputs.sort != "" { + queryParams = append(queryParams, management.Parameter("sort", inputs.sort)) + } - var queryParams []management.RequestOption + list, err := getWithPagination( + cmd.Context(), + inputs.number, + func(opts ...management.RequestOption) (result []interface{}, hasNext bool, err error) { + opts = append(opts, queryParams...) - if len(inputs.sort) == 0 { - queryParams = append(queryParams, management.Query(auth0.StringValue(&inputs.query))) - } else { - queryParams = append(queryParams, - management.Query(auth0.StringValue(&inputs.query)), - management.Parameter("sort", auth0.StringValue(&inputs.sort)), - ) + userList, err := cli.api.User.Search(opts...) + if err != nil { + return nil, false, err + } + + var output []interface{} + for _, user := range userList.Users { + output = append(output, user) + } + + return output, userList.HasNext(), nil + }, + ) + if err != nil { + return fmt.Errorf("failed to search for users: %w", err) } - if err := ansi.Waiting(func() error { - var err error - search, err = cli.api.User.Search(queryParams...) - return err - }); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) + var foundUsers []*management.User + for _, item := range list { + foundUsers = append(foundUsers, item.(*management.User)) } - cli.renderer.UserSearch(search.Users) + cli.renderer.UserSearch(foundUsers) + return nil }, } @@ -175,6 +197,7 @@ func searchUsersCmd(cli *cli) *cobra.Command { cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.") userQuery.RegisterString(cmd, &inputs.query, "") userSort.RegisterString(cmd, &inputs.sort, "") + userNumber.RegisterInt(cmd, &inputs.number, defaultPageSize) return cmd } diff --git a/test/integration/scripts/get-user-id.sh b/test/integration/scripts/get-user-id.sh index 2963b22b7..a447b3d9f 100755 --- a/test/integration/scripts/get-user-id.sh +++ b/test/integration/scripts/get-user-id.sh @@ -1,6 +1,6 @@ #! /bin/bash -user=$( auth0 users create -n integration-test-user-better -c Username-Password-Authentication -e newuser@example.com -p testUser12 --json --no-input ) +user=$( auth0 users create -n integration-test-user-better -c Username-Password-Authentication -e newuser@example.com -p testUser12 -u cli-test --json --no-input ) mkdir -p ./test/integration/identifiers echo "$user" | jq -r '.["user_id"]' > ./test/integration/identifiers/user-id diff --git a/test/integration/test-cases.yaml b/test/integration/test-cases.yaml index 255573f57..40acad049 100644 --- a/test/integration/test-cases.yaml +++ b/test/integration/test-cases.yaml @@ -399,7 +399,7 @@ tests: # Test 'users create' users create and check data: - command: auth0 users create --name integration-test-user-new --connection Username-Password-Authentication --email testuser@example.com --password testUser12 --json --no-input + command: auth0 users create --name integration-test-user-new --connection Username-Password-Authentication --email testuser@example.com --password testUser12 --username testuser1 --json --no-input exit-code: 0 stdout: json: @@ -407,7 +407,7 @@ tests: connection: "Username-Password-Authentication" users create and check output: - command: auth0 users create --name integration-test-user-new2 --connection Username-Password-Authentication --email testuser2@example.com --password testUser12 --no-input + command: auth0 users create --name integration-test-user-new2 --connection Username-Password-Authentication --email testuser2@example.com --password testUser12 --username testuser2 --no-input exit-code: 0 stdout: contains: @@ -435,6 +435,13 @@ tests: - CONNECTION Username-Password-Authentication exit-code: 0 + users search: + command: auth0 users search --query user_id:"$(cat ./test/integration/identifiers/user-id)" --number 1 --sort "name:-1" + exit-code: 0 + stdout: + contains: + - newuser@example.com + # Test 'users update' users update email: command: auth0 users update $(cat ./test/integration/identifiers/user-id) --email betteruser@example.com --json --no-input