github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/shared/new_v3_based_clients_test.go (about)

     1  package shared_test
     2  
     3  import (
     4  	"net/http"
     5  	"runtime"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v6/shared"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  	. "github.com/onsi/gomega/ghttp"
    18  )
    19  
    20  var _ = Describe("New V3 Based Clients", func() {
    21  	var (
    22  		binaryName string
    23  		fakeConfig *commandfakes.FakeConfig
    24  		testUI     *ui.UI
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		binaryName = "faceman"
    29  		fakeConfig = new(commandfakes.FakeConfig)
    30  		fakeConfig.BinaryNameReturns(binaryName)
    31  
    32  		testUI = ui.NewTestUI(NewBuffer(), NewBuffer(), NewBuffer())
    33  	})
    34  
    35  	When("the api endpoint is not set", func() {
    36  		It("returns the NoAPISetError", func() {
    37  			_, _, err := NewV3BasedClients(fakeConfig, testUI, true)
    38  			Expect(err).To(MatchError(translatableerror.NoAPISetError{
    39  				BinaryName: binaryName,
    40  			}))
    41  		})
    42  	})
    43  
    44  	When("hitting the target returns an error", func() {
    45  		var server *Server
    46  		BeforeEach(func() {
    47  			server = NewTLSServer()
    48  
    49  			fakeConfig.TargetReturns(server.URL())
    50  			fakeConfig.SkipSSLValidationReturns(true)
    51  		})
    52  
    53  		AfterEach(func() {
    54  			server.Close()
    55  		})
    56  
    57  		When("the error is a cloud controller request error", func() {
    58  			BeforeEach(func() {
    59  				fakeConfig.TargetReturns("https://127.0.0.1:9876")
    60  			})
    61  
    62  			It("returns a command api request error", func() {
    63  				_, _, err := NewV3BasedClients(fakeConfig, testUI, true)
    64  				Expect(err).To(MatchError(ContainSubstring("dial")))
    65  			})
    66  		})
    67  
    68  		When("the error is a cloud controller api not found error", func() {
    69  			BeforeEach(func() {
    70  				server.AppendHandlers(
    71  					CombineHandlers(
    72  						VerifyRequest(http.MethodGet, "/"),
    73  						RespondWith(http.StatusNotFound, "something which is not json"),
    74  					),
    75  				)
    76  			})
    77  
    78  			It("returns a command api not found error", func() {
    79  				_, _, err := NewV3BasedClients(fakeConfig, testUI, true)
    80  				Expect(err).To(MatchError(ccerror.APINotFoundError{URL: server.URL()}))
    81  			})
    82  		})
    83  
    84  		When("the error is generic and the body is valid json", func() {
    85  			BeforeEach(func() {
    86  				server.AppendHandlers(
    87  					CombineHandlers(
    88  						VerifyRequest(http.MethodGet, "/"),
    89  						RespondWith(http.StatusTeapot, `{ "some-error": "invalid" }`),
    90  					),
    91  				)
    92  			})
    93  
    94  			It("returns a V3UnexpectedResponseError", func() {
    95  				_, _, err := NewV3BasedClients(fakeConfig, testUI, true)
    96  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
    97  			})
    98  		})
    99  	})
   100  
   101  	When("the DialTimeout is set", func() {
   102  		BeforeEach(func() {
   103  			if runtime.GOOS == "windows" {
   104  				Skip("due to timing issues on windows")
   105  			}
   106  			fakeConfig.TargetReturns("https://potato.bananapants11122.co.uk")
   107  			fakeConfig.DialTimeoutReturns(time.Nanosecond)
   108  		})
   109  
   110  		It("passes the value to the target", func() {
   111  			_, _, err := NewV3BasedClients(fakeConfig, testUI, true)
   112  			Expect(err.Error()).To(MatchRegexp("timeout"))
   113  		})
   114  	})
   115  
   116  	When("not targetting", func() {
   117  		It("does not target and returns no UAA client", func() {
   118  			ccClient, uaaClient, err := NewV3BasedClients(fakeConfig, testUI, false)
   119  			Expect(err).ToNot(HaveOccurred())
   120  			Expect(ccClient).ToNot(BeNil())
   121  			Expect(uaaClient).To(BeNil())
   122  			Expect(fakeConfig.SkipSSLValidationCallCount()).To(Equal(0))
   123  		})
   124  	})
   125  })