github.com/orange-cloudfoundry/cli@v7.1.0+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 CombineHandlers( 87 VerifyRequest(http.MethodGet, "/"), 88 RespondWith(http.StatusOK, `{ "links": {"log_cache": {"href": "api.coolbeans.log-cache"}}}`), 89 ), 90 ) 91 }) 92 93 AfterEach(func() { 94 server.Close() 95 }) 96 97 It("outputs a warning", func() { 98 _, _, err := GetNewClientsAndConnectToCF(fakeConfig, testUI) 99 Expect(err).To(HaveOccurred()) 100 Expect(testUI.Err).To(Say("Your CF API version .+ is no longer supported. Upgrade to a newer version of the API .+")) 101 }) 102 }) 103 104 }) 105 Describe("NewWrapedCloudControllerClient", func() { 106 It("returns a cloud controller client and an auth wrapper", func() { 107 ccClient, authWrapper := NewWrappedCloudControllerClient(fakeConfig, testUI) 108 Expect(ccClient).ToNot(BeNil()) 109 Expect(authWrapper).ToNot(BeNil()) 110 }) 111 }) 112 113 Describe("NewRouterClient", func() { 114 It("should return a new router client", func() { 115 routerClient, err := NewRouterClient(fakeConfig, testUI, fakeUaaClient) 116 Expect(err).ToNot(HaveOccurred()) 117 Expect(routerClient).ToNot(BeNil()) 118 }) 119 120 It("reads the app name and app version for its own config", func() { 121 _, _ = NewRouterClient(fakeConfig, testUI, fakeUaaClient) 122 Expect(fakeConfig.BinaryNameCallCount()).To(Equal(1)) 123 Expect(fakeConfig.BinaryVersionCallCount()).To(Equal(1)) 124 }) 125 }) 126 127 })