github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/delete_route_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/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/errors" 8 "github.com/cloudfoundry/cli/cf/models" 9 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 10 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 11 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 12 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 16 . "github.com/cloudfoundry/cli/testhelpers/matchers" 17 ) 18 19 var _ = Describe("delete-route command", func() { 20 var ( 21 ui *testterm.FakeUI 22 requirementsFactory *testreq.FakeReqFactory 23 routeRepo *testapi.FakeRouteRepository 24 deps command_registry.Dependency 25 configRepo core_config.Repository 26 ) 27 28 updateCommandDependency := func(pluginCall bool) { 29 deps.Ui = ui 30 deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo) 31 deps.Config = configRepo 32 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-route").SetDependency(deps, pluginCall)) 33 } 34 35 BeforeEach(func() { 36 ui = &testterm.FakeUI{Inputs: []string{"yes"}} 37 38 routeRepo = &testapi.FakeRouteRepository{} 39 requirementsFactory = &testreq.FakeReqFactory{ 40 LoginSuccess: true, 41 } 42 }) 43 44 runCommand := func(args ...string) bool { 45 configRepo = testconfig.NewRepositoryWithDefaults() 46 return testcmd.RunCliCommand("delete-route", args, requirementsFactory, updateCommandDependency, false) 47 } 48 49 Context("when not logged in", func() { 50 BeforeEach(func() { 51 requirementsFactory.LoginSuccess = false 52 }) 53 54 It("does not pass requirements", func() { 55 Expect(runCommand("-n", "my-host", "example.com")).To(BeFalse()) 56 }) 57 }) 58 59 Context("when logged in successfully", func() { 60 BeforeEach(func() { 61 requirementsFactory.LoginSuccess = true 62 route := models.Route{Guid: "route-guid"} 63 route.Domain = models.DomainFields{ 64 Guid: "domain-guid", 65 Name: "example.com", 66 } 67 routeRepo.FindByHostAndDomainReturns.Route = route 68 }) 69 70 It("fails with usage when given zero args", func() { 71 runCommand() 72 Expect(ui.Outputs).To(ContainSubstrings( 73 []string{"Incorrect Usage", "Requires an argument"}, 74 )) 75 }) 76 77 It("does not fail with usage when provided with a domain", func() { 78 runCommand("example.com") 79 Expect(ui.FailedWithUsage).To(BeFalse()) 80 }) 81 82 It("does not fail with usage when provided a hostname", func() { 83 runCommand("-n", "my-host", "example.com") 84 Expect(ui.FailedWithUsage).To(BeFalse()) 85 }) 86 87 It("deletes routes when the user confirms", func() { 88 runCommand("-n", "my-host", "example.com") 89 90 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the route my-host"})) 91 92 Expect(ui.Outputs).To(ContainSubstrings( 93 []string{"Deleting route", "my-host.example.com"}, 94 []string{"OK"}, 95 )) 96 Expect(routeRepo.DeletedRouteGuids).To(Equal([]string{"route-guid"})) 97 }) 98 99 It("does not prompt the user to confirm when they pass the '-f' flag", func() { 100 ui.Inputs = []string{} 101 runCommand("-f", "-n", "my-host", "example.com") 102 103 Expect(ui.Prompts).To(BeEmpty()) 104 105 Expect(ui.Outputs).To(ContainSubstrings( 106 []string{"Deleting", "my-host.example.com"}, 107 []string{"OK"}, 108 )) 109 Expect(routeRepo.DeletedRouteGuids).To(Equal([]string{"route-guid"})) 110 }) 111 112 It("succeeds with a warning when the route does not exist", func() { 113 routeRepo.FindByHostAndDomainReturns.Error = errors.NewModelNotFoundError("Org", "not found") 114 115 runCommand("-n", "my-host", "example.com") 116 117 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-host", "does not exist"})) 118 119 Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"OK"})) 120 }) 121 }) 122 })