code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/orgs_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/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/resources"
    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("orgs Command", func() {
    20  	var (
    21  		cmd             OrgsCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    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.FakeActor)
    35  
    36  		cmd = OrgsCommand{
    37  			BaseCommand: BaseCommand{
    38  				UI:          testUI,
    39  				Config:      fakeConfig,
    40  				SharedActor: fakeSharedActor,
    41  				Actor:       fakeActor,
    42  			},
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  	})
    48  
    49  	JustBeforeEach(func() {
    50  		executeErr = cmd.Execute(nil)
    51  	})
    52  
    53  	When("an error is encountered checking if the environment is setup correctly", func() {
    54  		BeforeEach(func() {
    55  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    56  		})
    57  
    58  		It("returns an error", func() {
    59  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    60  
    61  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    62  			checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    63  			Expect(checkTargetedOrgArg).To(BeFalse())
    64  			Expect(checkTargetedSpaceArg).To(BeFalse())
    65  		})
    66  	})
    67  
    68  	When("the user is logged in and an org is targeted", func() {
    69  		When("getting the current user fails", func() {
    70  			BeforeEach(func() {
    71  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error"))
    72  			})
    73  
    74  			It("returns the error", func() {
    75  				Expect(executeErr).To(MatchError("get-user-error"))
    76  			})
    77  		})
    78  
    79  		When("getting the current user succeeds", func() {
    80  			BeforeEach(func() {
    81  				fakeConfig.CurrentUserReturns(
    82  					configv3.User{Name: "some-user"},
    83  					nil)
    84  			})
    85  
    86  			When("there are no orgs", func() {
    87  				BeforeEach(func() {
    88  					fakeActor.GetOrganizationsReturns(
    89  						[]resources.Organization{},
    90  						v7action.Warnings{"get-orgs-warning"},
    91  						nil)
    92  				})
    93  
    94  				It("displays that there are no orgs", func() {
    95  					Expect(executeErr).ToNot(HaveOccurred())
    96  
    97  					Expect(testUI.Out).To(Say(`Getting orgs as some-user\.\.\.`))
    98  					Expect(testUI.Out).To(Say(""))
    99  					Expect(testUI.Out).To(Say(`No orgs found\.`))
   100  
   101  					Expect(testUI.Err).To(Say("get-orgs-warning"))
   102  
   103  					Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   104  				})
   105  			})
   106  
   107  			When("there are multiple orgs", func() {
   108  				BeforeEach(func() {
   109  					fakeActor.GetOrganizationsReturns(
   110  						[]resources.Organization{
   111  							{Name: "org-1"},
   112  							{Name: "org-2"},
   113  						},
   114  						v7action.Warnings{"get-orgs-warning"},
   115  						nil)
   116  				})
   117  
   118  				It("displays all the orgs in the org", func() {
   119  					Expect(executeErr).ToNot(HaveOccurred())
   120  
   121  					Expect(testUI.Out).To(Say(`Getting orgs as some-user\.\.\.`))
   122  					Expect(testUI.Out).To(Say(""))
   123  					Expect(testUI.Out).To(Say("name"))
   124  					Expect(testUI.Out).To(Say("org-1"))
   125  					Expect(testUI.Out).To(Say("org-2"))
   126  
   127  					Expect(testUI.Err).To(Say("get-orgs-warning"))
   128  
   129  					Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   130  				})
   131  
   132  				When("a label selector is provided to filter the orgs", func() {
   133  					BeforeEach(func() {
   134  						cmd.Labels = "some-label-selector"
   135  					})
   136  					It("passes the label selector to the actor", func() {
   137  						Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   138  						Expect(fakeActor.GetOrganizationsArgsForCall(0)).To(Equal("some-label-selector"))
   139  					})
   140  				})
   141  			})
   142  
   143  			When("a translatable error is encountered getting orgs", func() {
   144  				BeforeEach(func() {
   145  					fakeActor.GetOrganizationsReturns(
   146  						nil,
   147  						v7action.Warnings{"get-orgs-warning"},
   148  						actionerror.OrganizationNotFoundError{Name: "not-found-org"})
   149  				})
   150  
   151  				It("returns a translatable error", func() {
   152  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "not-found-org"}))
   153  
   154  					Expect(testUI.Out).To(Say(`Getting orgs as some-user\.\.\.`))
   155  					Expect(testUI.Out).To(Say(""))
   156  
   157  					Expect(testUI.Err).To(Say("get-orgs-warning"))
   158  
   159  					Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   160  				})
   161  			})
   162  		})
   163  	})
   164  })