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