Skip to content

Commit

Permalink
Fix users search command to correctly search through result pages
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jan 9, 2023
1 parent 3056670 commit e8d3a52
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
66 changes: 47 additions & 19 deletions internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand All @@ -139,41 +146,62 @@ 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" --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))
}

if inputs.number < 1 || inputs.number > 1000 {
return fmt.Errorf("number flag invalid, please pass a number between 1 and 1000")
}

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
},
}

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
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scripts/get-user-id.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/bash

user=$( auth0 users create -n integration-test-user-better -c Username-Password-Authentication -e [email protected] -p testUser12 --json --no-input )
user=$( auth0 users create -n integration-test-user-better -c Username-Password-Authentication -e [email protected] -p testUser12 -u cli-test --json --no-input )

mkdir -p ./test/integration/identifiers
echo "$user" | jq -r '.["user_id"]' > ./test/integration/identifiers/user-id
11 changes: 9 additions & 2 deletions test/integration/test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,15 @@ tests:

# Test 'users create'
users create and check data:
command: auth0 users create --name integration-test-user-new --connection Username-Password-Authentication --email [email protected] --password testUser12 --json --no-input
command: auth0 users create --name integration-test-user-new --connection Username-Password-Authentication --email [email protected] --password testUser12 --username testuser1 --json --no-input
exit-code: 0
stdout:
json:
email: "[email protected]"
connection: "Username-Password-Authentication"

users create and check output:
command: auth0 users create --name integration-test-user-new2 --connection Username-Password-Authentication --email [email protected] --password testUser12 --no-input
command: auth0 users create --name integration-test-user-new2 --connection Username-Password-Authentication --email [email protected] --password testUser12 --username testuser2 --no-input
exit-code: 0
stdout:
contains:
Expand Down Expand Up @@ -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:
- [email protected]

# Test 'users update'
users update email:
command: auth0 users update $(cat ./test/integration/identifiers/user-id) --email [email protected] --json --no-input
Expand Down

0 comments on commit e8d3a52

Please sign in to comment.