github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/user/space_users_test.go (about) 1 package user_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/api/apifakes" 7 "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 13 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 14 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 15 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 16 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 17 plugin_models "code.cloudfoundry.org/cli/plugin/models" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 21 "os" 22 23 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 24 ) 25 26 var _ = Describe("space-users command", func() { 27 var ( 28 ui *testterm.FakeUI 29 requirementsFactory *requirementsfakes.FakeFactory 30 spaceRepo *spacesfakes.FakeSpaceRepository 31 userRepo *apifakes.FakeUserRepository 32 configRepo coreconfig.Repository 33 deps commandregistry.Dependency 34 ) 35 36 updateCommandDependency := func(pluginCall bool) { 37 deps.UI = ui 38 deps.Config = configRepo 39 deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo) 40 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 41 42 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("space-users").SetDependency(deps, pluginCall)) 43 } 44 45 BeforeEach(func() { 46 configRepo = testconfig.NewRepositoryWithDefaults() 47 ui = &testterm.FakeUI{} 48 requirementsFactory = new(requirementsfakes.FakeFactory) 49 spaceRepo = new(spacesfakes.FakeSpaceRepository) 50 userRepo = new(apifakes.FakeUserRepository) 51 deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "") 52 }) 53 54 runCommand := func(args ...string) bool { 55 return testcmd.RunCLICommand("space-users", args, requirementsFactory, updateCommandDependency, false, ui) 56 } 57 58 Describe("requirements", func() { 59 It("fails when not logged in", func() { 60 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 61 Expect(runCommand("my-org", "my-space")).To(BeFalse()) 62 }) 63 64 It("succeeds when logged in", func() { 65 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 66 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 67 organizationReq.GetOrganizationReturns( 68 models.Organization{ 69 OrganizationFields: models.OrganizationFields{ 70 Name: "some-org", 71 }, 72 }, 73 ) 74 spaceRepo.FindByNameInOrgReturns( 75 models.Space{ 76 SpaceFields: models.SpaceFields{ 77 Name: "whatever-space", 78 }, 79 }, nil) 80 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 81 passed := runCommand("some-org", "whatever-space") 82 83 Expect(passed).To(BeTrue()) 84 Expect(ui.Outputs()).To(ContainSubstrings([]string{"Getting users in org some-org / space whatever-space as my-user"})) 85 }) 86 }) 87 88 It("fails with usage when not invoked with exactly two args", func() { 89 runCommand("my-org") 90 Expect(ui.Outputs()).To(ContainSubstrings( 91 []string{"Incorrect Usage", "Requires arguments"}, 92 )) 93 }) 94 95 Context("when logged in and given some users in the org and space", func() { 96 BeforeEach(func() { 97 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 98 99 org := models.Organization{} 100 org.Name = "Org1" 101 org.GUID = "org1-guid" 102 space := models.Space{} 103 space.Name = "Space1" 104 space.GUID = "space1-guid" 105 106 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 107 organizationReq.GetOrganizationReturns(org) 108 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 109 spaceRepo.FindByNameInOrgReturns(space, nil) 110 111 user := models.UserFields{} 112 user.Username = "user1" 113 user2 := models.UserFields{} 114 user2.Username = "user2" 115 user3 := models.UserFields{} 116 user3.Username = "user3" 117 user4 := models.UserFields{} 118 user4.Username = "user4" 119 userRepo.ListUsersInSpaceForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 120 userFields := map[models.Role][]models.UserFields{ 121 models.RoleSpaceManager: {user, user2}, 122 models.RoleSpaceDeveloper: {user4}, 123 models.RoleSpaceAuditor: {user3}, 124 }[roleName] 125 return userFields, nil 126 } 127 }) 128 129 It("tells you about the space users in the given space", func() { 130 runCommand("my-org", "my-space") 131 132 actualSpaceName, actualOrgGUID := spaceRepo.FindByNameInOrgArgsForCall(0) 133 Expect(actualSpaceName).To(Equal("my-space")) 134 Expect(actualOrgGUID).To(Equal("org1-guid")) 135 136 Expect(userRepo.ListUsersInSpaceForRoleWithNoUAACallCount()).To(Equal(3)) 137 for i, expectedRole := range []models.Role{models.RoleSpaceManager, models.RoleSpaceDeveloper, models.RoleSpaceAuditor} { 138 spaceGUID, actualRole := userRepo.ListUsersInSpaceForRoleWithNoUAAArgsForCall(i) 139 Expect(spaceGUID).To(Equal("space1-guid")) 140 Expect(actualRole).To(Equal(expectedRole)) 141 } 142 143 Expect(ui.Outputs()).To(BeInDisplayOrder( 144 []string{"Getting users in org", "Org1", "Space1", "my-user"}, 145 []string{"SPACE MANAGER"}, 146 []string{"user1"}, 147 []string{"user2"}, 148 []string{"SPACE DEVELOPER"}, 149 []string{"user4"}, 150 []string{"SPACE AUDITOR"}, 151 []string{"user3"}, 152 )) 153 }) 154 155 It("fails with an error when user network call fails", func() { 156 userRepo.ListUsersInSpaceForRoleWithNoUAAStub = func(_ string, role models.Role) ([]models.UserFields, error) { 157 if role == models.RoleSpaceManager { 158 return []models.UserFields{}, errors.New("internet badness occurred") 159 } 160 return []models.UserFields{}, nil 161 } 162 runCommand("my-org", "my-space") 163 Expect(ui.Outputs()).To(BeInDisplayOrder( 164 []string{"Getting users in org", "Org1"}, 165 []string{"internet badness occurred"}, 166 )) 167 }) 168 }) 169 170 Context("when logged in and given a space with a client", func() { 171 BeforeEach(func() { 172 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 173 174 org := models.Organization{} 175 org.Name = "Org1" 176 org.GUID = "org1-guid" 177 space := models.Space{} 178 space.Name = "Space1" 179 space.GUID = "space1-guid" 180 181 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 182 organizationReq.GetOrganizationReturns(org) 183 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 184 spaceRepo.FindByNameInOrgReturns(space, nil) 185 186 clientUser := models.UserFields{GUID: "some-client"} 187 userRepo.ListUsersInSpaceForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 188 userFields := map[models.Role][]models.UserFields{ 189 models.RoleSpaceManager: {clientUser}, 190 models.RoleSpaceDeveloper: {}, 191 models.RoleSpaceAuditor: {}, 192 }[roleName] 193 return userFields, nil 194 } 195 }) 196 197 It("lists a client user and indicates the user is a client", func() { 198 runCommand("my-org", "my-space") 199 200 Expect(ui.Outputs()).To(ContainSubstrings( 201 []string{"Getting users in org", "Org1", "Space1", "my-user"}, 202 []string{"SPACE MANAGER"}, 203 []string{"some-client (client)"}, 204 )) 205 }) 206 }) 207 208 Context("when logged in and there are no non-managers in the space", func() { 209 BeforeEach(func() { 210 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 211 212 org := models.Organization{} 213 org.Name = "Org1" 214 org.GUID = "org1-guid" 215 space := models.Space{} 216 space.Name = "Space1" 217 space.GUID = "space1-guid" 218 219 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 220 organizationReq.GetOrganizationReturns(org) 221 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 222 spaceRepo.FindByNameInOrgReturns(space, nil) 223 224 user := models.UserFields{} 225 user.Username = "mr-pointy-hair" 226 userRepo.ListUsersInSpaceForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 227 userFields := map[models.Role][]models.UserFields{ 228 models.RoleSpaceManager: {user}, 229 models.RoleSpaceDeveloper: {}, 230 models.RoleSpaceAuditor: {}, 231 }[roleName] 232 return userFields, nil 233 } 234 }) 235 236 It("shows a friendly message when there are no users in a role", func() { 237 runCommand("my-org", "my-space") 238 239 Expect(ui.Outputs()).To(BeInDisplayOrder( 240 []string{"Getting users in org"}, 241 []string{"SPACE MANAGER"}, 242 []string{"mr-pointy-hair"}, 243 []string{"SPACE DEVELOPER"}, 244 []string{"No SPACE DEVELOPER found"}, 245 []string{"SPACE AUDITOR"}, 246 []string{"No SPACE AUDITOR found"}, 247 )) 248 }) 249 }) 250 251 Describe("when invoked by a plugin", func() { 252 var ( 253 pluginUserModel []plugin_models.GetSpaceUsers_Model 254 ) 255 256 Context("single roles", func() { 257 BeforeEach(func() { 258 org := models.Organization{} 259 org.Name = "the-org" 260 org.GUID = "the-org-guid" 261 262 space := models.Space{} 263 space.Name = "the-space" 264 space.GUID = "the-space-guid" 265 266 // space managers 267 user := models.UserFields{} 268 user.Username = "user1" 269 user.GUID = "1111" 270 271 user2 := models.UserFields{} 272 user2.Username = "user2" 273 user2.GUID = "2222" 274 275 // space auditor 276 user3 := models.UserFields{} 277 user3.Username = "user3" 278 user3.GUID = "3333" 279 280 // space developer 281 user4 := models.UserFields{} 282 user4.Username = "user4" 283 user4.GUID = "4444" 284 285 userRepo.ListUsersInSpaceForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 286 userFields := map[models.Role][]models.UserFields{ 287 models.RoleSpaceManager: {user, user2}, 288 models.RoleSpaceDeveloper: {user4}, 289 models.RoleSpaceAuditor: {user3}, 290 }[roleName] 291 return userFields, nil 292 } 293 294 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 295 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 296 organizationReq.GetOrganizationReturns(org) 297 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 298 pluginUserModel = []plugin_models.GetSpaceUsers_Model{} 299 deps.PluginModels.SpaceUsers = &pluginUserModel 300 }) 301 302 It("populates the plugin model with users with single roles", func() { 303 testcmd.RunCLICommand("space-users", []string{"the-org", "the-space"}, requirementsFactory, updateCommandDependency, true, ui) 304 305 Expect(pluginUserModel).To(HaveLen(4)) 306 for _, u := range pluginUserModel { 307 switch u.Username { 308 case "user1": 309 Expect(u.Guid).To(Equal("1111")) 310 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceManager"})) 311 case "user2": 312 Expect(u.Guid).To(Equal("2222")) 313 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceManager"})) 314 case "user3": 315 Expect(u.Guid).To(Equal("3333")) 316 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceAuditor"})) 317 case "user4": 318 Expect(u.Guid).To(Equal("4444")) 319 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceDeveloper"})) 320 default: 321 Fail("unexpected user: " + u.Username) 322 } 323 } 324 }) 325 }) 326 327 Context("multiple roles", func() { 328 BeforeEach(func() { 329 org := models.Organization{} 330 org.Name = "the-org" 331 org.GUID = "the-org-guid" 332 333 space := models.Space{} 334 space.Name = "the-space" 335 space.GUID = "the-space-guid" 336 337 // space managers 338 user := models.UserFields{} 339 user.Username = "user1" 340 user.GUID = "1111" 341 342 user2 := models.UserFields{} 343 user2.Username = "user2" 344 user2.GUID = "2222" 345 346 // space auditor 347 user3 := models.UserFields{} 348 user3.Username = "user3" 349 user3.GUID = "3333" 350 351 // space developer 352 user4 := models.UserFields{} 353 user4.Username = "user4" 354 user4.GUID = "4444" 355 356 userRepo.ListUsersInSpaceForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) { 357 userFields := map[models.Role][]models.UserFields{ 358 models.RoleSpaceManager: {user, user2, user3, user4}, 359 models.RoleSpaceDeveloper: {user2, user4}, 360 models.RoleSpaceAuditor: {user, user3}, 361 }[roleName] 362 return userFields, nil 363 } 364 365 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 366 organizationReq := new(requirementsfakes.FakeOrganizationRequirement) 367 organizationReq.GetOrganizationReturns(org) 368 requirementsFactory.NewOrganizationRequirementReturns(organizationReq) 369 pluginUserModel = []plugin_models.GetSpaceUsers_Model{} 370 deps.PluginModels.SpaceUsers = &pluginUserModel 371 }) 372 373 It("populates the plugin model with users with multiple roles", func() { 374 testcmd.RunCLICommand("space-users", []string{"the-org", "the-space"}, requirementsFactory, updateCommandDependency, true, ui) 375 376 Expect(pluginUserModel).To(HaveLen(4)) 377 for _, u := range pluginUserModel { 378 switch u.Username { 379 case "user1": 380 Expect(u.Guid).To(Equal("1111")) 381 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceManager", "RoleSpaceAuditor"})) 382 case "user2": 383 Expect(u.Guid).To(Equal("2222")) 384 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceManager", "RoleSpaceDeveloper"})) 385 case "user3": 386 Expect(u.Guid).To(Equal("3333")) 387 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceManager", "RoleSpaceAuditor"})) 388 case "user4": 389 Expect(u.Guid).To(Equal("4444")) 390 Expect(u.Roles).To(ConsistOf([]string{"RoleSpaceManager", "RoleSpaceDeveloper"})) 391 default: 392 Fail("unexpected user: " + u.Username) 393 } 394 } 395 }) 396 }) 397 }) 398 })