github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/delete_space_test.go (about)

     1  package space_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     7  	"code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  
    19  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    20  )
    21  
    22  var _ = Describe("delete-space command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		space               models.Space
    26  		config              coreconfig.Repository
    27  		spaceRepo           *spacesfakes.FakeSpaceRepository
    28  		orgRepo             *organizationsfakes.FakeOrganizationRepository
    29  		requirementsFactory *requirementsfakes.FakeFactory
    30  		deps                commandregistry.Dependency
    31  	)
    32  
    33  	updateCommandDependency := func(pluginCall bool) {
    34  		deps.UI = ui
    35  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    36  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    37  		deps.Config = config
    38  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-space").SetDependency(deps, pluginCall))
    39  	}
    40  
    41  	runCommand := func(args ...string) bool {
    42  		return testcmd.RunCLICommand("delete-space", args, requirementsFactory, updateCommandDependency, false, ui)
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		ui = &testterm.FakeUI{}
    47  		spaceRepo = new(spacesfakes.FakeSpaceRepository)
    48  		orgRepo = new(organizationsfakes.FakeOrganizationRepository)
    49  		config = testconfig.NewRepositoryWithDefaults()
    50  
    51  		space = models.Space{SpaceFields: models.SpaceFields{
    52  			Name: "space-to-delete",
    53  			GUID: "space-to-delete-guid",
    54  		}}
    55  
    56  		requirementsFactory = new(requirementsfakes.FakeFactory)
    57  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    58  		requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    59  		spaceReq := new(requirementsfakes.FakeSpaceRequirement)
    60  		spaceReq.GetSpaceReturns(space)
    61  		requirementsFactory.NewSpaceRequirementReturns(spaceReq)
    62  	})
    63  
    64  	Describe("requirements", func() {
    65  		BeforeEach(func() {
    66  			ui.Inputs = []string{"y"}
    67  		})
    68  		It("fails when not logged in", func() {
    69  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    70  
    71  			Expect(runCommand("my-space")).To(BeFalse())
    72  		})
    73  
    74  		It("fails when not targeting an org and not providing -o", func() {
    75  			targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    76  			targetedOrgReq.ExecuteReturns(errors.New("no org targeted"))
    77  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    78  
    79  			Expect(runCommand("my-space")).To(BeFalse())
    80  		})
    81  
    82  		It("succeeds if you use the -o flag but don't have an org targeted", func() {
    83  			targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    84  			targetedOrgReq.ExecuteReturns(errors.New("no org targeted"))
    85  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    86  
    87  			Expect(runCommand("-o", "other-org", "my-space")).To(BeTrue())
    88  		})
    89  	})
    90  
    91  	It("deletes a space, given its name", func() {
    92  		ui.Inputs = []string{"yes"}
    93  		runCommand("space-to-delete")
    94  
    95  		Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the space space-to-delete"}))
    96  		Expect(ui.Outputs()).To(ContainSubstrings(
    97  			[]string{"Deleting space", "space-to-delete", "my-org", "my-user"},
    98  			[]string{"OK"},
    99  		))
   100  		Expect(spaceRepo.DeleteArgsForCall(0)).To(Equal("space-to-delete-guid"))
   101  		Expect(config.HasSpace()).To(Equal(true))
   102  	})
   103  
   104  	It("does not prompt when the -f flag is given", func() {
   105  		runCommand("-f", "space-to-delete")
   106  
   107  		Expect(ui.Prompts).To(BeEmpty())
   108  		Expect(ui.Outputs()).To(ContainSubstrings(
   109  			[]string{"Deleting", "space-to-delete"},
   110  			[]string{"OK"},
   111  		))
   112  		Expect(spaceRepo.DeleteArgsForCall(0)).To(Equal("space-to-delete-guid"))
   113  	})
   114  
   115  	It("deletes a space in a different org, given the dash-o flag and a space-name", func() {
   116  		otherSpace := models.Space{
   117  			SpaceFields: models.SpaceFields{
   118  				Name: "other-space-to-delete",
   119  				GUID: "other-space-to-delete-guid",
   120  			}}
   121  
   122  		otherOrg := models.Organization{
   123  			OrganizationFields: models.OrganizationFields{
   124  				Name: "other-org",
   125  				GUID: "other-org-guid",
   126  			}}
   127  		orgRepo.FindByNameReturns(otherOrg, nil)
   128  		spaceRepo.FindByNameInOrgReturns(otherSpace, nil)
   129  
   130  		ui.Inputs = []string{"yes"}
   131  		runCommand("-o", "other-org", "other-space-to-delete")
   132  
   133  		Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the space other-space-to-delete"}))
   134  		Expect(ui.Outputs()).To(ContainSubstrings(
   135  			[]string{"Deleting space", "space-to-delete", "other-org", "my-user"},
   136  			[]string{"OK"},
   137  		))
   138  
   139  		Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("other-org"))
   140  
   141  		spaceArg, orgArg := spaceRepo.FindByNameInOrgArgsForCall(0)
   142  		Expect(spaceArg).To(Equal("other-space-to-delete"))
   143  		Expect(orgArg).To(Equal("other-org-guid"))
   144  		Expect(spaceRepo.DeleteArgsForCall(0)).To(Equal("other-space-to-delete-guid"))
   145  		Expect(config.HasSpace()).To(Equal(true))
   146  	})
   147  
   148  	It("clears the space from the config, when deleting the space currently targeted", func() {
   149  		config.SetSpaceFields(space.SpaceFields)
   150  		runCommand("-f", "space-to-delete")
   151  
   152  		Expect(config.HasSpace()).To(Equal(false))
   153  	})
   154  
   155  	It("clears the space from the config, when deleting the space currently targeted even if space name is case insensitive", func() {
   156  		config.SetSpaceFields(space.SpaceFields)
   157  		runCommand("-f", "Space-To-Delete")
   158  
   159  		Expect(config.HasSpace()).To(Equal(false))
   160  	})
   161  })