github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/unmap_route_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	. "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("unmap-route Command", func() {
    22  	var (
    23  		cmd             UnmapRouteCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeUnmapRouteActor
    28  		input           *Buffer
    29  		binaryName      string
    30  		executeErr      error
    31  		domain          string
    32  		appName         string
    33  		hostname        string
    34  		path            string
    35  		orgGUID         string
    36  		orgName         string
    37  		spaceGUID       string
    38  		spaceName       string
    39  		userName        string
    40  	)
    41  
    42  	BeforeEach(func() {
    43  		input = NewBuffer()
    44  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    45  		fakeConfig = new(commandfakes.FakeConfig)
    46  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    47  		fakeActor = new(v7fakes.FakeUnmapRouteActor)
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns(binaryName)
    51  		appName = "my-app"
    52  		domain = "some-domain.com"
    53  		hostname = "host"
    54  		path = "/path"
    55  		orgGUID = "some-org-guid"
    56  		orgName = "some-org"
    57  		spaceGUID = "some-space-guid"
    58  		spaceName = "some-space"
    59  		userName = "steve"
    60  
    61  		cmd = UnmapRouteCommand{
    62  			RequiredArgs: flag.AppDomain{App: appName, Domain: domain},
    63  			Hostname:     hostname,
    64  			Path:         flag.V7RoutePath{Path: path},
    65  			UI:           testUI,
    66  			Config:       fakeConfig,
    67  			SharedActor:  fakeSharedActor,
    68  			Actor:        fakeActor,
    69  		}
    70  
    71  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    72  			Name: orgName,
    73  			GUID: orgGUID,
    74  		})
    75  
    76  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    77  			Name: spaceName,
    78  			GUID: spaceGUID,
    79  		})
    80  
    81  		fakeConfig.CurrentUserReturns(configv3.User{Name: userName}, nil)
    82  	})
    83  
    84  	JustBeforeEach(func() {
    85  		executeErr = cmd.Execute(nil)
    86  	})
    87  
    88  	When("checking target fails", func() {
    89  		BeforeEach(func() {
    90  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    91  		})
    92  
    93  		It("returns an error", func() {
    94  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    95  
    96  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    97  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    98  			Expect(checkTargetedOrg).To(BeTrue())
    99  			Expect(checkTargetedSpace).To(BeTrue())
   100  		})
   101  	})
   102  
   103  	When("the user is not logged in", func() {
   104  		var expectedErr error
   105  
   106  		BeforeEach(func() {
   107  			expectedErr = errors.New("some current user error")
   108  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   109  		})
   110  
   111  		It("return an error", func() {
   112  			Expect(executeErr).To(Equal(expectedErr))
   113  		})
   114  	})
   115  
   116  	When("the user is logged in and targeted", func() {
   117  		When("getting the domain errors", func() {
   118  			BeforeEach(func() {
   119  				fakeActor.GetDomainByNameReturns(v7action.Domain{}, v7action.Warnings{"get-domain-warnings"}, errors.New("get-domain-error"))
   120  			})
   121  
   122  			It("returns the error and displays warnings", func() {
   123  				Expect(testUI.Err).To(Say("get-domain-warnings"))
   124  				Expect(executeErr).To(MatchError(errors.New("get-domain-error")))
   125  
   126  				Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   127  				Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domain))
   128  
   129  				Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(0))
   130  				Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(0))
   131  				Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   132  			})
   133  		})
   134  
   135  		When("getting the domain succeeds", func() {
   136  			BeforeEach(func() {
   137  				fakeActor.GetDomainByNameReturns(
   138  					v7action.Domain{Name: "some-domain.com", GUID: "domain-guid"},
   139  					v7action.Warnings{"get-domain-warnings"},
   140  					nil,
   141  				)
   142  			})
   143  
   144  			When("getting the app errors", func() {
   145  				BeforeEach(func() {
   146  					fakeActor.GetApplicationByNameAndSpaceReturns(
   147  						v7action.Application{},
   148  						v7action.Warnings{"get-app-warnings"},
   149  						errors.New("get-app-error"),
   150  					)
   151  				})
   152  
   153  				It("returns the error and displays warnings", func() {
   154  					Expect(testUI.Err).To(Say("get-domain-warnings"))
   155  					Expect(testUI.Err).To(Say("get-app-warnings"))
   156  					Expect(executeErr).To(MatchError(errors.New("get-app-error")))
   157  
   158  					Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   159  					Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domain))
   160  
   161  					Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   162  					actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   163  					Expect(actualAppName).To(Equal(appName))
   164  					Expect(actualSpaceGUID).To(Equal(spaceGUID))
   165  
   166  					Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(0))
   167  					Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   168  				})
   169  			})
   170  
   171  			When("getting the app succeeds", func() {
   172  				BeforeEach(func() {
   173  					fakeActor.GetApplicationByNameAndSpaceReturns(
   174  						v7action.Application{Name: "app", GUID: "app-guid"},
   175  						v7action.Warnings{"get-app-warnings"},
   176  						nil,
   177  					)
   178  				})
   179  
   180  				When("getting the route errors", func() {
   181  					BeforeEach(func() {
   182  						fakeActor.GetRouteByAttributesReturns(
   183  							v7action.Route{},
   184  							v7action.Warnings{"get-route-warnings"},
   185  							errors.New("get-route-error"),
   186  						)
   187  					})
   188  
   189  					It("returns the error and displays warnings", func() {
   190  						Expect(testUI.Err).To(Say("get-domain-warnings"))
   191  						Expect(testUI.Err).To(Say("get-app-warnings"))
   192  						Expect(testUI.Err).To(Say("get-route-warnings"))
   193  						Expect(executeErr).To(MatchError(errors.New("get-route-error")))
   194  
   195  						Expect(fakeActor.GetDomainByNameCallCount()).To(Equal(1))
   196  						Expect(fakeActor.GetDomainByNameArgsForCall(0)).To(Equal(domain))
   197  
   198  						Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   199  						actualAppName, actualSpaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   200  						Expect(actualAppName).To(Equal(appName))
   201  						Expect(actualSpaceGUID).To(Equal(spaceGUID))
   202  
   203  						Expect(fakeActor.GetRouteByAttributesCallCount()).To(Equal(1))
   204  						actualDomainName, actualDomainGUID, actualHostname, actualPath := fakeActor.GetRouteByAttributesArgsForCall(0)
   205  						Expect(actualDomainName).To(Equal("some-domain.com"))
   206  						Expect(actualDomainGUID).To(Equal("domain-guid"))
   207  						Expect(actualHostname).To(Equal(hostname))
   208  						Expect(actualPath).To(Equal(path))
   209  
   210  						Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   211  					})
   212  				})
   213  
   214  				When("getting the route succeeds", func() {
   215  					BeforeEach(func() {
   216  						fakeActor.GetRouteByAttributesReturns(
   217  							v7action.Route{GUID: "route-guid"},
   218  							v7action.Warnings{"get-route-warnings"},
   219  							nil,
   220  						)
   221  					})
   222  
   223  					It("prints flavor text", func() {
   224  						Expect(testUI.Out).To(Say(
   225  							`Removing route %s from app %s in org %s / space %s as %s\.\.\.`,
   226  							"host.some-domain.com/path",
   227  							appName,
   228  							orgName,
   229  							spaceName,
   230  							userName,
   231  						))
   232  					})
   233  
   234  					When("getting the route destination fails because the app is not mapped", func() {
   235  						BeforeEach(func() {
   236  							fakeActor.GetRouteDestinationByAppGUIDReturns(
   237  								v7action.RouteDestination{},
   238  								v7action.Warnings{"get-destination-warning"},
   239  								actionerror.RouteDestinationNotFoundError{},
   240  							)
   241  						})
   242  
   243  						It("prints a message and returns without an error", func() {
   244  							Expect(fakeActor.GetRouteDestinationByAppGUIDCallCount()).To(Equal(1))
   245  							givenRouteGUID, givenAppGUID := fakeActor.GetRouteDestinationByAppGUIDArgsForCall(0)
   246  							Expect(givenRouteGUID).To(Equal("route-guid"))
   247  							Expect(givenAppGUID).To(Equal("app-guid"))
   248  
   249  							Expect(executeErr).NotTo(HaveOccurred())
   250  							Expect(testUI.Err).To(Say("get-destination-warning"))
   251  							Expect(testUI.Out).To(Say("Route to be unmapped is not currently mapped to the application."))
   252  							Expect(testUI.Out).To(Say("OK"))
   253  						})
   254  					})
   255  
   256  					When("getting the route destination fails for another reason", func() {
   257  						BeforeEach(func() {
   258  							fakeActor.GetRouteDestinationByAppGUIDReturns(
   259  								v7action.RouteDestination{},
   260  								v7action.Warnings{"get-destination-warning"},
   261  								errors.New("failed to get destination"),
   262  							)
   263  						})
   264  
   265  						It("prints warnings and returns the error", func() {
   266  							Expect(fakeActor.GetRouteDestinationByAppGUIDCallCount()).To(Equal(1))
   267  							givenRouteGUID, givenAppGUID := fakeActor.GetRouteDestinationByAppGUIDArgsForCall(0)
   268  							Expect(givenRouteGUID).To(Equal("route-guid"))
   269  							Expect(givenAppGUID).To(Equal("app-guid"))
   270  
   271  							Expect(testUI.Err).To(Say("get-destination-warning"))
   272  							Expect(executeErr).To(MatchError("failed to get destination"))
   273  
   274  							Expect(fakeActor.UnmapRouteCallCount()).To(Equal(0))
   275  						})
   276  					})
   277  
   278  					When("getting the route destination succeeds", func() {
   279  						BeforeEach(func() {
   280  							fakeActor.GetRouteDestinationByAppGUIDReturns(
   281  								v7action.RouteDestination{GUID: "destination-guid"},
   282  								v7action.Warnings{"get-destination-warning"},
   283  								nil,
   284  							)
   285  						})
   286  
   287  						When("unmapping the route fails", func() {
   288  							BeforeEach(func() {
   289  								fakeActor.UnmapRouteReturns(
   290  									v7action.Warnings{"unmap-route-warnings"},
   291  									errors.New("failed to unmap route"),
   292  								)
   293  							})
   294  
   295  							It("prints warnings and returns the error", func() {
   296  								Expect(fakeActor.UnmapRouteCallCount()).To(Equal(1))
   297  								givenRouteGUID, givenDestinationGUID := fakeActor.UnmapRouteArgsForCall(0)
   298  								Expect(givenRouteGUID).To(Equal("route-guid"))
   299  								Expect(givenDestinationGUID).To(Equal("destination-guid"))
   300  
   301  								Expect(testUI.Err).To(Say("get-destination-warning"))
   302  
   303  								Expect(executeErr).To(MatchError("failed to unmap route"))
   304  							})
   305  						})
   306  
   307  						When("unmapping the route succeeds", func() {
   308  							BeforeEach(func() {
   309  								fakeActor.UnmapRouteReturns(
   310  									v7action.Warnings{"unmap-route-warnings"},
   311  									nil,
   312  								)
   313  							})
   314  
   315  							It("prints warnings and does not return an error", func() {
   316  								Expect(fakeActor.UnmapRouteCallCount()).To(Equal(1))
   317  								givenRouteGUID, givenDestinationGUID := fakeActor.UnmapRouteArgsForCall(0)
   318  								Expect(givenRouteGUID).To(Equal("route-guid"))
   319  								Expect(givenDestinationGUID).To(Equal("destination-guid"))
   320  
   321  								Expect(testUI.Err).To(Say("get-destination-warning"))
   322  								Expect(testUI.Out).To(Say("OK"))
   323  
   324  								Expect(executeErr).NotTo(HaveOccurred())
   325  							})
   326  						})
   327  					})
   328  				})
   329  			})
   330  		})
   331  	})
   332  })