Skip to content

Commit

Permalink
Guard against user specifying an invalid type on generate
Browse files Browse the repository at this point in the history
[#140148083] Wrong error presented when setting with an invalid type
  • Loading branch information
bitops committed Feb 18, 2017
1 parent 8096b61 commit d01ec18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions commands/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands

import (
"errors"

"github.com/pivotal-cf/credhub-cli/actions"
"github.com/pivotal-cf/credhub-cli/client"
"github.com/pivotal-cf/credhub-cli/config"
Expand Down Expand Up @@ -37,9 +39,14 @@ type GenerateCommand struct {
}

func (cmd GenerateCommand) Execute([]string) error {
validTypes := []string{"value", "password", "ssh", "rsa", "certificate"}

if cmd.SecretType == "" {
cmd.SecretType = "password"
}
if !contains(validTypes, cmd.SecretType) {
return errors.New("The request does not include a valid type. Valid values include 'value', 'password', 'certificate', 'ssh' and 'rsa'.")
}

cfg := config.ReadConfig()
repository := repositories.NewSecretRepository(client.NewHttpClient(cfg))
Expand Down
12 changes: 12 additions & 0 deletions commands/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ var _ = Describe("Generate", func() {
}
})

It("displays an error when invalid type is specified", func() {
server.AppendHandlers(
RespondWith(http.StatusBadRequest, `{"error": "you fail."}`),
)

session := runCommand("generate", "-t", "potato", "-n", "my-value")

Eventually(session).Should(Exit(1))

Expect(session.Err).To(Say("The request does not include a valid type. Valid values include 'value', 'password', 'certificate', 'ssh' and 'rsa'."))
})

It("displays the server provided error when an error is received", func() {
server.AppendHandlers(
RespondWith(http.StatusBadRequest, `{"error": "you fail."}`),
Expand Down

0 comments on commit d01ec18

Please sign in to comment.