Skip to content

Commit

Permalink
fixed selection of user on interactive mode (#43)
Browse files Browse the repository at this point in the history
wrapped MaxNArgs from cobra to enable tests to be debugged
  • Loading branch information
aricart authored Dec 8, 2018
1 parent fb0fad4 commit 1e3ccb7
Show file tree
Hide file tree
Showing 27 changed files with 103 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/addaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func CreateAddAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account",
Short: "Add an account",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,

RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/addcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createAddClusterCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "Add a cluster (operator only)",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
Example: `nsc add cluster --name mycluster --trusted-accounts actkey1,actkey2 --trusted-operators opkey1,opkey2`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/addexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createAddExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Add an export",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
Example: `nsc add export -i
nsc add export --subject "a.b.c.>"
nsc add export --service --subject a.b
Expand Down
2 changes: 1 addition & 1 deletion cmd/addimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func createAddImportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "import",
Short: "Add an import",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
Example: params.longHelp(),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/addserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createAddServerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "Add a server to a cluster (operator only)",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
Example: `nsc add server -i`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/adduser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func CreateAddUserCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "user",
Short: "Add an user to the account",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
Example: `nsc add user -i
nsc add user --name u --deny-pubsub "bar.>"
Expand Down
10 changes: 10 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"strings"
"time"

"github.com/spf13/cobra"

"github.com/dustin/go-humanize"
"github.com/mitchellh/go-homedir"
"github.com/nats-io/nkeys"
Expand Down Expand Up @@ -354,3 +356,11 @@ func AbbrevHomePaths(fp string) string {
}
return fp
}

func MaxArgs(max int) cobra.PositionalArgs {
// if we are running in a test, remove the limit
if strings.Contains(strings.Join(os.Args, " "), "-test.v") {
return nil
}
return cobra.MaximumNArgs(max)
}
2 changes: 1 addition & 1 deletion cmd/deleteexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createDeleteExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Delete an export",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/deleteimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createDeleteImportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "import",
Short: "Delete an import",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
Example: `nsc delete import -i
nsc delete import -s "bar.>"`,
SilenceUsage: true,
Expand Down
2 changes: 1 addition & 1 deletion cmd/describeaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createDescribeAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account",
Short: "Describes an account",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/describecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createDescribeClusterCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "Describes a cluster",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/describejwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createDescribeJwtCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "jwt",
Short: "Describe a jwt file",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
Example: `nsc describe -f pathorurl`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/describeoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createDescribeOperatorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operator",
Short: "Describes the operator",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunMaybeStorelessAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/describeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createDescribeServerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "Describes a server",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/describeuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createDescribeUserCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "user",
Short: "Describes an user",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/editaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createEditAccount() *cobra.Command {
cmd := &cobra.Command{
Use: "account",
Short: "Edit an account",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/editcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createEditClusterCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "Edit a cluster",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/editserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createEditServerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "Edit a server",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/edituser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func createEditUserCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "user",
Short: "Edit an user",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createEnvCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "env",
Short: fmt.Sprintf("Prints and manage the %s environment", GetToolName()),
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceErrors: false,
SilenceUsage: false,
Example: "env",
Expand Down
2 changes: 1 addition & 1 deletion cmd/generateactivation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createGenerateActivationCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "activation",
Short: "Generate an export activation jwt token",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := RunAction(cmd, args, &params); err != nil {
Expand Down
15 changes: 6 additions & 9 deletions cmd/generateconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createGenerateConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Generate a config file for an user",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
Example: `nsc generate config --account a --user u`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -83,15 +83,12 @@ func (p *GenerateConfigParams) SetDefaults(ctx ActionCtx) error {

func (p *GenerateConfigParams) PreInteractive(ctx ActionCtx) error {
var err error

if err = p.AccountContextParams.Edit(ctx); err != nil {
return err
}
if p.user == "" {
p.user, err = ctx.StoreCtx().PickUser(p.AccountContextParams.Name)
if err != nil {
return err
}
p.user, err = ctx.StoreCtx().PickUser(p.AccountContextParams.Name)
if err != nil {
return err
}
return nil
}
Expand All @@ -111,7 +108,7 @@ func (p *GenerateConfigParams) Validate(ctx ActionCtx) error {
return fmt.Errorf("account is required")
}
if p.user == "" {
return fmt.Errorf("name is required")
return fmt.Errorf("user is required")
}

p.entityKP, err = ctx.StoreCtx().KeyStore.GetUserKey(p.AccountContextParams.Name, p.user)
Expand All @@ -120,7 +117,7 @@ func (p *GenerateConfigParams) Validate(ctx ActionCtx) error {
}

if !ctx.StoreCtx().Store.Has(store.Accounts, p.AccountContextParams.Name, store.Users, store.JwtName(p.user)) {
return fmt.Errorf("user %q not found", p.user)
return fmt.Errorf("user %q not found in %q", p.user, p.AccountContextParams.Name)
}

p.entityJwt, err = ctx.StoreCtx().Store.Read(store.Accounts, p.AccountContextParams.Name, store.Users, store.JwtName(p.user))
Expand Down
60 changes: 59 additions & 1 deletion cmd/generateconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
package cmd

import (
"os"
"testing"

"github.com/nats-io/jwt"

"github.com/nats-io/nsc/cmd/store"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -93,7 +96,7 @@ func TestGenerateConfig_MultipleUsers(t *testing.T) {

_, _, err = ExecuteCmd(createGenerateConfigCmd())
require.Error(t, err)
require.Equal(t, "name is required", err.Error())
require.Equal(t, "user is required", err.Error())

stdout, _, err := ExecuteCmd(createGenerateConfigCmd(), "--account", "A", "--user", "u")
require.NoError(t, err)
Expand Down Expand Up @@ -121,3 +124,58 @@ func TestGenerateConfig_Interactive(t *testing.T) {
require.Contains(t, stdout, string(accountJwt))
require.Contains(t, stdout, seed)
}

func TestGenerateConfig_HonorsAccount(t *testing.T) {
ts := NewTestStore(t, "operator")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddUser(t, "A", "au")
ts.AddAccount(t, "B")
ts.AddUser(t, "B", "bu")

stdout, _, err := ExecuteCmd(createGenerateConfigCmd(), "--account", "A")
require.NoError(t, err)
userToken, _ := ExtractToken(stdout)

uc, err := jwt.DecodeUserClaims(userToken)
require.NoError(t, err)
require.Equal(t, "au", uc.Name)

stdout, _, err = ExecuteCmd(createGenerateConfigCmd(), "--account", "B")
require.NoError(t, err)
userToken, _ = ExtractToken(stdout)

uc, err = jwt.DecodeUserClaims(userToken)
require.NoError(t, err)
require.Equal(t, "bu", uc.Name)
}

func TestGenerateConfig_InteractiveHonorsAccount(t *testing.T) {
ts := NewTestStore(t, "operator")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddUser(t, "A", "au")
ts.AddAccount(t, "B")
ts.AddUser(t, "B", "bu")

t.Log(os.Args[0])

inputs := []interface{}{0}
stdout, _, err := ExecuteInteractiveCmd(createGenerateConfigCmd(), inputs)
require.NoError(t, err)
userToken, _ := ExtractToken(stdout)

uc, err := jwt.DecodeUserClaims(userToken)
require.NoError(t, err)
require.Equal(t, "au", uc.Name)

//stdout, _, err = ExecuteCmd(createGenerateConfigCmd(), "--account", "B")
//require.NoError(t, err)
//userToken, _ = ExtractToken(stdout)
//
//uc, err = jwt.DecodeUserClaims(userToken)
//require.NoError(t, err)
//require.Equal(t, "bu", uc.Name)
}
2 changes: 1 addition & 1 deletion cmd/generateoperatorconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createGenerateOperatorConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operator",
Short: "Generate an operator config",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
SilenceUsage: true,
Example: `nsc generate operator --n a`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func CreateInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Init a configuration directory",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
Example: `init --name operatorname
init --interactive
`,
Expand Down
10 changes: 5 additions & 5 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func createListOperatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operators",
Short: "List operators",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfig()
if config.StoreRoot == "" {
Expand Down Expand Up @@ -97,7 +97,7 @@ func createListAccountsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "accounts",
Short: "List accounts",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfig()
if config.StoreRoot == "" {
Expand Down Expand Up @@ -143,7 +143,7 @@ func createListUsersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "users",
Short: "List users",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfig()
if config.StoreRoot == "" {
Expand Down Expand Up @@ -191,7 +191,7 @@ func createListClustersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "clusters",
Short: "List clusters",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfig()
if config.StoreRoot == "" {
Expand Down Expand Up @@ -238,7 +238,7 @@ func createListServersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "servers",
Short: "List servers",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfig()
if config.StoreRoot == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ update --release-notes
`,
Use: "update",
Short: "Update this tool to latest version",
Args: cobra.MaximumNArgs(0),
Args: MaxArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
v := semver.MustParse(GetRootCmd().Version)

Expand Down

0 comments on commit 1e3ccb7

Please sign in to comment.