github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/target_test.go (about) 1 package commands_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes" 5 "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 8 "code.cloudfoundry.org/cli/cf/errors" 9 "code.cloudfoundry.org/cli/cf/flags" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 16 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 17 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 18 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 19 20 "code.cloudfoundry.org/cli/cf/commands" 21 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 22 ) 23 24 var _ = Describe("target command", func() { 25 var ( 26 orgRepo *organizationsfakes.FakeOrganizationRepository 27 spaceRepo *spacesfakes.FakeSpaceRepository 28 requirementsFactory *requirementsfakes.FakeFactory 29 config coreconfig.Repository 30 ui *testterm.FakeUI 31 deps commandregistry.Dependency 32 ) 33 34 updateCommandDependency := func(pluginCall bool) { 35 deps.UI = ui 36 deps.Config = config 37 deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo) 38 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 39 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("target").SetDependency(deps, pluginCall)) 40 } 41 42 listSpacesStub := func(spaces []models.Space) func(func(models.Space) bool) error { 43 return func(cb func(models.Space) bool) error { 44 var keepGoing bool 45 for _, s := range spaces { 46 keepGoing = cb(s) 47 if !keepGoing { 48 break 49 } 50 } 51 return nil 52 } 53 } 54 55 BeforeEach(func() { 56 ui = new(testterm.FakeUI) 57 orgRepo = new(organizationsfakes.FakeOrganizationRepository) 58 spaceRepo = new(spacesfakes.FakeSpaceRepository) 59 config = testconfig.NewRepository() 60 requirementsFactory = new(requirementsfakes.FakeFactory) 61 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 62 requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Passing{}) 63 }) 64 65 var callTarget = func(args []string) bool { 66 return testcmd.RunCLICommand("target", args, requirementsFactory, updateCommandDependency, false, ui) 67 } 68 69 Context("when there are too many arguments given", func() { 70 var cmd commandregistry.Command 71 var flagContext flags.FlagContext 72 73 BeforeEach(func() { 74 cmd = new(commands.Target) 75 cmd.SetDependency(deps, false) 76 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 77 }) 78 79 It("fails with usage", func() { 80 flagContext.Parse("blahblah") 81 82 reqs, err := cmd.Requirements(requirementsFactory, flagContext) 83 Expect(err).NotTo(HaveOccurred()) 84 85 err = testcmd.RunRequirements(reqs) 86 Expect(err).To(HaveOccurred()) 87 Expect(err.Error()).To(ContainSubstring("Incorrect Usage")) 88 Expect(err.Error()).To(ContainSubstring("No argument required")) 89 }) 90 }) 91 92 Describe("when there is no api endpoint set", func() { 93 BeforeEach(func() { 94 requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Failing{Message: "no api set"}) 95 }) 96 97 It("fails requirements", func() { 98 Expect(callTarget([]string{})).To(BeFalse()) 99 }) 100 }) 101 102 Describe("when the user is not logged in", func() { 103 BeforeEach(func() { 104 config.SetAccessToken("") 105 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 106 }) 107 108 It("prints the target info when no org or space is specified", func() { 109 Expect(callTarget([]string{})).To(BeFalse()) 110 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 111 }) 112 113 It("fails requirements when targeting a space or org", func() { 114 Expect(callTarget([]string{"-o", "some-crazy-org-im-not-in"})).To(BeFalse()) 115 116 Expect(callTarget([]string{"-s", "i-love-space"})).To(BeFalse()) 117 }) 118 }) 119 120 Context("when the user is logged in", func() { 121 BeforeEach(func() { 122 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 123 }) 124 125 var expectOrgToBeCleared = func() { 126 Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{})) 127 } 128 129 var expectSpaceToBeCleared = func() { 130 Expect(config.SpaceFields()).To(Equal(models.SpaceFields{})) 131 } 132 133 Context("there are no errors", func() { 134 BeforeEach(func() { 135 org := models.Organization{} 136 org.Name = "my-organization" 137 org.GUID = "my-organization-guid" 138 139 orgRepo.ListOrgsReturns([]models.Organization{org}, nil) 140 orgRepo.FindByNameReturns(org, nil) 141 config.SetOrganizationFields(models.OrganizationFields{Name: org.Name, GUID: org.GUID}) 142 }) 143 144 It("it updates the organization in the config", func() { 145 callTarget([]string{"-o", "my-organization"}) 146 147 Expect(orgRepo.FindByNameCallCount()).To(Equal(1)) 148 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 149 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 150 151 Expect(config.OrganizationFields().GUID).To(Equal("my-organization-guid")) 152 }) 153 154 It("updates the space in the config", func() { 155 space := models.Space{} 156 space.Name = "my-space" 157 space.GUID = "my-space-guid" 158 159 spaceRepo.FindByNameReturns(space, nil) 160 161 callTarget([]string{"-s", "my-space"}) 162 163 Expect(spaceRepo.FindByNameCallCount()).To(Equal(1)) 164 Expect(spaceRepo.FindByNameArgsForCall(0)).To(Equal("my-space")) 165 Expect(config.SpaceFields().GUID).To(Equal("my-space-guid")) 166 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 167 }) 168 169 It("updates both the organization and the space in the config", func() { 170 space := models.Space{} 171 space.Name = "my-space" 172 space.GUID = "my-space-guid" 173 spaceRepo.FindByNameReturns(space, nil) 174 175 callTarget([]string{"-o", "my-organization", "-s", "my-space"}) 176 177 Expect(orgRepo.FindByNameCallCount()).To(Equal(1)) 178 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 179 Expect(config.OrganizationFields().GUID).To(Equal("my-organization-guid")) 180 181 Expect(spaceRepo.FindByNameCallCount()).To(Equal(1)) 182 Expect(spaceRepo.FindByNameArgsForCall(0)).To(Equal("my-space")) 183 Expect(config.SpaceFields().GUID).To(Equal("my-space-guid")) 184 185 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 186 }) 187 188 It("only updates the organization in the config when the space can't be found", func() { 189 config.SetSpaceFields(models.SpaceFields{}) 190 191 spaceRepo.FindByNameReturns(models.Space{}, errors.New("Error finding space by name.")) 192 193 callTarget([]string{"-o", "my-organization", "-s", "my-space"}) 194 195 Expect(orgRepo.FindByNameCallCount()).To(Equal(1)) 196 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 197 Expect(config.OrganizationFields().GUID).To(Equal("my-organization-guid")) 198 199 Expect(spaceRepo.FindByNameCallCount()).To(Equal(1)) 200 Expect(spaceRepo.FindByNameArgsForCall(0)).To(Equal("my-space")) 201 Expect(config.SpaceFields().GUID).To(Equal("")) 202 203 Expect(ui.ShowConfigurationCalled).To(BeFalse()) 204 Expect(ui.Outputs()).To(ContainSubstrings( 205 []string{"FAILED"}, 206 []string{"Unable to access space", "my-space"}, 207 )) 208 }) 209 210 Describe("when there is only a single space", func() { 211 It("target space automatically ", func() { 212 space := models.Space{} 213 space.Name = "my-space" 214 space.GUID = "my-space-guid" 215 spaceRepo.FindByNameReturns(space, nil) 216 spaceRepo.ListSpacesStub = listSpacesStub([]models.Space{space}) 217 218 callTarget([]string{"-o", "my-organization"}) 219 220 Expect(config.OrganizationFields().GUID).To(Equal("my-organization-guid")) 221 Expect(config.SpaceFields().GUID).To(Equal("my-space-guid")) 222 223 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 224 }) 225 226 }) 227 228 It("not target space automatically for orgs having multiple spaces", func() { 229 space1 := models.Space{} 230 space1.Name = "my-space" 231 space1.GUID = "my-space-guid" 232 space2 := models.Space{} 233 space2.Name = "my-space" 234 space2.GUID = "my-space-guid" 235 spaceRepo.ListSpacesStub = listSpacesStub([]models.Space{space1, space2}) 236 237 callTarget([]string{"-o", "my-organization"}) 238 239 Expect(config.OrganizationFields().GUID).To(Equal("my-organization-guid")) 240 Expect(config.SpaceFields().GUID).To(Equal("")) 241 242 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 243 }) 244 245 It("displays an update notification", func() { 246 callTarget([]string{"-o", "my-organization"}) 247 Expect(ui.NotifyUpdateIfNeededCallCount).To(Equal(1)) 248 }) 249 }) 250 251 Context("there are errors", func() { 252 It("fails when the user does not have access to the specified organization", func() { 253 orgRepo.FindByNameReturns(models.Organization{}, errors.New("Invalid access")) 254 255 callTarget([]string{"-o", "my-organization"}) 256 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 257 expectOrgToBeCleared() 258 expectSpaceToBeCleared() 259 }) 260 261 It("fails when the organization is not found", func() { 262 orgRepo.FindByNameReturns(models.Organization{}, errors.New("my-organization not found")) 263 264 callTarget([]string{"-o", "my-organization"}) 265 266 Expect(ui.Outputs()).To(ContainSubstrings( 267 []string{"FAILED"}, 268 []string{"my-organization", "not found"}, 269 )) 270 271 expectOrgToBeCleared() 272 expectSpaceToBeCleared() 273 }) 274 275 It("fails to target a space if no organization is targeted", func() { 276 config.SetOrganizationFields(models.OrganizationFields{}) 277 278 callTarget([]string{"-s", "my-space"}) 279 280 Expect(ui.Outputs()).To(ContainSubstrings( 281 []string{"FAILED"}, 282 []string{"An org must be targeted before targeting a space"}, 283 )) 284 285 expectSpaceToBeCleared() 286 }) 287 288 Context("when an org is targetted", func() { 289 var org models.Organization 290 BeforeEach(func() { 291 org = models.Organization{} 292 org.Name = "my-organization" 293 org.GUID = "my-organization-guid" 294 295 config.SetOrganizationFields(models.OrganizationFields{Name: org.Name, GUID: org.GUID}) 296 }) 297 298 It("fails when the user doesn't have access to the space", func() { 299 spaceRepo.FindByNameReturns(models.Space{}, errors.New("Error finding space by name.")) 300 301 callTarget([]string{"-s", "my-space"}) 302 303 Expect(ui.Outputs()).To(ContainSubstrings( 304 []string{"FAILED"}, 305 []string{"Unable to access space", "my-space"}, 306 )) 307 308 Expect(config.SpaceFields().GUID).To(Equal("")) 309 Expect(ui.ShowConfigurationCalled).To(BeFalse()) 310 311 Expect(config.OrganizationFields().GUID).NotTo(BeEmpty()) 312 expectSpaceToBeCleared() 313 }) 314 315 It("fails when the space is not found", func() { 316 spaceRepo.FindByNameReturns(models.Space{}, errors.NewModelNotFoundError("Space", "my-space")) 317 318 callTarget([]string{"-s", "my-space"}) 319 320 expectSpaceToBeCleared() 321 Expect(ui.Outputs()).To(ContainSubstrings( 322 []string{"FAILED"}, 323 []string{"my-space", "not found"}, 324 )) 325 }) 326 327 It("fails to target the space automatically if is not found", func() { 328 orgRepo.ListOrgsReturns([]models.Organization{org}, nil) 329 orgRepo.FindByNameReturns(org, nil) 330 331 spaceRepo.FindByNameReturns(models.Space{}, errors.NewModelNotFoundError("Space", "my-space")) 332 333 callTarget([]string{"-o", "my-organization"}) 334 335 Expect(config.OrganizationFields().GUID).To(Equal("my-organization-guid")) 336 Expect(config.SpaceFields().GUID).To(Equal("")) 337 338 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 339 }) 340 }) 341 }) 342 }) 343 })