github.com/swisscom/cloudfoundry-cli@v7.1.0+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/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  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/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 the given app 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  				Context("when the -r flag is provided", func() {
   118  					It("gets all the routes for an app", func() {
   119  						runCommand("-f", "-r", "app-to-delete")
   120  						Expect(appRepo.GetAppRoutesCallCount()).To(Equal(1))
   121  
   122  						appGUID := appRepo.GetAppRoutesArgsForCall(0)
   123  						Expect(appGUID).To(Equal(app.GUID))
   124  					})
   125  
   126  					Context("when getting the apps succeeds", func() {
   127  						BeforeEach(func() {
   128  							route1 := models.Route{}
   129  							route1.GUID = "the-first-route-guid"
   130  							route1.Host = "my-app-is-good.com"
   131  
   132  							route2 := models.Route{}
   133  							route2.GUID = "the-second-route-guid"
   134  							route2.Host = "my-app-is-bad.com"
   135  
   136  							appRepo.GetAppRoutesReturns([]models.Route{route1, route2}, nil)
   137  						})
   138  
   139  						Context("when deleting routes succeeds", func() {
   140  							It("deletes the app's routes", func() {
   141  								runCommand("-f", "-r", "app-to-delete")
   142  
   143  								Expect(routeRepo.DeleteCallCount()).To(Equal(2))
   144  								Expect(routeRepo.DeleteArgsForCall(0)).To(Equal("the-first-route-guid"))
   145  								Expect(routeRepo.DeleteArgsForCall(1)).To(Equal("the-second-route-guid"))
   146  							})
   147  						})
   148  
   149  						Context("when deleting routes fails", func() {
   150  							BeforeEach(func() {
   151  								routeRepo.DeleteReturns(errors.New("an-error"))
   152  							})
   153  
   154  							It("fails with the api error message", func() {
   155  								runCommand("-f", "-r", "app-to-delete")
   156  
   157  								Expect(ui.Outputs()).To(ContainSubstrings(
   158  									[]string{"Deleting", "app-to-delete"},
   159  									[]string{"FAILED"},
   160  								))
   161  							})
   162  						})
   163  					})
   164  
   165  					Context("when getting the app routes fails", func() {
   166  						BeforeEach(func() {
   167  							appRepo.GetAppRoutesReturns(nil, errors.New("get-app-routes-failed"))
   168  						})
   169  
   170  						It("returns that error", func() {
   171  							runCommand("-f", "-r", "app-to-delete")
   172  							Expect(ui.Outputs()).To(ContainSubstrings(
   173  								[]string{"Deleting", "app-to-delete"},
   174  								[]string{"FAILED"},
   175  								[]string{"get-app-routes-failed"},
   176  							))
   177  						})
   178  					})
   179  				})
   180  
   181  				Context("when the -r flag is not provided", func() {
   182  					It("does not delete mapped routes", func() {
   183  						runCommand("-f", "app-to-delete")
   184  						Expect(routeRepo.DeleteCallCount()).To(BeZero())
   185  					})
   186  				})
   187  			})
   188  		})
   189  
   190  		Context("when the app provided is not found", func() {
   191  			BeforeEach(func() {
   192  				appRepo.ReadReturns(models.Application{}, errors.NewModelNotFoundError("App", "the-app"))
   193  			})
   194  
   195  			It("warns the user when the provided app does not exist", func() {
   196  				runCommand("-f", "app-to-delete")
   197  
   198  				Expect(appRepo.ReadArgsForCall(0)).To(Equal("app-to-delete"))
   199  				Expect(appRepo.DeleteCallCount()).To(BeZero())
   200  
   201  				Expect(ui.Outputs()).To(ContainSubstrings(
   202  					[]string{"Deleting", "app-to-delete"},
   203  					[]string{"OK"},
   204  				))
   205  
   206  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"app-to-delete", "does not exist"}))
   207  			})
   208  		})
   209  	})
   210  })