github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/user/org_users_test.go (about) 1 package user_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/models" 8 "code.cloudfoundry.org/cli/cf/requirements" 9 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 10 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 11 "code.cloudfoundry.org/cli/plugin/models" 12 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 18 "os" 19 20 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 21 ) 22 23 var _ = Describe("org-users command", func() { 24 var ( 25 ui *testterm.FakeUI 26 requirementsFactory *requirementsfakes.FakeFactory 27 configRepo coreconfig.Repository 28 userRepo *apifakes.FakeUserRepository 29 deps commandregistry.Dependency 30 ) 31 32 updateCommandDependency := func(pluginCall bool) { 33 deps.UI = ui 34 deps.Config = configRepo 35 deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo) 36 37 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("org-users").SetDependency(deps, pluginCall)) 38 } 39 40 BeforeEach(func() { 41 ui = &testterm.FakeUI{} 42 userRepo = new(apifakes.FakeUserRepository) 43 configRepo = testconfig.NewRepositoryWithDefaults() 44 requirementsFactory = new(requirementsfakes.FakeFactory) 45 deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "") 46 }) 47 48 runCommand := func(args ...string) bool { 49 return testcmd.RunCLICommand("org-users", args, requirementsFactory, updateCommandDependency, false, ui) 50 } 51 52 Describe("requirements", func() { 53 It("fails with usage when invoked without an org name", func() { 54 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 55 runCommand() 56 Expect(ui.Outputs()).To(ContainSubstrings( 57 []string{"Incorrect Usage", "Requires an argument"}, 58 )) 59 }) 60 61 It("fails when not logged in", func() { 62 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 63 Expect(runCommand("say-hello-to-my-little-org")).To(BeFalse()) 64 }) 65 }) 66 67 Context("when logged in and given an org with no users in a particular role", func() { 68 var ( 69 user1, user2 models.UserFields 70 ) 71 72 BeforeEach(func() { 73 org := models.Organization{} 74 org.Name = "the-org" 75 org.GUID = "the-org-guid" 76 77 user1 = models.UserFields{} 78 user1.Username = "user1" 79 user2 = models.UserFields{} 80 user2.Username = "user2" 81 82 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 83 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 84 organizationReq.GetOrganizationReturns(org) 85 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 86 }) 87 88 Context("shows friendly messaage when no users in ORG_MANAGER role", func() { 89 It("shows the special users in the given org", func() { 90 userRepo.ListUsersInOrgForRoleStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 91 userFields := map[models.Role][]models.UserFields{ 92 models.RoleOrgManager: {}, 93 models.RoleBillingManager: {user1}, 94 models.RoleOrgAuditor: {user2}, 95 }[roleName] 96 return userFields, nil 97 } 98 99 runCommand("the-org") 100 101 Expect(userRepo.ListUsersInOrgForRoleCallCount()).To(Equal(3)) 102 for i, expectedRole := range []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor} { 103 orgGUID, actualRole := userRepo.ListUsersInOrgForRoleArgsForCall(i) 104 Expect(orgGUID).To(Equal("the-org-guid")) 105 Expect(actualRole).To(Equal(expectedRole)) 106 } 107 108 Expect(ui.Outputs()).To(BeInDisplayOrder( 109 []string{"Getting users in org", "the-org", "my-user"}, 110 []string{"ORG MANAGER"}, 111 []string{" No ORG MANAGER found"}, 112 []string{"BILLING MANAGER"}, 113 []string{" user1"}, 114 []string{"ORG AUDITOR"}, 115 []string{" user2"}, 116 )) 117 }) 118 }) 119 120 Context("shows friendly messaage when no users in BILLING_MANAGER role", func() { 121 It("shows the special users in the given org", func() { 122 userRepo.ListUsersInOrgForRoleStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 123 userFields := map[models.Role][]models.UserFields{ 124 models.RoleOrgManager: {user1}, 125 models.RoleBillingManager: {}, 126 models.RoleOrgAuditor: {user2}, 127 }[roleName] 128 return userFields, nil 129 } 130 131 runCommand("the-org") 132 133 Expect(userRepo.ListUsersInOrgForRoleCallCount()).To(Equal(3)) 134 for i, expectedRole := range []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor} { 135 orgGUID, actualRole := userRepo.ListUsersInOrgForRoleArgsForCall(i) 136 Expect(orgGUID).To(Equal("the-org-guid")) 137 Expect(actualRole).To(Equal(expectedRole)) 138 } 139 140 Expect(ui.Outputs()).To(BeInDisplayOrder( 141 []string{"Getting users in org", "the-org", "my-user"}, 142 []string{"ORG MANAGER"}, 143 []string{" user1"}, 144 []string{"BILLING MANAGER"}, 145 []string{" No BILLING MANAGER found"}, 146 []string{"ORG AUDITOR"}, 147 []string{" user2"}, 148 )) 149 }) 150 }) 151 152 Context("shows friendly messaage when no users in ORG_AUDITOR role", func() { 153 It("shows the special users in the given org", func() { 154 userRepo.ListUsersInOrgForRoleStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 155 userFields := map[models.Role][]models.UserFields{ 156 models.RoleOrgManager: {user1}, 157 models.RoleBillingManager: {user2}, 158 models.RoleOrgAuditor: {}, 159 }[roleName] 160 return userFields, nil 161 } 162 163 runCommand("the-org") 164 165 Expect(userRepo.ListUsersInOrgForRoleCallCount()).To(Equal(3)) 166 for i, expectedRole := range []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor} { 167 orgGUID, actualRole := userRepo.ListUsersInOrgForRoleArgsForCall(i) 168 Expect(orgGUID).To(Equal("the-org-guid")) 169 Expect(actualRole).To(Equal(expectedRole)) 170 } 171 Expect(ui.Outputs()).To(BeInDisplayOrder( 172 []string{"Getting users in org", "the-org", "my-user"}, 173 []string{"ORG MANAGER"}, 174 []string{" user1"}, 175 []string{"BILLING MANAGER"}, 176 []string{" user2"}, 177 []string{"ORG AUDITOR"}, 178 []string{" No ORG AUDITOR found"}, 179 )) 180 }) 181 }) 182 183 }) 184 185 Context("when logged in and given an org with users", func() { 186 BeforeEach(func() { 187 org := models.Organization{} 188 org.Name = "the-org" 189 org.GUID = "the-org-guid" 190 191 user := models.UserFields{Username: "user1"} 192 user2 := models.UserFields{Username: "user2"} 193 user3 := models.UserFields{Username: "user3"} 194 user4 := models.UserFields{Username: "user4"} 195 userRepo.ListUsersInOrgForRoleStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 196 userFields := map[models.Role][]models.UserFields{ 197 models.RoleOrgManager: {user, user2}, 198 models.RoleBillingManager: {user4}, 199 models.RoleOrgAuditor: {user3}, 200 }[roleName] 201 return userFields, nil 202 } 203 204 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 205 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 206 organizationReq.GetOrganizationReturns(org) 207 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 208 }) 209 210 It("shows the special users in the given org", func() { 211 runCommand("the-org") 212 213 orgGUID, _ := userRepo.ListUsersInOrgForRoleArgsForCall(0) 214 Expect(orgGUID).To(Equal("the-org-guid")) 215 Expect(ui.Outputs()).To(ContainSubstrings( 216 []string{"Getting users in org", "the-org", "my-user"}, 217 []string{"ORG MANAGER"}, 218 []string{"user1"}, 219 []string{"user2"}, 220 []string{"BILLING MANAGER"}, 221 []string{"user4"}, 222 []string{"ORG AUDITOR"}, 223 []string{"user3"}, 224 )) 225 }) 226 227 Context("when the -a flag is provided", func() { 228 BeforeEach(func() { 229 user := models.UserFields{Username: "user1"} 230 user2 := models.UserFields{Username: "user2"} 231 userRepo.ListUsersInOrgForRoleStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 232 userFields := map[models.Role][]models.UserFields{ 233 models.RoleOrgUser: {user, user2}, 234 }[roleName] 235 return userFields, nil 236 } 237 }) 238 239 It("lists all org users, regardless of role", func() { 240 runCommand("-a", "the-org") 241 242 orgGUID, _ := userRepo.ListUsersInOrgForRoleArgsForCall(0) 243 Expect(orgGUID).To(Equal("the-org-guid")) 244 Expect(ui.Outputs()).To(ContainSubstrings( 245 []string{"Getting users in org", "the-org", "my-user"}, 246 []string{"USERS"}, 247 []string{"user1"}, 248 []string{"user2"}, 249 )) 250 }) 251 }) 252 253 Context("when cc api verson is >= 2.21.0", func() { 254 It("calls ListUsersInOrgForRoleWithNoUAA()", func() { 255 configRepo.SetAPIVersion("2.22.0") 256 runCommand("the-org") 257 258 Expect(userRepo.ListUsersInOrgForRoleWithNoUAACallCount()).To(BeNumerically(">=", 1)) 259 Expect(userRepo.ListUsersInOrgForRoleCallCount()).To(Equal(0)) 260 }) 261 }) 262 263 Context("when cc api verson is < 2.21.0", func() { 264 It("calls ListUsersInOrgForRole()", func() { 265 configRepo.SetAPIVersion("2.20.0") 266 runCommand("the-org") 267 268 Expect(userRepo.ListUsersInOrgForRoleWithNoUAACallCount()).To(Equal(0)) 269 Expect(userRepo.ListUsersInOrgForRoleCallCount()).To(BeNumerically(">=", 1)) 270 }) 271 }) 272 }) 273 274 Describe("when invoked by a plugin", func() { 275 var ( 276 pluginUserModel []plugin_models.GetOrgUsers_Model 277 ) 278 279 BeforeEach(func() { 280 configRepo.SetAPIVersion("2.22.0") 281 }) 282 283 Context("single roles", func() { 284 285 BeforeEach(func() { 286 org := models.Organization{} 287 org.Name = "the-org" 288 org.GUID = "the-org-guid" 289 290 // org managers 291 user := models.UserFields{} 292 user.Username = "user1" 293 user.GUID = "1111" 294 295 user2 := models.UserFields{} 296 user2.Username = "user2" 297 user2.GUID = "2222" 298 299 // billing manager 300 user3 := models.UserFields{} 301 user3.Username = "user3" 302 user3.GUID = "3333" 303 304 // auditors 305 user4 := models.UserFields{} 306 user4.Username = "user4" 307 user4.GUID = "4444" 308 309 userRepo.ListUsersInOrgForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 310 userFields := map[models.Role][]models.UserFields{ 311 models.RoleOrgManager: {user, user2}, 312 models.RoleBillingManager: {user4}, 313 models.RoleOrgAuditor: {user3}, 314 models.RoleOrgUser: {user3}, 315 }[roleName] 316 return userFields, nil 317 } 318 319 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 320 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 321 organizationReq.GetOrganizationReturns(org) 322 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 323 pluginUserModel = []plugin_models.GetOrgUsers_Model{} 324 deps.PluginModels.OrgUsers = &pluginUserModel 325 }) 326 327 It("populates the plugin model with users with single roles", func() { 328 testcmd.RunCLICommand("org-users", []string{"the-org"}, requirementsFactory, updateCommandDependency, true, ui) 329 Expect(pluginUserModel).To(HaveLen(4)) 330 331 for _, u := range pluginUserModel { 332 switch u.Username { 333 case "user1": 334 Expect(u.Guid).To(Equal("1111")) 335 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager"})) 336 case "user2": 337 Expect(u.Guid).To(Equal("2222")) 338 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager"})) 339 case "user3": 340 Expect(u.Guid).To(Equal("3333")) 341 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgAuditor"})) 342 case "user4": 343 Expect(u.Guid).To(Equal("4444")) 344 Expect(u.Roles).To(ConsistOf([]string{"RoleBillingManager"})) 345 default: 346 Fail("unexpected user: " + u.Username) 347 } 348 } 349 350 }) 351 352 It("populates the plugin model with users with single roles -a flag", func() { 353 testcmd.RunCLICommand("org-users", []string{"-a", "the-org"}, requirementsFactory, updateCommandDependency, true, ui) 354 Expect(pluginUserModel).To(HaveLen(1)) 355 Expect(pluginUserModel[0].Username).To(Equal("user3")) 356 Expect(pluginUserModel[0].Guid).To(Equal("3333")) 357 Expect(pluginUserModel[0].Roles[0]).To(Equal("RoleOrgUser")) 358 }) 359 360 }) 361 362 Context("multiple roles", func() { 363 364 BeforeEach(func() { 365 org := models.Organization{} 366 org.Name = "the-org" 367 org.GUID = "the-org-guid" 368 369 // org managers 370 user := models.UserFields{} 371 user.Username = "user1" 372 user.GUID = "1111" 373 user.IsAdmin = true 374 375 user2 := models.UserFields{} 376 user2.Username = "user2" 377 user2.GUID = "2222" 378 379 // billing manager 380 user3 := models.UserFields{} 381 user3.Username = "user3" 382 user3.GUID = "3333" 383 384 // auditors 385 user4 := models.UserFields{} 386 user4.Username = "user4" 387 user4.GUID = "4444" 388 389 userRepo.ListUsersInOrgForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 390 userFields := map[models.Role][]models.UserFields{ 391 models.RoleOrgManager: {user, user2, user3, user4}, 392 models.RoleBillingManager: {user2, user4}, 393 models.RoleOrgAuditor: {user, user3}, 394 models.RoleOrgUser: {user, user2, user3, user4}, 395 }[roleName] 396 return userFields, nil 397 } 398 399 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 400 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 401 organizationReq.GetOrganizationReturns(org) 402 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 403 pluginUserModel = []plugin_models.GetOrgUsers_Model{} 404 deps.PluginModels.OrgUsers = &pluginUserModel 405 }) 406 407 It("populates the plugin model with users with multiple roles", func() { 408 testcmd.RunCLICommand("org-users", []string{"the-org"}, requirementsFactory, updateCommandDependency, true, ui) 409 410 Expect(pluginUserModel).To(HaveLen(4)) 411 for _, u := range pluginUserModel { 412 switch u.Username { 413 case "user1": 414 Expect(u.Guid).To(Equal("1111")) 415 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager", "RoleOrgAuditor"})) 416 Expect(u.IsAdmin).To(BeTrue()) 417 case "user2": 418 Expect(u.Guid).To(Equal("2222")) 419 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager", "RoleBillingManager"})) 420 case "user3": 421 Expect(u.Guid).To(Equal("3333")) 422 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgAuditor", "RoleOrgManager"})) 423 case "user4": 424 Expect(u.Guid).To(Equal("4444")) 425 Expect(u.Roles).To(ConsistOf([]string{"RoleBillingManager", "RoleOrgManager"})) 426 default: 427 Fail("unexpected user: " + u.Username) 428 } 429 } 430 431 }) 432 433 It("populates the plugin model with users with multiple roles -a flag", func() { 434 testcmd.RunCLICommand("org-users", []string{"-a", "the-org"}, requirementsFactory, updateCommandDependency, true, ui) 435 436 Expect(pluginUserModel).To(HaveLen(4)) 437 for _, u := range pluginUserModel { 438 switch u.Username { 439 case "user1": 440 Expect(u.Guid).To(Equal("1111")) 441 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"})) 442 case "user2": 443 Expect(u.Guid).To(Equal("2222")) 444 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"})) 445 case "user3": 446 Expect(u.Guid).To(Equal("3333")) 447 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"})) 448 case "user4": 449 Expect(u.Guid).To(Equal("4444")) 450 Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"})) 451 default: 452 Fail("unexpected user: " + u.Username) 453 } 454 } 455 456 }) 457 458 }) 459 460 }) 461 })