github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/check_route_command_test.go (about) 1 package v7_test 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/actor/v7action" 6 "code.cloudfoundry.org/cli/cf/errors" 7 "code.cloudfoundry.org/cli/command/commandfakes" 8 "code.cloudfoundry.org/cli/command/flag" 9 v7 "code.cloudfoundry.org/cli/command/v7" 10 "code.cloudfoundry.org/cli/command/v7/v7fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("check-route Command", func() { 19 var ( 20 cmd v7.CheckRouteCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v7fakes.FakeCheckRouteActor 25 binaryName string 26 executeErr error 27 ) 28 29 BeforeEach(func() { 30 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 31 fakeConfig = new(commandfakes.FakeConfig) 32 fakeSharedActor = new(commandfakes.FakeSharedActor) 33 fakeActor = new(v7fakes.FakeCheckRouteActor) 34 35 binaryName = "faceman" 36 fakeConfig.BinaryNameReturns(binaryName) 37 38 cmd = v7.CheckRouteCommand{ 39 UI: testUI, 40 Config: fakeConfig, 41 SharedActor: fakeSharedActor, 42 Actor: fakeActor, 43 RequiredArgs: flag.Domain{Domain: "some-domain.com"}, 44 } 45 }) 46 47 JustBeforeEach(func() { 48 executeErr = cmd.Execute(nil) 49 }) 50 51 When("checking target fails", func() { 52 BeforeEach(func() { 53 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 54 }) 55 56 It("returns an error", func() { 57 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 58 59 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 60 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 61 Expect(checkTargetedOrg).To(BeTrue()) 62 Expect(checkTargetedSpace).To(BeFalse()) 63 }) 64 }) 65 66 When("the user is not logged in", func() { 67 BeforeEach(func() { 68 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 69 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 70 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("no current user")) 71 }) 72 73 It("returns an error", func() { 74 Expect(executeErr).To(MatchError("no current user")) 75 76 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 77 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 78 Expect(checkTargetedOrg).To(BeTrue()) 79 Expect(checkTargetedSpace).To(BeFalse()) 80 }) 81 }) 82 83 When("targeting an org and logged in", func() { 84 BeforeEach(func() { 85 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 86 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 87 fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil) 88 }) 89 90 When("checking for existing route returns an error", func() { 91 BeforeEach(func() { 92 fakeActor.CheckRouteReturns( 93 false, 94 v7action.Warnings{"check-route-warning"}, 95 errors.New("failed to check route"), 96 ) 97 }) 98 99 It("returns the error", func() { 100 Expect(executeErr).To(MatchError("failed to check route")) 101 102 Expect(testUI.Out).To(Say("Checking for route...")) 103 104 Expect(fakeActor.CheckRouteCallCount()).To(Equal(1)) 105 givenDomain, givenHostname, givenPath := fakeActor.CheckRouteArgsForCall(0) 106 Expect(givenDomain).To(Equal("some-domain.com")) 107 Expect(givenHostname).To(Equal("")) 108 Expect(givenPath).To(Equal("")) 109 }) 110 }) 111 112 When("checking for route returns true", func() { 113 BeforeEach(func() { 114 fakeActor.CheckRouteReturns( 115 true, 116 v7action.Warnings{"check-route-warning"}, 117 nil, 118 ) 119 }) 120 121 It("checks the route and displays the result", func() { 122 Expect(executeErr).NotTo(HaveOccurred()) 123 124 Expect(testUI.Out).To(Say("Checking for route...")) 125 Expect(testUI.Out).To(Say(`Route 'some-domain.com' does exist\.`)) 126 Expect(testUI.Out).To(Say("OK")) 127 128 Expect(fakeActor.CheckRouteCallCount()).To(Equal(1)) 129 givenDomain, givenHostname, givenPath := fakeActor.CheckRouteArgsForCall(0) 130 Expect(givenDomain).To(Equal("some-domain.com")) 131 Expect(givenHostname).To(Equal("")) 132 Expect(givenPath).To(Equal("")) 133 }) 134 }) 135 136 When("checking for route returns false", func() { 137 BeforeEach(func() { 138 fakeActor.CheckRouteReturns( 139 false, 140 v7action.Warnings{"check-route-warning"}, 141 nil, 142 ) 143 }) 144 145 It("checks the route and displays the result", func() { 146 Expect(executeErr).NotTo(HaveOccurred()) 147 148 Expect(testUI.Out).To(Say("Checking for route...")) 149 Expect(testUI.Out).To(Say(`Route 'some-domain\.com' does not exist\.`)) 150 Expect(testUI.Out).To(Say("OK")) 151 152 Expect(fakeActor.CheckRouteCallCount()).To(Equal(1)) 153 givenDomain, givenHostname, givenPath := fakeActor.CheckRouteArgsForCall(0) 154 Expect(givenDomain).To(Equal("some-domain.com")) 155 Expect(givenHostname).To(Equal("")) 156 Expect(givenPath).To(Equal("")) 157 }) 158 }) 159 160 When("passing hostname and path flags", func() { 161 BeforeEach(func() { 162 cmd.Path.Path = "/some-path" 163 cmd.Hostname = "some-host" 164 165 fakeActor.CheckRouteReturns( 166 true, 167 v7action.Warnings{"check-route-warning"}, 168 nil, 169 ) 170 }) 171 172 It("checks the route with correct arguments and displays the result", func() { 173 Expect(executeErr).NotTo(HaveOccurred()) 174 175 Expect(testUI.Out).To(Say("Checking for route...")) 176 Expect(testUI.Out).To(Say(`Route 'some-host\.some-domain\.com/some-path' does exist\.`)) 177 Expect(testUI.Out).To(Say("OK")) 178 179 Expect(fakeActor.CheckRouteCallCount()).To(Equal(1)) 180 givenDomain, givenHostname, givenPath := fakeActor.CheckRouteArgsForCall(0) 181 Expect(givenDomain).To(Equal("some-domain.com")) 182 Expect(givenHostname).To(Equal("some-host")) 183 Expect(givenPath).To(Equal("/some-path")) 184 }) 185 }) 186 }) 187 })