github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/create_org_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccerror" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/command/flag" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 11 12 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 14 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 15 v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 16 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 17 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 . "github.com/onsi/gomega/gbytes" 21 ) 22 23 var _ = Describe("create-org Command", func() { 24 var ( 25 cmd v7.CreateOrgCommand 26 testUI *ui.UI 27 fakeConfig *commandfakes.FakeConfig 28 fakeSharedActor *commandfakes.FakeSharedActor 29 fakeActor *v7fakes.FakeActor 30 binaryName string 31 executeErr error 32 33 orgName string 34 quotaName string 35 currentUsername string 36 ) 37 38 BeforeEach(func() { 39 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 40 fakeConfig = new(commandfakes.FakeConfig) 41 fakeSharedActor = new(commandfakes.FakeSharedActor) 42 fakeActor = new(v7fakes.FakeActor) 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 orgName = "some-org" 47 currentUsername = "bob" 48 fakeActor.GetCurrentUserReturns(configv3.User{Name: currentUsername}, nil) 49 quotaName = "quota-name" 50 51 cmd = v7.CreateOrgCommand{ 52 BaseCommand: v7.BaseCommand{ 53 UI: testUI, 54 Config: fakeConfig, 55 SharedActor: fakeSharedActor, 56 Actor: fakeActor, 57 }, 58 RequiredArgs: flag.Organization{Organization: orgName}, 59 Quota: quotaName, 60 } 61 }) 62 63 JustBeforeEach(func() { 64 executeErr = cmd.Execute(nil) 65 }) 66 67 When("the environment is not set up correctly", func() { 68 BeforeEach(func() { 69 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 70 }) 71 72 It("returns an error", func() { 73 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 74 75 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 76 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 77 Expect(checkTargetedOrg).To(BeFalse()) 78 Expect(checkTargetedSpace).To(BeFalse()) 79 }) 80 }) 81 82 It("prints text indicating it is creating a org", func() { 83 Expect(executeErr).NotTo(HaveOccurred()) 84 Expect(testUI.Out).To(Say(`Creating org %s as %s\.\.\.`, orgName, currentUsername)) 85 }) 86 87 When("creating the org errors", func() { 88 BeforeEach(func() { 89 fakeActor.CreateOrganizationReturns(resources.Organization{}, v7action.Warnings{"warnings-1", "warnings-2"}, errors.New("err-create-org")) 90 }) 91 92 It("returns an error and displays warnings", func() { 93 Expect(executeErr).To(MatchError("err-create-org")) 94 Expect(testUI.Err).To(Say("warnings-1")) 95 Expect(testUI.Err).To(Say("warnings-2")) 96 }) 97 }) 98 99 When("the org already exists", func() { 100 BeforeEach(func() { 101 fakeActor.CreateOrganizationReturns( 102 resources.Organization{}, 103 v7action.Warnings{"some-warning"}, 104 ccerror.OrganizationNameTakenError{ 105 UnprocessableEntityError: ccerror.UnprocessableEntityError{ 106 Message: "Organization 'some-org' already exists.", 107 }, 108 }, 109 ) 110 }) 111 112 It("displays all warnings, that the org already exists, and does not error", func() { 113 Expect(executeErr).ToNot(HaveOccurred()) 114 115 Expect(testUI.Err).To(Say("some-warning")) 116 Expect(testUI.Out).To(Say(`Creating org %s as %s\.\.\.`, orgName, currentUsername)) 117 Expect(testUI.Out).To(Say(`Organization '%s' already exists\.`, orgName)) 118 Expect(testUI.Out).To(Say("OK")) 119 }) 120 }) 121 122 When("applying the quota errors", func() { 123 BeforeEach(func() { 124 cmd.Quota = "quota-name" 125 fakeActor.CreateOrganizationReturns(resources.Organization{Name: orgName, GUID: "some-org-guid"}, v7action.Warnings{}, nil) 126 fakeActor.ApplyOrganizationQuotaByNameReturns(v7action.Warnings{"quota-warnings-1", "quota-warnings-2"}, errors.New("quota-error")) 127 128 }) 129 130 It("returns an error and displays warnings", func() { 131 Expect(executeErr).To(MatchError("quota-error")) 132 Expect(testUI.Err).To(Say("quota-warnings-1")) 133 Expect(testUI.Err).To(Say("quota-warnings-2")) 134 }) 135 }) 136 137 When("the quota does not exist", func() { 138 BeforeEach(func() { 139 fakeActor.ApplyOrganizationQuotaByNameReturns(v7action.Warnings{"quota-warnings-1", "quota-warnings-2"}, actionerror.OrganizationQuotaNotFoundForNameError{Name: quotaName}) 140 }) 141 142 It("returns an error and displays warnings", func() { 143 Expect(testUI.Err).To(Say("quota-warnings-1")) 144 Expect(testUI.Err).To(Say("quota-warnings-2")) 145 Expect(executeErr).To(MatchError(fmt.Sprintf("Organization quota with name '%s' not found.", quotaName))) 146 }) 147 }) 148 149 When("creating the org is successful", func() { 150 var orgGUID = "some-org-guid" 151 BeforeEach(func() { 152 fakeActor.CreateOrganizationReturns(resources.Organization{Name: orgName, GUID: orgGUID}, v7action.Warnings{"warnings-1", "warnings-2"}, nil) 153 fakeActor.ApplyOrganizationQuotaByNameReturns(v7action.Warnings{"quota-warnings-1", "quota-warnings-2"}, nil) 154 }) 155 156 It("prints all warnings, text indicating creation completion, ok and then a tip", func() { 157 Expect(executeErr).ToNot(HaveOccurred()) 158 159 Expect(testUI.Out).To(Say(`Creating org %s as %s\.\.\.`, orgName, currentUsername)) 160 Expect(testUI.Err).To(Say("warnings-1")) 161 Expect(testUI.Err).To(Say("warnings-2")) 162 Expect(testUI.Out).To(Say("OK")) 163 164 Expect(testUI.Out).To(Say(`Setting org quota %s to org %s as %s\.\.\.`, quotaName, orgName, currentUsername)) 165 Expect(testUI.Err).To(Say("quota-warnings-1")) 166 Expect(testUI.Err).To(Say("quota-warnings-2")) 167 Expect(testUI.Out).To(Say("OK")) 168 169 Expect(testUI.Out).To(Say(`TIP: Use 'cf target -o "%s"' to target new org`, orgName)) 170 }) 171 172 It("creates the org", func() { 173 Expect(fakeActor.CreateOrganizationCallCount()).To(Equal(1)) 174 expectedOrgName := fakeActor.CreateOrganizationArgsForCall(0) 175 Expect(expectedOrgName).To(Equal(orgName)) 176 }) 177 178 It("applies he quota to the org", func() { 179 passedQuotaName, passedGuid := fakeActor.ApplyOrganizationQuotaByNameArgsForCall(0) 180 Expect(passedQuotaName).To(Equal(quotaName)) 181 Expect(passedGuid).To(Equal(orgGUID)) 182 }) 183 }) 184 })