github.com/thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/command/flag/credentials_or_json_test.go (about)

     1  package flag_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	. "code.cloudfoundry.org/cli/command/flag"
     8  	flags "github.com/jessevdk/go-flags"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/ginkgo/extensions/table"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("CredentialsOrJSON", func() {
    15  	var credsOrJSON CredentialsOrJSON
    16  
    17  	BeforeEach(func() {
    18  		credsOrJSON = CredentialsOrJSON{}
    19  	})
    20  
    21  	Describe("default value", func() {
    22  		It("is not set", func() {
    23  			Expect(credsOrJSON.IsSet).To(BeFalse())
    24  		})
    25  
    26  		It("is empty", func() {
    27  			Expect(credsOrJSON.Value).To(BeEmpty())
    28  		})
    29  
    30  		It("does not need to prompt the user for credentials", func() {
    31  			Expect(credsOrJSON.UserPromptCredentials).To(BeEmpty())
    32  		})
    33  	})
    34  
    35  	// The Complete method is not tested because it shares the same code as
    36  	// Path.Complete().
    37  
    38  	Describe("UnmarshalFlag", func() {
    39  		Describe("empty credentials", func() {
    40  			BeforeEach(func() {
    41  				err := credsOrJSON.UnmarshalFlag("")
    42  				Expect(err).NotTo(HaveOccurred())
    43  			})
    44  
    45  			It("is set", func() {
    46  				Expect(credsOrJSON.IsSet).To(BeTrue())
    47  			})
    48  
    49  			It("is empty", func() {
    50  				Expect(credsOrJSON.Value).To(BeEmpty())
    51  			})
    52  
    53  			It("does not need to prompt the user for credentials", func() {
    54  				Expect(credsOrJSON.UserPromptCredentials).To(BeEmpty())
    55  			})
    56  		})
    57  
    58  		DescribeTable("when the input is valid JSON",
    59  			func(input string) {
    60  				err := credsOrJSON.UnmarshalFlag(input)
    61  				Expect(err).NotTo(HaveOccurred())
    62  
    63  				Expect(credsOrJSON.IsSet).To(BeTrue())
    64  				Expect(credsOrJSON.UserPromptCredentials).To(BeEmpty())
    65  				Expect(credsOrJSON.Value).To(HaveLen(1))
    66  				Expect(credsOrJSON.Value).To(HaveKeyWithValue("some", "json"))
    67  			},
    68  			Entry("valid JSON", `{"some": "json"}`),
    69  			Entry("valid JSON in single quotes", `'{"some": "json"}'`),
    70  			Entry("valid JSON in double quotes", `"{"some": "json"}"`),
    71  		)
    72  
    73  		Describe("reading JSON from a file", func() {
    74  			var path string
    75  
    76  			AfterEach(func() {
    77  				os.Remove(path)
    78  			})
    79  
    80  			When("the file contains valid JSON", func() {
    81  				BeforeEach(func() {
    82  					path = tempFile(`{"some":"json"}`)
    83  				})
    84  
    85  				It("reads the JSON from the file", func() {
    86  					err := credsOrJSON.UnmarshalFlag(path)
    87  					Expect(err).NotTo(HaveOccurred())
    88  
    89  					Expect(credsOrJSON.IsSet).To(BeTrue())
    90  					Expect(credsOrJSON.Value).To(HaveLen(1))
    91  					Expect(credsOrJSON.Value).To(HaveKeyWithValue("some", "json"))
    92  				})
    93  
    94  				It("does not need to prompt the user for credentials", func() {
    95  					Expect(credsOrJSON.UserPromptCredentials).To(BeEmpty())
    96  				})
    97  			})
    98  
    99  			When("the file has invalid JSON", func() {
   100  				BeforeEach(func() {
   101  					path = tempFile(`{"this is":"invalid JSON"`)
   102  				})
   103  
   104  				It("errors with the invalid configuration error", func() {
   105  					err := credsOrJSON.UnmarshalFlag(path)
   106  					Expect(err).To(Equal(&flags.Error{
   107  						Type:    flags.ErrRequired,
   108  						Message: fmt.Sprintf("The file '%s' contains invalid JSON. Please provide a path to a file containing a valid JSON object.", path),
   109  					}))
   110  				})
   111  			})
   112  		})
   113  
   114  		Describe("prompting the user to enter credentials", func() {
   115  			When("there is a credential", func() {
   116  				BeforeEach(func() {
   117  					err := credsOrJSON.UnmarshalFlag("foo")
   118  					Expect(err).NotTo(HaveOccurred())
   119  				})
   120  
   121  				It("says the user must be prompted for a credential", func() {
   122  					Expect(credsOrJSON.Value).To(BeEmpty())
   123  					Expect(credsOrJSON.UserPromptCredentials).To(ConsistOf("foo"))
   124  				})
   125  			})
   126  
   127  			When("there are many credentials", func() {
   128  				BeforeEach(func() {
   129  					err := credsOrJSON.UnmarshalFlag("foo, bar,baz ,bax moo")
   130  					Expect(err).NotTo(HaveOccurred())
   131  				})
   132  
   133  				It("says the user must be prompted for the credential", func() {
   134  					Expect(credsOrJSON.Value).To(BeEmpty())
   135  					Expect(credsOrJSON.UserPromptCredentials).To(ConsistOf("foo", "bar", "baz", "bax moo"))
   136  				})
   137  			})
   138  		})
   139  	})
   140  })