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