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

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v7action"
     6  	"code.cloudfoundry.org/cli/cf/errors"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	. "code.cloudfoundry.org/cli/command/v7"
     9  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("delete-space-quota Command", func() {
    18  
    19  	var (
    20  		cmd             DeleteSpaceQuotaCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		input           *Buffer
    26  		binaryName      string
    27  		quotaName       string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		input = NewBuffer()
    33  		fakeActor = new(v7fakes.FakeActor)
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    37  
    38  		binaryName = "faceman"
    39  		fakeConfig.BinaryNameReturns(binaryName)
    40  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    41  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org", GUID: "targeted-org-guid"})
    42  		fakeConfig.TargetedOrganizationNameReturns("some-org")
    43  
    44  		cmd = DeleteSpaceQuotaCommand{
    45  			BaseCommand: BaseCommand{
    46  				Actor:       fakeActor,
    47  				UI:          testUI,
    48  				Config:      fakeConfig,
    49  				SharedActor: fakeSharedActor,
    50  			},
    51  		}
    52  
    53  		quotaName = "some-quota"
    54  		cmd.RequiredArgs.Quota = quotaName
    55  		cmd.Force = true
    56  	})
    57  
    58  	JustBeforeEach(func() {
    59  		executeErr = cmd.Execute(nil)
    60  	})
    61  
    62  	When("checking target fails", func() {
    63  		BeforeEach(func() {
    64  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    65  		})
    66  
    67  		It("returns an error if the check fails", func() {
    68  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    69  
    70  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    71  			shouldCheckTargetedOrg, shouldCheckTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    72  			Expect(shouldCheckTargetedOrg).To(BeTrue())
    73  			Expect(shouldCheckTargetedSpace).To(BeFalse())
    74  		})
    75  	})
    76  
    77  	When("the deletion completes successfully", func() {
    78  		BeforeEach(func() {
    79  			fakeActor.DeleteSpaceQuotaByNameReturns(v7action.Warnings{"some-warning"}, nil)
    80  		})
    81  
    82  		When("--force is specified", func() {
    83  			BeforeEach(func() {
    84  				cmd.Force = true
    85  			})
    86  
    87  			It("deletes the quota with warnings and appropriate output", func() {
    88  				Expect(fakeActor.DeleteSpaceQuotaByNameCallCount()).To(Equal(1))
    89  
    90  				givenQuotaName, givenOrgGUID := fakeActor.DeleteSpaceQuotaByNameArgsForCall(0)
    91  				Expect(givenQuotaName).To(Equal(quotaName))
    92  				Expect(givenOrgGUID).To(Equal("targeted-org-guid"))
    93  
    94  				Expect(testUI.Err).To(Say("some-warning"))
    95  				Expect(testUI.Out).To(Say("Deleting space quota some-quota for org some-org as some-user..."))
    96  				Expect(testUI.Out).To(Say("OK"))
    97  			})
    98  		})
    99  
   100  		When("--force is not specified", func() {
   101  			BeforeEach(func() {
   102  				cmd.Force = false
   103  			})
   104  
   105  			When("the user inputs yes", func() {
   106  				BeforeEach(func() {
   107  					_, err := input.Write([]byte("y\n"))
   108  					Expect(err).ToNot(HaveOccurred())
   109  				})
   110  
   111  				It("goes through with the deletion", func() {
   112  					Expect(testUI.Out).To(Say("Really delete the space quota some-quota in org some-org?"))
   113  					Expect(testUI.Out).To(Say("Deleting space quota some-quota for org some-org as some-user..."))
   114  					Expect(testUI.Out).To(Say("OK"))
   115  				})
   116  			})
   117  
   118  			When("the user inputs no", func() {
   119  				BeforeEach(func() {
   120  					_, err := input.Write([]byte("n\n"))
   121  					Expect(err).ToNot(HaveOccurred())
   122  				})
   123  
   124  				It("cancels the delete", func() {
   125  					Expect(testUI.Out).To(Say("Really delete the space quota some-quota in org some-org?"))
   126  					Expect(testUI.Out).To(Say("'some-quota' has not been deleted."))
   127  					Expect(testUI.Out).NotTo(Say("Deleting space quota some-quota for org some-org as some-user..."))
   128  				})
   129  			})
   130  
   131  			When("the user presses enter without inputting any text", func() {
   132  				BeforeEach(func() {
   133  					_, err := input.Write([]byte("\n"))
   134  					Expect(err).ToNot(HaveOccurred())
   135  				})
   136  
   137  				It("defaults to canceling the delete", func() {
   138  					Expect(testUI.Out).To(Say("Really delete the space quota some-quota in org some-org?"))
   139  					Expect(testUI.Out).To(Say("'some-quota' has not been deleted."))
   140  					Expect(testUI.Out).NotTo(Say("Deleting space quota some-quota for org some-org as some-user..."))
   141  				})
   142  			})
   143  		})
   144  	})
   145  
   146  	When("the deletion request returns an error", func() {
   147  		BeforeEach(func() {
   148  			fakeActor.DeleteSpaceQuotaByNameReturns(
   149  				v7action.Warnings{"a-warning"},
   150  				errors.New("uh oh"),
   151  			)
   152  		})
   153  
   154  		It("prints warnings and returns error", func() {
   155  			Expect(executeErr).To(MatchError("uh oh"))
   156  			Expect(testUI.Err).To(Say("a-warning"))
   157  		})
   158  	})
   159  
   160  	When("the quota does not exist", func() {
   161  		BeforeEach(func() {
   162  			fakeActor.DeleteSpaceQuotaByNameReturns(
   163  				v7action.Warnings{"a-warning"},
   164  				actionerror.SpaceQuotaNotFoundForNameError{Name: quotaName},
   165  			)
   166  		})
   167  
   168  		It("prints warnings and helpful message, but exits with OK", func() {
   169  			Expect(executeErr).NotTo(HaveOccurred())
   170  			Expect(testUI.Err).To(Say("a-warning"))
   171  			Expect(testUI.Err).To(Say(`Space quota with name 'some-quota' not found\.`))
   172  			Expect(testUI.Out).To(Say(`OK`))
   173  		})
   174  	})
   175  })