github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/set_org_quota_command_test.go (about) 1 package v7_test 2 3 import ( 4 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 5 "github.com/LukasHeimann/cloudfoundrycli/v8/cf/errors" 6 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/flag" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 12 13 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("set-org-quota Command", func() { 20 var ( 21 cmd SetOrgQuotaCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v7fakes.FakeActor 26 binaryName string 27 executeErr error 28 input *Buffer 29 30 getOrgWarning string 31 applyQuotaWarning string 32 currentUser string 33 ) 34 35 BeforeEach(func() { 36 input = NewBuffer() 37 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 38 fakeConfig = new(commandfakes.FakeConfig) 39 fakeSharedActor = new(commandfakes.FakeSharedActor) 40 fakeActor = new(v7fakes.FakeActor) 41 getOrgWarning = RandomString("get-org-warning") 42 applyQuotaWarning = RandomString("apply-quota-warning") 43 44 cmd = SetOrgQuotaCommand{ 45 BaseCommand: BaseCommand{ 46 UI: testUI, 47 Config: fakeConfig, 48 SharedActor: fakeSharedActor, 49 Actor: fakeActor, 50 }, 51 } 52 53 binaryName = "faceman" 54 fakeConfig.BinaryNameReturns(binaryName) 55 56 currentUser = "current-user" 57 fakeActor.GetCurrentUserReturns(configv3.User{Name: currentUser}, nil) 58 59 fakeActor.GetOrganizationByNameReturns( 60 resources.Organization{GUID: "some-org-guid"}, 61 v7action.Warnings{getOrgWarning}, 62 nil, 63 ) 64 65 fakeActor.ApplyOrganizationQuotaByNameReturns( 66 v7action.Warnings{applyQuotaWarning}, 67 nil, 68 ) 69 }) 70 71 JustBeforeEach(func() { 72 executeErr = cmd.Execute(nil) 73 }) 74 75 When("the environment is not set up correctly", func() { 76 BeforeEach(func() { 77 fakeSharedActor.CheckTargetReturns(errors.New("check-target-failure")) 78 }) 79 80 It("does not require a targeted org or space", func() { 81 targetedOrgRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0) 82 Expect(targetedOrgRequired).To(BeFalse()) 83 Expect(targetedSpaceRequired).To(BeFalse()) 84 }) 85 86 It("should return the error", func() { 87 Expect(executeErr).To(MatchError("check-target-failure")) 88 }) 89 }) 90 91 When("applying the quota succeeds", func() { 92 BeforeEach(func() { 93 cmd.RequiredArgs = flag.SetOrgQuotaArgs{} 94 cmd.RequiredArgs.Organization = RandomString("org-name") 95 cmd.RequiredArgs.OrganizationQuota = RandomString("org-quota-name") 96 }) 97 98 It("gets the organization by name", func() { 99 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 100 }) 101 102 It("applies the quota to the organization", func() { 103 Expect(fakeActor.ApplyOrganizationQuotaByNameCallCount()).To(Equal(1)) 104 orgQuotaName, orgGUID := fakeActor.ApplyOrganizationQuotaByNameArgsForCall(0) 105 Expect(orgQuotaName).To(Equal(cmd.RequiredArgs.OrganizationQuota)) 106 Expect(orgGUID).To(Equal("some-org-guid")) 107 }) 108 109 It("displays warnings and returns without error", func() { 110 Expect(executeErr).NotTo(HaveOccurred()) 111 112 Expect(testUI.Err).To(Say(getOrgWarning)) 113 Expect(testUI.Err).To(Say(applyQuotaWarning)) 114 }) 115 116 It("displays flavor text and returns without error", func() { 117 Expect(executeErr).NotTo(HaveOccurred()) 118 119 Expect(testUI.Out).To(Say("Setting quota %s to org %s as %s...", cmd.RequiredArgs.OrganizationQuota, cmd.RequiredArgs.Organization, currentUser)) 120 Expect(testUI.Out).To(Say("OK")) 121 }) 122 }) 123 124 When("getting the current user fails", func() { 125 BeforeEach(func() { 126 fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("current-user-error")) 127 }) 128 129 It("returns the error", func() { 130 Expect(executeErr).To(MatchError("current-user-error")) 131 }) 132 }) 133 134 When("getting the org fails", func() { 135 BeforeEach(func() { 136 fakeActor.GetOrganizationByNameReturns( 137 resources.Organization{GUID: "some-org-guid"}, 138 v7action.Warnings{getOrgWarning}, 139 errors.New("get-org-error"), 140 ) 141 }) 142 143 It("displays warnings and returns the error", func() { 144 Expect(testUI.Err).To(Say(getOrgWarning)) 145 146 Expect(executeErr).To(MatchError("get-org-error")) 147 }) 148 }) 149 150 When("applying the quota to the org fails", func() { 151 BeforeEach(func() { 152 fakeActor.ApplyOrganizationQuotaByNameReturns( 153 v7action.Warnings{applyQuotaWarning}, 154 errors.New("apply-org-quota-error"), 155 ) 156 }) 157 158 It("displays warnings and returns the error", func() { 159 Expect(testUI.Err).To(Say(getOrgWarning)) 160 Expect(testUI.Err).To(Say(applyQuotaWarning)) 161 162 Expect(executeErr).To(MatchError("apply-org-quota-error")) 163 }) 164 }) 165 })