github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_delete_command_test.go (about)

     1  package v3_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/v3"
    13  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    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             v3.V3DeleteCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v3fakes.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(v3fakes.FakeV3DeleteActor)
    40  
    41  		binaryName = "faceman"
    42  		fakeConfig.BinaryNameReturns(binaryName)
    43  		app = "some-app"
    44  
    45  		cmd = v3.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  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3)
    66  	})
    67  
    68  	JustBeforeEach(func() {
    69  		executeErr = cmd.Execute(nil)
    70  	})
    71  
    72  	Context("when the API version is below the minimum", func() {
    73  		BeforeEach(func() {
    74  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    75  		})
    76  
    77  		It("returns a MinimumAPIVersionNotMetError", func() {
    78  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    79  				CurrentVersion: "0.0.0",
    80  				MinimumVersion: ccversion.MinVersionV3,
    81  			}))
    82  		})
    83  
    84  		It("displays the experimental warning", func() {
    85  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    86  		})
    87  	})
    88  
    89  	Context("when checking target fails", func() {
    90  		BeforeEach(func() {
    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  	Context("when the user is not logged in", func() {
   105  		var expectedErr error
   106  
   107  		BeforeEach(func() {
   108  			expectedErr = errors.New("some current user error")
   109  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   110  		})
   111  
   112  		It("return an error", func() {
   113  			Expect(executeErr).To(Equal(expectedErr))
   114  		})
   115  	})
   116  
   117  	Context("when the -f flag is NOT provided", func() {
   118  		BeforeEach(func() {
   119  			cmd.Force = false
   120  		})
   121  
   122  		Context("when the user inputs yes", func() {
   123  			BeforeEach(func() {
   124  				_, err := input.Write([]byte("y\n"))
   125  				Expect(err).ToNot(HaveOccurred())
   126  
   127  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, nil)
   128  			})
   129  
   130  			It("deletes the app", func() {
   131  				Expect(executeErr).ToNot(HaveOccurred())
   132  
   133  				Expect(testUI.Err).To(Say("some-warning"))
   134  				Expect(testUI.Out).To(Say("Deleting app some-app in org some-org / space some-space as steve\\.\\.\\."))
   135  				Expect(testUI.Out).To(Say("OK"))
   136  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   137  			})
   138  		})
   139  
   140  		Context("when the user inputs no", func() {
   141  			BeforeEach(func() {
   142  				_, err := input.Write([]byte("n\n"))
   143  				Expect(err).ToNot(HaveOccurred())
   144  			})
   145  
   146  			It("cancels the delete", func() {
   147  				Expect(executeErr).ToNot(HaveOccurred())
   148  
   149  				Expect(testUI.Out).To(Say("Delete cancelled"))
   150  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   151  			})
   152  		})
   153  
   154  		Context("when the user chooses the default", func() {
   155  			BeforeEach(func() {
   156  				_, err := input.Write([]byte("\n"))
   157  				Expect(err).ToNot(HaveOccurred())
   158  			})
   159  
   160  			It("cancels the delete", func() {
   161  				Expect(executeErr).ToNot(HaveOccurred())
   162  
   163  				Expect(testUI.Out).To(Say("Delete cancelled"))
   164  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   165  			})
   166  		})
   167  
   168  		Context("when the user input is invalid", func() {
   169  			BeforeEach(func() {
   170  				_, err := input.Write([]byte("e\n\n"))
   171  				Expect(err).ToNot(HaveOccurred())
   172  			})
   173  
   174  			It("asks the user again", func() {
   175  				Expect(executeErr).NotTo(HaveOccurred())
   176  
   177  				Expect(testUI.Out).To(Say("Really delete the app some-app\\? \\[yN\\]"))
   178  				Expect(testUI.Out).To(Say("invalid input \\(not y, n, yes, or no\\)"))
   179  				Expect(testUI.Out).To(Say("Really delete the app some-app\\? \\[yN\\]"))
   180  
   181  				Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0))
   182  			})
   183  		})
   184  	})
   185  
   186  	Context("when the -f flag is provided", func() {
   187  		BeforeEach(func() {
   188  			cmd.Force = true
   189  		})
   190  
   191  		Context("when deleting the app errors", func() {
   192  			Context("generic error", func() {
   193  				BeforeEach(func() {
   194  					fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, errors.New("some-error"))
   195  				})
   196  
   197  				It("displays all warnings, and returns the erorr", func() {
   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).ToNot(Say("OK"))
   201  					Expect(executeErr).To(MatchError("some-error"))
   202  				})
   203  			})
   204  		})
   205  
   206  		Context("when the app doesn't exist", func() {
   207  			BeforeEach(func() {
   208  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, actionerror.ApplicationNotFoundError{Name: "some-app"})
   209  			})
   210  
   211  			It("displays all warnings, that the app wasn't found, and does not error", func() {
   212  				Expect(executeErr).ToNot(HaveOccurred())
   213  
   214  				Expect(testUI.Err).To(Say("some-warning"))
   215  				Expect(testUI.Out).To(Say("Deleting app some-app in org some-org / space some-space as steve\\.\\.\\."))
   216  				Expect(testUI.Out).To(Say("App some-app does not exist"))
   217  				Expect(testUI.Out).To(Say("OK"))
   218  			})
   219  		})
   220  
   221  		Context("when the app exists", func() {
   222  			BeforeEach(func() {
   223  				fakeActor.DeleteApplicationByNameAndSpaceReturns(v3action.Warnings{"some-warning"}, nil)
   224  			})
   225  
   226  			It("displays all warnings, and does not error", func() {
   227  				Expect(executeErr).ToNot(HaveOccurred())
   228  
   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).To(Say("OK"))
   232  				Expect(testUI.Out).NotTo(Say("App some-app does not exist"))
   233  			})
   234  		})
   235  	})
   236  })