Skip to content

Commit

Permalink
Adds --output-json for get command
Browse files Browse the repository at this point in the history
- Refactoring of test string constants

[#145087513] Return json should be an application option for all commands

Signed-off-by: Edie Beer <ebeer@pivotal.io>
  • Loading branch information
Kelly Gerritz authored and Edie Beer committed Jun 5, 2017
1 parent 4acc068 commit 1921e3f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 34 deletions.
21 changes: 14 additions & 7 deletions commands/commands_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ const RSA_CREDENTIAL_RESPONSE_YAML = "id: " + UUID + "\nname: %s\ntype: rsa\nval
const USER_CREDENTIAL_RESPONSE_YAML = "id: " + UUID + "\nname: %s\ntype: user\nvalue:\n password: %s\n password_hash: %s\n username: %s\nversion_created_at: " + TIMESTAMP
const USER_WITHOUT_USERNAME_CREDENTIAL_RESPONSE_YAML = "id: " + UUID + "\nname: %s\ntype: user\nvalue:\n password: %s\n password_hash: %s\n username: null\nversion_created_at: " + TIMESTAMP

var responseMyValuePotatoes = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-value", "value", "potatoes")
var responseMyPasswordPotatoes = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-password", "password", "potatoes")
var responseMyJson = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_YAML, "json-secret", " an:\n - array\n foo: bar\n nested:\n a: 1")
var responseMyCertificate = fmt.Sprintf(CERTIFICATE_CREDENTIAL_RESPONSE_YAML, "my-secret", "my-ca", "my-cert", "my-priv")
var responseMySSHFoo = fmt.Sprintf(SSH_CREDENTIAL_RESPONSE_YAML, "foo-ssh-key", "some-private-key", "some-public-key")
var responseMyRSAFoo = fmt.Sprintf(RSA_CREDENTIAL_RESPONSE_YAML, "foo-rsa-key", "some-private-key", "some-public-key")
var responseMyUsername = fmt.Sprintf(USER_CREDENTIAL_RESPONSE_YAML, "my-username-credential", "test-password", "passw0rd-H4$h", "my-username")
var responseMyValuePotatoesYaml = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-value", "value", "potatoes")
var responseMyValuePotatoesJson = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_JSON, "value", "my-value", "potatoes")
var responseMyPasswordPotatoesYaml = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-password", "password", "potatoes")
var responseMyPasswordPotatoesJson = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_JSON, "password", "my-password", "potatoes")
var responseMyJsonFormatYaml = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_YAML, "json-secret", " an:\n - array\n foo: bar\n nested:\n a: 1")
var responseMyJsonFormatJson = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_JSON, "json-secret", "{\"an\": [\"array\"], \"foo\": \"bar\", \"nested\": {\"a\": 1}}")
var responseMyCertificateYaml = fmt.Sprintf(CERTIFICATE_CREDENTIAL_RESPONSE_YAML, "my-secret", "my-ca", "my-cert", "my-priv")
var responseMyCertificateJson = fmt.Sprintf(CERTIFICATE_CREDENTIAL_RESPONSE_JSON, "my-secret", "my-ca", "my-cert", "my-priv")
var responseMySSHFooYaml = fmt.Sprintf(SSH_CREDENTIAL_RESPONSE_YAML, "foo-ssh-key", "some-private-key", "some-public-key")
var responseMySSHFooJson = fmt.Sprintf(RSA_SSH_CREDENTIAL_RESPONSE_JSON, "ssh", "foo-ssh-key", "some-public-key", "some-private-key")
var responseMyRSAFooYaml = fmt.Sprintf(RSA_CREDENTIAL_RESPONSE_YAML, "foo-rsa-key", "some-private-key", "some-public-key")
var responseMyRSAFooJson = fmt.Sprintf(RSA_SSH_CREDENTIAL_RESPONSE_JSON, "rsa", "foo-rsa-key", "some-public-key", "some-private-key")
var responseMyUsernameYaml = fmt.Sprintf(USER_CREDENTIAL_RESPONSE_YAML, "my-username-credential", "test-password", "passw0rd-H4$h", "my-username")
var responseMyUsernameJson = fmt.Sprintf(USER_CREDENTIAL_RESPONSE_JSON, "my-username-credential", "my-username", "test-password", "passw0rd-H4$h")
var responseMySpecialCharacterValue = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-character-test", "value", `'{"password":"some-still-bad-password"}'`)
var responseMySpecialCharacterPassword = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-character-test", "password", `'{"password":"some-still-bad-password"}'`)
var responseMySpecialCharacterJson = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_YAML, "my-character-test", " foo: b\"ar")
Expand Down
14 changes: 7 additions & 7 deletions commands/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", "my-password", "-t", "password")

Eventually(session).Should(Exit(0))
Expect(session.Out).To(Say(responseMyPasswordPotatoes))
Expect(session.Out).To(Say(responseMyPasswordPotatoesYaml))
})

It("can print the generated password secret as JSON", func() {
Expand All @@ -68,7 +68,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", "my-password", "-t", "password")

Eventually(session).Should(Exit(0))
Expect(session.Out).To(Say(responseMyPasswordPotatoes))
Expect(session.Out).To(Say(responseMyPasswordPotatoesYaml))
})

It("can print the secret as JSON", func() {
Expand Down Expand Up @@ -135,7 +135,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", "foo-ssh-key", "-t", "ssh")

Eventually(session).Should(Exit(0))
Expect(session.Out).To(Say(responseMySSHFoo))
Expect(session.Out).To(Say(responseMySSHFooYaml))
})

It("can print the SSH key as JSON", func() {
Expand Down Expand Up @@ -183,7 +183,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", "foo-rsa-key", "-t", "rsa")

Eventually(session).Should(Exit(0))
Expect(session.Out).To(Say(responseMyRSAFoo))
Expect(session.Out).To(Say(responseMyRSAFooYaml))
})

It("can print the RSA key as JSON", func() {
Expand Down Expand Up @@ -225,7 +225,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", "my-secret", "-t", "certificate", "--common-name", "common.name.io")

Eventually(session).Should(Exit(0))
Expect(session.Out).To(Say(responseMyCertificate))
Expect(session.Out).To(Say(responseMyCertificateYaml))
})

It("can print the certificate as JSON", func() {
Expand Down Expand Up @@ -368,7 +368,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", name, "-t", "user")

Eventually(session).Should(Exit(0))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml))
})

It("should accept a statically provided username", func() {
Expand All @@ -383,7 +383,7 @@ var _ = Describe("Generate", func() {
session := runCommand("generate", "-n", name, "-t", "user", "-z", "my-username")

Eventually(session).Should(Exit(0))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml))
})

It("with with no-overwrite", func() {
Expand Down
14 changes: 7 additions & 7 deletions commands/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "-n", "my-value")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyValuePotatoes))
Eventually(session.Out).Should(Say(responseMyValuePotatoesYaml))
})

It("gets a password secret", func() {
Expand All @@ -71,7 +71,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "-n", "my-password")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyPasswordPotatoes))
Eventually(session.Out).Should(Say(responseMyPasswordPotatoesYaml))
})

It("gets a json secret", func() {
Expand All @@ -87,7 +87,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "-n", "json-secret")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyJson))
Eventually(session.Out).Should(Say(responseMyJsonFormatYaml))
})

It("gets a certificate secret", func() {
Expand All @@ -103,7 +103,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "-n", "my-secret")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyCertificate))
Eventually(session.Out).Should(Say(responseMyCertificateYaml))
})

It("gets an rsa secret", func() {
Expand All @@ -119,7 +119,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "-n", "foo-rsa-key")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyRSAFoo))
Eventually(session.Out).Should(Say(responseMyRSAFooYaml))
})

It("can output json", func() {
Expand Down Expand Up @@ -157,7 +157,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "-n", "my-username-credential")

Eventually(session).Should(Exit(0))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml))
})

It("gets a secret by ID", func() {
Expand All @@ -173,7 +173,7 @@ var _ = Describe("Get", func() {
session := runCommand("get", "--id", UUID)

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyPasswordPotatoes))
Eventually(session.Out).Should(Say(responseMyPasswordPotatoesYaml))
})

It("does not use Printf on user-supplied data", func() {
Expand Down
3 changes: 2 additions & 1 deletion commands/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type SetCommand struct {
PublicString string `short:"U" long:"public-string" description:"[SSH, RSA] Sets the public key from string input"`
Username string `short:"z" long:"username" description:"[User] Sets the username value of the credential"`
Password string `short:"w" long:"password" description:"[Password, User] Sets the password value of the credential"`
OutputJson bool `long:"output-json" description:"Return response in JSON format"`
}

func (cmd SetCommand) Execute([]string) error {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (cmd SetCommand) Execute([]string) error {
if err != nil {
return err
}
models.Println(credential, false)
models.Println(credential, cmd.OutputJson)

return nil
}
Expand Down
92 changes: 80 additions & 12 deletions commands/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("Set", func() {
session := runCommand("set", "-n", "my-value", "-v", "potatoes", "-t", "value")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyValuePotatoes))
Eventually(session.Out).Should(Say(responseMyValuePotatoesYaml))
})

It("escapes special characters in the value", func() {
Expand All @@ -53,6 +53,15 @@ var _ = Describe("Set", func() {
Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMySpecialCharacterValue))
})

It("puts a secret using explicit value type and returns in json format", func() {
setupPutValueServer("my-value", "value", "potatoes")

session := runCommand("set", "-n", "my-value", "-v", "potatoes", "-t", "value", "--output-json")

Eventually(session).Should(Exit(0))
Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyValuePotatoesJson))
})
})

Describe("setting json secrets", func() {
Expand All @@ -63,7 +72,7 @@ var _ = Describe("Set", func() {
session := runCommand("set", "-n", "json-secret", "-v", jsonValue, "-t", "json")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyJson))
Eventually(session.Out).Should(Say(responseMyJsonFormatYaml))
})

It("escapes special characters in the json", func() {
Expand All @@ -74,6 +83,17 @@ var _ = Describe("Set", func() {
Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMySpecialCharacterJson))
})

It("puts a secret using explicit json type and returns in json format", func() {
jsonValue := `{"foo":"bar","nested":{"a":1},"an":["array"]}`
setupPutJsonServer("json-secret", jsonValue)

session := runCommand("set", "-n", "json-secret", "-v", jsonValue, "-t", "json", "--output-json")

Eventually(session).Should(Exit(0))
Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyJsonFormatJson))
})

})

Describe("setting SSH secrets", func() {
Expand All @@ -83,7 +103,7 @@ var _ = Describe("Set", func() {
session := runCommand("set", "-n", "foo-ssh-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "ssh")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMySSHFoo))
Eventually(session.Out).Should(Say(responseMySSHFooYaml))
})

It("puts a secret using values read from files", func() {
Expand All @@ -100,7 +120,7 @@ var _ = Describe("Set", func() {

os.RemoveAll(tempDir)
Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMySSHFoo))
Eventually(session.Out).Should(Say(responseMySSHFooYaml))
})

It("puts a secret specifying no-overwrite", func() {
Expand All @@ -110,6 +130,15 @@ var _ = Describe("Set", func() {

Eventually(session).Should(Exit(0))
})

It("puts a secret using explicit ssh type and returns in json format", func() {
setupPutRsaSshServer("foo-ssh-key", "ssh", "some-public-key", "some-private-key", true)

session := runCommand("set", "-n", "foo-ssh-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "ssh", "--output-json")

Eventually(session).Should(Exit(0))
Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMySSHFooJson))
})
})

Describe("setting RSA secrets", func() {
Expand All @@ -119,7 +148,7 @@ var _ = Describe("Set", func() {
session := runCommand("set", "-n", "foo-rsa-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "rsa")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyRSAFoo))
Eventually(session.Out).Should(Say(responseMyRSAFooYaml))
})

It("puts a secret using values read from files", func() {
Expand All @@ -136,7 +165,7 @@ var _ = Describe("Set", func() {

os.RemoveAll(tempDir)
Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyRSAFoo))
Eventually(session.Out).Should(Say(responseMyRSAFooYaml))
})

It("puts a secret specifying no-overwrite", func() {
Expand All @@ -146,6 +175,15 @@ var _ = Describe("Set", func() {

Eventually(session).Should(Exit(0))
})

It("puts a secret using explicit rsa type and returns in json format", func() {
setupPutRsaSshServer("foo-rsa-key", "rsa", "some-public-key", "some-private-key", true)

session := runCommand("set", "-n", "foo-rsa-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "rsa", "--output-json")

Eventually(session).Should(Exit(0))
Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyRSAFooJson))
})
})

Describe("setting password secrets", func() {
Expand All @@ -157,13 +195,13 @@ var _ = Describe("Set", func() {
Eventually(session).Should(Exit(0))
})

It("puts a secret using explicit password type", func() {
It("puts a secret using explicit password type and returns in yaml format", func() {
setupPutValueServer("my-password", "password", "potatoes")

session := runCommand("set", "-n", "my-password", "-w", "potatoes", "-t", "password")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyPasswordPotatoes))
Eventually(session.Out).Should(Say(responseMyPasswordPotatoesYaml))
})

It("prompts for value if value is not provided", func() {
Expand All @@ -172,7 +210,7 @@ var _ = Describe("Set", func() {
session := runCommandWithStdin(strings.NewReader("potatoes\n"), "set", "-n", "my-password", "-t", "password")

Eventually(session.Out).Should(Say("password:"))
Eventually(session.Wait("10s").Out).Should(Say(responseMyPasswordPotatoes))
Eventually(session.Wait("10s").Out).Should(Say(responseMyPasswordPotatoesYaml))
Eventually(session).Should(Exit(0))
})

Expand All @@ -196,6 +234,15 @@ var _ = Describe("Set", func() {
Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMySpecialCharacterPassword))
})

It("puts a secret using explicit password type and returns in json format", func() {
setupPutValueServer("my-password", "password", "potatoes")

session := runCommand("set", "-n", "my-password", "-w", "potatoes", "-t", "password", "--output-json")

Eventually(session).Should(Exit(0))
Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyPasswordPotatoesJson))
})
})

Describe("setting certificate secrets", func() {
Expand All @@ -207,7 +254,7 @@ var _ = Describe("Set", func() {
"--certificate-string", "my-cert", "--private-string", "my-priv")

Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyCertificate))
Eventually(session.Out).Should(Say(responseMyCertificateYaml))
})

It("puts a secret using explicit certificate type and string values with no-overwrite", func() {
Expand All @@ -233,7 +280,7 @@ var _ = Describe("Set", func() {

os.RemoveAll(tempDir)
Eventually(session).Should(Exit(0))
Eventually(session.Out).Should(Say(responseMyCertificate))
Eventually(session.Out).Should(Say(responseMyCertificateYaml))
})

It("fails to put a secret when failing to read from a file", func() {
Expand All @@ -247,6 +294,17 @@ var _ = Describe("Set", func() {
testSetCertFileDuplicationFailure("--certificate-string", "my-cert")
testSetCertFileDuplicationFailure("--private-string", "my-priv")
})

It("puts a secret using explicit certificate type and string values in json format", func() {
setupPutCertificateServer("my-secret", "my-ca", "my-cert", "my-priv")

session := runCommand("set", "-n", "my-secret",
"-t", "certificate", "--root-string", "my-ca",
"--certificate-string", "my-cert", "--private-string", "my-priv", "--output-json")

Eventually(session).Should(Exit(0))
Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyCertificateJson))
})
})

Describe("setting User secrets", func() {
Expand All @@ -256,7 +314,7 @@ var _ = Describe("Set", func() {
session := runCommand("set", "-n", "my-username-credential", "-z", "my-username", "-w", "test-password", "-t", "user")

Eventually(session).Should(Exit(0))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername))
Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml))
})

It("puts a secret specifying no-overwrite", func() {
Expand Down Expand Up @@ -290,6 +348,16 @@ var _ = Describe("Set", func() {
Eventually(session.Wait("10s").Out.Contents()).Should(ContainSubstring(response))
Eventually(session).Should(Exit(0))
})

It("puts a secret using explicit user type in json format", func() {
setupPutUserServer("my-username-credential", `{"username": "my-username", "password": "test-password"}`, "my-username", "test-password", "passw0rd-H4$h", true)

session := runCommand("set", "-n", "my-username-credential", "-z", "my-username", "-w", "test-password", "-t", "user",
"--output-json")

Eventually(session).Should(Exit(0))
Expect(string(session.Out.Contents())).Should(MatchJSON(responseMyUsernameJson))
})
})

Describe("Help", func() {
Expand Down

0 comments on commit 1921e3f

Please sign in to comment.