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