github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/target_test.go (about) 1 package commands_test 2 3 import ( 4 "errors" 5 6 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 7 fake_org "github.com/cloudfoundry/cli/cf/api/organizations/fakes" 8 . "github.com/cloudfoundry/cli/cf/commands" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/models" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 14 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 15 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 16 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 17 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 18 19 . "github.com/cloudfoundry/cli/testhelpers/matchers" 20 ) 21 22 var _ = Describe("target command", func() { 23 var ( 24 orgRepo *fake_org.FakeOrganizationRepository 25 spaceRepo *testapi.FakeSpaceRepository 26 requirementsFactory *testreq.FakeReqFactory 27 config core_config.ReadWriter 28 ui *testterm.FakeUI 29 ) 30 31 BeforeEach(func() { 32 ui = new(testterm.FakeUI) 33 orgRepo = new(fake_org.FakeOrganizationRepository) 34 spaceRepo = new(testapi.FakeSpaceRepository) 35 requirementsFactory = new(testreq.FakeReqFactory) 36 config = testconfig.NewRepositoryWithDefaults() 37 requirementsFactory.ApiEndpointSuccess = true 38 }) 39 40 var callTarget = func(args []string) bool { 41 cmd := NewTarget(ui, config, orgRepo, spaceRepo) 42 return testcmd.RunCommand(cmd, args, requirementsFactory) 43 } 44 45 It("fails with usage when called with an argument but no flags", func() { 46 callTarget([]string{"some-argument"}) 47 Expect(ui.FailedWithUsage).To(BeTrue()) 48 }) 49 50 Describe("when there is no api endpoint set", func() { 51 BeforeEach(func() { 52 requirementsFactory.ApiEndpointSuccess = false 53 }) 54 55 It("fails requirements", func() { 56 Expect(callTarget([]string{})).To(BeFalse()) 57 }) 58 }) 59 60 Describe("when the user is not logged in", func() { 61 BeforeEach(func() { 62 config.SetAccessToken("") 63 }) 64 65 It("prints the target info when no org or space is specified", func() { 66 Expect(callTarget([]string{})).To(BeTrue()) 67 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 68 }) 69 70 It("panics silently so that it returns an exit code of 1", func() { 71 callTarget([]string{}) 72 Expect(ui.PanickedQuietly).To(BeTrue()) 73 }) 74 75 It("fails requirements when targeting a space or org", func() { 76 Expect(callTarget([]string{"-o", "some-crazy-org-im-not-in"})).To(BeFalse()) 77 78 Expect(callTarget([]string{"-s", "i-love-space"})).To(BeFalse()) 79 }) 80 }) 81 82 Context("when the user is logged in", func() { 83 BeforeEach(func() { 84 requirementsFactory.LoginSuccess = true 85 }) 86 87 var expectOrgToBeCleared = func() { 88 Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{})) 89 } 90 91 var expectSpaceToBeCleared = func() { 92 Expect(config.SpaceFields()).To(Equal(models.SpaceFields{})) 93 } 94 95 Context("there are no errors", func() { 96 BeforeEach(func() { 97 org := models.Organization{} 98 org.Name = "my-organization" 99 org.Guid = "my-organization-guid" 100 101 orgRepo.ListOrgsReturns([]models.Organization{org}, nil) 102 orgRepo.FindByNameReturns(org, nil) 103 }) 104 105 It("it updates the organization in the config", func() { 106 callTarget([]string{"-o", "my-organization"}) 107 108 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 109 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 110 111 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 112 }) 113 114 It("updates the space in the config", func() { 115 space := models.Space{} 116 space.Name = "my-space" 117 space.Guid = "my-space-guid" 118 119 spaceRepo.Spaces = []models.Space{space} 120 spaceRepo.FindByNameSpace = space 121 122 callTarget([]string{"-s", "my-space"}) 123 124 Expect(spaceRepo.FindByNameName).To(Equal("my-space")) 125 Expect(config.SpaceFields().Guid).To(Equal("my-space-guid")) 126 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 127 }) 128 129 It("updates both the organization and the space in the config", func() { 130 space := models.Space{} 131 space.Name = "my-space" 132 space.Guid = "my-space-guid" 133 spaceRepo.Spaces = []models.Space{space} 134 135 callTarget([]string{"-o", "my-organization", "-s", "my-space"}) 136 137 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 138 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 139 140 Expect(spaceRepo.FindByNameName).To(Equal("my-space")) 141 Expect(config.SpaceFields().Guid).To(Equal("my-space-guid")) 142 143 Expect(ui.ShowConfigurationCalled).To(BeTrue()) 144 }) 145 146 It("only updates the organization in the config when the space can't be found", func() { 147 config.SetSpaceFields(models.SpaceFields{}) 148 149 spaceRepo.FindByNameErr = true 150 151 callTarget([]string{"-o", "my-organization", "-s", "my-space"}) 152 153 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) 154 Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) 155 156 Expect(spaceRepo.FindByNameName).To(Equal("my-space")) 157 Expect(config.SpaceFields().Guid).To(Equal("")) 158 159 Expect(ui.ShowConfigurationCalled).To(BeFalse()) 160 Expect(ui.Outputs).To(ContainSubstrings( 161 []string{"FAILED"}, 162 []string{"Unable to access space", "my-space"}, 163 )) 164 }) 165 }) 166 167 Context("there are errors", func() { 168 It("fails when the user does not have access to the specified organization", func() { 169 orgRepo.FindByNameReturns(models.Organization{}, errors.New("Invalid access")) 170 171 callTarget([]string{"-o", "my-organization"}) 172 Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"})) 173 expectOrgToBeCleared() 174 expectSpaceToBeCleared() 175 }) 176 177 It("fails when the organization is not found", func() { 178 orgRepo.FindByNameReturns(models.Organization{}, errors.New("my-organization not found")) 179 180 callTarget([]string{"-o", "my-organization"}) 181 182 Expect(ui.Outputs).To(ContainSubstrings( 183 []string{"FAILED"}, 184 []string{"my-organization", "not found"}, 185 )) 186 187 expectOrgToBeCleared() 188 expectSpaceToBeCleared() 189 }) 190 191 It("fails to target a space if no organization is targeted", func() { 192 config.SetOrganizationFields(models.OrganizationFields{}) 193 194 callTarget([]string{"-s", "my-space"}) 195 196 Expect(ui.Outputs).To(ContainSubstrings( 197 []string{"FAILED"}, 198 []string{"An org must be targeted before targeting a space"}, 199 )) 200 201 expectSpaceToBeCleared() 202 }) 203 204 It("fails when the user doesn't have access to the space", func() { 205 spaceRepo.FindByNameErr = true 206 207 callTarget([]string{"-s", "my-space"}) 208 209 Expect(ui.Outputs).To(ContainSubstrings( 210 []string{"FAILED"}, 211 []string{"Unable to access space", "my-space"}, 212 )) 213 214 Expect(config.SpaceFields().Guid).To(Equal("")) 215 Expect(ui.ShowConfigurationCalled).To(BeFalse()) 216 217 Expect(config.OrganizationFields().Guid).NotTo(BeEmpty()) 218 expectSpaceToBeCleared() 219 }) 220 221 It("fails when the space is not found", func() { 222 spaceRepo.FindByNameNotFound = true 223 224 callTarget([]string{"-s", "my-space"}) 225 226 expectSpaceToBeCleared() 227 Expect(ui.Outputs).To(ContainSubstrings( 228 []string{"FAILED"}, 229 []string{"my-space", "not found"}, 230 )) 231 }) 232 }) 233 }) 234 })