. "github.com/onsi/gomega" ) var _ = Describe("SpaceRequirement", func() { var ( ui *testterm.FakeUI ) BeforeEach(func() { ui = new(testterm.FakeUI) }) Context("when a space with the given name exists", func() { It("succeeds", func() { space := models.Space{} space.Name = "awesome-sauce-space" space.Guid = "my-space-guid" spaceRepo := &testapi.FakeSpaceRepository{Spaces: []models.Space{space}} spaceReq := NewSpaceRequirement("awesome-sauce-space", ui, spaceRepo) Expect(spaceReq.Execute()).To(BeTrue()) Expect(spaceRepo.FindByNameName).To(Equal("awesome-sauce-space")) Expect(spaceReq.GetSpace()).To(Equal(space)) }) }) Context("when a space with the given name does not exist", func() { It("fails", func() { spaceRepo := &testapi.FakeSpaceRepository{FindByNameNotFound: true} testassert.AssertPanic(testterm.QuietPanic, func() {
requirementsFactory.TargetedOrgSuccess = true runCommand() Expect(testcmd.CommandDidPassRequirements).To(BeFalse()) }) It("fails when an org is not targeted", func() { requirementsFactory.LoginSuccess = true runCommand() Expect(testcmd.CommandDidPassRequirements).To(BeFalse()) }) }) Context("when logged in and an org is targeted", func() { BeforeEach(func() { space := models.Space{} space.Name = "space1" space2 := models.Space{} space2.Name = "space2" space3 := models.Space{} space3.Name = "space3" spaceRepo.Spaces = []models.Space{space, space2, space3} requirementsFactory.LoginSuccess = true requirementsFactory.TargetedOrgSuccess = true }) It("lists all of the spaces", func() { runCommand() Expect(ui.Outputs).To(ContainSubstrings( []string{"Getting spaces in org", "my-org", "my-user"}, []string{"space1"},
}) It("fails with usage when not invoked with exactly two args", func() { runCommand("my-org") Expect(ui.FailedWithUsage).To(BeTrue()) }) Context("when logged in and given some users in the org and space", func() { BeforeEach(func() { requirementsFactory.LoginSuccess = true org := models.Organization{} org.Name = "Org1" org.Guid = "org1-guid" space := models.Space{} space.Name = "Space1" space.Guid = "space1-guid" requirementsFactory.Organization = org spaceRepo.FindByNameInOrgSpace = space user := models.UserFields{} user.Username = "******" user2 := models.UserFields{} user2.Username = "******" user3 := models.UserFields{} user3.Username = "******" user4 := models.UserFields{} user4.Username = "******" userRepo.ListUsersByRole = map[string][]models.UserFields{ models.SPACE_MANAGER: []models.UserFields{user, user2},
AccessToken: "my_access_token", RefreshToken: "my_refresh_token", Config: Config, } endpointRepo = &testapi.FakeEndpointRepo{} org = models.Organization{} org.Name = "my-new-org" org.Guid = "my-new-org-guid" orgRepo = &fake_organizations.FakeOrganizationRepository{} orgRepo.ListOrgsReturns([]models.Organization{org}, nil) space := models.Space{} space.Guid = "my-space-guid" space.Name = "my-space" spaceRepo = &testapi.FakeSpaceRepository{ Spaces: []models.Space{space}, } authRepo.GetLoginPromptsReturns.Prompts = map[string]core_config.AuthPrompt{ "username": core_config.AuthPrompt{ DisplayName: "Username", Type: core_config.AuthPromptTypeText, }, "password": core_config.AuthPrompt{ DisplayName: "Password", Type: core_config.AuthPromptTypePassword, }, }
callRenameSpace([]string{"my-space", "my-new-space"}) Expect(testcmd.CommandDidPassRequirements).To(BeFalse()) }) }) Describe("when the user provides fewer than two args", func() { It("fails with usage", func() { callRenameSpace([]string{"foo"}) Expect(ui.FailedWithUsage).To(BeTrue()) }) }) Describe("when the user is logged in and has provided an old and new space name", func() { BeforeEach(func() { space := models.Space{} space.Name = "the-old-space-name" space.Guid = "the-old-space-guid" requirementsFactory.Space = space }) It("renames a space", func() { originalSpaceName := configRepo.SpaceFields().Name callRenameSpace([]string{"the-old-space-name", "my-new-space"}) Expect(ui.Outputs).To(ContainSubstrings( []string{"Renaming space", "the-old-space-name", "my-new-space", "my-org", "my-user"}, []string{"OK"}, )) Expect(spaceRepo.RenameSpaceGuid).To(Equal("the-old-space-guid")) Expect(spaceRepo.RenameNewName).To(Equal("my-new-space"))
Expect(ui.Outputs).To(ContainSubstrings( []string{"FAILED"}, []string{"Space", "space-name", "not found"}, )) }) }) Context("everything is hunky dory", func() { BeforeEach(func() { org := models.Organization{} org.Name = "org-name" org.Guid = "org-guid" fakeOrgRepo.ListOrgsReturns([]models.Organization{org}, nil) space := models.Space{} space.Name = "space-name" space.Guid = "space-guid" fakeSpaceRepo.FindByNameInOrgSpace = space securityGroup := models.SecurityGroup{} securityGroup.Name = "security-group" securityGroup.Guid = "security-group-guid" fakeSecurityGroupRepo.ReadReturns(securityGroup, nil) }) JustBeforeEach(func() { runCommand("security-group", "org-name", "space-name") }) It("assigns the security group to the space", func() { secGroupGuid, spaceGuid := fakeSpaceBinder.BindSpaceArgsForCall(0)
domain := models.DomainFields{} domain.Name = "domain1" domain.Guid = "domain1-guid" domains := []models.DomainFields{domain} serviceInstance := models.ServiceInstanceFields{} serviceInstance.Name = "service1" serviceInstance.Guid = "service1-guid" services := []models.ServiceInstanceFields{serviceInstance} securityGroup1 := models.SecurityGroupFields{Name: "Nacho Security"} securityGroup2 := models.SecurityGroupFields{Name: "Nacho Prime"} securityGroups := []models.SecurityGroupFields{securityGroup1, securityGroup2} space := models.Space{} space.Name = "whose-space-is-it-anyway" space.Organization = org space.Applications = apps space.Domains = domains space.ServiceInstances = services space.SecurityGroups = securityGroups space.SpaceQuotaGuid = "runaway-guid" quota := models.SpaceQuota{} quota.Guid = "runaway-guid" quota.Name = "runaway" quota.MemoryLimit = 102400 quota.InstanceMemoryLimit = -1 quota.RoutesLimit = 111 quota.ServicesLimit = 222 quota.NonBasicServicesAllowed = false
orgRepo.ListOrgsReturns([]models.Organization{org}, nil) orgRepo.FindByNameReturns(org, nil) }) It("it updates the organization in the config", func() { callTarget([]string{"-o", "my-organization"}) Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-organization")) Expect(ui.ShowConfigurationCalled).To(BeTrue()) Expect(config.OrganizationFields().Guid).To(Equal("my-organization-guid")) }) It("updates the space in the config", func() { space := models.Space{} space.Name = "my-space" space.Guid = "my-space-guid" spaceRepo.Spaces = []models.Space{space} spaceRepo.FindByNameSpace = space callTarget([]string{"-s", "my-space"}) Expect(spaceRepo.FindByNameName).To(Equal("my-space")) Expect(config.SpaceFields().Guid).To(Equal("my-space-guid")) Expect(ui.ShowConfigurationCalled).To(BeTrue()) }) It("updates both the organization and the space in the config", func() { space := models.Space{} space.Name = "my-space"