github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/unset_space_quota_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/v7action"
     5  	"code.cloudfoundry.org/cli/cf/errors"
     6  	"code.cloudfoundry.org/cli/command/commandfakes"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
     9  	"code.cloudfoundry.org/cli/util/configv3"
    10  	"code.cloudfoundry.org/cli/util/ui"
    11  
    12  	. "code.cloudfoundry.org/cli/command/v7"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("unset-space-quota Command", func() {
    19  	var (
    20  		cmd             UnsetSpaceQuotaCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		binaryName      string
    26  		executeErr      error
    27  		input           *Buffer
    28  
    29  		unsetQuotaWarning string
    30  		currentUser       = "some-user"
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		input = NewBuffer()
    35  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeActor)
    39  		unsetQuotaWarning = RandomString("unset-quota-warning")
    40  
    41  		cmd = UnsetSpaceQuotaCommand{
    42  			BaseCommand: BaseCommand{
    43  				UI:          testUI,
    44  				Config:      fakeConfig,
    45  				SharedActor: fakeSharedActor,
    46  				Actor:       fakeActor,
    47  			},
    48  		}
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("the environment is not set up correctly", func() {
    59  		BeforeEach(func() {
    60  			fakeSharedActor.CheckTargetReturns(errors.New("check-target-failure"))
    61  		})
    62  
    63  		It("does not require a targeted org or space", func() {
    64  			targetedOrgRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    65  			Expect(targetedOrgRequired).To(BeTrue())
    66  			Expect(targetedSpaceRequired).To(BeFalse())
    67  		})
    68  
    69  		It("should return the error", func() {
    70  			Expect(executeErr).To(MatchError("check-target-failure"))
    71  		})
    72  	})
    73  
    74  	When("getting the current user fails", func() {
    75  		BeforeEach(func() {
    76  			fakeConfig.CurrentUserNameReturns("", errors.New("current-user-error"))
    77  		})
    78  
    79  		It("returns the error", func() {
    80  			Expect(executeErr).To(MatchError("current-user-error"))
    81  		})
    82  	})
    83  
    84  	When("unsetting the quota succeeds", func() {
    85  		BeforeEach(func() {
    86  			cmd.RequiredArgs = flag.UnsetSpaceQuotaArgs{}
    87  			cmd.RequiredArgs.Space = RandomString("space-name")
    88  			cmd.RequiredArgs.SpaceQuota = RandomString("space-quota-name")
    89  
    90  			fakeActor.UnsetSpaceQuotaReturns(
    91  				v7action.Warnings{unsetQuotaWarning},
    92  				nil)
    93  
    94  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    95  				Name: "some-org-name",
    96  				GUID: "some-org-guid",
    97  			})
    98  
    99  			fakeConfig.CurrentUserNameReturns(currentUser, nil)
   100  
   101  		})
   102  
   103  		It("unsets the quota to the organization", func() {
   104  			Expect(fakeActor.UnsetSpaceQuotaCallCount()).To(Equal(1))
   105  			spaceQuotaName, spaceName, orgGUID := fakeActor.UnsetSpaceQuotaArgsForCall(0)
   106  			Expect(spaceQuotaName).To(Equal(cmd.RequiredArgs.SpaceQuota))
   107  			Expect(spaceName).To(Equal(cmd.RequiredArgs.Space))
   108  			Expect(orgGUID).To(Equal("some-org-guid"))
   109  		})
   110  
   111  		It("displays warnings and returns without error", func() {
   112  			Expect(executeErr).NotTo(HaveOccurred())
   113  			Expect(testUI.Err).To(Say(unsetQuotaWarning))
   114  		})
   115  
   116  		It("displays flavor text and returns without error", func() {
   117  			Expect(executeErr).NotTo(HaveOccurred())
   118  
   119  			Expect(testUI.Out).To(Say("Unassigning space quota %s from space %s as %s...", cmd.RequiredArgs.SpaceQuota, cmd.RequiredArgs.Space, currentUser))
   120  			Expect(testUI.Out).To(Say("OK"))
   121  		})
   122  	})
   123  
   124  	When("the quota is already dissociated", func() {
   125  		BeforeEach(func() {
   126  			cmd.RequiredArgs = flag.UnsetSpaceQuotaArgs{}
   127  			cmd.RequiredArgs.Space = RandomString("space-name")
   128  			cmd.RequiredArgs.SpaceQuota = RandomString("space-quota-name")
   129  
   130  			fakeActor.UnsetSpaceQuotaReturns(
   131  				v7action.Warnings{unsetQuotaWarning},
   132  				nil)
   133  
   134  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   135  				Name: "some-org-name",
   136  				GUID: "some-org-guid",
   137  			})
   138  
   139  			fakeConfig.CurrentUserNameReturns(currentUser, nil)
   140  
   141  		})
   142  
   143  		It("unsets the quota to the organization", func() {
   144  			Expect(fakeActor.UnsetSpaceQuotaCallCount()).To(Equal(1))
   145  			spaceQuotaName, spaceName, orgGUID := fakeActor.UnsetSpaceQuotaArgsForCall(0)
   146  			Expect(spaceQuotaName).To(Equal(cmd.RequiredArgs.SpaceQuota))
   147  			Expect(spaceName).To(Equal(cmd.RequiredArgs.Space))
   148  			Expect(orgGUID).To(Equal("some-org-guid"))
   149  		})
   150  
   151  		It("displays warnings and returns without error", func() {
   152  			Expect(executeErr).NotTo(HaveOccurred())
   153  			Expect(testUI.Err).To(Say(unsetQuotaWarning))
   154  		})
   155  
   156  		It("displays flavor text and returns without error", func() {
   157  			Expect(executeErr).NotTo(HaveOccurred())
   158  
   159  			Expect(testUI.Out).To(Say("Unassigning space quota %s from space %s as %s...", cmd.RequiredArgs.SpaceQuota, cmd.RequiredArgs.Space, currentUser))
   160  			Expect(testUI.Out).To(Say("OK"))
   161  		})
   162  	})
   163  })