Skip to content

Commit

Permalink
refactor: use app picker for test login command (#223)
Browse files Browse the repository at this point in the history
* refactor: use app picker for test login command

* refactor: apply changes

Co-authored-by: Rita Zerrizuela <[email protected]>
  • Loading branch information
as-herzog and Widcket authored Apr 2, 2021
1 parent 93b8b04 commit bb52349
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
5 changes: 4 additions & 1 deletion internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"gopkg.in/auth0.v5/management"
)

// errNoApps signifies no applications exist in a tenant
var errNoApps = errors.New("there are currently no applications")

const (
appTypeNative = "native"
appTypeSPA = "spa"
Expand Down Expand Up @@ -819,7 +822,7 @@ func (c *cli) appPickerOptions() (pickerOptions, error) {
}

if len(opts)+len(priorityOpts) == 0 {
return nil, errors.New("There are currently no applications.")
return nil, errNoApps
}

return append(priorityOpts, opts...), nil
Expand Down
53 changes: 43 additions & 10 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import (

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth/authutil"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
)

var (
testClientIDArg = Argument{
Name: "Client ID",
Help: "Client Id of an Auth0 application.",
}

testClientID = Flag{
Name: "Client ID",
LongForm: "client-id",
Expand Down Expand Up @@ -73,27 +80,44 @@ If --client-id is not provided, the default client "CLI Login Testing" will be u
Example: `auth0 test login
auth0 test login --client-id <id>
auth0 test login -c <id> --connection <connection>`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
const commandKey = "test_login"
var userInfo *authutil.UserInfo
isTempClient := false

tenant, err := cli.getTenant()
if err != nil {
return err
}

// use the client ID as passed in by the user, or default to the
// "CLI Login Testing" client if none passed. This client is only
// used for testing login from the CLI and will be created if it
// does not exist.
if inputs.ClientID == "" {
client, err := getOrCreateCLITesterClient(cli.api.Client)
if len(args) == 0 {
err := testClientIDArg.Pick(cmd, &inputs.ClientID, cli.appPickerOptions)
if err != nil {
return fmt.Errorf("Unable to create an app for testing the login box: %w", err)
if err != errNoApps {
return err
}
cli.renderer.Infof("No applications to select from, we will create a default test application " +
"for you and remove it once the test is complete.")
client := &management.Client{
Name: auth0.String(cliLoginTestingClientName),
Description: auth0.String(cliLoginTestingClientDescription),
Callbacks: []interface{}{cliLoginTestingCallbackURL},
InitiateLoginURI: auth0.String(cliLoginTestingInitiateLoginURI),
}
if err := cli.api.Client.Create(client); err != nil {
return fmt.Errorf("Unable to create an app for testing the login box: %w", err)
}
inputs.ClientID = client.GetClientID()
isTempClient = true
cli.renderer.Infof("Default test application successfully created\n")
}
inputs.ClientID = client.GetClientID()
} else {
inputs.ClientID = args[0]
}

defer cleanupTempApplication(isTempClient, cli, inputs.ClientID)

client, err := cli.api.Client.Read(inputs.ClientID)
if err != nil {
return fmt.Errorf("Unable to find client %s; if you specified a client, please verify it exists, otherwise re-run the command", inputs.ClientID)
Expand Down Expand Up @@ -141,13 +165,11 @@ auth0 test login -c <id> --connection <connection>`,
return err
}
}

return nil
},
}

cmd.SetUsageTemplate(resourceUsageTemplate())
testClientID.RegisterString(cmd, &inputs.ClientID, "")
testAudience.RegisterString(cmd, &inputs.Audience, "")
testConnection.RegisterString(cmd, &inputs.ConnectionName, "")
return cmd
Expand Down Expand Up @@ -240,3 +262,14 @@ auth0 test token --client-id <id> --audience <audience> --scopes <scope1,scope2>
testScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil)
return cmd
}

// cleanupTempApplication will delete the specified application if it is marked
// as a temporary application. It will log success or failure to the user.
func cleanupTempApplication(isTemp bool, cli *cli, id string) {
if isTemp {
if err := cli.api.Client.Delete(id); err != nil {
cli.renderer.Errorf("unable to remove the default test application", err.Error())
}
cli.renderer.Infof("Default test application removed")
}
}

0 comments on commit bb52349

Please sign in to comment.