github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("spaces Command", func() {
    19  	var (
    20  		cmd             SpacesCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeSpacesActor
    25  		binaryName      string
    26  		executeErr      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    33  		fakeActor = new(v7fakes.FakeSpacesActor)
    34  
    35  		cmd = SpacesCommand{
    36  			UI:          testUI,
    37  			Config:      fakeConfig,
    38  			SharedActor: fakeSharedActor,
    39  			Actor:       fakeActor,
    40  		}
    41  
    42  		binaryName = "faceman"
    43  		fakeConfig.BinaryNameReturns(binaryName)
    44  	})
    45  
    46  	JustBeforeEach(func() {
    47  		executeErr = cmd.Execute(nil)
    48  	})
    49  
    50  	When("an error is encountered checking if the environment is setup correctly", func() {
    51  		BeforeEach(func() {
    52  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    53  		})
    54  
    55  		It("returns an error", func() {
    56  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    57  
    58  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    59  			checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    60  			Expect(checkTargetedOrgArg).To(BeTrue())
    61  			Expect(checkTargetedSpaceArg).To(BeFalse())
    62  		})
    63  	})
    64  
    65  	When("the user is logged in and an org is targeted", func() {
    66  		BeforeEach(func() {
    67  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    68  				GUID: "some-org-guid",
    69  				Name: "some-org",
    70  			})
    71  		})
    72  
    73  		When("getting the current user fails", func() {
    74  			BeforeEach(func() {
    75  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error"))
    76  			})
    77  
    78  			It("returns the error", func() {
    79  				Expect(executeErr).To(MatchError("get-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("there are no spaces", func() {
    91  				BeforeEach(func() {
    92  					fakeActor.GetOrganizationSpacesWithLabelSelectorReturns(
    93  						[]v7action.Space{},
    94  						v7action.Warnings{"get-spaces-warning"},
    95  						nil,
    96  					)
    97  				})
    98  
    99  				It("displays that there are no spaces", func() {
   100  					Expect(executeErr).ToNot(HaveOccurred())
   101  
   102  					Expect(testUI.Out).To(Say(`Getting spaces in org some-org as some-user\.\.\.`))
   103  					Expect(testUI.Out).To(Say(""))
   104  					Expect(testUI.Out).To(Say(`No spaces found\.`))
   105  
   106  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   107  
   108  					Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   109  
   110  					orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   111  					Expect(orgGUID).To(Equal("some-org-guid"))
   112  					Expect(labelSelector).To(Equal(""))
   113  				})
   114  			})
   115  
   116  			When("there are multiple spaces", func() {
   117  				BeforeEach(func() {
   118  					fakeActor.GetOrganizationSpacesWithLabelSelectorReturns(
   119  						[]v7action.Space{
   120  							{Name: "space-1"},
   121  							{Name: "space-2"},
   122  						},
   123  						v7action.Warnings{"get-spaces-warning"},
   124  						nil,
   125  					)
   126  				})
   127  
   128  				It("displays all the spaces in the org", func() {
   129  					Expect(executeErr).ToNot(HaveOccurred())
   130  
   131  					Expect(testUI.Out).To(Say(`Getting spaces in org some-org as some-user\.\.\.`))
   132  					Expect(testUI.Out).To(Say(""))
   133  					Expect(testUI.Out).To(Say("name"))
   134  					Expect(testUI.Out).To(Say("space-1"))
   135  					Expect(testUI.Out).To(Say("space-2"))
   136  
   137  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   138  
   139  					Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   140  					orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   141  					Expect(orgGUID).To(Equal("some-org-guid"))
   142  					Expect(labelSelector).To(Equal(""))
   143  				})
   144  
   145  				When("a label selector is provided to filter the spaces", func() {
   146  					BeforeEach(func() {
   147  						cmd.Labels = "some-label-selector"
   148  					})
   149  					It("passes the label selector to the actor", func() {
   150  						Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   151  						orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   152  						Expect(orgGUID).To(Equal("some-org-guid"))
   153  						Expect(labelSelector).To(Equal("some-label-selector"))
   154  					})
   155  				})
   156  			})
   157  
   158  			When("a translatable error is encountered getting spaces", func() {
   159  				BeforeEach(func() {
   160  					fakeActor.GetOrganizationSpacesWithLabelSelectorReturns(
   161  						nil,
   162  						v7action.Warnings{"get-spaces-warning"},
   163  						actionerror.OrganizationNotFoundError{Name: "not-found-org"},
   164  					)
   165  				})
   166  
   167  				It("returns a translatable error", func() {
   168  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "not-found-org"}))
   169  
   170  					Expect(testUI.Out).To(Say(`Getting spaces in org some-org as some-user\.\.\.`))
   171  					Expect(testUI.Out).To(Say(""))
   172  
   173  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   174  
   175  					Expect(fakeActor.GetOrganizationSpacesWithLabelSelectorCallCount()).To(Equal(1))
   176  					orgGUID, labelSelector := fakeActor.GetOrganizationSpacesWithLabelSelectorArgsForCall(0)
   177  					Expect(orgGUID).To(Equal("some-org-guid"))
   178  					Expect(labelSelector).To(Equal(""))
   179  				})
   180  			})
   181  		})
   182  	})
   183  })