github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/delete_org_command_test.go (about)

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