github.com/niteshexa/cloudfoundry_cli@v7.1.0+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.FakeActor
    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.FakeActor)
    34  
    35  		binaryName = "faceman"
    36  		fakeConfig.BinaryNameReturns(binaryName)
    37  
    38  		cmd = v7.CheckRouteCommand{
    39  			BaseCommand: v7.BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  			RequiredArgs: flag.Domain{Domain: "some-domain.com"},
    46  		}
    47  
    48  		fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    49  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    50  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    51  		fakeActor.CheckRouteReturns(
    52  			true,
    53  			v7action.Warnings{"check-route-warning"},
    54  			nil,
    55  		)
    56  	})
    57  
    58  	JustBeforeEach(func() {
    59  		executeErr = cmd.Execute(nil)
    60  	})
    61  
    62  	It("checks the target", func() {
    63  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    64  		checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    65  		Expect(checkTargetedOrg).To(BeTrue())
    66  		Expect(checkTargetedSpace).To(BeFalse())
    67  	})
    68  
    69  	When("checking target fails", func() {
    70  		BeforeEach(func() {
    71  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    72  		})
    73  
    74  		It("returns an error", func() {
    75  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    76  		})
    77  	})
    78  
    79  	It("checks if the user is logged in", func() {
    80  		Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
    81  	})
    82  
    83  	When("the user is not logged in", func() {
    84  		BeforeEach(func() {
    85  			fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("no current user"))
    86  		})
    87  
    88  		It("returns an error", func() {
    89  			Expect(executeErr).To(MatchError("no current user"))
    90  		})
    91  	})
    92  
    93  	It("displays it's checking for the route", func() {
    94  		Expect(testUI.Out).To(Say("Checking for route..."))
    95  	})
    96  
    97  	It("checks if the route exists, displaying warnings", func() {
    98  		Expect(testUI.Err).To(Say("check-route-warning"))
    99  
   100  		Expect(fakeActor.CheckRouteCallCount()).To(Equal(1))
   101  		domain, host, path, port := fakeActor.CheckRouteArgsForCall(0)
   102  		Expect(domain).To(Equal(cmd.RequiredArgs.Domain))
   103  		Expect(host).To(Equal(cmd.Hostname))
   104  		Expect(path).To(Equal(cmd.Path.Path))
   105  		Expect(port).To(Equal(0))
   106  	})
   107  
   108  	When("checking for existing route returns an error", func() {
   109  		BeforeEach(func() {
   110  			fakeActor.CheckRouteReturns(
   111  				false,
   112  				v7action.Warnings{"check-route-warning"},
   113  				errors.New("failed to check route"),
   114  			)
   115  		})
   116  
   117  		It("returns the error", func() {
   118  			Expect(executeErr).To(MatchError("failed to check route"))
   119  		})
   120  	})
   121  
   122  	It("confirms that the route exists", func() {
   123  		Expect(testUI.Out).To(Say(`Route 'some-domain.com' does exist\.`))
   124  		Expect(testUI.Out).To(Say("OK"))
   125  	})
   126  
   127  	When("there's no matching route", func() {
   128  		BeforeEach(func() {
   129  			fakeActor.CheckRouteReturns(
   130  				false,
   131  				v7action.Warnings{"check-route-warning"},
   132  				nil,
   133  			)
   134  		})
   135  
   136  		It("checks the route and displays the result", func() {
   137  			Expect(testUI.Out).To(Say(`Route 'some-domain\.com' does not exist\.`))
   138  		})
   139  	})
   140  
   141  	When("passing hostname and path flags", func() {
   142  		BeforeEach(func() {
   143  			cmd.Path.Path = "/some-path"
   144  			cmd.Hostname = "some-host"
   145  
   146  			fakeActor.CheckRouteReturns(
   147  				true,
   148  				v7action.Warnings{"check-route-warning"},
   149  				nil,
   150  			)
   151  		})
   152  
   153  		It("checks the route with correct arguments and displays the result", func() {
   154  			Expect(executeErr).NotTo(HaveOccurred())
   155  
   156  			Expect(testUI.Out).To(Say("Checking for route..."))
   157  			Expect(testUI.Out).To(Say(`Route 'some-host\.some-domain\.com/some-path' does exist\.`))
   158  			Expect(testUI.Out).To(Say("OK"))
   159  
   160  			Expect(fakeActor.CheckRouteCallCount()).To(Equal(1))
   161  			givenDomain, givenHostname, givenPath, givenPort := fakeActor.CheckRouteArgsForCall(0)
   162  			Expect(givenDomain).To(Equal("some-domain.com"))
   163  			Expect(givenHostname).To(Equal("some-host"))
   164  			Expect(givenPath).To(Equal("/some-path"))
   165  			Expect(givenPort).To(Equal(0))
   166  		})
   167  	})
   168  
   169  	When("passing in a port flag (for TCP routes)", func() {
   170  		BeforeEach(func() {
   171  			cmd.Port = 1024
   172  
   173  			fakeActor.CheckRouteReturns(
   174  				true,
   175  				v7action.Warnings{"check-route-warning"},
   176  				nil,
   177  			)
   178  		})
   179  
   180  		It("checks the route with correct arguments and displays the result", func() {
   181  			Expect(executeErr).NotTo(HaveOccurred())
   182  
   183  			Expect(testUI.Out).To(Say("Checking for route..."))
   184  			Expect(testUI.Out).To(Say(`Route 'some-domain\.com:1024' does exist\.`))
   185  			Expect(testUI.Out).To(Say("OK"))
   186  
   187  			Expect(fakeActor.CheckRouteCallCount()).To(Equal(1))
   188  			_, _, _, givenPort := fakeActor.CheckRouteArgsForCall(0)
   189  			Expect(givenPort).To(Equal(1024))
   190  		})
   191  	})
   192  
   193  	It("displays 'OK' and returns nil (no error)", func() {
   194  		Expect(testUI.Out).To(Say("OK"))
   195  
   196  		Expect(executeErr).NotTo(HaveOccurred())
   197  	})
   198  })