github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/api_test.go (about)

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