github.com/swisscom/cloudfoundry-cli@v7.1.0+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  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  	plugin_models "code.cloudfoundry.org/cli/plugin/models"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	"os"
    19  
    20  	. "code.cloudfoundry.org/cli/cf/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.ListUsersInOrgForRoleWithNoUAAStub = 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.ListUsersInOrgForRoleWithNoUAACallCount()).To(Equal(3))
   102  				for i, expectedRole := range []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor} {
   103  					orgGUID, actualRole := userRepo.ListUsersInOrgForRoleWithNoUAAArgsForCall(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.ListUsersInOrgForRoleWithNoUAAStub = 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.ListUsersInOrgForRoleWithNoUAACallCount()).To(Equal(3))
   134  				for i, expectedRole := range []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor} {
   135  					orgGUID, actualRole := userRepo.ListUsersInOrgForRoleWithNoUAAArgsForCall(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.ListUsersInOrgForRoleWithNoUAAStub = 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.ListUsersInOrgForRoleWithNoUAACallCount()).To(Equal(3))
   166  				for i, expectedRole := range []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor} {
   167  					orgGUID, actualRole := userRepo.ListUsersInOrgForRoleWithNoUAAArgsForCall(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.ListUsersInOrgForRoleWithNoUAAStub = 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.ListUsersInOrgForRoleWithNoUAAArgsForCall(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.ListUsersInOrgForRoleWithNoUAAStub = 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.ListUsersInOrgForRoleWithNoUAAArgsForCall(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  
   254  	Context("when logged in and given an org with clients", func() {
   255  		BeforeEach(func() {
   256  			org := models.Organization{}
   257  			org.Name = "the-org"
   258  			org.GUID = "the-org-guid"
   259  
   260  			client := models.UserFields{GUID: "some-client"}
   261  			userRepo.ListUsersInOrgForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) {
   262  				userFields := map[models.Role][]models.UserFields{
   263  					models.RoleOrgManager:     {client},
   264  					models.RoleBillingManager: {},
   265  					models.RoleOrgAuditor:     {},
   266  				}[roleName]
   267  				return userFields, nil
   268  			}
   269  
   270  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   271  			organizationReq := new(requirementsfakes.FakeOrganizationRequirement)
   272  			organizationReq.GetOrganizationReturns(org)
   273  			requirementsFactory.NewOrganizationRequirementReturns(organizationReq)
   274  		})
   275  
   276  		It("lists a client user", func() {
   277  			runCommand("the-org")
   278  
   279  			orgGUID, _ := userRepo.ListUsersInOrgForRoleWithNoUAAArgsForCall(0)
   280  			Expect(orgGUID).To(Equal("the-org-guid"))
   281  			Expect(ui.Outputs()).To(ContainSubstrings(
   282  				[]string{"Getting users in org", "the-org", "my-user"},
   283  				[]string{"ORG MANAGER"},
   284  				[]string{"some-client (client)"},
   285  			))
   286  		})
   287  	})
   288  
   289  	Describe("when invoked by a plugin", func() {
   290  		var (
   291  			pluginUserModel []plugin_models.GetOrgUsers_Model
   292  		)
   293  
   294  		BeforeEach(func() {
   295  			configRepo.SetAPIVersion("2.22.0")
   296  		})
   297  
   298  		Context("single roles", func() {
   299  
   300  			BeforeEach(func() {
   301  				org := models.Organization{}
   302  				org.Name = "the-org"
   303  				org.GUID = "the-org-guid"
   304  
   305  				// org managers
   306  				user := models.UserFields{}
   307  				user.Username = "user1"
   308  				user.GUID = "1111"
   309  
   310  				user2 := models.UserFields{}
   311  				user2.Username = "user2"
   312  				user2.GUID = "2222"
   313  
   314  				// billing manager
   315  				user3 := models.UserFields{}
   316  				user3.Username = "user3"
   317  				user3.GUID = "3333"
   318  
   319  				// auditors
   320  				user4 := models.UserFields{}
   321  				user4.Username = "user4"
   322  				user4.GUID = "4444"
   323  
   324  				userRepo.ListUsersInOrgForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) {
   325  					userFields := map[models.Role][]models.UserFields{
   326  						models.RoleOrgManager:     {user, user2},
   327  						models.RoleBillingManager: {user4},
   328  						models.RoleOrgAuditor:     {user3},
   329  						models.RoleOrgUser:        {user3},
   330  					}[roleName]
   331  					return userFields, nil
   332  				}
   333  
   334  				requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   335  				organizationReq := new(requirementsfakes.FakeOrganizationRequirement)
   336  				organizationReq.GetOrganizationReturns(org)
   337  				requirementsFactory.NewOrganizationRequirementReturns(organizationReq)
   338  				pluginUserModel = []plugin_models.GetOrgUsers_Model{}
   339  				deps.PluginModels.OrgUsers = &pluginUserModel
   340  			})
   341  
   342  			It("populates the plugin model with users with single roles", func() {
   343  				testcmd.RunCLICommand("org-users", []string{"the-org"}, requirementsFactory, updateCommandDependency, true, ui)
   344  				Expect(pluginUserModel).To(HaveLen(4))
   345  
   346  				for _, u := range pluginUserModel {
   347  					switch u.Username {
   348  					case "user1":
   349  						Expect(u.Guid).To(Equal("1111"))
   350  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager"}))
   351  					case "user2":
   352  						Expect(u.Guid).To(Equal("2222"))
   353  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager"}))
   354  					case "user3":
   355  						Expect(u.Guid).To(Equal("3333"))
   356  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgAuditor"}))
   357  					case "user4":
   358  						Expect(u.Guid).To(Equal("4444"))
   359  						Expect(u.Roles).To(ConsistOf([]string{"RoleBillingManager"}))
   360  					default:
   361  						Fail("unexpected user: " + u.Username)
   362  					}
   363  				}
   364  
   365  			})
   366  
   367  			It("populates the plugin model with users with single roles -a flag", func() {
   368  				testcmd.RunCLICommand("org-users", []string{"-a", "the-org"}, requirementsFactory, updateCommandDependency, true, ui)
   369  				Expect(pluginUserModel).To(HaveLen(1))
   370  				Expect(pluginUserModel[0].Username).To(Equal("user3"))
   371  				Expect(pluginUserModel[0].Guid).To(Equal("3333"))
   372  				Expect(pluginUserModel[0].Roles[0]).To(Equal("RoleOrgUser"))
   373  			})
   374  
   375  		})
   376  
   377  		Context("multiple roles", func() {
   378  
   379  			BeforeEach(func() {
   380  				org := models.Organization{}
   381  				org.Name = "the-org"
   382  				org.GUID = "the-org-guid"
   383  
   384  				// org managers
   385  				user := models.UserFields{}
   386  				user.Username = "user1"
   387  				user.GUID = "1111"
   388  				user.IsAdmin = true
   389  
   390  				user2 := models.UserFields{}
   391  				user2.Username = "user2"
   392  				user2.GUID = "2222"
   393  
   394  				// billing manager
   395  				user3 := models.UserFields{}
   396  				user3.Username = "user3"
   397  				user3.GUID = "3333"
   398  
   399  				// auditors
   400  				user4 := models.UserFields{}
   401  				user4.Username = "user4"
   402  				user4.GUID = "4444"
   403  
   404  				userRepo.ListUsersInOrgForRoleWithNoUAAStub = func(_ string, roleName models.Role) ([]models.UserFields, error) {
   405  					userFields := map[models.Role][]models.UserFields{
   406  						models.RoleOrgManager:     {user, user2, user3, user4},
   407  						models.RoleBillingManager: {user2, user4},
   408  						models.RoleOrgAuditor:     {user, user3},
   409  						models.RoleOrgUser:        {user, user2, user3, user4},
   410  					}[roleName]
   411  					return userFields, nil
   412  				}
   413  
   414  				requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   415  				organizationReq := new(requirementsfakes.FakeOrganizationRequirement)
   416  				organizationReq.GetOrganizationReturns(org)
   417  				requirementsFactory.NewOrganizationRequirementReturns(organizationReq)
   418  				pluginUserModel = []plugin_models.GetOrgUsers_Model{}
   419  				deps.PluginModels.OrgUsers = &pluginUserModel
   420  			})
   421  
   422  			It("populates the plugin model with users with multiple roles", func() {
   423  				testcmd.RunCLICommand("org-users", []string{"the-org"}, requirementsFactory, updateCommandDependency, true, ui)
   424  
   425  				Expect(pluginUserModel).To(HaveLen(4))
   426  				for _, u := range pluginUserModel {
   427  					switch u.Username {
   428  					case "user1":
   429  						Expect(u.Guid).To(Equal("1111"))
   430  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager", "RoleOrgAuditor"}))
   431  						Expect(u.IsAdmin).To(BeTrue())
   432  					case "user2":
   433  						Expect(u.Guid).To(Equal("2222"))
   434  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgManager", "RoleBillingManager"}))
   435  					case "user3":
   436  						Expect(u.Guid).To(Equal("3333"))
   437  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgAuditor", "RoleOrgManager"}))
   438  					case "user4":
   439  						Expect(u.Guid).To(Equal("4444"))
   440  						Expect(u.Roles).To(ConsistOf([]string{"RoleBillingManager", "RoleOrgManager"}))
   441  					default:
   442  						Fail("unexpected user: " + u.Username)
   443  					}
   444  				}
   445  
   446  			})
   447  
   448  			It("populates the plugin model with users with multiple roles -a flag", func() {
   449  				testcmd.RunCLICommand("org-users", []string{"-a", "the-org"}, requirementsFactory, updateCommandDependency, true, ui)
   450  
   451  				Expect(pluginUserModel).To(HaveLen(4))
   452  				for _, u := range pluginUserModel {
   453  					switch u.Username {
   454  					case "user1":
   455  						Expect(u.Guid).To(Equal("1111"))
   456  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"}))
   457  					case "user2":
   458  						Expect(u.Guid).To(Equal("2222"))
   459  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"}))
   460  					case "user3":
   461  						Expect(u.Guid).To(Equal("3333"))
   462  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"}))
   463  					case "user4":
   464  						Expect(u.Guid).To(Equal("4444"))
   465  						Expect(u.Roles).To(ConsistOf([]string{"RoleOrgUser"}))
   466  					default:
   467  						Fail("unexpected user: " + u.Username)
   468  					}
   469  				}
   470  
   471  			})
   472  
   473  		})
   474  
   475  	})
   476  })