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

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v6"
    11  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("v3-delete Command", func() {
    20  	var (
    21  		cmd             V3DeleteCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v6fakes.FakeV3DeleteActor
    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(v6fakes.FakeV3DeleteActor)
    38  
    39  		binaryName = "faceman"
    40  		fakeConfig.BinaryNameReturns(binaryName)
    41  		app = "some-app"
    42  
    43  		cmd = V3DeleteCommand{
    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  	It("displays the experimental warning", func() {
    70  		Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    71  	})
    72  
    73  	When("checking target fails", func() {
    74  		BeforeEach(func() {
    75  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    76  		})
    77  
    78  		It("returns an error", func() {
    79  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    80  
    81  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    82  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    83  			Expect(checkTargetedOrg).To(BeTrue())
    84  			Expect(checkTargetedSpace).To(BeTrue())
    85  		})
    86  	})
    87  
    88  	When("the user is not logged in", func() {
    89  		var expectedErr error
    90  
    91  		BeforeEach(func() {
    92  			expectedErr = errors.New("some current user error")
    93  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    94  		})
    95  
    96  		It("return an error", func() {
    97  			Expect(executeErr).To(Equal(expectedErr))
    98  		})
    99  	})
   100  
   101  	When("the -f flag is NOT provided", func() {
   102  		BeforeEach(func() {
   103  			cmd.Force = false
   104  		})
   105  
   106  		When("the user inputs yes", func() {
   107  			BeforeEach(func() {
   108  				_, err := input.Write([]byte("y\n"))
   109  				Expect(err).ToNot(HaveOccurred())
   110  
   111  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, nil)
   112  			})
   113  
   114  			It("deletes the app", func() {
   115  				Expect(executeErr).ToNot(HaveOccurred())
   116  
   117  				Expect(testUI.Err).To(Say("some-warning"))
   118  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   119  				Expect(testUI.Out).To(Say("OK"))
   120  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   121  			})
   122  		})
   123  
   124  		When("the user inputs no", func() {
   125  			BeforeEach(func() {
   126  				_, err := input.Write([]byte("n\n"))
   127  				Expect(err).ToNot(HaveOccurred())
   128  			})
   129  
   130  			It("cancels the delete", func() {
   131  				Expect(executeErr).ToNot(HaveOccurred())
   132  
   133  				Expect(testUI.Out).To(Say("Delete cancelled"))
   134  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   135  			})
   136  		})
   137  
   138  		When("the user chooses the default", func() {
   139  			BeforeEach(func() {
   140  				_, err := input.Write([]byte("\n"))
   141  				Expect(err).ToNot(HaveOccurred())
   142  			})
   143  
   144  			It("cancels the delete", func() {
   145  				Expect(executeErr).ToNot(HaveOccurred())
   146  
   147  				Expect(testUI.Out).To(Say("Delete cancelled"))
   148  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   149  			})
   150  		})
   151  
   152  		When("the user input is invalid", func() {
   153  			BeforeEach(func() {
   154  				_, err := input.Write([]byte("e\n\n"))
   155  				Expect(err).ToNot(HaveOccurred())
   156  			})
   157  
   158  			It("asks the user again", func() {
   159  				Expect(executeErr).NotTo(HaveOccurred())
   160  
   161  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   162  				Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   163  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   164  
   165  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   166  			})
   167  		})
   168  	})
   169  
   170  	When("the -f flag is provided", func() {
   171  		BeforeEach(func() {
   172  			cmd.Force = true
   173  		})
   174  
   175  		When("deleting the app errors", func() {
   176  			Context("generic error", func() {
   177  				BeforeEach(func() {
   178  					fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, errors.New("some-error"))
   179  				})
   180  
   181  				It("displays all warnings, and returns the erorr", func() {
   182  					Expect(testUI.Err).To(Say("some-warning"))
   183  					Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   184  					Expect(testUI.Out).ToNot(Say("OK"))
   185  					Expect(executeErr).To(MatchError("some-error"))
   186  				})
   187  			})
   188  		})
   189  
   190  		When("the app doesn't exist", func() {
   191  			BeforeEach(func() {
   192  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, actionerror.ApplicationNotFoundError{Name: "some-app"})
   193  			})
   194  
   195  			It("displays all warnings, that the app wasn't found, and does not error", func() {
   196  				Expect(executeErr).ToNot(HaveOccurred())
   197  
   198  				Expect(testUI.Err).To(Say("some-warning"))
   199  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   200  				Expect(testUI.Out).To(Say("App some-app does not exist"))
   201  				Expect(testUI.Out).To(Say("OK"))
   202  			})
   203  		})
   204  
   205  		When("the app exists", func() {
   206  			BeforeEach(func() {
   207  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, nil)
   208  			})
   209  
   210  			It("displays all warnings, and does not error", func() {
   211  				Expect(executeErr).ToNot(HaveOccurred())
   212  
   213  				Expect(testUI.Err).To(Say("some-warning"))
   214  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   215  				Expect(testUI.Out).To(Say("OK"))
   216  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   217  			})
   218  		})
   219  	})
   220  })