github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/orgs_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("orgs Command", func() {
    20  	var (
    21  		cmd             OrgsCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v2fakes.FakeOrgsActor
    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.FakeOrgsActor)
    35  
    36  		cmd = OrgsCommand{
    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(BeFalse())
    62  			Expect(checkTargetedSpaceArg).To(BeFalse())
    63  		})
    64  	})
    65  
    66  	Context("when the user is logged in and an org is targeted", func() {
    67  		Context("when getting the current user fails", func() {
    68  			BeforeEach(func() {
    69  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error"))
    70  			})
    71  
    72  			It("returns the error", func() {
    73  				Expect(executeErr).To(MatchError("get-user-error"))
    74  			})
    75  		})
    76  
    77  		Context("when getting the current user succeeds", func() {
    78  			BeforeEach(func() {
    79  				fakeConfig.CurrentUserReturns(
    80  					configv3.User{Name: "some-user"},
    81  					nil)
    82  			})
    83  
    84  			Context("when there are no orgs", func() {
    85  				BeforeEach(func() {
    86  					fakeActor.GetOrganizationsReturns(
    87  						[]v2action.Organization{},
    88  						v2action.Warnings{"get-orgs-warning"},
    89  						nil)
    90  				})
    91  
    92  				It("displays that there are no orgs", func() {
    93  					Expect(executeErr).ToNot(HaveOccurred())
    94  
    95  					Expect(testUI.Out).To(Say("Getting orgs as some-user\\.\\.\\."))
    96  					Expect(testUI.Out).To(Say(""))
    97  					Expect(testUI.Out).To(Say("No orgs found\\."))
    98  
    99  					Expect(testUI.Err).To(Say("get-orgs-warning"))
   100  
   101  					Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   102  				})
   103  			})
   104  
   105  			Context("when there are multiple orgs", func() {
   106  				BeforeEach(func() {
   107  					fakeActor.GetOrganizationsReturns(
   108  						[]v2action.Organization{
   109  							{Name: "org-1"},
   110  							{Name: "org-2"},
   111  						},
   112  						v2action.Warnings{"get-orgs-warning"},
   113  						nil)
   114  				})
   115  
   116  				It("displays all the orgs in the org", func() {
   117  					Expect(executeErr).ToNot(HaveOccurred())
   118  
   119  					Expect(testUI.Out).To(Say("Getting orgs as some-user\\.\\.\\."))
   120  					Expect(testUI.Out).To(Say(""))
   121  					Expect(testUI.Out).To(Say("name"))
   122  					Expect(testUI.Out).To(Say("org-1"))
   123  					Expect(testUI.Out).To(Say("org-2"))
   124  
   125  					Expect(testUI.Err).To(Say("get-orgs-warning"))
   126  
   127  					Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   128  				})
   129  			})
   130  
   131  			Context("when a translatable error is encountered getting orgs", func() {
   132  				BeforeEach(func() {
   133  					fakeActor.GetOrganizationsReturns(
   134  						nil,
   135  						v2action.Warnings{"get-orgs-warning"},
   136  						v2action.OrganizationNotFoundError{Name: "not-found-org"})
   137  				})
   138  
   139  				It("returns a translatable error", func() {
   140  					Expect(executeErr).To(MatchError(translatableerror.OrganizationNotFoundError{Name: "not-found-org"}))
   141  
   142  					Expect(testUI.Out).To(Say("Getting orgs as some-user\\.\\.\\."))
   143  					Expect(testUI.Out).To(Say(""))
   144  
   145  					Expect(testUI.Err).To(Say("get-orgs-warning"))
   146  
   147  					Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1))
   148  				})
   149  			})
   150  		})
   151  	})
   152  })