github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/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("orgs Command", func() { 19 var ( 20 cmd OrgsCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v7fakes.FakeOrgsActor 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.FakeOrgsActor) 34 35 cmd = OrgsCommand{ 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(BeFalse()) 61 Expect(checkTargetedSpaceArg).To(BeFalse()) 62 }) 63 }) 64 65 When("the user is logged in and an org is targeted", func() { 66 When("getting the current user fails", func() { 67 BeforeEach(func() { 68 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error")) 69 }) 70 71 It("returns the error", func() { 72 Expect(executeErr).To(MatchError("get-user-error")) 73 }) 74 }) 75 76 When("getting the current user succeeds", func() { 77 BeforeEach(func() { 78 fakeConfig.CurrentUserReturns( 79 configv3.User{Name: "some-user"}, 80 nil) 81 }) 82 83 When("there are no orgs", func() { 84 BeforeEach(func() { 85 fakeActor.GetOrganizationsReturns( 86 []v7action.Organization{}, 87 v7action.Warnings{"get-orgs-warning"}, 88 nil) 89 }) 90 91 It("displays that there are no orgs", func() { 92 Expect(executeErr).ToNot(HaveOccurred()) 93 94 Expect(testUI.Out).To(Say(`Getting orgs as some-user\.\.\.`)) 95 Expect(testUI.Out).To(Say("")) 96 Expect(testUI.Out).To(Say(`No orgs found\.`)) 97 98 Expect(testUI.Err).To(Say("get-orgs-warning")) 99 100 Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1)) 101 }) 102 }) 103 104 When("there are multiple orgs", func() { 105 BeforeEach(func() { 106 fakeActor.GetOrganizationsReturns( 107 []v7action.Organization{ 108 {Name: "org-1"}, 109 {Name: "org-2"}, 110 }, 111 v7action.Warnings{"get-orgs-warning"}, 112 nil) 113 }) 114 115 It("displays all the orgs in the org", func() { 116 Expect(executeErr).ToNot(HaveOccurred()) 117 118 Expect(testUI.Out).To(Say(`Getting orgs as some-user\.\.\.`)) 119 Expect(testUI.Out).To(Say("")) 120 Expect(testUI.Out).To(Say("name")) 121 Expect(testUI.Out).To(Say("org-1")) 122 Expect(testUI.Out).To(Say("org-2")) 123 124 Expect(testUI.Err).To(Say("get-orgs-warning")) 125 126 Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1)) 127 }) 128 129 When("a label selector is provided to filter the orgs", func() { 130 BeforeEach(func() { 131 cmd.Labels = "some-label-selector" 132 }) 133 It("passes the label selector to the actor", func() { 134 Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1)) 135 Expect(fakeActor.GetOrganizationsArgsForCall(0)).To(Equal("some-label-selector")) 136 }) 137 }) 138 }) 139 140 When("a translatable error is encountered getting orgs", func() { 141 BeforeEach(func() { 142 fakeActor.GetOrganizationsReturns( 143 nil, 144 v7action.Warnings{"get-orgs-warning"}, 145 actionerror.OrganizationNotFoundError{Name: "not-found-org"}) 146 }) 147 148 It("returns a translatable error", func() { 149 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "not-found-org"})) 150 151 Expect(testUI.Out).To(Say(`Getting orgs as some-user\.\.\.`)) 152 Expect(testUI.Out).To(Say("")) 153 154 Expect(testUI.Err).To(Say("get-orgs-warning")) 155 156 Expect(fakeActor.GetOrganizationsCallCount()).To(Equal(1)) 157 }) 158 }) 159 }) 160 }) 161 })