github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/delete_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	testApplication "github.com/cloudfoundry/cli/cf/api/applications/fakes"
     5  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     6  	. "github.com/cloudfoundry/cli/cf/commands/application"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/errors"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    18  )
    19  
    20  var _ = Describe("delete app command", func() {
    21  	var (
    22  		cmd                 *DeleteApp
    23  		ui                  *testterm.FakeUI
    24  		app                 models.Application
    25  		configRepo          core_config.ReadWriter
    26  		appRepo             *testApplication.FakeApplicationRepository
    27  		routeRepo           *testapi.FakeRouteRepository
    28  		requirementsFactory *testreq.FakeReqFactory
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		app = models.Application{}
    33  		app.Name = "app-to-delete"
    34  		app.Guid = "app-to-delete-guid"
    35  
    36  		ui = &testterm.FakeUI{}
    37  		appRepo = &testApplication.FakeApplicationRepository{}
    38  		routeRepo = &testapi.FakeRouteRepository{}
    39  		requirementsFactory = &testreq.FakeReqFactory{}
    40  
    41  		configRepo = testconfig.NewRepositoryWithDefaults()
    42  		cmd = NewDeleteApp(ui, configRepo, appRepo, routeRepo)
    43  	})
    44  
    45  	runCommand := func(args ...string) bool {
    46  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    47  	}
    48  
    49  	It("fails requirements when not logged in", func() {
    50  		requirementsFactory.LoginSuccess = false
    51  		Expect(runCommand("-f", "delete-this-app-plz")).To(BeFalse())
    52  	})
    53  
    54  	Context("when logged in", func() {
    55  		BeforeEach(func() {
    56  			requirementsFactory.LoginSuccess = true
    57  		})
    58  
    59  		It("fails with usage when not provided exactly one arg", func() {
    60  			runCommand()
    61  			Expect(ui.FailedWithUsage).To(BeTrue())
    62  		})
    63  
    64  		Context("When provided an app that exists", func() {
    65  			BeforeEach(func() {
    66  				appRepo.ReadReturns.App = app
    67  			})
    68  
    69  			It("deletes an app when the user confirms", func() {
    70  				ui.Inputs = []string{"y"}
    71  
    72  				runCommand("app-to-delete")
    73  
    74  				Expect(appRepo.ReadArgs.Name).To(Equal("app-to-delete"))
    75  				Expect(appRepo.DeletedAppGuid).To(Equal("app-to-delete-guid"))
    76  
    77  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the app app-to-delete"}))
    78  
    79  				Expect(ui.Outputs).To(ContainSubstrings(
    80  					[]string{"Deleting", "app-to-delete", "my-org", "my-space", "my-user"},
    81  					[]string{"OK"},
    82  				))
    83  			})
    84  
    85  			It("does not prompt when the -f flag is provided", func() {
    86  				runCommand("-f", "app-to-delete")
    87  
    88  				Expect(appRepo.ReadArgs.Name).To(Equal("app-to-delete"))
    89  				Expect(appRepo.DeletedAppGuid).To(Equal("app-to-delete-guid"))
    90  				Expect(ui.Prompts).To(BeEmpty())
    91  
    92  				Expect(ui.Outputs).To(ContainSubstrings(
    93  					[]string{"Deleting", "app-to-delete"},
    94  					[]string{"OK"},
    95  				))
    96  			})
    97  
    98  			Describe("mapped routes", func() {
    99  				BeforeEach(func() {
   100  					route1 := models.RouteSummary{}
   101  					route1.Guid = "the-first-route-guid"
   102  					route1.Host = "my-app-is-good.com"
   103  
   104  					route2 := models.RouteSummary{}
   105  					route2.Guid = "the-second-route-guid"
   106  					route2.Host = "my-app-is-bad.com"
   107  
   108  					appRepo.ReadReturns.App = models.Application{
   109  						Routes: []models.RouteSummary{route1, route2},
   110  					}
   111  				})
   112  
   113  				Context("when the -r flag is provided", func() {
   114  					Context("when deleting routes succeeds", func() {
   115  						It("deletes the app's routes", func() {
   116  							runCommand("-f", "-r", "app-to-delete")
   117  
   118  							Expect(routeRepo.DeletedRouteGuids).To(ContainElement("the-first-route-guid"))
   119  							Expect(routeRepo.DeletedRouteGuids).To(ContainElement("the-second-route-guid"))
   120  						})
   121  					})
   122  
   123  					Context("when deleting routes fails", func() {
   124  						BeforeEach(func() {
   125  							routeRepo.DeleteErr = errors.New("badness")
   126  						})
   127  
   128  						It("fails with the api error message", func() {
   129  							runCommand("-f", "-r", "app-to-delete")
   130  
   131  							Expect(ui.Outputs).To(ContainSubstrings(
   132  								[]string{"Deleting", "app-to-delete"},
   133  								[]string{"FAILED"},
   134  							))
   135  						})
   136  					})
   137  				})
   138  
   139  				Context("when the -r flag is not provided", func() {
   140  					It("does not delete mapped routes", func() {
   141  						runCommand("-f", "app-to-delete")
   142  						Expect(routeRepo.DeletedRouteGuids).To(BeEmpty())
   143  					})
   144  				})
   145  			})
   146  		})
   147  
   148  		Context("when the app provided is not found", func() {
   149  			BeforeEach(func() {
   150  				appRepo.ReadReturns.Error = errors.NewModelNotFoundError("App", "the-app")
   151  			})
   152  
   153  			It("warns the user when the provided app does not exist", func() {
   154  				runCommand("-f", "app-to-delete")
   155  
   156  				Expect(appRepo.ReadArgs.Name).To(Equal("app-to-delete"))
   157  				Expect(appRepo.DeletedAppGuid).To(Equal(""))
   158  
   159  				Expect(ui.Outputs).To(ContainSubstrings(
   160  					[]string{"Deleting", "app-to-delete"},
   161  					[]string{"OK"},
   162  				))
   163  
   164  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"app-to-delete", "does not exist"}))
   165  			})
   166  		})
   167  	})
   168  })