github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/user/unset_space_role_test.go (about) 1 package user_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/commands/user" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 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 14 "code.cloudfoundry.org/cli/cf/api/apifakes" 15 "code.cloudfoundry.org/cli/cf/api/featureflags/featureflagsfakes" 16 "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 17 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 18 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 19 20 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("UnsetSpaceRole", func() { 26 var ( 27 ui *testterm.FakeUI 28 configRepo coreconfig.Repository 29 userRepo *apifakes.FakeUserRepository 30 spaceRepo *spacesfakes.FakeSpaceRepository 31 flagRepo *featureflagsfakes.FakeFeatureFlagRepository 32 33 cmd commandregistry.Command 34 deps commandregistry.Dependency 35 factory *requirementsfakes.FakeFactory 36 flagContext flags.FlagContext 37 38 loginRequirement requirements.Requirement 39 userRequirement *requirementsfakes.FakeUserRequirement 40 organizationRequirement *requirementsfakes.FakeOrganizationRequirement 41 ) 42 43 BeforeEach(func() { 44 ui = &testterm.FakeUI{} 45 configRepo = testconfig.NewRepositoryWithDefaults() 46 userRepo = new(apifakes.FakeUserRepository) 47 repoLocator := deps.RepoLocator.SetUserRepository(userRepo) 48 spaceRepo = new(spacesfakes.FakeSpaceRepository) 49 repoLocator = repoLocator.SetSpaceRepository(spaceRepo) 50 flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository) 51 repoLocator = repoLocator.SetFeatureFlagRepository(flagRepo) 52 53 deps = commandregistry.Dependency{ 54 UI: ui, 55 Config: configRepo, 56 RepoLocator: repoLocator, 57 } 58 59 cmd = &user.UnsetSpaceRole{} 60 cmd.SetDependency(deps, false) 61 62 flagContext = flags.NewFlagContext(map[string]flags.FlagSet{}) 63 64 factory = new(requirementsfakes.FakeFactory) 65 66 loginRequirement = &passingRequirement{} 67 factory.NewLoginRequirementReturns(loginRequirement) 68 69 userRequirement = new(requirementsfakes.FakeUserRequirement) 70 userRequirement.ExecuteReturns(nil) 71 factory.NewUserRequirementReturns(userRequirement) 72 73 organizationRequirement = new(requirementsfakes.FakeOrganizationRequirement) 74 organizationRequirement.ExecuteReturns(nil) 75 factory.NewOrganizationRequirementReturns(organizationRequirement) 76 }) 77 78 Describe("Requirements", func() { 79 Context("when not provided exactly four args", func() { 80 BeforeEach(func() { 81 flagContext.Parse("the-user-name", "the-org-name", "the-space-name") 82 }) 83 84 It("fails with usage", func() { 85 _, err := cmd.Requirements(factory, flagContext) 86 Expect(err).To(HaveOccurred()) 87 Expect(ui.Outputs()).To(ContainSubstrings( 88 []string{"Incorrect Usage. Requires USERNAME, ORG, SPACE, ROLE as arguments"}, 89 []string{"NAME"}, 90 []string{"USAGE"}, 91 )) 92 }) 93 }) 94 95 Context("when provided four args", func() { 96 BeforeEach(func() { 97 flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager") 98 }) 99 100 It("returns a LoginRequirement", func() { 101 actualRequirements, err := cmd.Requirements(factory, flagContext) 102 Expect(err).NotTo(HaveOccurred()) 103 Expect(factory.NewLoginRequirementCallCount()).To(Equal(1)) 104 105 Expect(actualRequirements).To(ContainElement(loginRequirement)) 106 }) 107 108 It("returns an OrgRequirement", func() { 109 actualRequirements, err := cmd.Requirements(factory, flagContext) 110 Expect(err).NotTo(HaveOccurred()) 111 Expect(factory.NewOrganizationRequirementCallCount()).To(Equal(1)) 112 Expect(factory.NewOrganizationRequirementArgsForCall(0)).To(Equal("the-org-name")) 113 114 Expect(actualRequirements).To(ContainElement(organizationRequirement)) 115 }) 116 117 It("requests the unset_roles_by_username flag", func() { 118 _, err := cmd.Requirements(factory, flagContext) 119 Expect(err).NotTo(HaveOccurred()) 120 Expect(flagRepo.FindByNameCallCount()).To(Equal(1)) 121 Expect(flagRepo.FindByNameArgsForCall(0)).To(Equal("unset_roles_by_username")) 122 }) 123 124 Context("when the unset_roles_by_username flag exists and is enabled", func() { 125 BeforeEach(func() { 126 flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: true}, nil) 127 }) 128 129 It("returns a UserRequirement", func() { 130 actualRequirements, err := cmd.Requirements(factory, flagContext) 131 Expect(err).NotTo(HaveOccurred()) 132 Expect(factory.NewUserRequirementCallCount()).To(Equal(1)) 133 actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0) 134 Expect(actualUsername).To(Equal("the-user-name")) 135 Expect(actualWantGUID).To(BeFalse()) 136 137 Expect(actualRequirements).To(ContainElement(userRequirement)) 138 }) 139 }) 140 141 Context("when the unset_roles_by_username flag exists and is disabled", func() { 142 BeforeEach(func() { 143 flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: false}, nil) 144 }) 145 146 It("returns a UserRequirement", func() { 147 actualRequirements, err := cmd.Requirements(factory, flagContext) 148 Expect(err).NotTo(HaveOccurred()) 149 Expect(factory.NewUserRequirementCallCount()).To(Equal(1)) 150 actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0) 151 Expect(actualUsername).To(Equal("the-user-name")) 152 Expect(actualWantGUID).To(BeTrue()) 153 154 Expect(actualRequirements).To(ContainElement(userRequirement)) 155 }) 156 }) 157 158 Context("when the unset_roles_by_username flag cannot be retrieved", func() { 159 BeforeEach(func() { 160 flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("some error")) 161 }) 162 163 It("returns a UserRequirement", func() { 164 actualRequirements, err := cmd.Requirements(factory, flagContext) 165 Expect(err).NotTo(HaveOccurred()) 166 Expect(factory.NewUserRequirementCallCount()).To(Equal(1)) 167 actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0) 168 Expect(actualUsername).To(Equal("the-user-name")) 169 Expect(actualWantGUID).To(BeTrue()) 170 171 Expect(actualRequirements).To(ContainElement(userRequirement)) 172 }) 173 }) 174 }) 175 }) 176 177 Describe("Execute", func() { 178 var ( 179 org models.Organization 180 err error 181 ) 182 183 BeforeEach(func() { 184 flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager") 185 cmd.Requirements(factory, flagContext) 186 187 org = models.Organization{} 188 org.GUID = "the-org-guid" 189 org.Name = "the-org-name" 190 organizationRequirement.GetOrganizationReturns(org) 191 }) 192 193 JustBeforeEach(func() { 194 err = cmd.Execute(flagContext) 195 }) 196 197 Context("when the space is not found", func() { 198 BeforeEach(func() { 199 spaceRepo.FindByNameInOrgReturns(models.Space{}, errors.New("space-repo-error")) 200 }) 201 202 It("doesn't call CC", func() { 203 Expect(userRepo.UnsetSpaceRoleByGUIDCallCount()).To(BeZero()) 204 Expect(userRepo.UnsetSpaceRoleByUsernameCallCount()).To(BeZero()) 205 }) 206 207 It("return an error", func() { 208 Expect(err).To(HaveOccurred()) 209 Expect(err.Error()).To(Equal("space-repo-error")) 210 }) 211 }) 212 213 Context("when the space is found", func() { 214 BeforeEach(func() { 215 space := models.Space{} 216 space.GUID = "the-space-guid" 217 space.Name = "the-space-name" 218 space.Organization = org.OrganizationFields 219 spaceRepo.FindByNameInOrgReturns(space, nil) 220 }) 221 222 Context("when the UserRequirement returns a user with a GUID", func() { 223 BeforeEach(func() { 224 userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"} 225 userRequirement.GetUserReturns(userFields) 226 }) 227 228 It("tells the user it is removing the role", func() { 229 Expect(err).NotTo(HaveOccurred()) 230 Expect(ui.Outputs()).To(ContainSubstrings( 231 []string{"Removing role", "SpaceManager", "the-user-name", "the-org", "the-user-name"}, 232 []string{"OK"}, 233 )) 234 }) 235 236 It("removes the role using the GUID", func() { 237 Expect(err).NotTo(HaveOccurred()) 238 Expect(userRepo.UnsetSpaceRoleByGUIDCallCount()).To(Equal(1)) 239 actualUserGUID, actualSpaceGUID, actualRole := userRepo.UnsetSpaceRoleByGUIDArgsForCall(0) 240 Expect(actualUserGUID).To(Equal("the-user-guid")) 241 Expect(actualSpaceGUID).To(Equal("the-space-guid")) 242 Expect(actualRole).To(Equal(models.RoleSpaceManager)) 243 }) 244 245 Context("when the call to CC fails", func() { 246 BeforeEach(func() { 247 userRepo.UnsetSpaceRoleByGUIDReturns(errors.New("user-repo-error")) 248 }) 249 250 It("return an error", func() { 251 Expect(err).To(HaveOccurred()) 252 Expect(err.Error()).To(Equal("user-repo-error")) 253 }) 254 }) 255 }) 256 257 Context("when the UserRequirement returns a user without a GUID", func() { 258 BeforeEach(func() { 259 userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"}) 260 }) 261 262 It("removes the role using the given username", func() { 263 Expect(err).NotTo(HaveOccurred()) 264 Expect(userRepo.UnsetSpaceRoleByUsernameCallCount()).To(Equal(1)) 265 actualUsername, actualSpaceGUID, actualRole := userRepo.UnsetSpaceRoleByUsernameArgsForCall(0) 266 Expect(actualUsername).To(Equal("the-user-name")) 267 Expect(actualSpaceGUID).To(Equal("the-space-guid")) 268 Expect(actualRole).To(Equal(models.RoleSpaceManager)) 269 }) 270 271 It("tells the user it is removing the role", func() { 272 Expect(err).NotTo(HaveOccurred()) 273 Expect(ui.Outputs()).To(ContainSubstrings( 274 []string{"Removing role", "SpaceManager", "the-user-name", "the-org", "the-user-name"}, 275 []string{"OK"}, 276 )) 277 }) 278 279 Context("when the call to CC fails", func() { 280 BeforeEach(func() { 281 userRepo.UnsetSpaceRoleByUsernameReturns(errors.New("user-repo-error")) 282 }) 283 284 It("return an error", func() { 285 Expect(err).To(HaveOccurred()) 286 Expect(err.Error()).To(Equal("user-repo-error")) 287 }) 288 }) 289 }) 290 }) 291 }) 292 })