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