github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/spaces_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
     9  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    10  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    11  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    12  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    13  	"github.com/liamawhite/cli-with-i18n/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       *v2fakes.FakeSpacesActor
    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(v2fakes.FakeSpacesActor)
    35  
    36  		cmd = SpacesCommand{
    37  			UI:          testUI,
    38  			Config:      fakeConfig,
    39  			SharedActor: fakeSharedActor,
    40  			Actor:       fakeActor,
    41  		}
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  	})
    46  
    47  	JustBeforeEach(func() {
    48  		executeErr = cmd.Execute(nil)
    49  	})
    50  
    51  	Context("when an error is encountered checking if the environment is setup correctly", func() {
    52  		BeforeEach(func() {
    53  			fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    54  		})
    55  
    56  		It("returns an error", func() {
    57  			Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName}))
    58  
    59  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    60  			_, checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0)
    61  			Expect(checkTargetedOrgArg).To(BeTrue())
    62  			Expect(checkTargetedSpaceArg).To(BeFalse())
    63  		})
    64  	})
    65  
    66  	Context("when the user is logged in and an org is targeted", func() {
    67  		BeforeEach(func() {
    68  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    69  				GUID: "some-org-guid",
    70  				Name: "some-org",
    71  			})
    72  		})
    73  
    74  		Context("when getting the current user fails", func() {
    75  			BeforeEach(func() {
    76  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error"))
    77  			})
    78  
    79  			It("returns the error", func() {
    80  				Expect(executeErr).To(MatchError("get-user-error"))
    81  			})
    82  		})
    83  
    84  		Context("when getting the current user succeeds", func() {
    85  			BeforeEach(func() {
    86  				fakeConfig.CurrentUserReturns(
    87  					configv3.User{Name: "some-user"},
    88  					nil)
    89  			})
    90  
    91  			Context("when there are no spaces", func() {
    92  				BeforeEach(func() {
    93  					fakeActor.GetOrganizationSpacesReturns(
    94  						[]v2action.Space{},
    95  						v2action.Warnings{"get-spaces-warning"},
    96  						nil)
    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.GetOrganizationSpacesCallCount()).To(Equal(1))
   109  					Expect(fakeActor.GetOrganizationSpacesArgsForCall(0)).To(Equal("some-org-guid"))
   110  				})
   111  			})
   112  
   113  			Context("when there are multiple spaces", func() {
   114  				BeforeEach(func() {
   115  					fakeActor.GetOrganizationSpacesReturns(
   116  						[]v2action.Space{
   117  							{Name: "space-1"},
   118  							{Name: "space-2"},
   119  						},
   120  						v2action.Warnings{"get-spaces-warning"},
   121  						nil)
   122  				})
   123  
   124  				It("displays all the spaces in the org", func() {
   125  					Expect(executeErr).ToNot(HaveOccurred())
   126  
   127  					Expect(testUI.Out).To(Say("Getting spaces in org some-org as some-user\\.\\.\\."))
   128  					Expect(testUI.Out).To(Say(""))
   129  					Expect(testUI.Out).To(Say("name"))
   130  					Expect(testUI.Out).To(Say("space-1"))
   131  					Expect(testUI.Out).To(Say("space-2"))
   132  
   133  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   134  
   135  					Expect(fakeActor.GetOrganizationSpacesCallCount()).To(Equal(1))
   136  					Expect(fakeActor.GetOrganizationSpacesArgsForCall(0)).To(Equal("some-org-guid"))
   137  				})
   138  			})
   139  
   140  			Context("when a translatable error is encountered getting spaces", func() {
   141  				BeforeEach(func() {
   142  					fakeActor.GetOrganizationSpacesReturns(
   143  						nil,
   144  						v2action.Warnings{"get-spaces-warning"},
   145  						v2action.OrganizationNotFoundError{Name: "not-found-org"})
   146  				})
   147  
   148  				It("returns a translatable error", func() {
   149  					Expect(executeErr).To(MatchError(translatableerror.OrganizationNotFoundError{Name: "not-found-org"}))
   150  
   151  					Expect(testUI.Out).To(Say("Getting spaces in org some-org as some-user\\.\\.\\."))
   152  					Expect(testUI.Out).To(Say(""))
   153  
   154  					Expect(testUI.Err).To(Say("get-spaces-warning"))
   155  
   156  					Expect(fakeActor.GetOrganizationSpacesCallCount()).To(Equal(1))
   157  					Expect(fakeActor.GetOrganizationSpacesArgsForCall(0)).To(Equal("some-org-guid"))
   158  				})
   159  			})
   160  		})
   161  	})
   162  })