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