github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/delete_route_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("delete-route Command", func() {
    21  	var (
    22  		cmd             DeleteRouteCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v7fakes.FakeActor
    27  		input           *Buffer
    28  		binaryName      string
    29  		executeErr      error
    30  		domain          string
    31  		hostname        string
    32  		path            string
    33  		port            int
    34  	)
    35  
    36  	BeforeEach(func() {
    37  		input = NewBuffer()
    38  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    39  		fakeConfig = new(commandfakes.FakeConfig)
    40  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    41  		fakeActor = new(v7fakes.FakeActor)
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  		domain = "some-domain.com"
    46  		hostname = "host"
    47  		path = "/path"
    48  		port = 1234
    49  
    50  		cmd = DeleteRouteCommand{
    51  			RequiredArgs: flag.Domain{Domain: domain},
    52  			Hostname:     hostname,
    53  			Path:         flag.V7RoutePath{Path: path},
    54  			Port:         port,
    55  			BaseCommand: BaseCommand{UI: testUI,
    56  				Config:      fakeConfig,
    57  				SharedActor: fakeSharedActor,
    58  				Actor:       fakeActor,
    59  			},
    60  		}
    61  
    62  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    63  			Name: "some-org",
    64  			GUID: "some-org-guid",
    65  		})
    66  
    67  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    68  	})
    69  
    70  	JustBeforeEach(func() {
    71  		executeErr = cmd.Execute(nil)
    72  	})
    73  
    74  	When("checking target fails", func() {
    75  		BeforeEach(func() {
    76  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    77  		})
    78  
    79  		It("returns an error", func() {
    80  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    81  
    82  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    83  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    84  			Expect(checkTargetedOrg).To(BeTrue())
    85  			Expect(checkTargetedSpace).To(BeFalse())
    86  		})
    87  	})
    88  
    89  	When("the user is not logged in", func() {
    90  		var expectedErr error
    91  
    92  		BeforeEach(func() {
    93  			expectedErr = errors.New("some current user error")
    94  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    95  		})
    96  
    97  		It("return an error", func() {
    98  			Expect(executeErr).To(Equal(expectedErr))
    99  		})
   100  	})
   101  
   102  	When("the -f flag is NOT provided", func() {
   103  		BeforeEach(func() {
   104  			cmd.Force = false
   105  		})
   106  
   107  		When("the user inputs yes", func() {
   108  			BeforeEach(func() {
   109  				_, err := input.Write([]byte("y\n"))
   110  				Expect(err).ToNot(HaveOccurred())
   111  
   112  				fakeActor.DeleteRouteReturns(v7action.Warnings{"some-warning"}, nil)
   113  			})
   114  
   115  			It("delegates to the Actor", func() {
   116  				actualDomainName, actualHostname, actualPath, actualPort := fakeActor.DeleteRouteArgsForCall(0)
   117  				Expect(actualDomainName).To(Equal(domain))
   118  				Expect(actualHostname).To(Equal(hostname))
   119  				Expect(actualPath).To(Equal(path))
   120  				Expect(actualPort).To(Equal(port))
   121  			})
   122  
   123  			It("deletes the route", func() {
   124  				Expect(executeErr).ToNot(HaveOccurred())
   125  
   126  				Expect(testUI.Err).To(Say("some-warning"))
   127  				Expect(testUI.Out).To(Say(`Deleting route %s.%s%s:%d\.\.\.`, hostname, domain, path, port))
   128  				Expect(testUI.Out).To(Say("OK"))
   129  				Expect(testUI.Out).NotTo(Say("Route 'some-domain' does not exist"))
   130  			})
   131  		})
   132  
   133  		When("the user inputs no", func() {
   134  			BeforeEach(func() {
   135  				_, err := input.Write([]byte("n\n"))
   136  				Expect(err).ToNot(HaveOccurred())
   137  			})
   138  
   139  			It("cancels the delete", func() {
   140  				Expect(executeErr).ToNot(HaveOccurred())
   141  
   142  				Expect(testUI.Out).To(Say("'%s.%s%s:%d' has not been deleted.", hostname, domain, path, port))
   143  				Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0))
   144  			})
   145  		})
   146  
   147  		When("the user chooses the default", func() {
   148  			BeforeEach(func() {
   149  				_, err := input.Write([]byte("\n"))
   150  				Expect(err).ToNot(HaveOccurred())
   151  			})
   152  
   153  			It("cancels the delete", func() {
   154  				Expect(executeErr).ToNot(HaveOccurred())
   155  
   156  				Expect(testUI.Out).To(Say("'%s.%s%s:%d' has not been deleted.", hostname, domain, path, port))
   157  				Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0))
   158  			})
   159  		})
   160  
   161  		When("the user input is invalid", func() {
   162  			BeforeEach(func() {
   163  				_, err := input.Write([]byte("e\n\n"))
   164  				Expect(err).ToNot(HaveOccurred())
   165  			})
   166  
   167  			It("asks the user again", func() {
   168  				Expect(executeErr).NotTo(HaveOccurred())
   169  
   170  				Expect(testUI.Out).To(Say(`Really delete the route host.some-domain\.com/path:1234\? \[yN\]`))
   171  				Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   172  				Expect(testUI.Out).To(Say(`Really delete the route host.some-domain\.com/path:1234\? \[yN\]`))
   173  
   174  				Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0))
   175  			})
   176  		})
   177  	})
   178  
   179  	When("the -f flag is provided", func() {
   180  		BeforeEach(func() {
   181  			cmd.Force = true
   182  		})
   183  
   184  		When("deleting the route errors", func() {
   185  			Context("generic error", func() {
   186  				BeforeEach(func() {
   187  					fakeActor.DeleteRouteReturns(v7action.Warnings{"some-warning"}, errors.New("some-error"))
   188  				})
   189  
   190  				It("displays all warnings, and returns the error", func() {
   191  					Expect(testUI.Err).To(Say("some-warning"))
   192  					Expect(testUI.Out).To(Say(`Deleting route host.some-domain.com\/path:1234\.\.\.`))
   193  					Expect(testUI.Out).ToNot(Say("OK"))
   194  					Expect(executeErr).To(MatchError("some-error"))
   195  				})
   196  			})
   197  		})
   198  
   199  		When("the route doesn't exist", func() {
   200  			BeforeEach(func() {
   201  				fakeActor.DeleteRouteReturns(
   202  					v7action.Warnings{"some-warning"},
   203  					actionerror.RouteNotFoundError{
   204  						Host:       hostname,
   205  						DomainName: domain,
   206  						Path:       path},
   207  				)
   208  			})
   209  
   210  			It("displays all warnings, that the domain wasn't found, and does not error", func() {
   211  				Expect(executeErr).ToNot(HaveOccurred())
   212  
   213  				Expect(testUI.Err).To(Say("some-warning"))
   214  				Expect(testUI.Out).To(Say(`Deleting route %s.%s%s:%d\.\.\.`, hostname, domain, path, port))
   215  				Expect(testUI.Out).To(Say(`Unable to delete\. Route with host '%s', domain '%s', and path '%s' not found.`, hostname, domain, path))
   216  				Expect(testUI.Out).To(Say("OK"))
   217  			})
   218  		})
   219  
   220  		When("the route exists", func() {
   221  			BeforeEach(func() {
   222  				fakeActor.DeleteRouteReturns(v7action.Warnings{"some-warning"}, nil)
   223  			})
   224  
   225  			It("displays all warnings, and does not error", func() {
   226  				Expect(executeErr).ToNot(HaveOccurred())
   227  
   228  				Expect(testUI.Err).To(Say("some-warning"))
   229  				Expect(testUI.Out).To(Say(`Deleting route %s\.%s%s:%d\.\.\.`, hostname, domain, path, port))
   230  				Expect(testUI.Out).To(Say("OK"))
   231  			})
   232  		})
   233  	})
   234  })