github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/delete_orphaned_routes_test.go (about)

     1  package route_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/models"
     6  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     7  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     8  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
     9  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    10  
    11  	"github.com/cloudfoundry/cli/cf/command_registry"
    12  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    13  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("delete-orphaned-routes command", func() {
    19  	var (
    20  		ui         *testterm.FakeUI
    21  		routeRepo  *testapi.FakeRouteRepository
    22  		configRepo core_config.Repository
    23  		reqFactory *testreq.FakeReqFactory
    24  		deps       command_registry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.Ui = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo)
    30  		deps.Config = configRepo
    31  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-orphaned-routes").SetDependency(deps, pluginCall))
    32  	}
    33  
    34  	callDeleteOrphanedRoutes := func(confirmation string, args []string, reqFactory *testreq.FakeReqFactory, routeRepo *testapi.FakeRouteRepository) (*testterm.FakeUI, bool) {
    35  		ui = &testterm.FakeUI{Inputs: []string{confirmation}}
    36  		configRepo = testconfig.NewRepositoryWithDefaults()
    37  		passed := testcmd.RunCliCommand("delete-orphaned-routes", args, reqFactory, updateCommandDependency, false)
    38  
    39  		return ui, passed
    40  	}
    41  
    42  	BeforeEach(func() {
    43  		routeRepo = &testapi.FakeRouteRepository{}
    44  		reqFactory = &testreq.FakeReqFactory{}
    45  	})
    46  
    47  	It("fails requirements when not logged in", func() {
    48  		_, passed := callDeleteOrphanedRoutes("y", []string{}, reqFactory, routeRepo)
    49  		Expect(passed).To(BeFalse())
    50  	})
    51  	It("should fail with usage when provided any arguments", func() {
    52  		reqFactory.LoginSuccess = true
    53  		ui, passed := callDeleteOrphanedRoutes("y", []string{"blahblah"}, reqFactory, routeRepo)
    54  		Expect(passed).To(BeFalse())
    55  		Expect(ui.Outputs).To(ContainSubstrings(
    56  			[]string{"Incorrect Usage", "No argument required"},
    57  		))
    58  	})
    59  
    60  	Context("when logged in successfully", func() {
    61  
    62  		BeforeEach(func() {
    63  			reqFactory.LoginSuccess = true
    64  		})
    65  
    66  		It("passes requirements when logged in", func() {
    67  			_, passed := callDeleteOrphanedRoutes("y", []string{}, reqFactory, routeRepo)
    68  			Expect(passed).To(BeTrue())
    69  		})
    70  
    71  		It("passes when confirmation is provided", func() {
    72  			var ui *testterm.FakeUI
    73  			domain := models.DomainFields{Name: "example.com"}
    74  			domain2 := models.DomainFields{Name: "cookieclicker.co"}
    75  
    76  			app1 := models.ApplicationFields{Name: "dora"}
    77  
    78  			route := models.Route{}
    79  			route.Host = "hostname-1"
    80  			route.Domain = domain
    81  			route.Apps = []models.ApplicationFields{app1}
    82  
    83  			route2 := models.Route{}
    84  			route2.Guid = "route2-guid"
    85  			route2.Host = "hostname-2"
    86  			route2.Domain = domain2
    87  
    88  			routeRepo.Routes = []models.Route{route, route2}
    89  
    90  			ui, _ = callDeleteOrphanedRoutes("y", []string{}, reqFactory, routeRepo)
    91  
    92  			Expect(ui.Prompts).To(ContainSubstrings(
    93  				[]string{"Really delete orphaned routes"},
    94  			))
    95  
    96  			Expect(ui.Outputs).To(ContainSubstrings(
    97  				[]string{"Deleting route", "hostname-2.cookieclicker.co"},
    98  				[]string{"OK"},
    99  			))
   100  			Expect(routeRepo.DeletedRouteGuids).To(ContainElement("route2-guid"))
   101  		})
   102  
   103  		It("passes when the force flag is used", func() {
   104  			var ui *testterm.FakeUI
   105  			domain := models.DomainFields{Name: "example.com"}
   106  			domain2 := models.DomainFields{Name: "cookieclicker.co"}
   107  
   108  			app1 := models.ApplicationFields{Name: "dora"}
   109  
   110  			route := models.Route{}
   111  			route.Host = "hostname-1"
   112  			route.Domain = domain
   113  			route.Apps = []models.ApplicationFields{app1}
   114  
   115  			route2 := models.Route{}
   116  			route2.Guid = "route2-guid"
   117  			route2.Host = "hostname-2"
   118  			route2.Domain = domain2
   119  
   120  			routeRepo.Routes = []models.Route{route, route2}
   121  
   122  			ui, _ = callDeleteOrphanedRoutes("", []string{"-f"}, reqFactory, routeRepo)
   123  
   124  			Expect(len(ui.Prompts)).To(Equal(0))
   125  
   126  			Expect(ui.Outputs).To(ContainSubstrings(
   127  				[]string{"Deleting route", "hostname-2.cookieclicker.co"},
   128  				[]string{"OK"},
   129  			))
   130  			Expect(routeRepo.DeletedRouteGuids).To(ContainElement("route2-guid"))
   131  		})
   132  	})
   133  })