github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/delete_orphaned_routes_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
     9  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    10  	"github.com/liamawhite/cli-with-i18n/command/v2"
    11  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    12  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    13  	"github.com/liamawhite/cli-with-i18n/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("deleted-orphaned-routes Command", func() {
    20  	var (
    21  		cmd             v2.DeleteOrphanedRoutesCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v2fakes.FakeDeleteOrphanedRoutesActor
    26  		input           *Buffer
    27  		binaryName      string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		input = NewBuffer()
    33  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v2fakes.FakeDeleteOrphanedRoutesActor)
    37  
    38  		cmd = v2.DeleteOrphanedRoutesCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  	})
    48  
    49  	JustBeforeEach(func() {
    50  		executeErr = cmd.Execute(nil)
    51  	})
    52  
    53  	Context("when a cloud controller API endpoint is set", func() {
    54  		BeforeEach(func() {
    55  			fakeConfig.TargetReturns("some-url")
    56  		})
    57  
    58  		Context("when checking target fails", func() {
    59  			BeforeEach(func() {
    60  				fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    61  			})
    62  
    63  			It("returns an error", func() {
    64  				Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: "faceman"}))
    65  
    66  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  				_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    68  				Expect(checkTargetedOrg).To(BeTrue())
    69  				Expect(checkTargetedSpace).To(BeTrue())
    70  			})
    71  		})
    72  
    73  		Context("when the user is logged in, and org and space are targeted", func() {
    74  			BeforeEach(func() {
    75  				fakeConfig.HasTargetedOrganizationReturns(true)
    76  				fakeConfig.HasTargetedSpaceReturns(true)
    77  				fakeConfig.TargetedSpaceReturns(configv3.Space{
    78  					GUID: "some-space-guid",
    79  					Name: "some-space",
    80  				})
    81  			})
    82  
    83  			Context("when getting the current user returns an error", func() {
    84  				var expectedErr error
    85  
    86  				BeforeEach(func() {
    87  					expectedErr = errors.New("getting current user error")
    88  					fakeConfig.CurrentUserReturns(
    89  						configv3.User{},
    90  						expectedErr)
    91  				})
    92  
    93  				It("returns the error", func() {
    94  					Expect(executeErr).To(MatchError(expectedErr))
    95  				})
    96  			})
    97  
    98  			Context("when getting the current user does not return an error", func() {
    99  				BeforeEach(func() {
   100  					fakeConfig.CurrentUserReturns(
   101  						configv3.User{Name: "some-user"},
   102  						nil)
   103  				})
   104  
   105  				Context("when the '-f' flag is provided", func() {
   106  					BeforeEach(func() {
   107  						cmd.Force = true
   108  					})
   109  
   110  					It("does not prompt for user confirmation", func() {
   111  						Expect(executeErr).ToNot(HaveOccurred())
   112  
   113  						Expect(testUI.Out).ToNot(Say("Really delete orphaned routes\\? \\[yN\\]:"))
   114  					})
   115  				})
   116  
   117  				Context("when the '-f' flag is not provided", func() {
   118  					Context("when user is prompted for confirmation", func() {
   119  						BeforeEach(func() {
   120  							_, err := input.Write([]byte("\n"))
   121  							Expect(err).NotTo(HaveOccurred())
   122  						})
   123  
   124  						It("displays the interactive prompt", func() {
   125  							Expect(executeErr).ToNot(HaveOccurred())
   126  
   127  							Expect(testUI.Out).To(Say("Really delete orphaned routes\\? \\[yN\\]:"))
   128  						})
   129  					})
   130  
   131  					Context("when the user inputs no", func() {
   132  						BeforeEach(func() {
   133  							_, err := input.Write([]byte("n\n"))
   134  							Expect(err).NotTo(HaveOccurred())
   135  						})
   136  
   137  						It("does not delete orphaned routes", func() {
   138  							Expect(executeErr).ToNot(HaveOccurred())
   139  
   140  							Expect(fakeActor.GetOrphanedRoutesBySpaceCallCount()).To(Equal(0))
   141  							Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0))
   142  						})
   143  					})
   144  
   145  					Context("when the user input is invalid", func() {
   146  						BeforeEach(func() {
   147  							_, err := input.Write([]byte("e\n"))
   148  							Expect(err).NotTo(HaveOccurred())
   149  						})
   150  
   151  						It("returns an error", func() {
   152  							Expect(executeErr).To(HaveOccurred())
   153  
   154  							Expect(fakeActor.GetOrphanedRoutesBySpaceCallCount()).To(Equal(0))
   155  							Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0))
   156  						})
   157  					})
   158  
   159  					Context("when the user inputs yes", func() {
   160  						var routes []v2action.Route
   161  
   162  						BeforeEach(func() {
   163  							_, err := input.Write([]byte("y\n"))
   164  							Expect(err).NotTo(HaveOccurred())
   165  
   166  							routes = []v2action.Route{
   167  								{
   168  									GUID: "route-1-guid",
   169  									Host: "route-1",
   170  									Domain: v2action.Domain{
   171  										Name: "bosh-lite.com",
   172  									},
   173  									Path: "/path",
   174  								},
   175  								{
   176  									GUID: "route-2-guid",
   177  									Host: "route-2",
   178  									Domain: v2action.Domain{
   179  										Name: "bosh-lite.com",
   180  									},
   181  								},
   182  							}
   183  
   184  							fakeActor.GetOrphanedRoutesBySpaceReturns(routes, nil, nil)
   185  						})
   186  
   187  						It("displays getting routes message", func() {
   188  							Expect(executeErr).ToNot(HaveOccurred())
   189  
   190  							Expect(testUI.Out).To(Say("Getting routes as some-user ...\n"))
   191  						})
   192  
   193  						It("deletes the routes and displays that they are deleted", func() {
   194  							Expect(executeErr).ToNot(HaveOccurred())
   195  
   196  							Expect(fakeActor.GetOrphanedRoutesBySpaceCallCount()).To(Equal(1))
   197  							Expect(fakeActor.GetOrphanedRoutesBySpaceArgsForCall(0)).To(Equal("some-space-guid"))
   198  							Expect(fakeActor.DeleteRouteCallCount()).To(Equal(2))
   199  							Expect(fakeActor.DeleteRouteArgsForCall(0)).To(Equal(routes[0].GUID))
   200  							Expect(fakeActor.DeleteRouteArgsForCall(1)).To(Equal(routes[1].GUID))
   201  
   202  							Expect(testUI.Out).To(Say("Deleting route route-1.bosh-lite.com/path..."))
   203  							Expect(testUI.Out).To(Say("Deleting route route-2.bosh-lite.com..."))
   204  							Expect(testUI.Out).To(Say("OK"))
   205  						})
   206  
   207  						Context("when there are warnings", func() {
   208  							BeforeEach(func() {
   209  								fakeActor.GetOrphanedRoutesBySpaceReturns(
   210  									[]v2action.Route{{GUID: "some-route-guid"}},
   211  									[]string{"foo", "bar"},
   212  									nil)
   213  								fakeActor.DeleteRouteReturns([]string{"baz"}, nil)
   214  							})
   215  
   216  							It("displays the warnings", func() {
   217  								Expect(executeErr).ToNot(HaveOccurred())
   218  
   219  								Expect(testUI.Err).To(Say("foo"))
   220  								Expect(testUI.Err).To(Say("bar"))
   221  								Expect(testUI.Err).To(Say("baz"))
   222  							})
   223  						})
   224  
   225  						Context("when getting the routes returns an error", func() {
   226  							var expectedErr error
   227  
   228  							Context("when the error is a DomainNotFoundError", func() {
   229  								BeforeEach(func() {
   230  									fakeActor.GetOrphanedRoutesBySpaceReturns(
   231  										nil,
   232  										nil,
   233  										v2action.DomainNotFoundError{
   234  											Name: "some-domain",
   235  											GUID: "some-domain-guid",
   236  										},
   237  									)
   238  								})
   239  
   240  								It("returns translatableerror.DomainNotFoundError", func() {
   241  									Expect(executeErr).To(MatchError(translatableerror.DomainNotFoundError{
   242  										Name: "some-domain",
   243  										GUID: "some-domain-guid",
   244  									}))
   245  								})
   246  							})
   247  
   248  							Context("when the error is an OrphanedRoutesNotFoundError", func() {
   249  								BeforeEach(func() {
   250  									expectedErr = v2action.OrphanedRoutesNotFoundError{}
   251  									fakeActor.GetOrphanedRoutesBySpaceReturns(nil, nil, expectedErr)
   252  								})
   253  
   254  								It("should not return an error and only display 'OK'", func() {
   255  									Expect(executeErr).ToNot(HaveOccurred())
   256  
   257  									Expect(fakeActor.DeleteRouteCallCount()).To(Equal(0))
   258  								})
   259  							})
   260  
   261  							Context("when there is a generic error", func() {
   262  								BeforeEach(func() {
   263  									expectedErr = errors.New("getting orphaned routes error")
   264  									fakeActor.GetOrphanedRoutesBySpaceReturns(nil, nil, expectedErr)
   265  								})
   266  
   267  								It("returns the error", func() {
   268  									Expect(executeErr).To(MatchError(expectedErr))
   269  								})
   270  							})
   271  						})
   272  
   273  						Context("when deleting a route returns an error", func() {
   274  							var expectedErr error
   275  
   276  							BeforeEach(func() {
   277  								expectedErr = errors.New("deleting route error")
   278  								fakeActor.GetOrphanedRoutesBySpaceReturns(
   279  									[]v2action.Route{{GUID: "some-route-guid"}},
   280  									nil,
   281  									nil)
   282  								fakeActor.DeleteRouteReturns(nil, expectedErr)
   283  							})
   284  
   285  							It("returns the error", func() {
   286  								Expect(executeErr).To(MatchError(expectedErr))
   287  							})
   288  						})
   289  					})
   290  				})
   291  			})
   292  		})
   293  	})
   294  })