github.com/arunkumar7540/cli@v6.45.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 := NewClients(fakeConfig, testUI, true) 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 := NewClients(fakeConfig, testUI, true) 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 := NewClients(fakeConfig, testUI, true) 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 NewClients(fakeConfig, testUI, true) 95 96 Expect(testUI.Err).To(Say("Your API version is no longer supported. Upgrade to a newer version of the API")) 97 }) 98 }) 99 100 When("not targetting", func() { 101 It("does not target and returns no UAA client", func() { 102 ccClient, uaaClient, err := NewClients(fakeConfig, testUI, false) 103 Expect(err).ToNot(HaveOccurred()) 104 Expect(ccClient).ToNot(BeNil()) 105 Expect(uaaClient).To(BeNil()) 106 Expect(fakeConfig.SkipSSLValidationCallCount()).To(Equal(0)) 107 }) 108 }) 109 }) 110 111 Describe("NewRouterClient", func() { 112 It("should return a new router client", func() { 113 routerClient, err := NewRouterClient(fakeConfig, testUI, fakeUaaClient) 114 Expect(err).ToNot(HaveOccurred()) 115 Expect(routerClient).ToNot(BeNil()) 116 }) 117 118 It("reads the app name and app version for its own config", func() { 119 _, _ = NewRouterClient(fakeConfig, testUI, fakeUaaClient) 120 Expect(fakeConfig.BinaryNameCallCount()).To(Equal(1)) 121 Expect(fakeConfig.BinaryVersionCallCount()).To(Equal(1)) 122 }) 123 }) 124 125 })