github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/delete_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  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("delete Command", func() {
    20  	var (
    21  		cmd             DeleteCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeDeleteActor
    26  		input           *Buffer
    27  		binaryName      string
    28  		executeErr      error
    29  		app             string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		input = NewBuffer()
    34  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v7fakes.FakeDeleteActor)
    38  
    39  		binaryName = "faceman"
    40  		fakeConfig.BinaryNameReturns(binaryName)
    41  		app = "some-app"
    42  
    43  		cmd = DeleteCommand{
    44  			RequiredArgs: flag.AppName{AppName: app},
    45  
    46  			UI:          testUI,
    47  			Config:      fakeConfig,
    48  			SharedActor: fakeSharedActor,
    49  			Actor:       fakeActor,
    50  		}
    51  
    52  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    53  			Name: "some-org",
    54  			GUID: "some-org-guid",
    55  		})
    56  
    57  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    58  			Name: "some-space",
    59  			GUID: "some-space-guid",
    60  		})
    61  
    62  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    63  	})
    64  
    65  	JustBeforeEach(func() {
    66  		executeErr = cmd.Execute(nil)
    67  	})
    68  
    69  	When("the -r flag is provided", func() {
    70  		BeforeEach(func() {
    71  			cmd.DeleteMappedRoutes = true
    72  			cmd.Force = false
    73  			_, err := input.Write([]byte("y\n"))
    74  			Expect(err).ToNot(HaveOccurred())
    75  
    76  			fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil)
    77  		})
    78  
    79  		It("asks for a special prompt about deleting associated routes", func() {
    80  			Expect(executeErr).ToNot(HaveOccurred())
    81  
    82  			Expect(testUI.Err).To(Say("some-warning"))
    83  			Expect(testUI.Out).To(Say(`Deleting the app and associated routes will make apps with this route, in any org, unreachable\.`))
    84  			Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
    85  			Expect(testUI.Out).To(Say("OK"))
    86  			Expect(testUI.Out).NotTo(Say(`App 'some-app' does not exist\.`))
    87  		})
    88  
    89  		It("deletes application and mapped routes", func() {
    90  			actualName, actualSpace, deleteMappedRoutes := fakeActor.DeleteApplicationByNameAndSpaceArgsForCall(0)
    91  			Expect(actualName).To(Equal(app))
    92  			Expect(actualSpace).To(Equal(fakeConfig.TargetedSpace().GUID))
    93  			Expect(deleteMappedRoutes).To(BeTrue())
    94  		})
    95  
    96  		When("the route is mapped to a different app", func() {
    97  			BeforeEach(func() {
    98  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, actionerror.RouteBoundToMultipleAppsError{})
    99  			})
   100  
   101  			It("returns the error", func() {
   102  				Expect(executeErr).To(MatchError(actionerror.RouteBoundToMultipleAppsError{}))
   103  			})
   104  
   105  			It("defers showing a tip", func() {
   106  				Expect(testUI.Out).NotTo(Say("TIP"))
   107  				testUI.FlushDeferred()
   108  				Expect(testUI.Out).To(Say(`\n\nTIP: Run 'cf delete some-app' to delete the app and 'cf delete-route' to delete the route\.`))
   109  			})
   110  		})
   111  	})
   112  
   113  	When("checking target fails", func() {
   114  		BeforeEach(func() {
   115  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
   116  		})
   117  
   118  		It("returns an error", func() {
   119  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
   120  
   121  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   122  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   123  			Expect(checkTargetedOrg).To(BeTrue())
   124  			Expect(checkTargetedSpace).To(BeTrue())
   125  		})
   126  	})
   127  
   128  	When("the user is not logged in", func() {
   129  		var expectedErr error
   130  
   131  		BeforeEach(func() {
   132  			expectedErr = errors.New("some current user error")
   133  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   134  		})
   135  
   136  		It("return an error", func() {
   137  			Expect(executeErr).To(Equal(expectedErr))
   138  		})
   139  	})
   140  
   141  	When("the -f flag is NOT provided", func() {
   142  		BeforeEach(func() {
   143  			cmd.Force = false
   144  		})
   145  
   146  		When("the user inputs yes", func() {
   147  			BeforeEach(func() {
   148  				_, err := input.Write([]byte("y\n"))
   149  				Expect(err).ToNot(HaveOccurred())
   150  
   151  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil)
   152  			})
   153  
   154  			It("delegates to the Actor", func() {
   155  				actualName, actualSpace, deleteMappedRoutes := fakeActor.DeleteApplicationByNameAndSpaceArgsForCall(0)
   156  				Expect(actualName).To(Equal(app))
   157  				Expect(actualSpace).To(Equal(fakeConfig.TargetedSpace().GUID))
   158  				Expect(deleteMappedRoutes).To(BeFalse())
   159  			})
   160  
   161  			It("deletes the app", func() {
   162  				Expect(executeErr).ToNot(HaveOccurred())
   163  
   164  				Expect(testUI.Err).To(Say("some-warning"))
   165  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   166  				Expect(testUI.Out).To(Say("OK"))
   167  				Expect(testUI.Out).NotTo(Say(`App 'some-app' does not exist\.`))
   168  			})
   169  		})
   170  
   171  		When("the user inputs no", func() {
   172  			BeforeEach(func() {
   173  				_, err := input.Write([]byte("n\n"))
   174  				Expect(err).ToNot(HaveOccurred())
   175  			})
   176  
   177  			It("cancels the delete", func() {
   178  				Expect(executeErr).ToNot(HaveOccurred())
   179  
   180  				Expect(testUI.Out).To(Say(`App 'some-app' has not been deleted\.`))
   181  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   182  			})
   183  		})
   184  
   185  		When("the user chooses the default", func() {
   186  			BeforeEach(func() {
   187  				_, err := input.Write([]byte("\n"))
   188  				Expect(err).ToNot(HaveOccurred())
   189  			})
   190  
   191  			It("cancels the delete", func() {
   192  				Expect(executeErr).ToNot(HaveOccurred())
   193  
   194  				Expect(testUI.Out).To(Say(`App 'some-app' has not been deleted\.`))
   195  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   196  			})
   197  		})
   198  
   199  		When("the user input is invalid", func() {
   200  			BeforeEach(func() {
   201  				_, err := input.Write([]byte("e\n\n"))
   202  				Expect(err).ToNot(HaveOccurred())
   203  			})
   204  
   205  			It("asks the user again", func() {
   206  				Expect(executeErr).NotTo(HaveOccurred())
   207  
   208  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   209  				Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   210  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   211  
   212  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   213  			})
   214  		})
   215  	})
   216  
   217  	When("the -f flag is provided", func() {
   218  		BeforeEach(func() {
   219  			cmd.Force = true
   220  		})
   221  
   222  		When("deleting the app errors", func() {
   223  			Context("generic error", func() {
   224  				BeforeEach(func() {
   225  					fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, errors.New("some-error"))
   226  				})
   227  
   228  				It("displays all warnings, and returns the erorr", func() {
   229  					Expect(testUI.Err).To(Say("some-warning"))
   230  					Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   231  					Expect(testUI.Out).ToNot(Say("OK"))
   232  					Expect(executeErr).To(MatchError("some-error"))
   233  				})
   234  			})
   235  		})
   236  
   237  		When("the app doesn't exist", func() {
   238  			BeforeEach(func() {
   239  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, actionerror.ApplicationNotFoundError{Name: "some-app"})
   240  			})
   241  
   242  			It("displays all warnings, that the app wasn't found, and does not error", func() {
   243  				Expect(executeErr).ToNot(HaveOccurred())
   244  
   245  				Expect(testUI.Err).To(Say("some-warning"))
   246  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   247  				Expect(testUI.Err).To(Say(`App 'some-app' does not exist\.`))
   248  				Expect(testUI.Out).To(Say("OK"))
   249  			})
   250  		})
   251  
   252  		When("the app exists", func() {
   253  			BeforeEach(func() {
   254  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil)
   255  			})
   256  
   257  			It("displays all warnings, and does not error", func() {
   258  				Expect(executeErr).ToNot(HaveOccurred())
   259  
   260  				Expect(testUI.Err).To(Say("some-warning"))
   261  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   262  				Expect(testUI.Out).To(Say("OK"))
   263  				Expect(testUI.Err).NotTo(Say(`App 'some-app' does not exist\.`))
   264  			})
   265  		})
   266  	})
   267  })