Skip to content

Commit

Permalink
HVT-4955: Add connect command flag validation (#32)
Browse files Browse the repository at this point in the history
* add connect flag validation

* fmt
  • Loading branch information
ccapurso authored Jan 22, 2024
1 parent 5cd0bf9 commit cfd994b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
defaultDirectory = ".config/hcp/hvd"
testDirectory = "hcptest"
fileName = "hvd_proxy_config.json"
directoryPermissions = 0755
directoryPermissions = 0o755

envVarCacheTestMode = "HCP_CACHE_TEST_MODE"
)
Expand Down
1 change: 0 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func Test_GetHCPConfiguration(t *testing.T) {
assert.Nil(t, tk)
assert.Nil(t, err)
}

})
}
}
Expand Down
7 changes: 6 additions & 1 deletion connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ func (c *HCPConnectCommand) setupClients() error {

if c.rmOrgClient == nil && c.rmProjClient == nil && c.vsClient == nil {
opts = []config.HCPConfigOption{config.FromEnv()}
if c.flagClientID != "" && c.flagSecretID != "" {

if c.flagClientID != "" && c.flagSecretID == "" {
return errors.New("secret-id is required when client-id is provided")
} else if c.flagSecretID != "" && c.flagClientID == "" {
return errors.New("client-id is required when secret-id is provided")
} else if c.flagClientID != "" && c.flagSecretID != "" {
opts = append(opts, config.WithClientCredentials(c.flagClientID, c.flagSecretID))
opts = append(opts, config.WithoutBrowserLogin())
}
Expand Down
48 changes: 46 additions & 2 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,52 @@ func testHCPConnectCommand() (*cli.MockUi, *HCPConnectCommand) {
return ui, &HCPConnectCommand{Ui: ui}
}

func Test_HCPConnect_FlagValidation(t *testing.T) {
cases := []struct {
name string
flags []string
code int
error string
}{
{
name: "invalid flags",
flags: []string{"-invalid", "abc123"},
code: 1,
error: "flag provided but not defined: -invalid",
},
{
name: "only client-id provided",
flags: []string{"-client-id", "abc123"},
code: 1,
error: "secret-id is required when client-id is provided",
},
{
name: "only secret-id provided",
flags: []string{"-secret-id", "abc123"},
code: 1,
error: "client-id is required when secret-id is provided",
},
}

for _, tc := range cases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ui, cmd := testHCPConnectCommand()
result := cmd.Run(tc.flags)
output := ui.OutputWriter.String() + ui.ErrorWriter.String()

assert.Equal(t, tc.code, result)

if tc.error != "" {
assert.Contains(t, output, tc.error)
}
})
}
}

func Test_HCPConnectCommand(t *testing.T) {
_, cmd := testHCPConnectCommand()

Expand Down Expand Up @@ -212,7 +258,6 @@ func Test_getOrganization(t *testing.T) {
}
})
}

}

func Test_getProject(t *testing.T) {
Expand Down Expand Up @@ -333,7 +378,6 @@ func Test_getProject(t *testing.T) {
}
})
}

}

func Test_getCluster(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"github.com/hashicorp/cli"
)

var (
_ cli.Command = (*HCPDisconnectCommand)(nil)
)
var _ cli.Command = (*HCPDisconnectCommand)(nil)

type HCPDisconnectCommand struct {
Ui cli.Ui
Expand Down
3 changes: 2 additions & 1 deletion testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package vaulthcplib

import (
"golang.org/x/oauth2"
"time"

"golang.org/x/oauth2"
)

type TestTokenSource struct{}
Expand Down

0 comments on commit cfd994b

Please sign in to comment.