github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/delete_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes"
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("delete app command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		app                 models.Application
    25  		configRepo          coreconfig.Repository
    26  		appRepo             *applicationsfakes.FakeRepository
    27  		routeRepo           *apifakes.FakeRouteRepository
    28  		requirementsFactory *requirementsfakes.FakeFactory
    29  		deps                commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.Config = configRepo
    35  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    36  		deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo)
    37  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		app = models.Application{}
    42  		app.Name = "app-to-delete"
    43  		app.GUID = "app-to-delete-guid"
    44  
    45  		ui = &testterm.FakeUI{}
    46  		appRepo = new(applicationsfakes.FakeRepository)
    47  		routeRepo = new(apifakes.FakeRouteRepository)
    48  		requirementsFactory = new(requirementsfakes.FakeFactory)
    49  
    50  		configRepo = testconfig.NewRepositoryWithDefaults()
    51  	})
    52  
    53  	runCommand := func(args ...string) bool {
    54  		return testcmd.RunCLICommand("delete", args, requirementsFactory, updateCommandDependency, false, ui)
    55  	}
    56  
    57  	It("fails requirements when not logged in", func() {
    58  		requirementsFactory.NewUsageRequirementReturns(requirements.Passing{})
    59  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    60  		Expect(runCommand("-f", "delete-this-app-plz")).To(BeFalse())
    61  	})
    62  
    63  	It("fails if a space is not targeted", func() {
    64  		requirementsFactory.NewUsageRequirementReturns(requirements.Passing{})
    65  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    66  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    67  		Expect(runCommand("-f", "delete-this-app-plz")).To(BeFalse())
    68  	})
    69  
    70  	It("fails with usage when not provided exactly one arg", func() {
    71  		requirementsFactory.NewUsageRequirementReturns(requirements.Failing{})
    72  		Expect(runCommand()).To(BeFalse())
    73  	})
    74  
    75  	Context("when passing requirements", func() {
    76  		BeforeEach(func() {
    77  			requirementsFactory.NewUsageRequirementReturns(requirements.Passing{})
    78  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    79  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    80  		})
    81  
    82  		Context("When provided an app that exists", func() {
    83  			BeforeEach(func() {
    84  				appRepo.ReadReturns(app, nil)
    85  			})
    86  
    87  			It("deletes an app when the user confirms", func() {
    88  				ui.Inputs = []string{"y"}
    89  
    90  				runCommand("app-to-delete")
    91  
    92  				Expect(appRepo.ReadArgsForCall(0)).To(Equal("app-to-delete"))
    93  				Expect(appRepo.DeleteArgsForCall(0)).To(Equal("app-to-delete-guid"))
    94  
    95  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the app app-to-delete"}))
    96  
    97  				Expect(ui.Outputs()).To(ContainSubstrings(
    98  					[]string{"Deleting", "app-to-delete", "my-org", "my-space", "my-user"},
    99  					[]string{"OK"},
   100  				))
   101  			})
   102  
   103  			It("does not prompt when the -f flag is provided", func() {
   104  				runCommand("-f", "app-to-delete")
   105  
   106  				Expect(appRepo.ReadArgsForCall(0)).To(Equal("app-to-delete"))
   107  				Expect(appRepo.DeleteArgsForCall(0)).To(Equal("app-to-delete-guid"))
   108  				Expect(ui.Prompts).To(BeEmpty())
   109  
   110  				Expect(ui.Outputs()).To(ContainSubstrings(
   111  					[]string{"Deleting", "app-to-delete"},
   112  					[]string{"OK"},
   113  				))
   114  			})
   115  
   116  			Describe("mapped routes", func() {
   117  				BeforeEach(func() {
   118  					route1 := models.RouteSummary{}
   119  					route1.GUID = "the-first-route-guid"
   120  					route1.Host = "my-app-is-good.com"
   121  
   122  					route2 := models.RouteSummary{}
   123  					route2.GUID = "the-second-route-guid"
   124  					route2.Host = "my-app-is-bad.com"
   125  
   126  					appRepo.ReadReturns(models.Application{
   127  						Routes: []models.RouteSummary{route1, route2},
   128  					}, nil)
   129  				})
   130  
   131  				Context("when the -r flag is provided", func() {
   132  					Context("when deleting routes succeeds", func() {
   133  						It("deletes the app's routes", func() {
   134  							runCommand("-f", "-r", "app-to-delete")
   135  
   136  							Expect(routeRepo.DeleteCallCount()).To(Equal(2))
   137  							Expect(routeRepo.DeleteArgsForCall(0)).To(Equal("the-first-route-guid"))
   138  							Expect(routeRepo.DeleteArgsForCall(1)).To(Equal("the-second-route-guid"))
   139  						})
   140  					})
   141  
   142  					Context("when deleting routes fails", func() {
   143  						BeforeEach(func() {
   144  							routeRepo.DeleteReturns(errors.New("an-error"))
   145  						})
   146  
   147  						It("fails with the api error message", func() {
   148  							runCommand("-f", "-r", "app-to-delete")
   149  
   150  							Expect(ui.Outputs()).To(ContainSubstrings(
   151  								[]string{"Deleting", "app-to-delete"},
   152  								[]string{"FAILED"},
   153  							))
   154  						})
   155  					})
   156  				})
   157  
   158  				Context("when the -r flag is not provided", func() {
   159  					It("does not delete mapped routes", func() {
   160  						runCommand("-f", "app-to-delete")
   161  						Expect(routeRepo.DeleteCallCount()).To(BeZero())
   162  					})
   163  				})
   164  			})
   165  		})
   166  
   167  		Context("when the app provided is not found", func() {
   168  			BeforeEach(func() {
   169  				appRepo.ReadReturns(models.Application{}, errors.NewModelNotFoundError("App", "the-app"))
   170  			})
   171  
   172  			It("warns the user when the provided app does not exist", func() {
   173  				runCommand("-f", "app-to-delete")
   174  
   175  				Expect(appRepo.ReadArgsForCall(0)).To(Equal("app-to-delete"))
   176  				Expect(appRepo.DeleteCallCount()).To(BeZero())
   177  
   178  				Expect(ui.Outputs()).To(ContainSubstrings(
   179  					[]string{"Deleting", "app-to-delete"},
   180  					[]string{"OK"},
   181  				))
   182  
   183  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"app-to-delete", "does not exist"}))
   184  			})
   185  		})
   186  	})
   187  })