github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/organization/create_org_test.go (about) 1 package organization_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/commands/user/userfakes" 5 "code.cloudfoundry.org/cli/cf/errors" 6 "code.cloudfoundry.org/cli/cf/models" 7 "code.cloudfoundry.org/cli/cf/requirements" 8 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 9 10 "code.cloudfoundry.org/cli/cf/api/featureflags/featureflagsfakes" 11 "code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes" 12 "code.cloudfoundry.org/cli/cf/api/quotas/quotasfakes" 13 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 14 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 15 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 16 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 17 18 "code.cloudfoundry.org/cli/cf/commandregistry" 19 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 ) 23 24 var _ = Describe("create-org command", func() { 25 var ( 26 config coreconfig.Repository 27 ui *testterm.FakeUI 28 requirementsFactory *requirementsfakes.FakeFactory 29 orgRepo *organizationsfakes.FakeOrganizationRepository 30 quotaRepo *quotasfakes.FakeQuotaRepository 31 deps commandregistry.Dependency 32 orgRoleSetter *userfakes.FakeOrgRoleSetter 33 flagRepo *featureflagsfakes.FakeFeatureFlagRepository 34 OriginalCommand commandregistry.Command 35 ) 36 37 updateCommandDependency := func(pluginCall bool) { 38 deps.UI = ui 39 deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo) 40 deps.RepoLocator = deps.RepoLocator.SetQuotaRepository(quotaRepo) 41 deps.RepoLocator = deps.RepoLocator.SetFeatureFlagRepository(flagRepo) 42 deps.Config = config 43 44 //inject fake 'command dependency' into registry 45 commandregistry.Register(orgRoleSetter) 46 47 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-org").SetDependency(deps, pluginCall)) 48 } 49 50 BeforeEach(func() { 51 ui = &testterm.FakeUI{} 52 config = testconfig.NewRepositoryWithDefaults() 53 requirementsFactory = new(requirementsfakes.FakeFactory) 54 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 55 orgRepo = new(organizationsfakes.FakeOrganizationRepository) 56 quotaRepo = new(quotasfakes.FakeQuotaRepository) 57 flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository) 58 config.SetAPIVersion("2.36.9") 59 60 orgRoleSetter = new(userfakes.FakeOrgRoleSetter) 61 //setup fakes to correctly interact with commandregistry 62 orgRoleSetter.SetDependencyStub = func(_ commandregistry.Dependency, _ bool) commandregistry.Command { 63 return orgRoleSetter 64 } 65 orgRoleSetter.MetaDataReturns(commandregistry.CommandMetadata{Name: "set-org-role"}) 66 67 //save original command and restore later 68 OriginalCommand = commandregistry.Commands.FindCommand("set-org-role") 69 }) 70 71 AfterEach(func() { 72 commandregistry.Register(OriginalCommand) 73 }) 74 75 runCommand := func(args ...string) bool { 76 return testcmd.RunCLICommand("create-org", args, requirementsFactory, updateCommandDependency, false, ui) 77 } 78 79 Describe("requirements", func() { 80 It("fails with usage when not provided exactly one arg", func() { 81 runCommand() 82 Expect(ui.Outputs()).To(ContainSubstrings( 83 []string{"Incorrect Usage", "Requires an argument"}, 84 )) 85 }) 86 87 It("fails when not logged in", func() { 88 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 89 Expect(runCommand("my-org")).To(BeFalse()) 90 }) 91 }) 92 93 Context("when logged in and provided the name of an org to create", func() { 94 BeforeEach(func() { 95 orgRepo.CreateReturns(nil) 96 }) 97 98 It("creates an org", func() { 99 runCommand("my-org") 100 101 Expect(ui.Outputs()).To(ContainSubstrings( 102 []string{"Creating org", "my-org", "my-user"}, 103 []string{"OK"}, 104 []string{`TIP: Use 'cf target -o "my-org"' to target new org`}, 105 )) 106 Expect(orgRepo.CreateArgsForCall(0).Name).To(Equal("my-org")) 107 }) 108 109 It("fails and warns the user when the org already exists", func() { 110 err := errors.NewHTTPError(400, errors.OrganizationNameTaken, "org already exists") 111 orgRepo.CreateReturns(err) 112 runCommand("my-org") 113 114 Expect(ui.Outputs()).To(ContainSubstrings( 115 []string{"Creating org", "my-org"}, 116 []string{"OK"}, 117 []string{"my-org", "already exists"}, 118 )) 119 120 Expect(ui.Outputs()).NotTo(ContainSubstrings( 121 []string{`TIP: Use 'cf target -o "my-org"' to target new org`}, 122 )) 123 }) 124 125 Context("when feature-flag 'set_roles_by_username' is enabled", func() { 126 BeforeEach(func() { 127 flagRepo.FindByNameReturns(models.FeatureFlag{ 128 Name: "set_roles_by_username", 129 Enabled: true, 130 }, nil) 131 orgRepo.FindByNameReturns(models.Organization{ 132 OrganizationFields: models.OrganizationFields{ 133 GUID: "my-org-guid", 134 }, 135 }, nil) 136 }) 137 138 It("assigns manager role to user", func() { 139 runCommand("my-org") 140 141 orgGUID, role, userGUID, userName := orgRoleSetter.SetOrgRoleArgsForCall(0) 142 143 Expect(orgRoleSetter.SetOrgRoleCallCount()).To(Equal(1)) 144 Expect(orgGUID).To(Equal("my-org-guid")) 145 Expect(role).To(Equal(models.RoleOrgManager)) 146 Expect(userGUID).To(Equal("")) 147 Expect(userName).To(Equal("my-user")) 148 }) 149 150 It("warns user about problem accessing feature-flag", func() { 151 flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("error error error")) 152 153 runCommand("my-org") 154 155 Expect(orgRoleSetter.SetOrgRoleCallCount()).To(Equal(0)) 156 Expect(ui.Outputs()).To(ContainSubstrings( 157 []string{"Warning", "error error error"}, 158 )) 159 160 }) 161 162 It("fails on failing getting the guid of the newly created org", func() { 163 orgRepo.FindByNameReturns(models.Organization{}, errors.New("cannot get org guid")) 164 165 runCommand("my-org") 166 167 Expect(orgRoleSetter.SetOrgRoleCallCount()).To(Equal(0)) 168 Expect(ui.Outputs()).To(ContainSubstrings( 169 []string{"FAILED"}, 170 []string{"cannot get org guid"}, 171 )) 172 173 }) 174 175 It("fails on failing assigning org role to user", func() { 176 orgRoleSetter.SetOrgRoleReturns(errors.New("failed to assign role")) 177 178 runCommand("my-org") 179 180 Expect(orgRoleSetter.SetOrgRoleCallCount()).To(Equal(1)) 181 Expect(ui.Outputs()).To(ContainSubstrings( 182 []string{"Assigning role OrgManager to user my-user in org my-org ..."}, 183 []string{"FAILED"}, 184 []string{"failed to assign role"}, 185 )) 186 187 }) 188 }) 189 190 Context("when allowing a non-defualt quota", func() { 191 var ( 192 quota models.QuotaFields 193 ) 194 195 BeforeEach(func() { 196 quota = models.QuotaFields{ 197 Name: "non-default-quota", 198 GUID: "non-default-quota-guid", 199 } 200 }) 201 202 It("creates an org with a non-default quota", func() { 203 quotaRepo.FindByNameReturns(quota, nil) 204 runCommand("-q", "non-default-quota", "my-org") 205 206 Expect(quotaRepo.FindByNameArgsForCall(0)).To(Equal("non-default-quota")) 207 Expect(orgRepo.CreateArgsForCall(0).QuotaDefinition.GUID).To(Equal("non-default-quota-guid")) 208 Expect(ui.Outputs()).To(ContainSubstrings( 209 []string{"Creating org", "my-org"}, 210 []string{"OK"}, 211 )) 212 }) 213 214 It("fails and warns the user when the quota cannot be found", func() { 215 quotaRepo.FindByNameReturns(models.QuotaFields{}, errors.New("Could not find quota")) 216 runCommand("-q", "non-default-quota", "my-org") 217 218 Expect(ui.Outputs()).To(ContainSubstrings( 219 []string{"Creating org", "my-org"}, 220 []string{"Could not find quota"}, 221 )) 222 }) 223 }) 224 }) 225 })