github.com/arunkumar7540/cli@v6.45.0+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 = true
    73  		})
    74  
    75  		It("prints a warning", func() {
    76  			Expect(testUI.Err).To(Say("-r flag not implemented - the mapped routes will not be deleted"))
    77  		})
    78  	})
    79  
    80  	When("checking target fails", func() {
    81  		BeforeEach(func() {
    82  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    83  		})
    84  
    85  		It("returns an error", func() {
    86  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    87  
    88  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    89  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    90  			Expect(checkTargetedOrg).To(BeTrue())
    91  			Expect(checkTargetedSpace).To(BeTrue())
    92  		})
    93  	})
    94  
    95  	When("the user is not logged in", func() {
    96  		var expectedErr error
    97  
    98  		BeforeEach(func() {
    99  			expectedErr = errors.New("some current user error")
   100  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   101  		})
   102  
   103  		It("return an error", func() {
   104  			Expect(executeErr).To(Equal(expectedErr))
   105  		})
   106  	})
   107  
   108  	When("the -f flag is NOT provided", func() {
   109  		BeforeEach(func() {
   110  			cmd.Force = false
   111  		})
   112  
   113  		When("the user inputs yes", func() {
   114  			BeforeEach(func() {
   115  				_, err := input.Write([]byte("y\n"))
   116  				Expect(err).ToNot(HaveOccurred())
   117  
   118  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil)
   119  			})
   120  
   121  			It("delegates to the Actor", func() {
   122  				actualName, actualSpace := fakeActor.DeleteApplicationByNameAndSpaceArgsForCall(0)
   123  				Expect(actualName).To(Equal(app))
   124  				Expect(actualSpace).To(Equal(fakeConfig.TargetedSpace().GUID))
   125  			})
   126  
   127  			It("deletes the app", func() {
   128  				Expect(executeErr).ToNot(HaveOccurred())
   129  
   130  				Expect(testUI.Err).To(Say("some-warning"))
   131  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   132  				Expect(testUI.Out).To(Say("OK"))
   133  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   134  			})
   135  		})
   136  
   137  		When("the user inputs no", func() {
   138  			BeforeEach(func() {
   139  				_, err := input.Write([]byte("n\n"))
   140  				Expect(err).ToNot(HaveOccurred())
   141  			})
   142  
   143  			It("cancels the delete", func() {
   144  				Expect(executeErr).ToNot(HaveOccurred())
   145  
   146  				Expect(testUI.Out).To(Say("Delete cancelled"))
   147  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   148  			})
   149  		})
   150  
   151  		When("the user chooses the default", func() {
   152  			BeforeEach(func() {
   153  				_, err := input.Write([]byte("\n"))
   154  				Expect(err).ToNot(HaveOccurred())
   155  			})
   156  
   157  			It("cancels the delete", func() {
   158  				Expect(executeErr).ToNot(HaveOccurred())
   159  
   160  				Expect(testUI.Out).To(Say("Delete cancelled"))
   161  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   162  			})
   163  		})
   164  
   165  		When("the user input is invalid", func() {
   166  			BeforeEach(func() {
   167  				_, err := input.Write([]byte("e\n\n"))
   168  				Expect(err).ToNot(HaveOccurred())
   169  			})
   170  
   171  			It("asks the user again", func() {
   172  				Expect(executeErr).NotTo(HaveOccurred())
   173  
   174  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   175  				Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   176  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   177  
   178  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   179  			})
   180  		})
   181  	})
   182  
   183  	When("the -f flag is provided", func() {
   184  		BeforeEach(func() {
   185  			cmd.Force = true
   186  		})
   187  
   188  		When("deleting the app errors", func() {
   189  			Context("generic error", func() {
   190  				BeforeEach(func() {
   191  					fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, errors.New("some-error"))
   192  				})
   193  
   194  				It("displays all warnings, and returns the erorr", func() {
   195  					Expect(testUI.Err).To(Say("some-warning"))
   196  					Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   197  					Expect(testUI.Out).ToNot(Say("OK"))
   198  					Expect(executeErr).To(MatchError("some-error"))
   199  				})
   200  			})
   201  		})
   202  
   203  		When("the app doesn't exist", func() {
   204  			BeforeEach(func() {
   205  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, actionerror.ApplicationNotFoundError{Name: "some-app"})
   206  			})
   207  
   208  			It("displays all warnings, that the app wasn't found, and does not error", func() {
   209  				Expect(executeErr).ToNot(HaveOccurred())
   210  
   211  				Expect(testUI.Err).To(Say("some-warning"))
   212  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   213  				Expect(testUI.Out).To(Say("App some-app does not exist"))
   214  				Expect(testUI.Out).To(Say("OK"))
   215  			})
   216  		})
   217  
   218  		When("the app exists", func() {
   219  			BeforeEach(func() {
   220  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil)
   221  			})
   222  
   223  			It("displays all warnings, and does not error", func() {
   224  				Expect(executeErr).ToNot(HaveOccurred())
   225  
   226  				Expect(testUI.Err).To(Say("some-warning"))
   227  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   228  				Expect(testUI.Out).To(Say("OK"))
   229  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   230  			})
   231  		})
   232  	})
   233  })