github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  	. "code.cloudfoundry.org/cli/command/v6"
    13  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("v3-delete Command", func() {
    22  	var (
    23  		cmd             V3DeleteCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v6fakes.FakeV3DeleteActor
    28  		input           *Buffer
    29  		binaryName      string
    30  		executeErr      error
    31  		app             string
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		input = NewBuffer()
    36  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		fakeActor = new(v6fakes.FakeV3DeleteActor)
    40  
    41  		binaryName = "faceman"
    42  		fakeConfig.BinaryNameReturns(binaryName)
    43  		app = "some-app"
    44  
    45  		cmd = V3DeleteCommand{
    46  			RequiredArgs: flag.AppName{AppName: app},
    47  
    48  			UI:          testUI,
    49  			Config:      fakeConfig,
    50  			SharedActor: fakeSharedActor,
    51  			Actor:       fakeActor,
    52  		}
    53  
    54  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    55  			Name: "some-org",
    56  			GUID: "some-org-guid",
    57  		})
    58  
    59  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    60  			Name: "some-space",
    61  			GUID: "some-space-guid",
    62  		})
    63  
    64  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    65  	})
    66  
    67  	JustBeforeEach(func() {
    68  		executeErr = cmd.Execute(nil)
    69  	})
    70  
    71  	When("the API version is below the minimum", func() {
    72  		BeforeEach(func() {
    73  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion)
    74  		})
    75  
    76  		It("returns a MinimumAPIVersionNotMetError", func() {
    77  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    78  				CurrentVersion: ccversion.MinV3ClientVersion,
    79  				MinimumVersion: ccversion.MinVersionApplicationFlowV3,
    80  			}))
    81  		})
    82  
    83  		It("displays the experimental warning", func() {
    84  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    85  		})
    86  	})
    87  
    88  	When("checking target fails", func() {
    89  		BeforeEach(func() {
    90  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
    91  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    92  		})
    93  
    94  		It("returns an error", func() {
    95  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    96  
    97  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    98  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    99  			Expect(checkTargetedOrg).To(BeTrue())
   100  			Expect(checkTargetedSpace).To(BeTrue())
   101  		})
   102  	})
   103  
   104  	When("the user is not logged in", func() {
   105  		var expectedErr error
   106  
   107  		BeforeEach(func() {
   108  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
   109  			expectedErr = errors.New("some current user error")
   110  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   111  		})
   112  
   113  		It("return an error", func() {
   114  			Expect(executeErr).To(Equal(expectedErr))
   115  		})
   116  	})
   117  
   118  	When("the -f flag is NOT provided", func() {
   119  		BeforeEach(func() {
   120  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
   121  			cmd.Force = false
   122  		})
   123  
   124  		When("the user inputs yes", func() {
   125  			BeforeEach(func() {
   126  				_, err := input.Write([]byte("y\n"))
   127  				Expect(err).ToNot(HaveOccurred())
   128  
   129  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, nil)
   130  			})
   131  
   132  			It("deletes the app", func() {
   133  				Expect(executeErr).ToNot(HaveOccurred())
   134  
   135  				Expect(testUI.Err).To(Say("some-warning"))
   136  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   137  				Expect(testUI.Out).To(Say("OK"))
   138  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   139  			})
   140  		})
   141  
   142  		When("the user inputs no", func() {
   143  			BeforeEach(func() {
   144  				_, err := input.Write([]byte("n\n"))
   145  				Expect(err).ToNot(HaveOccurred())
   146  			})
   147  
   148  			It("cancels the delete", func() {
   149  				Expect(executeErr).ToNot(HaveOccurred())
   150  
   151  				Expect(testUI.Out).To(Say("Delete cancelled"))
   152  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   153  			})
   154  		})
   155  
   156  		When("the user chooses the default", func() {
   157  			BeforeEach(func() {
   158  				_, err := input.Write([]byte("\n"))
   159  				Expect(err).ToNot(HaveOccurred())
   160  			})
   161  
   162  			It("cancels the delete", func() {
   163  				Expect(executeErr).ToNot(HaveOccurred())
   164  
   165  				Expect(testUI.Out).To(Say("Delete cancelled"))
   166  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   167  			})
   168  		})
   169  
   170  		When("the user input is invalid", func() {
   171  			BeforeEach(func() {
   172  				_, err := input.Write([]byte("e\n\n"))
   173  				Expect(err).ToNot(HaveOccurred())
   174  			})
   175  
   176  			It("asks the user again", func() {
   177  				Expect(executeErr).NotTo(HaveOccurred())
   178  
   179  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   180  				Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   181  				Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`))
   182  
   183  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   184  			})
   185  		})
   186  	})
   187  
   188  	When("the -f flag is provided", func() {
   189  		BeforeEach(func() {
   190  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3)
   191  			cmd.Force = true
   192  		})
   193  
   194  		When("deleting the app errors", func() {
   195  			Context("generic error", func() {
   196  				BeforeEach(func() {
   197  					fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, errors.New("some-error"))
   198  				})
   199  
   200  				It("displays all warnings, and returns the erorr", func() {
   201  					Expect(testUI.Err).To(Say("some-warning"))
   202  					Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   203  					Expect(testUI.Out).ToNot(Say("OK"))
   204  					Expect(executeErr).To(MatchError("some-error"))
   205  				})
   206  			})
   207  		})
   208  
   209  		When("the app doesn't exist", func() {
   210  			BeforeEach(func() {
   211  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, actionerror.ApplicationNotFoundError{Name: "some-app"})
   212  			})
   213  
   214  			It("displays all warnings, that the app wasn't found, and does not error", func() {
   215  				Expect(executeErr).ToNot(HaveOccurred())
   216  
   217  				Expect(testUI.Err).To(Say("some-warning"))
   218  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   219  				Expect(testUI.Out).To(Say("App some-app does not exist"))
   220  				Expect(testUI.Out).To(Say("OK"))
   221  			})
   222  		})
   223  
   224  		When("the app exists", func() {
   225  			BeforeEach(func() {
   226  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, nil)
   227  			})
   228  
   229  			It("displays all warnings, and does not error", func() {
   230  				Expect(executeErr).ToNot(HaveOccurred())
   231  
   232  				Expect(testUI.Err).To(Say("some-warning"))
   233  				Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`))
   234  				Expect(testUI.Out).To(Say("OK"))
   235  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   236  			})
   237  		})
   238  	})
   239  })