github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/shared/new_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/uaa"
     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 Clients", func() {
    21  	var (
    22  		binaryName    string
    23  		fakeConfig    *commandfakes.FakeConfig
    24  		testUI        *ui.UI
    25  		fakeUaaClient *uaa.Client
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		binaryName = "faceman"
    30  		fakeConfig = new(commandfakes.FakeConfig)
    31  		fakeUaaClient = &uaa.Client{}
    32  		testUI = ui.NewTestUI(NewBuffer(), NewBuffer(), NewBuffer())
    33  
    34  		fakeConfig.BinaryNameReturns(binaryName)
    35  	})
    36  
    37  	Describe("NewClients", func() {
    38  		When("the api endpoint is not set", func() {
    39  			It("returns an error", func() {
    40  				_, _, err := GetNewClientsAndConnectToCF(fakeConfig, testUI)
    41  				Expect(err).To(MatchError(translatableerror.NoAPISetError{
    42  					BinaryName: binaryName,
    43  				}))
    44  			})
    45  		})
    46  
    47  		When("the DialTimeout is set", func() {
    48  			BeforeEach(func() {
    49  				if runtime.GOOS == "windows" {
    50  					Skip("due to timing issues on windows")
    51  				}
    52  				fakeConfig.TargetReturns("https://potato.bananapants11122.co.uk")
    53  				fakeConfig.DialTimeoutReturns(time.Nanosecond)
    54  			})
    55  
    56  			It("passes the value to the target", func() {
    57  				_, _, err := GetNewClientsAndConnectToCF(fakeConfig, testUI)
    58  				Expect(err.Error()).To(MatchRegexp("timeout"))
    59  			})
    60  		})
    61  
    62  		When("the targeting a CF fails", func() {
    63  			BeforeEach(func() {
    64  				fakeConfig.TargetReturns("https://potato.bananapants11122.co.uk")
    65  			})
    66  
    67  			It("returns an error", func() {
    68  				_, _, err := GetNewClientsAndConnectToCF(fakeConfig, testUI)
    69  				Expect(err).To(HaveOccurred())
    70  			})
    71  		})
    72  
    73  		When("the targeted CF is older than the minimum supported version", func() {
    74  			var server *Server
    75  
    76  			BeforeEach(func() {
    77  				server = NewTLSServer()
    78  
    79  				fakeConfig.TargetReturns(server.URL())
    80  				fakeConfig.SkipSSLValidationReturns(true)
    81  				server.AppendHandlers(
    82  					CombineHandlers(
    83  						VerifyRequest(http.MethodGet, "/v2/info"),
    84  						RespondWith(http.StatusOK, `{ "api_version": "2.68.0" }`),
    85  					),
    86  				)
    87  			})
    88  
    89  			AfterEach(func() {
    90  				server.Close()
    91  			})
    92  
    93  			It("outputs a warning", func() {
    94  				GetNewClientsAndConnectToCF(fakeConfig, testUI)
    95  				Expect(testUI.Err).To(Say("Your CF API version .+ is no longer supported. Upgrade to a newer version of the API .+"))
    96  			})
    97  		})
    98  
    99  	})
   100  	Describe("NewWrapedCloudControllerClient", func() {
   101  		It("returns a cloud controller client and an auth wrapper", func() {
   102  			ccClient, authWrapper := NewWrappedCloudControllerClient(fakeConfig, testUI)
   103  			Expect(ccClient).ToNot(BeNil())
   104  			Expect(authWrapper).ToNot(BeNil())
   105  		})
   106  	})
   107  
   108  	Describe("NewRouterClient", func() {
   109  		It("should return a new router client", func() {
   110  			routerClient, err := NewRouterClient(fakeConfig, testUI, fakeUaaClient)
   111  			Expect(err).ToNot(HaveOccurred())
   112  			Expect(routerClient).ToNot(BeNil())
   113  		})
   114  
   115  		It("reads the app name and app version for its own config", func() {
   116  			_, _ = NewRouterClient(fakeConfig, testUI, fakeUaaClient)
   117  			Expect(fakeConfig.BinaryNameCallCount()).To(Equal(1))
   118  			Expect(fakeConfig.BinaryVersionCallCount()).To(Equal(1))
   119  		})
   120  	})
   121  
   122  })