github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/org_users_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("org-users Command", func() {
    21  	var (
    22  		cmd             OrgUsersCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v7fakes.FakeActor
    27  		binaryName      string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = OrgUsersCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  			AllUsers: false,
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("an error is encountered checking if the environment is setup correctly", func() {
    56  		BeforeEach(func() {
    57  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    58  		})
    59  
    60  		It("returns an error", func() {
    61  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    62  
    63  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    64  			checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    65  			Expect(checkTargetedOrgArg).To(BeFalse())
    66  			Expect(checkTargetedSpaceArg).To(BeFalse())
    67  
    68  		})
    69  	})
    70  
    71  	When("the user is logged in and an org is targeted", func() {
    72  		BeforeEach(func() {
    73  			cmd.RequiredArgs.Organization = "some-org-name"
    74  		})
    75  
    76  		When("getting the current user fails", func() {
    77  			BeforeEach(func() {
    78  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-current-user-error"))
    79  			})
    80  
    81  			It("returns the error", func() {
    82  				Expect(executeErr).To(MatchError("get-current-user-error"))
    83  			})
    84  		})
    85  
    86  		When("getting the current user succeeds", func() {
    87  			BeforeEach(func() {
    88  				fakeConfig.CurrentUserReturns(
    89  					configv3.User{Name: "some-user"},
    90  					nil)
    91  			})
    92  
    93  			When("getting the org guid fails", func() {
    94  				BeforeEach(func() {
    95  					fakeActor.GetOrganizationByNameReturns(
    96  						resources.Organization{},
    97  						v7action.Warnings{"get-org-by-name-warning"},
    98  						errors.New("get-org-by-name-error"))
    99  				})
   100  
   101  				It("returns the error and warnings", func() {
   102  					Expect(executeErr).To(MatchError("get-org-by-name-error"))
   103  					Expect(testUI.Err).To(Say("get-org-by-name-warning"))
   104  				})
   105  			})
   106  
   107  			When("getting the org guid succeeds", func() {
   108  				BeforeEach(func() {
   109  					fakeActor.GetOrganizationByNameReturns(
   110  						resources.Organization{
   111  							Name: "org-1",
   112  							GUID: "org-guid",
   113  						},
   114  						v7action.Warnings{"get-org-by-name-warning"},
   115  						nil)
   116  				})
   117  
   118  				When("There are all types of users", func() {
   119  					BeforeEach(func() {
   120  						abbyUser := resources.User{
   121  							Origin:           "ldap",
   122  							PresentationName: "abby",
   123  							GUID:             "abby-user-guid",
   124  						}
   125  						uaaAdmin := resources.User{
   126  							Origin:           "uaa",
   127  							PresentationName: "admin",
   128  							GUID:             "uaaAdmin-guid",
   129  						}
   130  						ldapAdmin := resources.User{
   131  							Origin:           "ldap",
   132  							PresentationName: "admin",
   133  							GUID:             "ldapAdmin-guid",
   134  						}
   135  						client := resources.User{
   136  							Origin:           "",
   137  							PresentationName: "admin",
   138  							GUID:             "client-guid",
   139  						}
   140  						billingManager := resources.User{
   141  							Origin:           "uaa",
   142  							PresentationName: "billing-manager",
   143  							GUID:             "billingManager-guid",
   144  						}
   145  						orgAuditor := resources.User{
   146  							Origin:           "uaa",
   147  							PresentationName: "org-auditor",
   148  							GUID:             "orgAuditor-guid",
   149  						}
   150  						orgUser := resources.User{
   151  							Origin:           "uaa",
   152  							PresentationName: "org-user",
   153  							GUID:             "orgUser-guid",
   154  						}
   155  
   156  						orgUsersByRole := map[constant.RoleType][]resources.User{
   157  							constant.OrgManagerRole:        {uaaAdmin, ldapAdmin, abbyUser, client},
   158  							constant.OrgBillingManagerRole: {billingManager, uaaAdmin},
   159  							constant.OrgAuditorRole:        {orgAuditor},
   160  							constant.OrgUserRole:           {orgUser},
   161  						}
   162  
   163  						fakeActor.GetOrgUsersByRoleTypeReturns(
   164  							orgUsersByRole,
   165  							v7action.Warnings{"get-org-by-name-warning"},
   166  							nil)
   167  					})
   168  
   169  					It("displays the alphabetized org-users in the org with origins", func() {
   170  						Expect(executeErr).ToNot(HaveOccurred())
   171  
   172  						Expect(testUI.Out).To(Say(`Getting users in org some-org-name as some-user\.\.\.`))
   173  						Expect(testUI.Out).To(Say(`\n`))
   174  						Expect(testUI.Out).To(Say(`\nORG MANAGER`))
   175  						Expect(testUI.Out).To(Say(`\n  abby \(ldap\)`))
   176  						Expect(testUI.Out).To(Say(`\n  admin \(uaa\)`))
   177  						Expect(testUI.Out).To(Say(`\n  admin \(ldap\)`))
   178  						Expect(testUI.Out).To(Say(`\n  admin \(client\)`))
   179  						Expect(testUI.Out).To(Say(`\n`))
   180  						Expect(testUI.Out).To(Say(`\nBILLING MANAGER`))
   181  						Expect(testUI.Out).To(Say(`\n  billing-manager \(uaa\)`))
   182  						Expect(testUI.Out).To(Say(`\n`))
   183  						Expect(testUI.Out).To(Say(`\nORG AUDITOR`))
   184  						Expect(testUI.Out).To(Say(`\n  org-auditor \(uaa\)`))
   185  
   186  						Expect(testUI.Err).To(Say("get-org-by-name-warning"))
   187  
   188  						Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1))
   189  					})
   190  
   191  					When("the --all-users flag is passed in", func() {
   192  						BeforeEach(func() {
   193  							cmd.AllUsers = true
   194  						})
   195  
   196  						It("displays the alphabetized org-users in the org with origins", func() {
   197  							Expect(executeErr).ToNot(HaveOccurred())
   198  
   199  							Expect(testUI.Out).To(Say(`Getting users in org some-org-name as some-user\.\.\.`))
   200  							Expect(testUI.Out).To(Say("\n\n"))
   201  							Expect(testUI.Out).To(Say("USERS"))
   202  							Expect(testUI.Out).To(Say(`abby \(ldap\)`))
   203  							Expect(testUI.Out).To(Say(`admin \(uaa\)`))
   204  							// Ensure that admin (uaa) does not appear twice, even though it has two roles
   205  							Expect(testUI.Out).NotTo(Say(`admin \(uaa\)`))
   206  							Expect(testUI.Out).To(Say(`admin \(ldap\)`))
   207  							Expect(testUI.Out).To(Say(`admin \(client\)`))
   208  							Expect(testUI.Out).To(Say(`billing-manager \(uaa\)`))
   209  							Expect(testUI.Out).To(Say(`org-auditor \(uaa\)`))
   210  							Expect(testUI.Out).To(Say(`org-user \(uaa\)`))
   211  
   212  							Expect(testUI.Err).To(Say("get-org-by-name-warning"))
   213  
   214  							Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1))
   215  						})
   216  					})
   217  				})
   218  
   219  				When("There are no org users", func() {
   220  					BeforeEach(func() {
   221  						orgUsersByRole := map[constant.RoleType][]resources.User{
   222  							constant.OrgManagerRole:        {},
   223  							constant.OrgBillingManagerRole: {},
   224  							constant.OrgAuditorRole:        {},
   225  						}
   226  
   227  						fakeActor.GetOrgUsersByRoleTypeReturns(
   228  							orgUsersByRole,
   229  							v7action.Warnings{"get-org-users-warning"},
   230  							nil)
   231  					})
   232  
   233  					It("displays the headings with an informative 'not found' message", func() {
   234  						Expect(executeErr).ToNot(HaveOccurred())
   235  
   236  						Expect(testUI.Out).To(Say(`Getting users in org some-org-name as some-user\.\.\.`))
   237  						Expect(testUI.Out).To(Say("\n\n"))
   238  						Expect(testUI.Out).To(Say("ORG MANAGER"))
   239  						Expect(testUI.Out).To(Say("No ORG MANAGER found"))
   240  						Expect(testUI.Out).To(Say("\n\n"))
   241  						Expect(testUI.Out).To(Say("BILLING MANAGER"))
   242  						Expect(testUI.Out).To(Say("No BILLING MANAGER found"))
   243  						Expect(testUI.Out).To(Say("\n\n"))
   244  						Expect(testUI.Out).To(Say("ORG AUDITOR"))
   245  						Expect(testUI.Out).To(Say("No ORG AUDITOR found"))
   246  
   247  						Expect(testUI.Err).To(Say("get-org-users-warning"))
   248  
   249  						Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1))
   250  					})
   251  				})
   252  
   253  				When("there is an error getting org-users", func() {
   254  					BeforeEach(func() {
   255  						fakeActor.GetOrgUsersByRoleTypeReturns(
   256  							nil,
   257  							v7action.Warnings{"get-org-users-warning"},
   258  							errors.New("get-org-users-error"))
   259  					})
   260  
   261  					It("returns an error with warnings", func() {
   262  						Expect(executeErr).To(MatchError("get-org-users-error"))
   263  						Expect(testUI.Err).To(Say("get-org-users-warning"))
   264  					})
   265  				})
   266  			})
   267  		})
   268  	})
   269  })