code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/spaces_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("spaces Command", func() {
    20  	var (
    21  		cmd             SpacesCommand
    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 = SpacesCommand{
    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(BeTrue())
    64  			Expect(checkTargetedSpaceArg).To(BeFalse())
    65  		})
    66  	})
    67  
    68  	When("the user is logged in and an org is targeted", func() {
    69  		BeforeEach(func() {
    70  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    71  				GUID: "some-org-guid",
    72  				Name: "some-org",
    73  			})
    74  		})
    75  
    76  		When("getting the current user fails", func() {
    77  			BeforeEach(func() {
    78  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error"))
    79  			})
    80  
    81  			It("returns the error", func() {
    82  				Expect(executeErr).To(MatchError("get-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("there are no spaces", func() {
    94  				BeforeEach(func() {
    95  					fakeActor.GetOrganizationSpacesWithLabelSelectorReturns(
    96  						[]resources.Space{},
    97  						v7action.Warnings{"get-spaces-warning"},
    98  						nil,
    99  					)
   100  				})
   101  
   102  				It("displays that there are no spaces", func() {
   103  					Expect(executeErr).ToNot(HaveOccurred())
   104  
   105  					Expect(testUI.Out).To(Say(`Getting spaces in org some-org as some-user\.\.\.`))
   106  					Expect(testUI.Out).To(Say(""))
   107  					Expect(testUI.Out).To(Say(`No spaces found\.`))
   108  
   109  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   110  
   111  					Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   112  
   113  					orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   114  					Expect(orgGUID).To(Equal("some-org-guid"))
   115  					Expect(labelSelector).To(Equal(""))
   116  				})
   117  			})
   118  
   119  			When("there are multiple spaces", func() {
   120  				BeforeEach(func() {
   121  					fakeActor.GetOrganizationSpacesWithLabelSelectorReturns(
   122  						[]resources.Space{
   123  							{Name: "space-1"},
   124  							{Name: "space-2"},
   125  						},
   126  						v7action.Warnings{"get-spaces-warning"},
   127  						nil,
   128  					)
   129  				})
   130  
   131  				It("displays all the spaces in the org", func() {
   132  					Expect(executeErr).ToNot(HaveOccurred())
   133  
   134  					Expect(testUI.Out).To(Say(`Getting spaces in org some-org as some-user\.\.\.`))
   135  					Expect(testUI.Out).To(Say(""))
   136  					Expect(testUI.Out).To(Say("name"))
   137  					Expect(testUI.Out).To(Say("space-1"))
   138  					Expect(testUI.Out).To(Say("space-2"))
   139  
   140  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   141  
   142  					Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   143  					orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   144  					Expect(orgGUID).To(Equal("some-org-guid"))
   145  					Expect(labelSelector).To(Equal(""))
   146  				})
   147  
   148  				When("a label selector is provided to filter the spaces", func() {
   149  					BeforeEach(func() {
   150  						cmd.Labels = "some-label-selector"
   151  					})
   152  					It("passes the label selector to the actor", func() {
   153  						Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   154  						orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   155  						Expect(orgGUID).To(Equal("some-org-guid"))
   156  						Expect(labelSelector).To(Equal("some-label-selector"))
   157  					})
   158  				})
   159  			})
   160  
   161  			When("a translatable error is encountered getting spaces", func() {
   162  				BeforeEach(func() {
   163  					fakeActor.GetOrganizationSpacesWithLabelSelectorReturns(
   164  						nil,
   165  						v7action.Warnings{"get-spaces-warning"},
   166  						actionerror.OrganizationNotFoundError{Name: "not-found-org"},
   167  					)
   168  				})
   169  
   170  				It("returns a translatable error", func() {
   171  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "not-found-org"}))
   172  
   173  					Expect(testUI.Out).To(Say(`Getting spaces in org some-org as some-user\.\.\.`))
   174  					Expect(testUI.Out).To(Say(""))
   175  
   176  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   177  
   178  					Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   179  					orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   180  					Expect(orgGUID).To(Equal("some-org-guid"))
   181  					Expect(labelSelector).To(Equal(""))
   182  				})
   183  			})
   184  		})
   185  	})
   186  })