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

     1  package route_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/flags"
     6  	"code.cloudfoundry.org/cli/cf/models"
     7  	"code.cloudfoundry.org/cli/cf/requirements"
     8  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
     9  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    10  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    11  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    12  
    13  	"code.cloudfoundry.org/cli/cf/commandregistry"
    14  	"code.cloudfoundry.org/cli/cf/commands/route"
    15  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    16  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("delete-orphaned-routes command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		routeRepo           *apifakes.FakeRouteRepository
    25  		configRepo          coreconfig.Repository
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo)
    33  		deps.Config = configRepo
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-orphaned-routes").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	callDeleteOrphanedRoutes := func(confirmation string, args []string, requirementsFactory *requirementsfakes.FakeFactory, routeRepo *apifakes.FakeRouteRepository) (*testterm.FakeUI, bool) {
    38  		ui = &testterm.FakeUI{Inputs: []string{confirmation}}
    39  		configRepo = testconfig.NewRepositoryWithDefaults()
    40  		passed := testcmd.RunCLICommand("delete-orphaned-routes", args, requirementsFactory, updateCommandDependency, false, ui)
    41  
    42  		return ui, passed
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		routeRepo = new(apifakes.FakeRouteRepository)
    47  		requirementsFactory = new(requirementsfakes.FakeFactory)
    48  	})
    49  
    50  	It("fails requirements when not logged in", func() {
    51  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    52  		_, passed := callDeleteOrphanedRoutes("y", []string{}, requirementsFactory, routeRepo)
    53  		Expect(passed).To(BeFalse())
    54  	})
    55  
    56  	Context("when arguments are provided", func() {
    57  		var cmd commandregistry.Command
    58  		var flagContext flags.FlagContext
    59  
    60  		BeforeEach(func() {
    61  			cmd = &route.DeleteOrphanedRoutes{}
    62  			cmd.SetDependency(deps, false)
    63  			flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    64  		})
    65  
    66  		It("should fail with usage", func() {
    67  			flagContext.Parse("blahblah")
    68  
    69  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    70  			Expect(err).NotTo(HaveOccurred())
    71  
    72  			err = testcmd.RunRequirements(reqs)
    73  			Expect(err).To(HaveOccurred())
    74  			Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    75  			Expect(err.Error()).To(ContainSubstring("No argument required"))
    76  		})
    77  	})
    78  
    79  	Context("when logged in successfully", func() {
    80  
    81  		BeforeEach(func() {
    82  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    83  		})
    84  
    85  		It("passes requirements when logged in", func() {
    86  			_, passed := callDeleteOrphanedRoutes("y", []string{}, requirementsFactory, routeRepo)
    87  			Expect(passed).To(BeTrue())
    88  		})
    89  
    90  		It("passes when confirmation is provided", func() {
    91  			var ui *testterm.FakeUI
    92  			domain := models.DomainFields{Name: "example.com"}
    93  			domain2 := models.DomainFields{Name: "cookieclicker.co"}
    94  
    95  			app1 := models.ApplicationFields{Name: "dora"}
    96  
    97  			routeRepo.ListRoutesStub = func(cb func(models.Route) bool) error {
    98  				route := models.Route{}
    99  				route.GUID = "route1-guid"
   100  				route.Host = "hostname-1"
   101  				route.Domain = domain
   102  				route.Apps = []models.ApplicationFields{app1}
   103  
   104  				route2 := models.Route{}
   105  				route2.GUID = "route2-guid"
   106  				route2.Host = "hostname-2"
   107  				route2.Domain = domain2
   108  
   109  				cb(route)
   110  				cb(route2)
   111  
   112  				return nil
   113  			}
   114  
   115  			ui, _ = callDeleteOrphanedRoutes("y", []string{}, requirementsFactory, routeRepo)
   116  
   117  			Expect(ui.Prompts).To(ContainSubstrings(
   118  				[]string{"Really delete orphaned routes"},
   119  			))
   120  
   121  			Expect(ui.Outputs()).To(ContainSubstrings(
   122  				[]string{"Deleting route", "hostname-2.cookieclicker.co"},
   123  				[]string{"OK"},
   124  			))
   125  
   126  			Expect(routeRepo.DeleteCallCount()).To(Equal(1))
   127  			Expect(routeRepo.DeleteArgsForCall(0)).To(Equal("route2-guid"))
   128  		})
   129  
   130  		It("passes when the force flag is used", func() {
   131  			var ui *testterm.FakeUI
   132  
   133  			routeRepo.ListRoutesStub = func(cb func(models.Route) bool) error {
   134  				route := models.Route{}
   135  				route.Host = "hostname-1"
   136  				route.Domain = models.DomainFields{Name: "example.com"}
   137  				route.Apps = []models.ApplicationFields{
   138  					{
   139  						Name: "dora",
   140  					},
   141  				}
   142  
   143  				route2 := models.Route{}
   144  				route2.GUID = "route2-guid"
   145  				route2.Host = "hostname-2"
   146  				route2.Domain = models.DomainFields{Name: "cookieclicker.co"}
   147  
   148  				cb(route)
   149  				cb(route2)
   150  
   151  				return nil
   152  			}
   153  
   154  			ui, _ = callDeleteOrphanedRoutes("", []string{"-f"}, requirementsFactory, routeRepo)
   155  
   156  			Expect(len(ui.Prompts)).To(Equal(0))
   157  
   158  			Expect(ui.Outputs()).To(ContainSubstrings(
   159  				[]string{"Deleting route", "hostname-2.cookieclicker.co"},
   160  				[]string{"OK"},
   161  			))
   162  			Expect(routeRepo.DeleteCallCount()).To(Equal(1))
   163  			Expect(routeRepo.DeleteArgsForCall(0)).To(Equal("route2-guid"))
   164  		})
   165  	})
   166  })