github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/check_route_test.go (about) 1 package route_test 2 3 import ( 4 "errors" 5 6 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 7 "github.com/cloudfoundry/cli/cf/command_registry" 8 "github.com/cloudfoundry/cli/cf/configuration/core_config" 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 14 . "github.com/cloudfoundry/cli/testhelpers/matchers" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("check-route command", func() { 20 var ( 21 ui *testterm.FakeUI 22 routeRepo *testapi.FakeRouteRepository 23 domainRepo *testapi.FakeDomainRepository 24 requirementsFactory *testreq.FakeReqFactory 25 config core_config.Repository 26 deps command_registry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.Ui = ui 31 deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo) 32 deps.RepoLocator = deps.RepoLocator.SetDomainRepository(domainRepo) 33 deps.Config = config 34 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("check-route").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{} 39 routeRepo = &testapi.FakeRouteRepository{} 40 domainRepo = &testapi.FakeDomainRepository{} 41 requirementsFactory = &testreq.FakeReqFactory{} 42 config = testconfig.NewRepositoryWithDefaults() 43 }) 44 45 runCommand := func(args ...string) bool { 46 return testcmd.RunCliCommand("check-route", args, requirementsFactory, updateCommandDependency, false) 47 } 48 49 Describe("requirements", func() { 50 It("fails when not logged in", func() { 51 requirementsFactory.TargetedOrgSuccess = true 52 53 Expect(runCommand("foobar.example.com", "bar.example.com")).To(BeFalse()) 54 }) 55 56 It("fails when no org is targeted", func() { 57 requirementsFactory.LoginSuccess = true 58 Expect(runCommand("foobar.example.com", "bar.example.com")).To(BeFalse()) 59 }) 60 61 It("fails when the number of arguments is greater than two", func() { 62 requirementsFactory.LoginSuccess = true 63 requirementsFactory.TargetedOrgSuccess = true 64 passed := runCommand("foobar.example.com", "hello", "world") 65 66 Expect(passed).To(BeFalse()) 67 Expect(ui.Outputs).To(ContainSubstrings( 68 []string{"Incorrect Usage", "Requires", "arguments"}, 69 )) 70 }) 71 72 It("fails when the number of arguments is less than two", func() { 73 requirementsFactory.LoginSuccess = true 74 requirementsFactory.TargetedOrgSuccess = true 75 passed := runCommand("foobar.example.com") 76 77 Expect(passed).To(BeFalse()) 78 Expect(ui.Outputs).To(ContainSubstrings( 79 []string{"Incorrect Usage", "Requires", "arguments"}, 80 )) 81 }) 82 }) 83 84 Context("when the route already exists", func() { 85 BeforeEach(func() { 86 requirementsFactory.LoginSuccess = true 87 requirementsFactory.TargetedOrgSuccess = true 88 routeRepo.CheckIfExistsFound = true 89 }) 90 91 It("prints out route does exist", func() { 92 requirementsFactory.LoginSuccess = true 93 requirementsFactory.TargetedOrgSuccess = true 94 95 runCommand("some-existing-route", "example.com") 96 Expect(ui.Outputs).To(ContainSubstrings( 97 []string{"Checking for route..."}, 98 []string{"OK"}, 99 []string{"Route some-existing-route.example.com does exist"}, 100 )) 101 }) 102 }) 103 104 Context("when the route does not exist", func() { 105 BeforeEach(func() { 106 requirementsFactory.LoginSuccess = true 107 requirementsFactory.TargetedOrgSuccess = true 108 routeRepo.CheckIfExistsFound = false 109 }) 110 111 It("prints out route does not exist", func() { 112 113 runCommand("non-existent-route", "example.com") 114 Expect(ui.Outputs).To(ContainSubstrings( 115 []string{"Checking for route..."}, 116 []string{"OK"}, 117 []string{"Route non-existent-route.example.com does not exist"}, 118 )) 119 }) 120 }) 121 122 Context("when finding the domain returns an error", func() { 123 BeforeEach(func() { 124 requirementsFactory.LoginSuccess = true 125 requirementsFactory.TargetedOrgSuccess = true 126 domainRepo.FindByNameInOrgApiResponse = errors.New("Domain not found") 127 }) 128 129 It("prints out route does not exist", func() { 130 131 runCommand("some-silly-route", "some-non-real-domain") 132 Expect(ui.Outputs).To(ContainSubstrings( 133 []string{"Checking for route..."}, 134 []string{"FAILED"}, 135 []string{"Domain not found"}, 136 )) 137 }) 138 }) 139 Context("when checking if the route exists returns an error", func() { 140 BeforeEach(func() { 141 requirementsFactory.LoginSuccess = true 142 requirementsFactory.TargetedOrgSuccess = true 143 routeRepo.CheckIfExistsError = errors.New("Some stupid error") 144 }) 145 146 It("prints out route does not exist", func() { 147 148 runCommand("some-silly-route", "some-non-real-domain") 149 Expect(ui.Outputs).To(ContainSubstrings( 150 []string{"Checking for route..."}, 151 []string{"FAILED"}, 152 []string{"Some stupid error"}, 153 )) 154 }) 155 }) 156 157 })