github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/command/v2/api_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/command" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v2" 10 "code.cloudfoundry.org/cli/command/v2/v2fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("api Command", func() { 19 var ( 20 cmd ApiCommand 21 testUI *ui.UI 22 fakeActor *v2fakes.FakeAPIActor 23 fakeConfig *commandfakes.FakeConfig 24 err error 25 ) 26 27 BeforeEach(func() { 28 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 29 fakeActor = new(v2fakes.FakeAPIActor) 30 fakeConfig = new(commandfakes.FakeConfig) 31 fakeConfig.BinaryNameReturns("faceman") 32 33 cmd = ApiCommand{ 34 UI: testUI, 35 Actor: fakeActor, 36 Config: fakeConfig, 37 } 38 }) 39 40 JustBeforeEach(func() { 41 err = cmd.Execute(nil) 42 }) 43 44 Context("when the API endpoint is not provided", func() { 45 Context("when the API is not set", func() { 46 It("displays a tip", func() { 47 Expect(err).ToNot(HaveOccurred()) 48 49 Expect(testUI.Out).To(Say("No api endpoint set. Use 'cf api' to set an endpoint")) 50 }) 51 }) 52 53 Context("when the API is set, the user is logged in and an org and space are targeted", func() { 54 BeforeEach(func() { 55 fakeConfig.TargetReturns("some-api-target") 56 fakeConfig.APIVersionReturns("some-version") 57 fakeConfig.CurrentUserReturns(configv3.User{ 58 Name: "admin", 59 }, nil) 60 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 61 Name: "some-org", 62 }) 63 fakeConfig.TargetedSpaceReturns(configv3.Space{ 64 Name: "some-space", 65 }) 66 }) 67 68 It("outputs target information", func() { 69 Expect(err).ToNot(HaveOccurred()) 70 Expect(testUI.Out).To(Say("API endpoint:\\s+some-api-target")) 71 Expect(testUI.Out).To(Say("API version:\\s+some-version")) 72 }) 73 }) 74 75 Context("when passed a --unset", func() { 76 BeforeEach(func() { 77 cmd.Unset = true 78 }) 79 80 It("clears the target", func() { 81 Expect(err).ToNot(HaveOccurred()) 82 Expect(testUI.Out).To(Say("Unsetting api endpoint...")) 83 Expect(testUI.Out).To(Say("OK")) 84 Expect(fakeActor.ClearTargetCallCount()).To(Equal(1)) 85 }) 86 }) 87 }) 88 89 Context("when a valid API endpoint is provided", func() { 90 Context("when the API has SSL", func() { 91 Context("with no protocol", func() { 92 var ( 93 CCAPI string 94 ) 95 96 BeforeEach(func() { 97 CCAPI = "api.foo.com" 98 cmd.OptionalArgs.URL = CCAPI 99 100 fakeConfig.TargetReturns("some-api-target") 101 fakeConfig.APIVersionReturns("some-version") 102 }) 103 104 Context("when the url has verified SSL", func() { 105 It("sets the target", func() { 106 Expect(err).ToNot(HaveOccurred()) 107 108 Expect(fakeActor.SetTargetCallCount()).To(Equal(1)) 109 _, settings := fakeActor.SetTargetArgsForCall(0) 110 Expect(settings.URL).To(Equal("https://" + CCAPI)) 111 Expect(settings.SkipSSLValidation).To(BeFalse()) 112 113 Expect(testUI.Out).To(Say("Setting api endpoint to %s...", CCAPI)) 114 Expect(testUI.Out).To(Say(`OK 115 116 API endpoint: some-api-target 117 API version: some-version`, 118 )) 119 }) 120 }) 121 122 Context("when the url has unverified SSL", func() { 123 Context("when --skip-ssl-validation is passed", func() { 124 BeforeEach(func() { 125 cmd.SkipSSLValidation = true 126 }) 127 128 It("sets the target", func() { 129 Expect(err).ToNot(HaveOccurred()) 130 131 Expect(fakeActor.SetTargetCallCount()).To(Equal(1)) 132 _, settings := fakeActor.SetTargetArgsForCall(0) 133 Expect(settings.URL).To(Equal("https://" + CCAPI)) 134 Expect(settings.SkipSSLValidation).To(BeTrue()) 135 136 Expect(testUI.Out).To(Say("Setting api endpoint to %s...", CCAPI)) 137 Expect(testUI.Out).To(Say(`OK 138 139 API endpoint: some-api-target 140 API version: some-version`, 141 )) 142 }) 143 }) 144 145 Context("when no additional flags are passed", func() { 146 BeforeEach(func() { 147 fakeActor.SetTargetReturns(nil, cloudcontroller.UnverifiedServerError{URL: CCAPI}) 148 }) 149 150 It("returns an error with a --skip-ssl-validation tip", func() { 151 Expect(err).To(MatchError(command.InvalidSSLCertError{API: CCAPI})) 152 Expect(testUI.Out).ToNot(Say("API endpoint:\\s+some-api-target")) 153 }) 154 }) 155 }) 156 }) 157 }) 158 159 Context("when the API does not have SSL", func() { 160 var CCAPI string 161 162 BeforeEach(func() { 163 CCAPI = "http://api.foo.com" 164 cmd.OptionalArgs.URL = CCAPI 165 }) 166 167 It("sets the target with a warning", func() { 168 Expect(err).ToNot(HaveOccurred()) 169 170 Expect(fakeActor.SetTargetCallCount()).To(Equal(1)) 171 _, settings := fakeActor.SetTargetArgsForCall(0) 172 Expect(settings.URL).To(Equal(CCAPI)) 173 Expect(settings.SkipSSLValidation).To(BeFalse()) 174 175 Expect(testUI.Out).To(Say("Setting api endpoint to %s...", CCAPI)) 176 Expect(testUI.Out).To(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")) 177 Expect(testUI.Out).To(Say("OK")) 178 }) 179 }) 180 181 Context("when the API is set but the user is not logged in", func() { 182 BeforeEach(func() { 183 cmd.OptionalArgs.URL = "https://api.foo.com" 184 fakeConfig.TargetReturns("something") 185 }) 186 187 It("outputs a 'not logged in' message", func() { 188 Expect(err).ToNot(HaveOccurred()) 189 190 Expect(testUI.Out).To(Say("Not logged in. Use 'faceman login' to log in.")) 191 }) 192 }) 193 194 Context("when the API is set but the user is logged in", func() { 195 BeforeEach(func() { 196 cmd.OptionalArgs.URL = "https://api.foo.com" 197 fakeConfig.TargetReturns("something") 198 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 199 }) 200 201 It("does not output a 'not logged in' message", func() { 202 Expect(err).ToNot(HaveOccurred()) 203 204 Expect(testUI.Out).ToNot(Say("Not logged in. Use 'faceman login' to log in.")) 205 }) 206 }) 207 208 Context("when the URL host does not exist", func() { 209 var ( 210 CCAPI string 211 requestErr cloudcontroller.RequestError 212 ) 213 214 BeforeEach(func() { 215 CCAPI = "i.do.not.exist.com" 216 cmd.OptionalArgs.URL = CCAPI 217 218 requestErr = cloudcontroller.RequestError{Err: errors.New("I am an error")} 219 fakeActor.SetTargetReturns(nil, requestErr) 220 }) 221 222 It("returns an APIRequestError", func() { 223 Expect(err).To(MatchError(command.APIRequestError{Err: requestErr.Err})) 224 }) 225 }) 226 }) 227 })