github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/target_test.go (about) 1 package commands_test 2 3 import ( 4 "errors" 5 6 "github.com/cloudfoundry/cli/cf" 7 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 8 fake_org "github.com/cloudfoundry/cli/cf/api/organizations/fakes" 9 "github.com/cloudfoundry/cli/cf/command_registry" 10 "github.com/cloudfoundry/cli/cf/configuration/core_config" 11 "github.com/cloudfoundry/cli/cf/models" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 16 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 17 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 18 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 19 20 . "github.com/cloudfoundry/cli/testhelpers/matchers" 21 ) 22 23 var _ = Describe("target command", func() { 24 var ( 25 orgRepo *fake_org.FakeOrganizationRepository 26 spaceRepo *testapi.FakeSpaceRepository 27 requirementsFactory *testreq.FakeReqFactory 28 config core_config.Repository 29 ui *testterm.FakeUI 30 deps command_registry.Dependency 31 ) 32 33 updateCommandDependency := func(pluginCall bool) { 34 deps.Ui = ui 35 deps.Config = config 36 deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo) 37 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 38 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("target").SetDependency(deps, pluginCall)) 39 } 40 41 BeforeEach(func() { 42 ui = &testterm.FakeUI{} 43 orgRepo = &fake_org.FakeOrganizationRepository{} 44 spaceRepo = new(testapi.FakeSpaceRepository) 45 requirementsFactory = new(testreq.FakeReqFactory) 46 config = testconfig.NewRepositoryWithDefaults() 47 requirementsFactory.ApiEndpointSuccess = true 48 }) 49 50 var callTarget = func(args []string) bool { 51 return testcmd.RunCliCommand("target", args, requirementsFactory, updateCommandDependency, false) 52 } 53 54 It("fails with usage when called with an argument but no flags", func() { 55 callTarget([]string{"some-argument"}) 56 Expect(ui.Outputs).To(ContainSubstrings( 57 []string{"Incorrect Usage", "No argument required"}, 58 )) 59 }) 60 61 Describe("when there is no api endpoint set", func() { 62 BeforeEach(func() { 63 requirementsFactory.ApiEndpointSuccess = false 64 }) 65 66 It("fails requirements", func() { 67 Expect(callTarget([]string{})).To(BeFalse()) 68 }) 69 }) 70 71 Describe("when the user is not logged in", func() { 72 BeforeEach(func() { 73 config.SetAccessToken("") 74 }) 75 76 It("prints the target info when no org or space is specified", func() { 77 Expect(callTarget([]string{})).To(BeTrue()) 78 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 79 }) 80 81 It("panics silently so that it returns an exit code of 1", func() { 82 callTarget([]string{}) 83 Expect(ui.PanickedQuietly).To(BeTrue()) 84 }) 85 86 It("fails requirements when targeting a space or org", func() { 87 Expect(callTarget([]string{"-o", "some-crazy-org-im-not-in"})).To(BeFalse()) 88 89 Expect(callTarget([]string{"-s", "i-love-space"})).To(BeFalse()) 90 }) 91 }) 92 93 Context("when the user is logged in", func() { 94 BeforeEach(func() { 95 requirementsFactory.LoginSuccess = true 96 }) 97 98 var expectOrgToBeCleared = func() { 99 Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{})) 100 } 101 102 var expectSpaceToBeCleared = func() { 103 Expect(config.SpaceFields()).To(Equal(models.SpaceFields{})) 104 } 105 106 Context("there are no errors", func() { 107 BeforeEach(func() { 108 org := models.Organization{} 109 org.Name = "my-organization" 110 org.Guid = "my-organization-guid" 111 112 orgRepo.ListOrgsReturns([]models.Organization{org}, nil) 113 orgRepo.FindByNameReturns(org, nil) 114 }) 115 116 It("it updates the organization in the config", func() { 117 callTarget([]string{"-o", "my-organization"}) 118 119 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 120 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 121 122 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 123 }) 124 125 It("updates the space in the config", func() { 126 space := models.Space{} 127 space.Name = "my-space" 128 space.Guid = "my-space-guid" 129 130 spaceRepo.Spaces = []models.Space{space} 131 spaceRepo.FindByNameSpace = space 132 133 callTarget([]string{"-s", "my-space"}) 134 135 Expect(spaceRepo.FindByNameName).To(Equal("my-space")) 136 Expect(config.SpaceFields().Guid).To(Equal("my-space-guid")) 137 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 138 }) 139 140 It("updates both the organization and the space in the config", func() { 141 space := models.Space{} 142 space.Name = "my-space" 143 space.Guid = "my-space-guid" 144 spaceRepo.Spaces = []models.Space{space} 145 146 callTarget([]string{"-o", "my-organization", "-s", "my-space"}) 147 148 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 149 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 150 151 Expect(spaceRepo.FindByNameName).To(Equal("my-space")) 152 Expect(config.SpaceFields().Guid).To(Equal("my-space-guid")) 153 154 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 155 }) 156 157 It("only updates the organization in the config when the space can't be found", func() { 158 config.SetSpaceFields(models.SpaceFields{}) 159 160 spaceRepo.FindByNameErr = true 161 162 callTarget([]string{"-o", "my-organization", "-s", "my-space"}) 163 164 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 165 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 166 167 Expect(spaceRepo.FindByNameName).To(Equal("my-space")) 168 Expect(config.SpaceFields().Guid).To(Equal("")) 169 170 Expect(ui.ShowConfigurationCalled).To(BeFalse()) 171 Expect(ui.Outputs).To(ContainSubstrings( 172 []string{"FAILED"}, 173 []string{"Unable to access space", "my-space"}, 174 )) 175 }) 176 177 Describe("when there is only a single space", func() { 178 It("target space automatically ", func() { 179 space := models.Space{} 180 space.Name = "my-space" 181 space.Guid = "my-space-guid" 182 spaceRepo.Spaces = []models.Space{space} 183 184 callTarget([]string{"-o", "my-organization"}) 185 186 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 187 Expect(config.SpaceFields().Guid).To(Equal("my-space-guid")) 188 189 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 190 }) 191 192 }) 193 194 It("not target space automatically for orgs having multiple spaces", func() { 195 space1 := models.Space{} 196 space1.Name = "my-space" 197 space1.Guid = "my-space-guid" 198 space2 := models.Space{} 199 space2.Name = "my-space" 200 space2.Guid = "my-space-guid" 201 spaceRepo.Spaces = []models.Space{space1, space2} 202 203 callTarget([]string{"-o", "my-organization"}) 204 205 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 206 Expect(config.SpaceFields().Guid).To(Equal("")) 207 208 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 209 }) 210 211 It("prompts users to upgrade if CLI version < min cli version requirement", func() { 212 config.SetMinCliVersion("5.0.0") 213 config.SetMinRecommendedCliVersion("5.5.0") 214 cf.Version = "4.5.0" 215 216 callTarget([]string{"-o", "my-organization"}) 217 218 Expect(ui.Outputs).To(ContainSubstrings( 219 []string{"To upgrade your CLI"}, 220 []string{"5.0.0"}, 221 )) 222 }) 223 }) 224 225 Context("there are errors", func() { 226 It("fails when the user does not have access to the specified organization", func() { 227 orgRepo.FindByNameReturns(models.Organization{}, errors.New("Invalid access")) 228 229 callTarget([]string{"-o", "my-organization"}) 230 Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"})) 231 expectOrgToBeCleared() 232 expectSpaceToBeCleared() 233 }) 234 235 It("fails when the organization is not found", func() { 236 orgRepo.FindByNameReturns(models.Organization{}, errors.New("my-organization not found")) 237 238 callTarget([]string{"-o", "my-organization"}) 239 240 Expect(ui.Outputs).To(ContainSubstrings( 241 []string{"FAILED"}, 242 []string{"my-organization", "not found"}, 243 )) 244 245 expectOrgToBeCleared() 246 expectSpaceToBeCleared() 247 }) 248 249 It("fails to target a space if no organization is targeted", func() { 250 config.SetOrganizationFields(models.OrganizationFields{}) 251 252 callTarget([]string{"-s", "my-space"}) 253 254 Expect(ui.Outputs).To(ContainSubstrings( 255 []string{"FAILED"}, 256 []string{"An org must be targeted before targeting a space"}, 257 )) 258 259 expectSpaceToBeCleared() 260 }) 261 262 It("fails when the user doesn't have access to the space", func() { 263 spaceRepo.FindByNameErr = true 264 265 callTarget([]string{"-s", "my-space"}) 266 267 Expect(ui.Outputs).To(ContainSubstrings( 268 []string{"FAILED"}, 269 []string{"Unable to access space", "my-space"}, 270 )) 271 272 Expect(config.SpaceFields().Guid).To(Equal("")) 273 Expect(ui.ShowConfigurationCalled).To(BeFalse()) 274 275 Expect(config.OrganizationFields().Guid).NotTo(BeEmpty()) 276 expectSpaceToBeCleared() 277 }) 278 279 It("fails when the space is not found", func() { 280 spaceRepo.FindByNameNotFound = true 281 282 callTarget([]string{"-s", "my-space"}) 283 284 expectSpaceToBeCleared() 285 Expect(ui.Outputs).To(ContainSubstrings( 286 []string{"FAILED"}, 287 []string{"my-space", "not found"}, 288 )) 289 }) 290 It("fails to target the space automatically if is not found", func() { 291 org := models.Organization{} 292 org.Name = "my-organization" 293 org.Guid = "my-organization-guid" 294 295 orgRepo.ListOrgsReturns([]models.Organization{org}, nil) 296 orgRepo.FindByNameReturns(org, nil) 297 298 spaceRepo.FindByNameNotFound = true 299 300 callTarget([]string{"-o", "my-organization"}) 301 302 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 303 Expect(config.SpaceFields().Guid).To(Equal("")) 304 305 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 306 }) 307 }) 308 }) 309 })