github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/create_service_broker_test.go (about) 1 package servicebroker_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/commands/servicebroker" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/flags" 10 "code.cloudfoundry.org/cli/cf/requirements" 11 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 12 13 "code.cloudfoundry.org/cli/cf/api/apifakes" 14 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 15 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 16 17 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("CreateServiceBroker", func() { 23 var ( 24 ui *testterm.FakeUI 25 configRepo coreconfig.Repository 26 serviceBrokerRepo *apifakes.FakeServiceBrokerRepository 27 28 cmd commandregistry.Command 29 deps commandregistry.Dependency 30 factory *requirementsfakes.FakeFactory 31 flagContext flags.FlagContext 32 33 loginRequirement requirements.Requirement 34 targetedSpaceRequirement requirements.Requirement 35 minAPIVersionRequirement requirements.Requirement 36 ) 37 38 BeforeEach(func() { 39 ui = &testterm.FakeUI{} 40 configRepo = testconfig.NewRepositoryWithDefaults() 41 serviceBrokerRepo = new(apifakes.FakeServiceBrokerRepository) 42 repoLocator := deps.RepoLocator.SetServiceBrokerRepository(serviceBrokerRepo) 43 44 deps = commandregistry.Dependency{ 45 UI: ui, 46 Config: configRepo, 47 RepoLocator: repoLocator, 48 } 49 50 cmd = &servicebroker.CreateServiceBroker{} 51 cmd.SetDependency(deps, false) 52 53 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 54 factory = new(requirementsfakes.FakeFactory) 55 56 loginRequirement = &passingRequirement{Name: "login-requirement"} 57 factory.NewLoginRequirementReturns(loginRequirement) 58 59 targetedSpaceRequirement = &passingRequirement{Name: "targeted-space-requirement"} 60 factory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement) 61 62 minAPIVersionRequirement = &passingRequirement{Name: "min-api-version-requirement"} 63 factory.NewMinAPIVersionRequirementReturns(minAPIVersionRequirement) 64 }) 65 66 It("has an alias of `csb`", func() { 67 cmd := &servicebroker.CreateServiceBroker{} 68 69 Expect(cmd.MetaData().ShortName).To(Equal("csb")) 70 }) 71 72 Describe("Requirements", func() { 73 Context("when not provided exactly four args", func() { 74 BeforeEach(func() { 75 flagContext.Parse("service-broker") 76 }) 77 78 It("fails with usage", func() { 79 _, err := cmd.Requirements(factory, flagContext) 80 Expect(err).To(HaveOccurred()) 81 Expect(ui.Outputs()).To(ContainSubstrings( 82 []string{"FAILED"}, 83 []string{"Incorrect Usage. Requires SERVICE_BROKER, USERNAME, PASSWORD, URL as arguments"}, 84 )) 85 }) 86 }) 87 88 Context("when provided exactly four args", func() { 89 BeforeEach(func() { 90 flagContext.Parse("service-broker", "username", "password", "url") 91 }) 92 93 It("returns a LoginRequirement", func() { 94 actualRequirements, err := cmd.Requirements(factory, flagContext) 95 Expect(err).NotTo(HaveOccurred()) 96 Expect(factory.NewLoginRequirementCallCount()).To(Equal(1)) 97 Expect(actualRequirements).To(ContainElement(loginRequirement)) 98 }) 99 }) 100 101 Context("when the --space-scoped flag is provided", func() { 102 BeforeEach(func() { 103 flagContext.Parse("service-broker", "username", "password", "url", "--space-scoped") 104 }) 105 106 It("returns a TargetedSpaceRequirement", func() { 107 actualRequirements, err := cmd.Requirements(factory, flagContext) 108 Expect(err).NotTo(HaveOccurred()) 109 Expect(factory.NewTargetedSpaceRequirementCallCount()).To(Equal(1)) 110 Expect(actualRequirements).To(ContainElement(targetedSpaceRequirement)) 111 }) 112 113 It("returns a MinAPIVersionRequirement", func() { 114 actualRequirements, err := cmd.Requirements(factory, flagContext) 115 Expect(err).NotTo(HaveOccurred()) 116 Expect(actualRequirements).To(ContainElement(minAPIVersionRequirement)) 117 }) 118 }) 119 }) 120 121 Describe("Execute", func() { 122 var runCLIErr error 123 124 BeforeEach(func() { 125 err := flagContext.Parse("service-broker", "username", "password", "url") 126 Expect(err).NotTo(HaveOccurred()) 127 cmd.Requirements(factory, flagContext) 128 }) 129 130 JustBeforeEach(func() { 131 runCLIErr = cmd.Execute(flagContext) 132 }) 133 134 It("tells the user it is creating the service broker", func() { 135 Expect(runCLIErr).NotTo(HaveOccurred()) 136 Expect(ui.Outputs()).To(ContainSubstrings( 137 []string{"Creating service broker", "service-broker", "my-user"}, 138 []string{"OK"}, 139 )) 140 }) 141 142 It("tries to create the service broker", func() { 143 Expect(runCLIErr).NotTo(HaveOccurred()) 144 Expect(serviceBrokerRepo.CreateCallCount()).To(Equal(1)) 145 name, url, username, password, spaceGUID := serviceBrokerRepo.CreateArgsForCall(0) 146 Expect(name).To(Equal("service-broker")) 147 Expect(url).To(Equal("url")) 148 Expect(username).To(Equal("username")) 149 Expect(password).To(Equal("password")) 150 Expect(spaceGUID).To(Equal("")) 151 }) 152 153 Context("when the --space-scoped flag is passed", func() { 154 BeforeEach(func() { 155 err := flagContext.Parse("service-broker", "username", "password", "url", "--space-scoped") 156 Expect(err).NotTo(HaveOccurred()) 157 }) 158 159 It("tries to create the service broker with the targeted space guid", func() { 160 Expect(runCLIErr).NotTo(HaveOccurred()) 161 Expect(serviceBrokerRepo.CreateCallCount()).To(Equal(1)) 162 name, url, username, password, spaceGUID := serviceBrokerRepo.CreateArgsForCall(0) 163 Expect(name).To(Equal("service-broker")) 164 Expect(url).To(Equal("url")) 165 Expect(username).To(Equal("username")) 166 Expect(password).To(Equal("password")) 167 Expect(spaceGUID).To(Equal("my-space-guid")) 168 }) 169 170 It("tells the user it is creating the service broker in the targeted org and space", func() { 171 Expect(runCLIErr).NotTo(HaveOccurred()) 172 Expect(ui.Outputs()).To(ContainSubstrings( 173 []string{"Creating service broker service-broker in org my-org / space my-space as my-user"}, 174 []string{"OK"}, 175 )) 176 }) 177 }) 178 179 Context("when creating the service broker succeeds", func() { 180 BeforeEach(func() { 181 serviceBrokerRepo.CreateReturns(nil) 182 }) 183 184 It("says OK", func() { 185 Expect(runCLIErr).NotTo(HaveOccurred()) 186 Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"})) 187 }) 188 }) 189 190 Context("when creating the service broker fails", func() { 191 BeforeEach(func() { 192 serviceBrokerRepo.CreateReturns(errors.New("create-err")) 193 }) 194 195 It("returns an error", func() { 196 Expect(runCLIErr).To(HaveOccurred()) 197 Expect(runCLIErr.Error()).To(Equal("create-err")) 198 }) 199 }) 200 }) 201 })