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