github.com/loafoe/cli@v7.1.0+incompatible/command/v7/org_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("org Command", func() { 20 var ( 21 cmd OrgCommand 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 = OrgCommand{ 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 cmd.RequiredArgs.Organization = "some-org" 48 }) 49 50 JustBeforeEach(func() { 51 executeErr = cmd.Execute(nil) 52 }) 53 54 When("checking the target fails", func() { 55 BeforeEach(func() { 56 fakeSharedActor.CheckTargetReturns( 57 actionerror.NotLoggedInError{BinaryName: binaryName}) 58 }) 59 60 It("returns an error", func() { 61 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 62 63 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 64 targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0) 65 Expect(targetedOrganizationRequired).To(Equal(false)) 66 Expect(targetedSpaceRequired).To(Equal(false)) 67 }) 68 }) 69 70 When("the --guid flag is provided", func() { 71 BeforeEach(func() { 72 cmd.GUID = true 73 }) 74 75 When("no errors occur", func() { 76 BeforeEach(func() { 77 fakeActor.GetOrganizationByNameReturns( 78 resources.Organization{GUID: "some-org-guid"}, 79 v7action.Warnings{"warning-1", "warning-2"}, 80 nil) 81 }) 82 83 It("displays the org guid and outputs all warnings", func() { 84 Expect(executeErr).ToNot(HaveOccurred()) 85 86 Expect(testUI.Out).To(Say("some-org-guid")) 87 Expect(testUI.Err).To(Say("warning-1")) 88 Expect(testUI.Err).To(Say("warning-2")) 89 90 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 91 orgName := fakeActor.GetOrganizationByNameArgsForCall(0) 92 Expect(orgName).To(Equal("some-org")) 93 }) 94 }) 95 96 When("getting the org returns an error", func() { 97 When("the error is translatable", func() { 98 BeforeEach(func() { 99 fakeActor.GetOrganizationByNameReturns( 100 resources.Organization{}, 101 v7action.Warnings{"warning-1", "warning-2"}, 102 actionerror.OrganizationNotFoundError{Name: "some-org"}) 103 }) 104 105 It("returns a translatable error and outputs all warnings", func() { 106 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 107 108 Expect(testUI.Err).To(Say("warning-1")) 109 Expect(testUI.Err).To(Say("warning-2")) 110 }) 111 }) 112 113 When("the error is not translatable", func() { 114 var expectedErr error 115 116 BeforeEach(func() { 117 expectedErr = errors.New("get org error") 118 fakeActor.GetOrganizationByNameReturns( 119 resources.Organization{}, 120 v7action.Warnings{"warning-1", "warning-2"}, 121 expectedErr) 122 }) 123 124 It("returns the error and all warnings", func() { 125 Expect(executeErr).To(MatchError(expectedErr)) 126 127 Expect(testUI.Err).To(Say("warning-1")) 128 Expect(testUI.Err).To(Say("warning-2")) 129 }) 130 }) 131 }) 132 }) 133 134 When("the --guid flag is not provided", func() { 135 When("no errors occur", func() { 136 BeforeEach(func() { 137 fakeConfig.CurrentUserReturns( 138 configv3.User{ 139 Name: "some-user", 140 }, 141 nil) 142 143 fakeActor.GetOrganizationSummaryByNameReturns( 144 v7action.OrganizationSummary{ 145 Organization: resources.Organization{ 146 Name: "some-org", 147 GUID: "some-org-guid", 148 }, 149 DomainNames: []string{ 150 "a-shared.com", 151 "b-private.com", 152 "c-shared.com", 153 "d-private.com", 154 }, 155 QuotaName: "some-quota", 156 SpaceNames: []string{ 157 "space1", 158 "space2", 159 }, 160 DefaultIsolationSegmentGUID: "default-isolation-segment-guid", 161 }, 162 v7action.Warnings{"warning-1", "warning-2"}, 163 nil) 164 }) 165 166 When("API version is above isolation segments minimum version", func() { 167 When("something", func() { 168 BeforeEach(func() { 169 fakeActor.GetIsolationSegmentsByOrganizationReturns( 170 []v7action.IsolationSegment{ 171 { 172 Name: "isolation-segment-1", 173 GUID: "default-isolation-segment-guid", 174 }, { 175 Name: "isolation-segment-2", 176 GUID: "some-other-isolation-segment-guid", 177 }, 178 }, 179 v7action.Warnings{"warning-3", "warning-4"}, 180 nil) 181 }) 182 183 It("displays warnings and a table with org domains, quota, spaces and isolation segments", func() { 184 Expect(executeErr).To(BeNil()) 185 186 Expect(testUI.Out).To(Say(`Getting info for org %s as some-user\.\.\.`, cmd.RequiredArgs.Organization)) 187 Expect(testUI.Err).To(Say("warning-1")) 188 Expect(testUI.Err).To(Say("warning-2")) 189 Expect(testUI.Err).To(Say("warning-3")) 190 Expect(testUI.Err).To(Say("warning-4")) 191 192 Expect(testUI.Out).To(Say(`name:\s+%s`, cmd.RequiredArgs.Organization)) 193 Expect(testUI.Out).To(Say(`domains:\s+a-shared.com, b-private.com, c-shared.com, d-private.com`)) 194 Expect(testUI.Out).To(Say(`quota:\s+some-quota`)) 195 Expect(testUI.Out).To(Say(`spaces:\s+space1, space2`)) 196 Expect(testUI.Out).To(Say(`isolation segments:\s+isolation-segment-1 \(default\), isolation-segment-2`)) 197 198 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 199 200 Expect(fakeActor.GetOrganizationSummaryByNameCallCount()).To(Equal(1)) 201 orgName := fakeActor.GetOrganizationSummaryByNameArgsForCall(0) 202 Expect(orgName).To(Equal("some-org")) 203 204 Expect(fakeActor.GetIsolationSegmentsByOrganizationCallCount()).To(Equal(1)) 205 orgGuid := fakeActor.GetIsolationSegmentsByOrganizationArgsForCall(0) 206 Expect(orgGuid).To(Equal("some-org-guid")) 207 }) 208 }) 209 210 When("getting the org isolation segments returns an error", func() { 211 var expectedErr error 212 213 BeforeEach(func() { 214 expectedErr = errors.New("get org iso segs error") 215 fakeActor.GetIsolationSegmentsByOrganizationReturns( 216 nil, 217 v7action.Warnings{"get iso seg warning"}, 218 expectedErr) 219 }) 220 221 It("returns the error and all warnings", func() { 222 Expect(executeErr).To(MatchError(expectedErr)) 223 Expect(testUI.Err).To(Say("get iso seg warning")) 224 }) 225 }) 226 }) 227 }) 228 229 When("getting the current user returns an error", func() { 230 var expectedErr error 231 232 BeforeEach(func() { 233 expectedErr = errors.New("getting current user error") 234 fakeConfig.CurrentUserReturns( 235 configv3.User{}, 236 expectedErr) 237 }) 238 239 It("returns the error", func() { 240 Expect(executeErr).To(MatchError(expectedErr)) 241 }) 242 }) 243 244 When("getting the org summary returns an error", func() { 245 When("the error is translatable", func() { 246 BeforeEach(func() { 247 fakeActor.GetOrganizationSummaryByNameReturns( 248 v7action.OrganizationSummary{}, 249 v7action.Warnings{"warning-1", "warning-2"}, 250 actionerror.OrganizationNotFoundError{Name: "some-org"}) 251 }) 252 253 It("returns a translatable error and outputs all warnings", func() { 254 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 255 256 Expect(testUI.Err).To(Say("warning-1")) 257 Expect(testUI.Err).To(Say("warning-2")) 258 }) 259 }) 260 261 When("the error is not translatable", func() { 262 var expectedErr error 263 264 BeforeEach(func() { 265 expectedErr = errors.New("get org error") 266 fakeActor.GetOrganizationSummaryByNameReturns( 267 v7action.OrganizationSummary{}, 268 v7action.Warnings{"warning-1", "warning-2"}, 269 expectedErr) 270 }) 271 272 It("returns the error and all warnings", func() { 273 Expect(executeErr).To(MatchError(expectedErr)) 274 275 Expect(testUI.Err).To(Say("warning-1")) 276 Expect(testUI.Err).To(Say("warning-2")) 277 }) 278 }) 279 }) 280 }) 281 })