github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/spacequota/unset_space_quota_test.go (about)

     1  package spacequota_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes"
     7  	"code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("unset-space-quota command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		quotaRepo           *spacequotasfakes.FakeSpaceQuotaRepository
    26  		spaceRepo           *spacesfakes.FakeSpaceRepository
    27  		requirementsFactory *requirementsfakes.FakeFactory
    28  		configRepo          coreconfig.Repository
    29  		deps                commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.Config = configRepo
    35  		deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo)
    36  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    37  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unset-space-quota").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository)
    44  		spaceRepo = new(spacesfakes.FakeSpaceRepository)
    45  		requirementsFactory = new(requirementsfakes.FakeFactory)
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCLICommand("unset-space-quota", args, requirementsFactory, updateCommandDependency, false, ui)
    50  	}
    51  
    52  	It("fails with usage when provided too many or two few args", func() {
    53  		runCommand("space")
    54  		Expect(ui.Outputs()).To(ContainSubstrings(
    55  			[]string{"Incorrect Usage", "Requires", "arguments"},
    56  		))
    57  
    58  		runCommand("space", "quota", "extra-stuff")
    59  		Expect(ui.Outputs()).To(ContainSubstrings(
    60  			[]string{"Incorrect Usage", "Requires", "arguments"},
    61  		))
    62  	})
    63  
    64  	Describe("requirements", func() {
    65  		It("requires the user to be logged in", func() {
    66  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    67  
    68  			Expect(runCommand("space", "quota")).To(BeFalse())
    69  		})
    70  
    71  		It("requires the user to target an org", func() {
    72  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    73  			orgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    74  			orgReq.ExecuteReturns(errors.New("not targeting org"))
    75  			requirementsFactory.NewTargetedOrgRequirementReturns(orgReq)
    76  
    77  			Expect(runCommand("space", "quota")).To(BeFalse())
    78  		})
    79  	})
    80  
    81  	Context("when requirements are met", func() {
    82  		BeforeEach(func() {
    83  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    84  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    85  		})
    86  
    87  		It("unassigns a quota from a space", func() {
    88  			space := models.Space{
    89  				SpaceFields: models.SpaceFields{
    90  					Name: "my-space",
    91  					GUID: "my-space-guid",
    92  				},
    93  			}
    94  
    95  			quota := models.SpaceQuota{Name: "my-quota", GUID: "my-quota-guid"}
    96  
    97  			quotaRepo.FindByNameReturns(quota, nil)
    98  			spaceRepo.FindByNameReturns(space, nil)
    99  
   100  			runCommand("my-space", "my-quota")
   101  
   102  			Expect(ui.Outputs()).To(ContainSubstrings(
   103  				[]string{"Unassigning space quota", "my-quota", "my-space", "my-user"},
   104  				[]string{"OK"},
   105  			))
   106  
   107  			Expect(quotaRepo.FindByNameArgsForCall(0)).To(Equal("my-quota"))
   108  			spaceGUID, quotaGUID := quotaRepo.UnassignQuotaFromSpaceArgsForCall(0)
   109  			Expect(spaceGUID).To(Equal("my-space-guid"))
   110  			Expect(quotaGUID).To(Equal("my-quota-guid"))
   111  		})
   112  	})
   113  })