github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/api_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    12  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  
    16  	"code.cloudfoundry.org/cli/cf/api"
    17  	"code.cloudfoundry.org/cli/cf/commands"
    18  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes"
    19  	"code.cloudfoundry.org/cli/cf/flags"
    20  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    21  )
    22  
    23  var _ = Describe("Api", func() {
    24  	var (
    25  		config              coreconfig.Repository
    26  		endpointRepo        *coreconfigfakes.FakeEndpointRepository
    27  		deps                commandregistry.Dependency
    28  		requirementsFactory *requirementsfakes.FakeFactory
    29  		ui                  *testterm.FakeUI
    30  		cmd                 commands.API
    31  		flagContext         flags.FlagContext
    32  		repoLocator         api.RepositoryLocator
    33  		runCLIErr           error
    34  	)
    35  
    36  	callApi := func(args []string) {
    37  		err := flagContext.Parse(args...)
    38  		Expect(err).NotTo(HaveOccurred())
    39  
    40  		runCLIErr = cmd.Execute(flagContext)
    41  	}
    42  
    43  	BeforeEach(func() {
    44  		ui = new(testterm.FakeUI)
    45  		requirementsFactory = new(requirementsfakes.FakeFactory)
    46  		config = testconfig.NewRepository()
    47  		endpointRepo = new(coreconfigfakes.FakeEndpointRepository)
    48  
    49  		endpointRepo.GetCCInfoStub = func(endpoint string) (*coreconfig.CCInfo, string, error) {
    50  			return &coreconfig.CCInfo{
    51  				APIVersion:               config.APIVersion(),
    52  				AuthorizationEndpoint:    config.AuthenticationEndpoint(),
    53  				MinCLIVersion:            config.MinCLIVersion(),
    54  				MinRecommendedCLIVersion: config.MinRecommendedCLIVersion(),
    55  				SSHOAuthClient:           config.SSHOAuthClient(),
    56  				RoutingAPIEndpoint:       config.RoutingAPIEndpoint(),
    57  			}, endpoint, nil
    58  		}
    59  
    60  		repoLocator = api.RepositoryLocator{}.SetEndpointRepository(endpointRepo)
    61  
    62  		deps = commandregistry.Dependency{
    63  			UI:          ui,
    64  			Config:      config,
    65  			RepoLocator: repoLocator,
    66  		}
    67  
    68  		cmd = commands.API{}.SetDependency(deps, false).(commands.API)
    69  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    70  	})
    71  
    72  	Context("when the api endpoint's ssl certificate is invalid", func() {
    73  		It("warns the user and prints out a tip", func() {
    74  			endpointRepo.GetCCInfoReturns(nil, "", errors.NewInvalidSSLCert("https://buttontomatoes.org", "why? no. go away"))
    75  
    76  			callApi([]string{"https://buttontomatoes.org"})
    77  			Expect(runCLIErr).To(HaveOccurred())
    78  			Expect(runCLIErr.Error()).To(ContainSubstring("Invalid SSL Cert for https://buttontomatoes.org"))
    79  			Expect(runCLIErr.Error()).To(ContainSubstring("TIP"))
    80  			Expect(runCLIErr.Error()).To(ContainSubstring("--skip-ssl-validation"))
    81  		})
    82  	})
    83  
    84  	Context("when the user does not provide an endpoint", func() {
    85  		Context("when the endpoint is set in the config", func() {
    86  			BeforeEach(func() {
    87  				config.SetAPIEndpoint("https://api.run.pivotal.io")
    88  				config.SetAPIVersion("2.0")
    89  				config.SetSSLDisabled(true)
    90  			})
    91  
    92  			It("prints out the api endpoint and appropriately sets the config", func() {
    93  				callApi([]string{})
    94  
    95  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"https://api.run.pivotal.io", "2.0"}))
    96  				Expect(config.IsSSLDisabled()).To(BeTrue())
    97  			})
    98  
    99  			Context("when the --unset flag is passed", func() {
   100  				It("unsets the APIEndpoint", func() {
   101  					callApi([]string{"--unset"})
   102  
   103  					Expect(ui.Outputs()).To(ContainSubstrings(
   104  						[]string{"Unsetting api endpoint..."},
   105  						[]string{"OK"},
   106  						[]string{"No api endpoint set."},
   107  					))
   108  					Expect(config.APIEndpoint()).To(Equal(""))
   109  				})
   110  			})
   111  		})
   112  
   113  		Context("when the endpoint is not set in the config", func() {
   114  			It("prompts the user to set an endpoint", func() {
   115  				callApi([]string{})
   116  
   117  				Expect(ui.Outputs()).To(ContainSubstrings(
   118  					[]string{"No api endpoint set", fmt.Sprintf("Use '%s api' to set an endpoint", cf.Name)},
   119  				))
   120  			})
   121  		})
   122  	})
   123  
   124  	Context("when the user provides the --skip-ssl-validation flag", func() {
   125  		It("updates the SSLDisabled field in config", func() {
   126  			config.SetSSLDisabled(false)
   127  			callApi([]string{"--skip-ssl-validation", "https://example.com"})
   128  
   129  			Expect(config.IsSSLDisabled()).To(Equal(true))
   130  		})
   131  	})
   132  
   133  	Context("the user provides an endpoint", func() {
   134  		Describe("when the user passed in the skip-ssl-validation flag", func() {
   135  			It("disables SSL validation in the config", func() {
   136  				callApi([]string{"--skip-ssl-validation", "https://example.com"})
   137  
   138  				Expect(endpointRepo.GetCCInfoCallCount()).To(Equal(1))
   139  				Expect(endpointRepo.GetCCInfoArgsForCall(0)).To(Equal("https://example.com"))
   140  				Expect(config.IsSSLDisabled()).To(BeTrue())
   141  			})
   142  		})
   143  
   144  		Context("when the user passed in the unset flag", func() {
   145  			Context("when the config.APIEndpoint is set", func() {
   146  				BeforeEach(func() {
   147  					config.SetAPIEndpoint("some-silly-thing")
   148  				})
   149  
   150  				It("unsets the APIEndpoint", func() {
   151  					callApi([]string{"--unset", "https://example.com"})
   152  
   153  					Expect(ui.Outputs()).To(ContainSubstrings(
   154  						[]string{"Unsetting api endpoint..."},
   155  						[]string{"OK"},
   156  						[]string{"No api endpoint set."},
   157  					))
   158  					Expect(config.APIEndpoint()).To(Equal(""))
   159  				})
   160  			})
   161  
   162  			Context("when the config.APIEndpoint is empty", func() {
   163  				It("unsets the APIEndpoint", func() {
   164  					callApi([]string{"--unset", "https://example.com"})
   165  
   166  					Expect(ui.Outputs()).To(ContainSubstrings(
   167  						[]string{"Unsetting api endpoint..."},
   168  						[]string{"OK"},
   169  						[]string{"No api endpoint set."},
   170  					))
   171  					Expect(config.APIEndpoint()).To(Equal(""))
   172  				})
   173  			})
   174  
   175  		})
   176  
   177  		Context("when the ssl certificate is valid", func() {
   178  			It("updates the api endpoint with the given url", func() {
   179  				callApi([]string{"https://example.com"})
   180  				Expect(endpointRepo.GetCCInfoCallCount()).To(Equal(1))
   181  				Expect(endpointRepo.GetCCInfoArgsForCall(0)).To(Equal("https://example.com"))
   182  				Expect(ui.Outputs()).To(ContainSubstrings(
   183  					[]string{"Setting api endpoint to", "example.com"},
   184  					[]string{"OK"},
   185  				))
   186  			})
   187  
   188  			It("trims trailing slashes from the api endpoint", func() {
   189  				callApi([]string{"https://example.com/"})
   190  				Expect(endpointRepo.GetCCInfoCallCount()).To(Equal(1))
   191  				Expect(endpointRepo.GetCCInfoArgsForCall(0)).To(Equal("https://example.com"))
   192  				Expect(ui.Outputs()).To(ContainSubstrings(
   193  					[]string{"Setting api endpoint to", "example.com"},
   194  					[]string{"OK"},
   195  				))
   196  			})
   197  		})
   198  
   199  		Context("when the ssl certificate is invalid", func() {
   200  			BeforeEach(func() {
   201  				endpointRepo.GetCCInfoReturns(nil, "", errors.NewInvalidSSLCert("https://example.com", "it don't work"))
   202  			})
   203  
   204  			It("fails and gives the user a helpful message about skipping", func() {
   205  				callApi([]string{"https://example.com"})
   206  				Expect(runCLIErr).To(HaveOccurred())
   207  				Expect(runCLIErr.Error()).To(ContainSubstring("Invalid SSL Cert"))
   208  				Expect(runCLIErr.Error()).To(ContainSubstring("https://example.com"))
   209  				Expect(runCLIErr.Error()).To(ContainSubstring("TIP"))
   210  
   211  				Expect(config.APIEndpoint()).To(Equal(""))
   212  			})
   213  		})
   214  
   215  		Describe("unencrypted http endpoints", func() {
   216  			It("warns the user", func() {
   217  				callApi([]string{"http://example.com"})
   218  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Warning"}))
   219  			})
   220  		})
   221  	})
   222  })