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