github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/organization/delete_org_test.go (about)

     1  package organization_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/errors"
     5  	"code.cloudfoundry.org/cli/cf/requirements"
     6  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    15  
    16  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("delete-org command", func() {
    22  	var (
    23  		config              coreconfig.Repository
    24  		ui                  *testterm.FakeUI
    25  		requirementsFactory *requirementsfakes.FakeFactory
    26  		orgRepo             *organizationsfakes.FakeOrganizationRepository
    27  		org                 models.Organization
    28  		deps                commandregistry.Dependency
    29  	)
    30  
    31  	updateCommandDependency := func(pluginCall bool) {
    32  		deps.UI = ui
    33  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    34  		deps.Config = config
    35  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-org").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{
    40  			Inputs: []string{"y"},
    41  		}
    42  		config = testconfig.NewRepositoryWithDefaults()
    43  		requirementsFactory = new(requirementsfakes.FakeFactory)
    44  
    45  		org = models.Organization{}
    46  		org.Name = "org-to-delete"
    47  		org.GUID = "org-to-delete-guid"
    48  		orgRepo = new(organizationsfakes.FakeOrganizationRepository)
    49  
    50  		orgRepo.ListOrgsReturns([]models.Organization{org}, nil)
    51  		orgRepo.FindByNameReturns(org, nil)
    52  	})
    53  
    54  	runCommand := func(args ...string) bool {
    55  		return testcmd.RunCLICommand("delete-org", args, requirementsFactory, updateCommandDependency, false, ui)
    56  	}
    57  
    58  	It("fails requirements when not logged in", func() {
    59  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    60  		Expect(runCommand("some-org-name")).To(BeFalse())
    61  	})
    62  
    63  	It("fails with usage if no arguments are given", func() {
    64  		runCommand()
    65  		Expect(ui.Outputs()).To(ContainSubstrings(
    66  			[]string{"Incorrect Usage", "Requires an argument"},
    67  		))
    68  	})
    69  
    70  	Context("when logged in", func() {
    71  		BeforeEach(func() {
    72  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    73  		})
    74  
    75  		Context("when deleting the currently targeted org", func() {
    76  			It("untargets the deleted org", func() {
    77  				config.SetOrganizationFields(org.OrganizationFields)
    78  				runCommand("org-to-delete")
    79  
    80  				Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{}))
    81  				Expect(config.SpaceFields()).To(Equal(models.SpaceFields{}))
    82  			})
    83  		})
    84  
    85  		Context("when deleting an org that is not targeted", func() {
    86  			BeforeEach(func() {
    87  				otherOrgFields := models.OrganizationFields{}
    88  				otherOrgFields.GUID = "some-other-org-guid"
    89  				otherOrgFields.Name = "some-other-org"
    90  				config.SetOrganizationFields(otherOrgFields)
    91  
    92  				spaceFields := models.SpaceFields{}
    93  				spaceFields.Name = "some-other-space"
    94  				config.SetSpaceFields(spaceFields)
    95  			})
    96  
    97  			It("deletes the org with the given name", func() {
    98  				runCommand("org-to-delete")
    99  
   100  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the org org-to-delete"}))
   101  
   102  				Expect(ui.Outputs()).To(ContainSubstrings(
   103  					[]string{"Deleting", "org-to-delete"},
   104  					[]string{"OK"},
   105  				))
   106  
   107  				Expect(orgRepo.DeleteArgsForCall(0)).To(Equal("org-to-delete-guid"))
   108  			})
   109  
   110  			It("does not untarget the org and space", func() {
   111  				runCommand("org-to-delete")
   112  
   113  				Expect(config.OrganizationFields().Name).To(Equal("some-other-org"))
   114  				Expect(config.SpaceFields().Name).To(Equal("some-other-space"))
   115  			})
   116  		})
   117  
   118  		It("does not prompt when the -f flag is given", func() {
   119  			ui.Inputs = []string{}
   120  			runCommand("-f", "org-to-delete")
   121  
   122  			Expect(ui.Outputs()).To(ContainSubstrings(
   123  				[]string{"Deleting", "org-to-delete"},
   124  				[]string{"OK"},
   125  			))
   126  
   127  			Expect(orgRepo.DeleteArgsForCall(0)).To(Equal("org-to-delete-guid"))
   128  		})
   129  
   130  		It("warns the user when the org does not exist", func() {
   131  			orgRepo.FindByNameReturns(models.Organization{}, errors.NewModelNotFoundError("Organization", "org org-to-delete does not exist"))
   132  
   133  			runCommand("org-to-delete")
   134  
   135  			Expect(orgRepo.DeleteCallCount()).To(Equal(0))
   136  
   137  			Expect(ui.Outputs()).To(ContainSubstrings(
   138  				[]string{"Deleting", "org-to-delete"},
   139  				[]string{"OK"},
   140  			))
   141  			Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"org-to-delete", "does not exist."}))
   142  		})
   143  	})
   144  })