github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/api_command_test.go (about)

     1  package v7_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/v7"
     9  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    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  *v7fakes.FakeActor
    22  		fakeConfig *commandfakes.FakeConfig
    23  		err        error
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    28  		fakeActor = new(v7fakes.FakeActor)
    29  		fakeConfig = new(commandfakes.FakeConfig)
    30  
    31  		cmd = APICommand{
    32  			BaseCommand: BaseCommand{
    33  				UI:     testUI,
    34  				Actor:  fakeActor,
    35  				Config: fakeConfig,
    36  			},
    37  		}
    38  
    39  		fakeConfig.BinaryNameReturns("faceman")
    40  		fakeConfig.TargetReturns("some-api-target")
    41  	})
    42  
    43  	JustBeforeEach(func() {
    44  		err = cmd.Execute(nil)
    45  	})
    46  
    47  	When("clearing the target (the --unset flag is given)", func() {
    48  		BeforeEach(func() {
    49  			cmd.Unset = true
    50  		})
    51  
    52  		It("clears the target", func() {
    53  			Expect(err).ToNot(HaveOccurred())
    54  			Expect(testUI.Out).To(Say("Unsetting API endpoint..."))
    55  			Expect(testUI.Out).To(Say("OK"))
    56  			Expect(fakeActor.ClearTargetCallCount()).To(Equal(1))
    57  		})
    58  	})
    59  
    60  	When("setting a new API endpoint (a valid API endpoint is provided)", func() {
    61  		var CCAPI string
    62  
    63  		BeforeEach(func() {
    64  			CCAPI = "https://api.foo.com"
    65  			cmd.OptionalArgs.URL = CCAPI
    66  
    67  			fakeConfig.APIVersionReturns("100.200.300")
    68  		})
    69  
    70  		It("signals that it's setting the API endpoint", func() {
    71  			Expect(testUI.Out).To(Say("Setting API endpoint to %s...", CCAPI))
    72  		})
    73  
    74  		When("the API endpoint does not specify a protocol/schema (does not begin with 'http')", func() {
    75  			BeforeEach(func() {
    76  				CCAPI = "api.foo.com"
    77  				cmd.OptionalArgs.URL = CCAPI
    78  			})
    79  
    80  			It("defaults to TLS (prepends 'https://' to the endpoint)", func() {
    81  				Expect(err).ToNot(HaveOccurred())
    82  
    83  				Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
    84  				settings := fakeActor.SetTargetArgsForCall(0)
    85  				Expect(settings.URL).To(Equal("https://" + CCAPI))
    86  				Expect(settings.SkipSSLValidation).To(BeFalse())
    87  				Expect(testUI.Out).To(Say("Setting API endpoint to %s...", CCAPI))
    88  				Expect(testUI.Out).To(Say(`OK
    89  
    90  API endpoint:   some-api-target
    91  API version:    100.200.300`,
    92  				))
    93  			})
    94  		})
    95  
    96  		When("--skip-ssl-validation is passed", func() {
    97  			BeforeEach(func() {
    98  				cmd.SkipSSLValidation = true
    99  			})
   100  
   101  			It("skips SSL validation", func() {
   102  				Expect(err).ToNot(HaveOccurred())
   103  
   104  				settings := fakeActor.SetTargetArgsForCall(0)
   105  				Expect(settings.SkipSSLValidation).To(BeTrue())
   106  
   107  				Expect(testUI.Out).To(Say(`OK
   108  
   109  API endpoint:   some-api-target
   110  API version:    100.200.300`,
   111  				))
   112  			})
   113  		})
   114  
   115  		When("when the endpoint is TLS but the certificate is unverified", func() {
   116  			BeforeEach(func() {
   117  				fakeActor.SetTargetReturns(nil, ccerror.UnverifiedServerError{URL: CCAPI})
   118  			})
   119  
   120  			It("returns an error with a --skip-ssl-validation tip", func() {
   121  				Expect(err).To(MatchError(ccerror.UnverifiedServerError{URL: CCAPI}))
   122  				Expect(testUI.Out).ToNot(Say(`API endpoint:\s+some-api-target`))
   123  			})
   124  		})
   125  
   126  		When("the endpoint specifies a non-TLS URL ('http://')", func() {
   127  			BeforeEach(func() {
   128  				CCAPI = "http://api.foo.com"
   129  				cmd.OptionalArgs.URL = CCAPI
   130  			})
   131  
   132  			It("sets the target with a warning", func() {
   133  				Expect(err).ToNot(HaveOccurred())
   134  
   135  				Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
   136  				settings := fakeActor.SetTargetArgsForCall(0)
   137  				Expect(settings.URL).To(Equal(CCAPI))
   138  				Expect(settings.SkipSSLValidation).To(BeFalse())
   139  
   140  				Expect(testUI.Out).To(Say("Setting API endpoint to %s...", CCAPI))
   141  				Expect(testUI.Out).To(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended"))
   142  				Expect(testUI.Out).To(Say("OK"))
   143  			})
   144  		})
   145  
   146  		When("the URL host does not exist", func() {
   147  			var (
   148  				requestErr ccerror.RequestError
   149  			)
   150  
   151  			BeforeEach(func() {
   152  				CCAPI = "i.do.not.exist.com"
   153  				cmd.OptionalArgs.URL = CCAPI
   154  
   155  				requestErr = ccerror.RequestError{Err: errors.New("I am an error")}
   156  				fakeActor.SetTargetReturns(nil, requestErr)
   157  			})
   158  
   159  			It("returns an APIRequestError", func() {
   160  				Expect(err).To(MatchError(ccerror.RequestError{Err: requestErr.Err}))
   161  			})
   162  		})
   163  
   164  		When("the API is set but the user is not logged in", func() {
   165  			It("outputs a 'not logged in' message", func() {
   166  				Expect(err).ToNot(HaveOccurred())
   167  
   168  				Expect(testUI.Out).To(Say("Not logged in. Use 'faceman login' or 'faceman login --sso' to log in."))
   169  			})
   170  		})
   171  	})
   172  
   173  	When("viewing the current API target (no endpoint is provided)", func() {
   174  		When("the API target is not set", func() {
   175  			BeforeEach(func() {
   176  				fakeConfig.TargetReturns("")
   177  			})
   178  
   179  			It("informs the user that the API endpoint is not set through a tip", func() {
   180  				Expect(err).ToNot(HaveOccurred())
   181  
   182  				Expect(testUI.Out).To(Say("No API endpoint set. Use 'cf api' to set an endpoint"))
   183  			})
   184  		})
   185  
   186  		When("the API is set, the user is logged in", func() {
   187  			BeforeEach(func() {
   188  				fakeConfig.APIVersionReturns("100.200.300")
   189  				fakeConfig.CurrentUserReturns(configv3.User{
   190  					Name: "admin",
   191  				}, nil)
   192  			})
   193  
   194  			It("outputs target information", func() {
   195  				Expect(err).ToNot(HaveOccurred())
   196  				Expect(testUI.Out).To(Say(`API endpoint:\s+some-api-target`))
   197  				Expect(testUI.Out).To(Say(`API version:\s+100.200.300`))
   198  			})
   199  		})
   200  
   201  		When("the API is set but the user is not logged in", func() {
   202  			It("outputs a 'not logged in' message", func() {
   203  				Expect(err).ToNot(HaveOccurred())
   204  
   205  				Expect(testUI.Out).To(Say("Not logged in. Use 'faceman login' or 'faceman login --sso' to log in."))
   206  			})
   207  		})
   208  	})
   209  })