github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/api_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	. "code.cloudfoundry.org/cli/command/v6"
     9  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("api Command", func() {
    18  	var (
    19  		cmd        ApiCommand
    20  		testUI     *ui.UI
    21  		fakeActor  *v6fakes.FakeApiActor
    22  		fakeConfig *commandfakes.FakeConfig
    23  		err        error
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    28  		fakeActor = new(v6fakes.FakeApiActor)
    29  		fakeConfig = new(commandfakes.FakeConfig)
    30  
    31  		cmd = ApiCommand{
    32  			UI:     testUI,
    33  			Actor:  fakeActor,
    34  			Config: fakeConfig,
    35  		}
    36  
    37  		fakeConfig.BinaryNameReturns("faceman")
    38  	})
    39  
    40  	JustBeforeEach(func() {
    41  		err = cmd.Execute(nil)
    42  	})
    43  
    44  	When("the API endpoint is not provided", func() {
    45  		When("the API is not set", func() {
    46  			It("displays a tip", func() {
    47  				Expect(err).ToNot(HaveOccurred())
    48  
    49  				Expect(testUI.Out).To(Say("No api endpoint set. Use 'cf api' to set an endpoint"))
    50  			})
    51  		})
    52  
    53  		When("the API is set, the user is logged in and an org and space are targeted", func() {
    54  			BeforeEach(func() {
    55  				fakeConfig.TargetReturns("some-api-target")
    56  				fakeConfig.APIVersionReturns("100.200.300")
    57  				fakeConfig.CurrentUserReturns(configv3.User{
    58  					Name: "admin",
    59  				}, nil)
    60  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    61  					Name: "some-org",
    62  				})
    63  				fakeConfig.TargetedSpaceReturns(configv3.Space{
    64  					Name: "some-space",
    65  				})
    66  			})
    67  
    68  			It("outputs target information", func() {
    69  				Expect(err).ToNot(HaveOccurred())
    70  				Expect(testUI.Out).To(Say(`api endpoint:\s+some-api-target`))
    71  				Expect(testUI.Out).To(Say(`api version:\s+100.200.300`))
    72  			})
    73  		})
    74  
    75  		When("passed a --unset", func() {
    76  			BeforeEach(func() {
    77  				cmd.Unset = true
    78  			})
    79  
    80  			It("clears the target", func() {
    81  				Expect(err).ToNot(HaveOccurred())
    82  				Expect(testUI.Out).To(Say("Unsetting api endpoint..."))
    83  				Expect(testUI.Out).To(Say("OK"))
    84  				Expect(fakeActor.ClearTargetCallCount()).To(Equal(1))
    85  			})
    86  		})
    87  	})
    88  
    89  	When("a valid API endpoint is provided", func() {
    90  		When("the API has SSL", func() {
    91  			Context("with no protocol", func() {
    92  				var (
    93  					CCAPI string
    94  				)
    95  
    96  				BeforeEach(func() {
    97  					CCAPI = "api.foo.com"
    98  					cmd.OptionalArgs.URL = CCAPI
    99  
   100  					fakeConfig.TargetReturns("some-api-target")
   101  					fakeConfig.APIVersionReturns("100.200.300")
   102  				})
   103  
   104  				When("the url has verified SSL", func() {
   105  					It("sets the target", func() {
   106  						Expect(err).ToNot(HaveOccurred())
   107  
   108  						Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
   109  						settings := fakeActor.SetTargetArgsForCall(0)
   110  						Expect(settings.URL).To(Equal("https://" + CCAPI))
   111  						Expect(settings.SkipSSLValidation).To(BeFalse())
   112  
   113  						Expect(testUI.Out).To(Say("Setting api endpoint to %s...", CCAPI))
   114  						Expect(testUI.Out).To(Say(`OK
   115  
   116  api endpoint:   some-api-target
   117  api version:    100.200.300`,
   118  						))
   119  					})
   120  				})
   121  
   122  				When("the url has unverified SSL", func() {
   123  					When("--skip-ssl-validation is passed", func() {
   124  						BeforeEach(func() {
   125  							cmd.SkipSSLValidation = true
   126  						})
   127  
   128  						It("sets the target", func() {
   129  							Expect(err).ToNot(HaveOccurred())
   130  
   131  							Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
   132  							settings := fakeActor.SetTargetArgsForCall(0)
   133  							Expect(settings.URL).To(Equal("https://" + CCAPI))
   134  							Expect(settings.SkipSSLValidation).To(BeTrue())
   135  
   136  							Expect(testUI.Out).To(Say("Setting api endpoint to %s...", CCAPI))
   137  							Expect(testUI.Out).To(Say(`OK
   138  
   139  api endpoint:   some-api-target
   140  api version:    100.200.300`,
   141  							))
   142  						})
   143  					})
   144  
   145  					When("no additional flags are passed", func() {
   146  						BeforeEach(func() {
   147  							fakeActor.SetTargetReturns(nil, ccerror.UnverifiedServerError{URL: CCAPI})
   148  						})
   149  
   150  						It("returns an error with a --skip-ssl-validation tip", func() {
   151  							Expect(err).To(MatchError(ccerror.UnverifiedServerError{URL: CCAPI}))
   152  							Expect(testUI.Out).ToNot(Say(`api endpoint:\s+some-api-target`))
   153  						})
   154  					})
   155  				})
   156  			})
   157  		})
   158  
   159  		When("the API does not have SSL", func() {
   160  			var CCAPI string
   161  
   162  			BeforeEach(func() {
   163  				CCAPI = "http://api.foo.com"
   164  				cmd.OptionalArgs.URL = CCAPI
   165  			})
   166  
   167  			It("sets the target with a warning", func() {
   168  				Expect(err).ToNot(HaveOccurred())
   169  
   170  				Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
   171  				settings := fakeActor.SetTargetArgsForCall(0)
   172  				Expect(settings.URL).To(Equal(CCAPI))
   173  				Expect(settings.SkipSSLValidation).To(BeFalse())
   174  
   175  				Expect(testUI.Out).To(Say("Setting api endpoint to %s...", CCAPI))
   176  				Expect(testUI.Out).To(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended"))
   177  				Expect(testUI.Out).To(Say("OK"))
   178  			})
   179  		})
   180  
   181  		When("the API is set but the user is not logged in", func() {
   182  			BeforeEach(func() {
   183  				cmd.OptionalArgs.URL = "https://api.foo.com"
   184  				fakeConfig.TargetReturns("something")
   185  			})
   186  
   187  			It("outputs a 'not logged in' message", func() {
   188  				Expect(err).ToNot(HaveOccurred())
   189  
   190  				Expect(testUI.Out).To(Say("Not logged in. Use 'faceman login' to log in."))
   191  			})
   192  		})
   193  
   194  		When("the API is set but the user is logged in", func() {
   195  			BeforeEach(func() {
   196  				cmd.OptionalArgs.URL = "https://api.foo.com"
   197  				fakeConfig.TargetReturns("something")
   198  				fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
   199  			})
   200  
   201  			It("does not output a 'not logged in' message", func() {
   202  				Expect(err).ToNot(HaveOccurred())
   203  
   204  				Expect(testUI.Out).ToNot(Say("Not logged in. Use 'faceman login' to log in."))
   205  			})
   206  		})
   207  
   208  		When("the API has an older version", func() {
   209  			BeforeEach(func() {
   210  				cmd.OptionalArgs.URL = "https://api.foo.com"
   211  				fakeConfig.TargetReturns("something")
   212  				fakeConfig.APIVersionReturns("1.2.3")
   213  			})
   214  
   215  			It("outputs a warning", func() {
   216  				Expect(err).ToNot(HaveOccurred())
   217  
   218  				Expect(testUI.Err).To(Say("Your API version is no longer supported. Upgrade to a newer version of the API"))
   219  			})
   220  		})
   221  
   222  		When("the URL host does not exist", func() {
   223  			var (
   224  				CCAPI      string
   225  				requestErr ccerror.RequestError
   226  			)
   227  
   228  			BeforeEach(func() {
   229  				CCAPI = "i.do.not.exist.com"
   230  				cmd.OptionalArgs.URL = CCAPI
   231  
   232  				requestErr = ccerror.RequestError{Err: errors.New("I am an error")}
   233  				fakeActor.SetTargetReturns(nil, requestErr)
   234  			})
   235  
   236  			It("returns an APIRequestError", func() {
   237  				Expect(err).To(MatchError(ccerror.RequestError{Err: requestErr.Err}))
   238  			})
   239  		})
   240  	})
   241  })