github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/create_service_broker_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 8 "code.cloudfoundry.org/cli/actor/actionerror" 9 "code.cloudfoundry.org/cli/actor/v7action" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 v7 "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/v7fakes" 13 "code.cloudfoundry.org/cli/resources" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("create-service-broker Command", func() { 22 const ( 23 binaryName = "cf-command" 24 user = "steve" 25 serviceBrokerName = "fake-service-broker-name" 26 username = "fake-username" 27 password = "fake-password" 28 url = "fake-url" 29 ) 30 31 var ( 32 cmd *v7.CreateServiceBrokerCommand 33 testUI *ui.UI 34 fakeConfig *commandfakes.FakeConfig 35 fakeSharedActor *commandfakes.FakeSharedActor 36 fakeActor *v7fakes.FakeActor 37 input *Buffer 38 executeErr error 39 ) 40 41 BeforeEach(func() { 42 input = NewBuffer() 43 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 44 fakeConfig = new(commandfakes.FakeConfig) 45 fakeSharedActor = new(commandfakes.FakeSharedActor) 46 fakeActor = new(v7fakes.FakeActor) 47 fakeActor.CreateServiceBrokerReturns(v7action.Warnings{"some default warning"}, nil) 48 49 fakeConfig.BinaryNameReturns(binaryName) 50 51 cmd = &v7.CreateServiceBrokerCommand{ 52 BaseCommand: v7.BaseCommand{ 53 UI: testUI, 54 Config: fakeConfig, 55 SharedActor: fakeSharedActor, 56 Actor: fakeActor, 57 }, 58 } 59 }) 60 61 JustBeforeEach(func() { 62 executeErr = cmd.Execute(nil) 63 }) 64 65 When("checking target fails", func() { 66 BeforeEach(func() { 67 fakeSharedActor.CheckTargetReturns(errors.New("an error occurred")) 68 }) 69 70 It("returns an error", func() { 71 Expect(executeErr).To(MatchError("an error occurred")) 72 }) 73 }) 74 75 When("fetching the current user fails", func() { 76 BeforeEach(func() { 77 fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("an error occurred")) 78 setPositionalFlags(cmd, serviceBrokerName, username, password, url) 79 }) 80 81 It("return an error", func() { 82 Expect(executeErr).To(MatchError("an error occurred")) 83 }) 84 }) 85 86 When("fetching the current user succeeds", func() { 87 BeforeEach(func() { 88 fakeActor.GetCurrentUserReturns(configv3.User{Name: user}, nil) 89 setPositionalFlags(cmd, serviceBrokerName, username, password, url) 90 }) 91 92 It("checks that there is a valid target", func() { 93 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 94 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 95 Expect(checkTargetedOrg).To(BeFalse()) 96 Expect(checkTargetedSpace).To(BeFalse()) 97 }) 98 99 It("displays a message with the username", func() { 100 Expect(testUI.Out).To(Say(`Creating service broker %s as %s\.\.\.`, serviceBrokerName, user)) 101 }) 102 103 It("passes the data to the actor layer", func() { 104 Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1)) 105 106 model := fakeActor.CreateServiceBrokerArgsForCall(0) 107 108 Expect(model.Name).To(Equal(serviceBrokerName)) 109 Expect(model.Username).To(Equal(username)) 110 Expect(model.Password).To(Equal(password)) 111 Expect(model.URL).To(Equal(url)) 112 Expect(model.SpaceGUID).To(Equal("")) 113 }) 114 115 It("displays the warnings", func() { 116 Expect(testUI.Err).To(Say("some default warning")) 117 }) 118 119 It("displays OK", func() { 120 Expect(testUI.Out).To(Say("OK")) 121 }) 122 123 When("the actor returns an error", func() { 124 BeforeEach(func() { 125 fakeActor.CreateServiceBrokerReturns(v7action.Warnings{"service-broker-warnings"}, errors.New("fake create-service-broker error")) 126 }) 127 128 It("prints the error and warnings", func() { 129 Expect(testUI.Out).NotTo(Say("OK")) 130 Expect(executeErr).To(MatchError("fake create-service-broker error")) 131 Expect(testUI.Err).To(Say("service-broker-warnings")) 132 }) 133 }) 134 135 When("creating a space scoped broker", func() { 136 const ( 137 orgName = "fake-org-name" 138 spaceName = "fake-space-name" 139 spaceGUID = "fake-space-guid" 140 ) 141 142 BeforeEach(func() { 143 cmd.SpaceScoped = true 144 fakeConfig.TargetedSpaceReturns(configv3.Space{ 145 Name: spaceName, 146 GUID: spaceGUID, 147 }) 148 fakeConfig.TargetedOrganizationNameReturns(orgName) 149 }) 150 151 It("checks that a space is targeted", func() { 152 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 153 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 154 Expect(checkTargetedOrg).To(BeTrue()) 155 Expect(checkTargetedSpace).To(BeTrue()) 156 }) 157 158 It("displays the space name in the message", func() { 159 Expect(testUI.Out).To(Say(`Creating service broker %s in org %s / space %s as %s\.\.\.`, serviceBrokerName, orgName, spaceName, user)) 160 }) 161 162 It("looks up the space guid and passes it to the actor", func() { 163 Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1)) 164 165 model := fakeActor.CreateServiceBrokerArgsForCall(0) 166 Expect(model.SpaceGUID).To(Equal(spaceGUID)) 167 }) 168 }) 169 }) 170 171 When("password is provided as environment variable", func() { 172 const ( 173 varName = "CF_BROKER_PASSWORD" 174 varPassword = "var-password" 175 ) 176 177 BeforeEach(func() { 178 setPositionalFlags(cmd, serviceBrokerName, username, url, "") 179 os.Setenv(varName, varPassword) 180 }) 181 182 AfterEach(func() { 183 os.Unsetenv(varName) 184 }) 185 186 It("passes the data to the actor layer", func() { 187 Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1)) 188 189 model := fakeActor.CreateServiceBrokerArgsForCall(0) 190 191 Expect(model.Name).To(Equal(serviceBrokerName)) 192 Expect(model.Username).To(Equal(username)) 193 Expect(model.Password).To(Equal(varPassword)) 194 Expect(model.URL).To(Equal(url)) 195 Expect(model.SpaceGUID).To(Equal("")) 196 }) 197 }) 198 199 When("password is provided via prompt", func() { 200 const promptPassword = "prompt-password" 201 202 BeforeEach(func() { 203 setPositionalFlags(cmd, serviceBrokerName, username, url, "") 204 205 _, err := input.Write([]byte(fmt.Sprintf("%s\n", promptPassword))) 206 Expect(err).NotTo(HaveOccurred()) 207 }) 208 209 It("prompts the user for credentials", func() { 210 Expect(testUI.Out).To(Say("Service Broker Password: ")) 211 }) 212 213 It("does not echo the credentials", func() { 214 Expect(testUI.Out).NotTo(Say(promptPassword)) 215 Expect(testUI.Err).NotTo(Say(promptPassword)) 216 }) 217 218 It("passes the data to the actor layer", func() { 219 Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1)) 220 221 model := fakeActor.CreateServiceBrokerArgsForCall(0) 222 223 Expect(model.Name).To(Equal(serviceBrokerName)) 224 Expect(model.Username).To(Equal(username)) 225 Expect(model.Password).To(Equal(promptPassword)) 226 Expect(model.URL).To(Equal(url)) 227 Expect(model.SpaceGUID).To(Equal("")) 228 }) 229 }) 230 231 When("the --update-if-exists flag is used", func() { 232 BeforeEach(func() { 233 setPositionalFlags(cmd, serviceBrokerName, username, password, url) 234 setFlag(cmd, "--update-if-exists") 235 }) 236 237 Context("and the broker does not exist", func() { 238 BeforeEach(func() { 239 fakeActor.GetServiceBrokerByNameReturns(resources.ServiceBroker{}, v7action.Warnings{}, actionerror.ServiceBrokerNotFoundError{}) 240 }) 241 242 It("checks to see whether the broker exists", func() { 243 Expect(fakeActor.GetServiceBrokerByNameCallCount()).To(Equal(1)) 244 Expect(fakeActor.GetServiceBrokerByNameArgsForCall(0)).To(Equal(serviceBrokerName)) 245 }) 246 247 It("creates a new service broker", func() { 248 Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1)) 249 250 model := fakeActor.CreateServiceBrokerArgsForCall(0) 251 252 Expect(model.Name).To(Equal(serviceBrokerName)) 253 Expect(model.Username).To(Equal(username)) 254 Expect(model.Password).To(Equal(password)) 255 Expect(model.URL).To(Equal(url)) 256 Expect(model.SpaceGUID).To(Equal("")) 257 }) 258 }) 259 260 Context("and the broker already exists", func() { 261 const brokerGUID = "fake-broker-guid" 262 263 BeforeEach(func() { 264 fakeActor.GetServiceBrokerByNameReturns(resources.ServiceBroker{GUID: brokerGUID}, v7action.Warnings{}, nil) 265 }) 266 267 It("checks to see whether the broker exists", func() { 268 Expect(fakeActor.GetServiceBrokerByNameCallCount()).To(Equal(1)) 269 Expect(fakeActor.GetServiceBrokerByNameArgsForCall(0)).To(Equal(serviceBrokerName)) 270 }) 271 272 It("updates an existing service broker", func() { 273 Expect(fakeActor.UpdateServiceBrokerCallCount()).To(Equal(1)) 274 275 guid, model := fakeActor.UpdateServiceBrokerArgsForCall(0) 276 277 Expect(guid).To(Equal(brokerGUID)) 278 Expect(model.Username).To(Equal(username)) 279 Expect(model.Password).To(Equal(password)) 280 Expect(model.URL).To(Equal(url)) 281 Expect(model.SpaceGUID).To(Equal("")) 282 }) 283 }) 284 }) 285 })