github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/delete_org_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/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v6"
    10  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("delete-org Command", func() {
    19  	var (
    20  		cmd             DeleteOrgCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v6fakes.FakeDeleteOrganizationActor
    25  		input           *Buffer
    26  		binaryName      string
    27  		executeErr      error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		input = NewBuffer()
    32  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v6fakes.FakeDeleteOrganizationActor)
    36  
    37  		cmd = DeleteOrgCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  		}
    43  
    44  		cmd.RequiredArgs.Organization = "some-org"
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  	})
    48  
    49  	JustBeforeEach(func() {
    50  		executeErr = cmd.Execute(nil)
    51  	})
    52  
    53  	When("a cloud controller API endpoint is set", func() {
    54  		BeforeEach(func() {
    55  			fakeConfig.TargetReturns("some-url")
    56  		})
    57  
    58  		When("checking target fails", func() {
    59  			BeforeEach(func() {
    60  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    61  			})
    62  
    63  			It("returns an error", func() {
    64  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    65  
    66  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    68  				Expect(checkTargetedOrg).To(BeFalse())
    69  				Expect(checkTargetedSpace).To(BeFalse())
    70  			})
    71  		})
    72  
    73  		When("the user is logged in", func() {
    74  			When("getting the current user returns an error", func() {
    75  				var returnedErr error
    76  
    77  				BeforeEach(func() {
    78  					returnedErr = errors.New("some error")
    79  					fakeConfig.CurrentUserReturns(configv3.User{}, returnedErr)
    80  				})
    81  
    82  				It("returns the error", func() {
    83  					Expect(executeErr).To(MatchError(returnedErr))
    84  				})
    85  			})
    86  
    87  			When("getting the current user does not return an error", func() {
    88  				BeforeEach(func() {
    89  					fakeConfig.CurrentUserReturns(
    90  						configv3.User{Name: "some-user"},
    91  						nil)
    92  				})
    93  
    94  				When("the '-f' flag is provided", func() {
    95  					BeforeEach(func() {
    96  						cmd.Force = true
    97  					})
    98  
    99  					When("no errors are encountered", func() {
   100  						BeforeEach(func() {
   101  							fakeActor.DeleteOrganizationReturns(v2action.Warnings{"warning-1", "warning-2"}, nil)
   102  						})
   103  
   104  						It("does not prompt for user confirmation, displays warnings, and deletes the org", func() {
   105  							Expect(executeErr).ToNot(HaveOccurred())
   106  
   107  							Expect(testUI.Out).ToNot(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`))
   108  							Expect(testUI.Out).To(Say("Deleting org some-org as some-user..."))
   109  
   110  							Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1))
   111  							orgName := fakeActor.DeleteOrganizationArgsForCall(0)
   112  							Expect(orgName).To(Equal("some-org"))
   113  
   114  							Expect(testUI.Err).To(Say("warning-1"))
   115  							Expect(testUI.Err).To(Say("warning-2"))
   116  							Expect(testUI.Out).To(Say("OK"))
   117  						})
   118  					})
   119  
   120  					When("an error is encountered deleting the org", func() {
   121  						When("the organization does not exist", func() {
   122  							BeforeEach(func() {
   123  								fakeActor.DeleteOrganizationReturns(
   124  									v2action.Warnings{"warning-1", "warning-2"},
   125  									actionerror.OrganizationNotFoundError{
   126  										Name: "some-org",
   127  									},
   128  								)
   129  							})
   130  
   131  							It("returns an OrganizationNotFoundError and displays all warnings", func() {
   132  								Expect(executeErr).NotTo(HaveOccurred())
   133  
   134  								Expect(testUI.Out).To(Say("Deleting org some-org as some-user..."))
   135  
   136  								Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1))
   137  								orgName := fakeActor.DeleteOrganizationArgsForCall(0)
   138  								Expect(orgName).To(Equal("some-org"))
   139  
   140  								Expect(testUI.Err).To(Say("warning-1"))
   141  								Expect(testUI.Err).To(Say("warning-2"))
   142  
   143  								Expect(testUI.Out).To(Say("Org some-org does not exist."))
   144  								Expect(testUI.Out).To(Say("OK"))
   145  							})
   146  						})
   147  
   148  						When("the organization does exist", func() {
   149  							var returnedErr error
   150  
   151  							BeforeEach(func() {
   152  								returnedErr = errors.New("some error")
   153  								fakeActor.DeleteOrganizationReturns(v2action.Warnings{"warning-1", "warning-2"}, returnedErr)
   154  							})
   155  
   156  							It("returns the error, displays all warnings, and does not delete the org", func() {
   157  								Expect(executeErr).To(MatchError(returnedErr))
   158  
   159  								Expect(testUI.Out).To(Say("Deleting org some-org as some-user..."))
   160  
   161  								Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1))
   162  								orgName := fakeActor.DeleteOrganizationArgsForCall(0)
   163  								Expect(orgName).To(Equal("some-org"))
   164  
   165  								Expect(testUI.Err).To(Say("warning-1"))
   166  								Expect(testUI.Err).To(Say("warning-2"))
   167  							})
   168  						})
   169  					})
   170  				})
   171  
   172  				// Testing the prompt.
   173  				When("the '-f' flag is not provided", func() {
   174  					When("the user chooses the default", func() {
   175  						BeforeEach(func() {
   176  							input.Write([]byte("\n"))
   177  						})
   178  
   179  						It("does not delete the org", func() {
   180  							Expect(executeErr).ToNot(HaveOccurred())
   181  
   182  							Expect(testUI.Out).To(Say("Delete cancelled"))
   183  
   184  							Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(0))
   185  						})
   186  					})
   187  
   188  					When("the user inputs no", func() {
   189  						BeforeEach(func() {
   190  							input.Write([]byte("n\n"))
   191  						})
   192  
   193  						It("does not delete the org", func() {
   194  							Expect(executeErr).ToNot(HaveOccurred())
   195  
   196  							Expect(testUI.Out).To(Say("Delete cancelled"))
   197  
   198  							Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(0))
   199  						})
   200  					})
   201  
   202  					When("the user inputs yes", func() {
   203  						BeforeEach(func() {
   204  							input.Write([]byte("y\n"))
   205  						})
   206  
   207  						It("deletes the org", func() {
   208  							Expect(executeErr).ToNot(HaveOccurred())
   209  
   210  							Expect(testUI.Out).To(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`))
   211  							Expect(testUI.Out).To(Say("Deleting org some-org as some-user..."))
   212  
   213  							Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1))
   214  							orgName := fakeActor.DeleteOrganizationArgsForCall(0)
   215  							Expect(orgName).To(Equal("some-org"))
   216  
   217  							Expect(testUI.Out).To(Say("OK"))
   218  						})
   219  					})
   220  
   221  					When("the user input is invalid", func() {
   222  						BeforeEach(func() {
   223  							input.Write([]byte("e\n\n"))
   224  						})
   225  
   226  						It("asks the user again", func() {
   227  							Expect(executeErr).NotTo(HaveOccurred())
   228  
   229  							Expect(testUI.Out).To(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`))
   230  							Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   231  							Expect(testUI.Out).To(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`))
   232  
   233  							Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(0))
   234  						})
   235  					})
   236  
   237  					When("displaying the prompt returns an error", func() {
   238  						// if nothing is written to input, display bool prompt returns EOF
   239  						It("returns the error", func() {
   240  							Expect(executeErr).To(MatchError("EOF"))
   241  						})
   242  					})
   243  				})
   244  
   245  				When("the user deletes the currently targeted org", func() {
   246  					BeforeEach(func() {
   247  						cmd.Force = true
   248  						fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
   249  					})
   250  
   251  					It("clears the targeted org and space from the config", func() {
   252  						Expect(executeErr).ToNot(HaveOccurred())
   253  						Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   254  					})
   255  				})
   256  
   257  				When("the user deletes an org that's not the currently targeted org", func() {
   258  					BeforeEach(func() {
   259  						cmd.Force = true
   260  					})
   261  
   262  					It("does not clear the targeted org and space from the config", func() {
   263  						Expect(executeErr).ToNot(HaveOccurred())
   264  						Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0))
   265  					})
   266  				})
   267  			})
   268  		})
   269  	})
   270  })