code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/delete_org_command_test.go (about)

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