github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/space/create_space_test.go (about) 1 package space_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/api/featureflags/featureflagsfakes" 6 "code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes" 7 "code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes" 8 "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 9 "code.cloudfoundry.org/cli/cf/commandregistry" 10 "code.cloudfoundry.org/cli/cf/commands/user" 11 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 12 "code.cloudfoundry.org/cli/cf/errors" 13 "code.cloudfoundry.org/cli/cf/models" 14 "code.cloudfoundry.org/cli/cf/requirements" 15 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 16 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 17 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 18 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 19 20 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("create-space command", func() { 26 var ( 27 ui *testterm.FakeUI 28 requirementsFactory *requirementsfakes.FakeFactory 29 configOrg models.OrganizationFields 30 configRepo coreconfig.Repository 31 spaceRepo *spacesfakes.FakeSpaceRepository 32 orgRepo *organizationsfakes.FakeOrganizationRepository 33 userRepo *apifakes.FakeUserRepository 34 spaceRoleSetter user.SpaceRoleSetter 35 flagRepo *featureflagsfakes.FakeFeatureFlagRepository 36 spaceQuotaRepo *spacequotasfakes.FakeSpaceQuotaRepository 37 OriginalCommand commandregistry.Command 38 deps commandregistry.Dependency 39 ) 40 41 updateCommandDependency := func(pluginCall bool) { 42 deps.UI = ui 43 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 44 deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(spaceQuotaRepo) 45 deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo) 46 deps.RepoLocator = deps.RepoLocator.SetFeatureFlagRepository(flagRepo) 47 deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo) 48 deps.Config = configRepo 49 50 //inject fake 'command dependency' into registry 51 commandregistry.Register(spaceRoleSetter) 52 53 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-space").SetDependency(deps, pluginCall)) 54 } 55 56 runCommand := func(args ...string) bool { 57 return testcmd.RunCLICommand("create-space", args, requirementsFactory, updateCommandDependency, false, ui) 58 } 59 60 BeforeEach(func() { 61 ui = &testterm.FakeUI{} 62 configRepo = testconfig.NewRepositoryWithDefaults() 63 64 orgRepo = new(organizationsfakes.FakeOrganizationRepository) 65 userRepo = new(apifakes.FakeUserRepository) 66 spaceRoleSetter = commandregistry.Commands.FindCommand("set-space-role").(user.SpaceRoleSetter) 67 spaceQuotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository) 68 flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository) 69 70 requirementsFactory = new(requirementsfakes.FakeFactory) 71 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 72 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 73 configOrg = models.OrganizationFields{ 74 Name: "my-org", 75 GUID: "my-org-guid", 76 } 77 78 //save original command and restore later 79 OriginalCommand = commandregistry.Commands.FindCommand("set-space-role") 80 81 spaceRepo = new(spacesfakes.FakeSpaceRepository) 82 space := models.Space{SpaceFields: models.SpaceFields{ 83 Name: "my-space", 84 GUID: "my-space-guid", 85 }} 86 spaceRepo.CreateReturns(space, nil) 87 }) 88 89 AfterEach(func() { 90 commandregistry.Register(OriginalCommand) 91 }) 92 93 Describe("Requirements", func() { 94 It("fails with usage when not provided exactly one argument", func() { 95 runCommand() 96 Expect(ui.Outputs()).To(ContainSubstrings( 97 []string{"Incorrect Usage", "Requires", "argument"}, 98 )) 99 }) 100 101 Context("when not logged in", func() { 102 BeforeEach(func() { 103 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 104 }) 105 106 It("fails requirements", func() { 107 Expect(runCommand("some-space")).To(BeFalse()) 108 }) 109 }) 110 111 Context("when a org is not targeted", func() { 112 BeforeEach(func() { 113 targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement) 114 targetedOrgReq.ExecuteReturns(errors.New("no org targeted")) 115 requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq) 116 }) 117 118 It("fails requirements", func() { 119 Expect(runCommand("what-is-space?")).To(BeFalse()) 120 }) 121 }) 122 }) 123 124 It("creates a space", func() { 125 runCommand("my-space") 126 Expect(ui.Outputs()).To(ContainSubstrings( 127 []string{"Creating space", "my-space", "my-org", "my-user"}, 128 []string{"OK"}, 129 []string{"Assigning", "SpaceManager", "my-user", "my-space"}, 130 []string{"Assigning", "SpaceDeveloper", "my-user", "my-space"}, 131 []string{"TIP"}, 132 )) 133 134 name, orgGUID, _ := spaceRepo.CreateArgsForCall(0) 135 Expect(name).To(Equal("my-space")) 136 Expect(orgGUID).To(Equal("my-org-guid")) 137 138 userGUID, spaceGUID, orgGUID, role := userRepo.SetSpaceRoleByGUIDArgsForCall(0) 139 Expect(userGUID).To(Equal("my-user-guid")) 140 Expect(spaceGUID).To(Equal("my-space-guid")) 141 Expect(orgGUID).To(Equal("my-org-guid")) 142 Expect(role).To(Equal(models.RoleSpaceManager)) 143 }) 144 145 It("warns the user when a space with that name already exists", func() { 146 spaceRepo.CreateReturns(models.Space{}, errors.NewHTTPError(400, errors.SpaceNameTaken, "Space already exists")) 147 runCommand("my-space") 148 149 Expect(ui.Outputs()).To(ContainSubstrings( 150 []string{"Creating space", "my-space"}, 151 []string{"OK"}, 152 )) 153 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-space", "already exists"})) 154 Expect(ui.Outputs()).ToNot(ContainSubstrings( 155 []string{"Assigning", "my-user", "my-space", "SpaceManager"}, 156 )) 157 158 Expect(spaceRepo.CreateCallCount()).To(Equal(1)) 159 actualSpaceName, _, _ := spaceRepo.CreateArgsForCall(0) 160 Expect(actualSpaceName).To(Equal("my-space")) 161 Expect(userRepo.SetSpaceRoleByGUIDCallCount()).To(BeZero()) 162 }) 163 164 Context("when the -o flag is provided", func() { 165 It("creates a space within that org", func() { 166 org := models.Organization{ 167 OrganizationFields: models.OrganizationFields{ 168 Name: "other-org", 169 GUID: "org-guid-1", 170 }} 171 orgRepo.FindByNameReturns(org, nil) 172 173 runCommand("-o", "other-org", "my-space") 174 175 Expect(ui.Outputs()).To(ContainSubstrings( 176 []string{"Creating space", "my-space", "other-org", "my-user"}, 177 []string{"OK"}, 178 []string{"Assigning", "my-user", "my-space", "SpaceManager"}, 179 []string{"Assigning", "my-user", "my-space", "SpaceDeveloper"}, 180 []string{"TIP"}, 181 )) 182 183 actualSpaceName, actualOrgGUID, _ := spaceRepo.CreateArgsForCall(0) 184 Expect(actualSpaceName).To(Equal("my-space")) 185 Expect(actualOrgGUID).To(Equal(org.GUID)) 186 187 userGUID, spaceGUID, orgGUID, role := userRepo.SetSpaceRoleByGUIDArgsForCall(0) 188 Expect(userGUID).To(Equal("my-user-guid")) 189 Expect(spaceGUID).To(Equal("my-space-guid")) 190 Expect(orgGUID).To(Equal("org-guid-1")) 191 Expect(role).To(Equal(models.RoleSpaceManager)) 192 }) 193 194 It("fails when the org provided does not exist", func() { 195 orgRepo.FindByNameReturns(models.Organization{}, errors.New("cool-organization does not exist")) 196 runCommand("-o", "cool-organization", "my-space") 197 198 Expect(ui.Outputs()).To(ContainSubstrings( 199 []string{"FAILED"}, 200 []string{"cool-organization", "does not exist"}, 201 )) 202 203 Expect(spaceRepo.CreateCallCount()).To(BeZero()) 204 }) 205 206 It("fails when finding the org returns an error", func() { 207 orgRepo.FindByNameReturns(models.Organization{}, errors.New("cool-organization does not exist")) 208 runCommand("-o", "cool-organization", "my-space") 209 210 Expect(ui.Outputs()).To(ContainSubstrings( 211 []string{"FAILED"}, 212 []string{"Error"}, 213 )) 214 215 Expect(spaceRepo.CreateCallCount()).To(BeZero()) 216 }) 217 }) 218 219 Context("when the -q flag is provided", func() { 220 It("assigns the space-quota specified to the space", func() { 221 spaceQuota := models.SpaceQuota{ 222 Name: "my-space-quota", 223 GUID: "my-space-quota-guid", 224 } 225 spaceQuotaRepo.FindByNameAndOrgGUIDReturns(spaceQuota, nil) 226 runCommand("-q", "my-space-quota", "my-space") 227 228 spaceQuotaName, orgGUID := spaceQuotaRepo.FindByNameAndOrgGUIDArgsForCall(0) 229 230 Expect(spaceQuotaName).To(Equal(spaceQuota.Name)) 231 Expect(orgGUID).To(Equal(configOrg.GUID)) 232 233 _, _, actualSpaceQuotaGUID := spaceRepo.CreateArgsForCall(0) 234 Expect(actualSpaceQuotaGUID).To(Equal(spaceQuota.GUID)) 235 }) 236 237 Context("when the space-quota provided does not exist", func() { 238 It("fails", func() { 239 spaceQuotaRepo.FindByNameAndOrgGUIDReturns(models.SpaceQuota{}, errors.New("Error")) 240 runCommand("-q", "my-space-quota", "my-space") 241 242 Expect(ui.Outputs()).To(ContainSubstrings( 243 []string{"FAILED"}, 244 []string{"Error"}, 245 )) 246 }) 247 }) 248 }) 249 250 Context("when the -o and -q flags are provided", func() { 251 BeforeEach(func() { 252 org := models.Organization{ 253 OrganizationFields: models.OrganizationFields{ 254 Name: "other-org", 255 GUID: "other-org-guid", 256 }} 257 orgRepo.FindByNameReturns(org, nil) 258 259 spaceQuota := models.SpaceQuota{ 260 Name: "my-space-quota", 261 GUID: "my-space-quota-guid", 262 } 263 spaceQuotaRepo.FindByNameAndOrgGUIDReturns(spaceQuota, nil) 264 }) 265 266 It("assigns the space-quota from the specified org to the space", func() { 267 runCommand("my-space", "-o", "other-org", "-q", "my-space-quota") 268 269 Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("other-org")) 270 spaceName, orgGUID := spaceQuotaRepo.FindByNameAndOrgGUIDArgsForCall(0) 271 Expect(spaceName).To(Equal("my-space-quota")) 272 Expect(orgGUID).To(Equal("other-org-guid")) 273 274 actualSpaceName, actualOrgGUID, actualSpaceQuotaGUID := spaceRepo.CreateArgsForCall(0) 275 Expect(actualSpaceName).To(Equal("my-space")) 276 Expect(actualOrgGUID).To(Equal("other-org-guid")) 277 Expect(actualSpaceQuotaGUID).To(Equal("my-space-quota-guid")) 278 }) 279 }) 280 })