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