github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/space_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("space-users Command", func() {
    21  	var (
    22  		cmd             SpaceUsersCommand
    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 = SpaceUsersCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  	})
    49  
    50  	JustBeforeEach(func() {
    51  		executeErr = cmd.Execute(nil)
    52  	})
    53  
    54  	When("an error is encountered checking if the environment is setup correctly", func() {
    55  		BeforeEach(func() {
    56  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    57  		})
    58  
    59  		It("returns an error", func() {
    60  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    61  
    62  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    63  			checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    64  			Expect(checkTargetedOrgArg).To(BeFalse())
    65  			Expect(checkTargetedSpaceArg).To(BeFalse())
    66  
    67  		})
    68  	})
    69  
    70  	When("the user is logged in and an org is targeted", func() {
    71  		BeforeEach(func() {
    72  			cmd.RequiredArgs.Organization = "some-org-name"
    73  			cmd.RequiredArgs.Space = "some-space-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("getting the space guid fails", func() {
   119  					BeforeEach(func() {
   120  						fakeActor.GetSpaceByNameAndOrganizationReturns(
   121  							resources.Space{},
   122  							v7action.Warnings{"get-space-by-name-and-org-warning"},
   123  							errors.New("get-space-by-name-and-org-error"))
   124  					})
   125  
   126  					It("returns the error and warnings", func() {
   127  						Expect(executeErr).To(MatchError("get-space-by-name-and-org-error"))
   128  						Expect(testUI.Err).To(Say("get-space-by-name-and-org-warning"))
   129  					})
   130  				})
   131  
   132  				When("getting the space guid succeeds", func() {
   133  					BeforeEach(func() {
   134  						fakeActor.GetOrganizationByNameReturns(
   135  							resources.Organization{
   136  								Name: "org-1",
   137  								GUID: "org-guid",
   138  							},
   139  							v7action.Warnings{"get-org-by-name-warning"},
   140  							nil)
   141  					})
   142  
   143  					When("There are all types of users", func() {
   144  						BeforeEach(func() {
   145  							abbyUser := resources.User{
   146  								Origin:           "ldap",
   147  								PresentationName: "abby",
   148  								GUID:             "abby-user-guid",
   149  							}
   150  							uaaAdmin := resources.User{
   151  								Origin:           "uaa",
   152  								PresentationName: "admin",
   153  								GUID:             "uaaAdmin-guid",
   154  							}
   155  							ldapAdmin := resources.User{
   156  								Origin:           "ldap",
   157  								PresentationName: "admin",
   158  								GUID:             "ldapAdmin-guid",
   159  							}
   160  							client := resources.User{
   161  								Origin:           "",
   162  								PresentationName: "admin",
   163  								GUID:             "client-guid",
   164  							}
   165  							spaceDeveloper := resources.User{
   166  								Origin:           "uaa",
   167  								PresentationName: "billing-manager",
   168  								GUID:             "spaceDeveloper-guid",
   169  							}
   170  							spaceAuditor := resources.User{
   171  								Origin:           "uaa",
   172  								PresentationName: "org-auditor",
   173  								GUID:             "spaceAuditor-guid",
   174  							}
   175  
   176  							spaceUsersByRole := map[constant.RoleType][]resources.User{
   177  								constant.SpaceManagerRole:   {uaaAdmin, ldapAdmin, abbyUser, client},
   178  								constant.SpaceDeveloperRole: {spaceDeveloper},
   179  								constant.SpaceAuditorRole:   {spaceAuditor},
   180  							}
   181  
   182  							fakeActor.GetSpaceUsersByRoleTypeReturns(
   183  								spaceUsersByRole,
   184  								v7action.Warnings{"get-space-users-by-name-warning"},
   185  								nil)
   186  						})
   187  
   188  						It("displays the alphabetized space-users in the space with origins", func() {
   189  							Expect(executeErr).ToNot(HaveOccurred())
   190  
   191  							Expect(testUI.Out).To(Say("Getting users in org some-org-name / space some-space-name as some-user..."))
   192  							Expect(testUI.Out).To(Say(`\n`))
   193  							Expect(testUI.Out).To(Say(`\nSPACE MANAGER`))
   194  							Expect(testUI.Out).To(Say(`\n  abby \(ldap\)`))
   195  							Expect(testUI.Out).To(Say(`\n  admin \(uaa\)`))
   196  							Expect(testUI.Out).To(Say(`\n  admin \(ldap\)`))
   197  							Expect(testUI.Out).To(Say(`\n  admin \(client\)`))
   198  							Expect(testUI.Out).To(Say(`\n`))
   199  							Expect(testUI.Out).To(Say(`\nSPACE DEVELOPER`))
   200  							Expect(testUI.Out).To(Say(`\n  billing-manager \(uaa\)`))
   201  							Expect(testUI.Out).To(Say(`\n`))
   202  							Expect(testUI.Out).To(Say(`\nSPACE AUDITOR`))
   203  							Expect(testUI.Out).To(Say(`\n  org-auditor \(uaa\)`))
   204  
   205  							Expect(testUI.Err).To(Say("get-space-users-by-name-warning"))
   206  
   207  							Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1))
   208  						})
   209  					})
   210  
   211  					When("There are no space users", func() {
   212  						BeforeEach(func() {
   213  							spaceUsersByRole := map[constant.RoleType][]resources.User{
   214  								constant.OrgManagerRole:        {},
   215  								constant.OrgBillingManagerRole: {},
   216  								constant.OrgAuditorRole:        {},
   217  							}
   218  
   219  							fakeActor.GetSpaceUsersByRoleTypeReturns(
   220  								spaceUsersByRole,
   221  								v7action.Warnings{"get-space-users-warning"},
   222  								nil)
   223  						})
   224  
   225  						It("displays the headings with an informative 'not found' message", func() {
   226  							Expect(executeErr).ToNot(HaveOccurred())
   227  
   228  							Expect(testUI.Out).To(Say("Getting users in org some-org-name / space some-space-name as some-user..."))
   229  							Expect(testUI.Out).To(Say("\n\n"))
   230  							Expect(testUI.Out).To(Say(`SPACE MANAGER`))
   231  							Expect(testUI.Out).To(Say("No SPACE MANAGER found"))
   232  							Expect(testUI.Out).To(Say("\n\n"))
   233  							Expect(testUI.Out).To(Say(`SPACE DEVELOPER`))
   234  							Expect(testUI.Out).To(Say("No SPACE DEVELOPER found"))
   235  							Expect(testUI.Out).To(Say("\n\n"))
   236  							Expect(testUI.Out).To(Say(`SPACE AUDITOR`))
   237  							Expect(testUI.Out).To(Say("No SPACE AUDITOR found"))
   238  
   239  							Expect(testUI.Err).To(Say("get-space-users-warning"))
   240  
   241  							Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1))
   242  						})
   243  					})
   244  
   245  					When("there is an error getting space-users", func() {
   246  						BeforeEach(func() {
   247  							fakeActor.GetSpaceUsersByRoleTypeReturns(
   248  								nil,
   249  								v7action.Warnings{"get-space-users-warning"},
   250  								errors.New("get-space-users-error"))
   251  						})
   252  
   253  						It("returns an error with warnings", func() {
   254  							Expect(executeErr).To(MatchError("get-space-users-error"))
   255  							Expect(testUI.Err).To(Say("get-space-users-warning"))
   256  						})
   257  					})
   258  				})
   259  			})
   260  		})
   261  	})
   262  })