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