github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/quota/delete_quota_test.go (about)

     1  package quota_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/quotas/quotasfakes"
     5  	"code.cloudfoundry.org/cli/cf/errors"
     6  	"code.cloudfoundry.org/cli/cf/models"
     7  	"code.cloudfoundry.org/cli/cf/requirements"
     8  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
     9  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    10  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    11  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    12  
    13  	"code.cloudfoundry.org/cli/cf/commandregistry"
    14  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    15  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("delete-quota command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		quotaRepo           *quotasfakes.FakeQuotaRepository
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		configRepo          coreconfig.Repository
    26  		deps                commandregistry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.UI = ui
    31  		deps.Config = configRepo
    32  		deps.RepoLocator = deps.RepoLocator.SetQuotaRepository(quotaRepo)
    33  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-quota").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		ui = &testterm.FakeUI{}
    38  		configRepo = testconfig.NewRepositoryWithDefaults()
    39  		quotaRepo = new(quotasfakes.FakeQuotaRepository)
    40  		requirementsFactory = new(requirementsfakes.FakeFactory)
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		return testcmd.RunCLICommand("delete-quota", args, requirementsFactory, updateCommandDependency, false, ui)
    45  	}
    46  
    47  	Context("when the user is not logged in", func() {
    48  		BeforeEach(func() {
    49  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    50  		})
    51  
    52  		It("fails requirements", func() {
    53  			Expect(runCommand("my-quota")).To(BeFalse())
    54  		})
    55  	})
    56  
    57  	Context("when the user is logged in", func() {
    58  		BeforeEach(func() {
    59  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    60  		})
    61  
    62  		It("fails requirements when called without a quota name", func() {
    63  			runCommand()
    64  			Expect(ui.Outputs()).To(ContainSubstrings(
    65  				[]string{"Incorrect Usage", "Requires an argument"},
    66  			))
    67  		})
    68  
    69  		Context("When the quota provided exists", func() {
    70  			BeforeEach(func() {
    71  				quota := models.QuotaFields{}
    72  				quota.Name = "my-quota"
    73  				quota.GUID = "my-quota-guid"
    74  
    75  				quotaRepo.FindByNameReturns(quota, nil)
    76  			})
    77  
    78  			It("deletes a quota with a given name when the user confirms", func() {
    79  				ui.Inputs = []string{"y"}
    80  
    81  				runCommand("my-quota")
    82  				Expect(quotaRepo.DeleteArgsForCall(0)).To(Equal("my-quota-guid"))
    83  
    84  				Expect(ui.Prompts).To(ContainSubstrings(
    85  					[]string{"Really delete the quota", "my-quota"},
    86  				))
    87  
    88  				Expect(ui.Outputs()).To(ContainSubstrings(
    89  					[]string{"Deleting quota", "my-quota", "my-user"},
    90  					[]string{"OK"},
    91  				))
    92  			})
    93  
    94  			It("does not prompt when the -f flag is provided", func() {
    95  				runCommand("-f", "my-quota")
    96  
    97  				Expect(quotaRepo.DeleteArgsForCall(0)).To(Equal("my-quota-guid"))
    98  
    99  				Expect(ui.Prompts).To(BeEmpty())
   100  			})
   101  
   102  			It("shows an error when deletion fails", func() {
   103  				quotaRepo.DeleteReturns(errors.New("some error"))
   104  
   105  				runCommand("-f", "my-quota")
   106  
   107  				Expect(ui.Outputs()).To(ContainSubstrings(
   108  					[]string{"Deleting", "my-quota"},
   109  					[]string{"FAILED"},
   110  				))
   111  			})
   112  		})
   113  
   114  		Context("when finding the quota fails", func() {
   115  			Context("when the quota provided does not exist", func() {
   116  				BeforeEach(func() {
   117  					quotaRepo.FindByNameReturns(models.QuotaFields{}, errors.NewModelNotFoundError("Quota", "non-existent-quota"))
   118  				})
   119  
   120  				It("warns the user when that the quota does not exist", func() {
   121  					runCommand("-f", "non-existent-quota")
   122  
   123  					Expect(ui.Outputs()).To(ContainSubstrings(
   124  						[]string{"Deleting", "non-existent-quota"},
   125  						[]string{"OK"},
   126  					))
   127  
   128  					Expect(ui.WarnOutputs).To(ContainSubstrings(
   129  						[]string{"non-existent-quota", "does not exist"},
   130  					))
   131  				})
   132  			})
   133  
   134  			Context("when other types of error occur", func() {
   135  				BeforeEach(func() {
   136  					quotaRepo.FindByNameReturns(models.QuotaFields{}, errors.New("some error"))
   137  				})
   138  
   139  				It("shows an error", func() {
   140  					runCommand("-f", "my-quota")
   141  
   142  					Expect(ui.WarnOutputs).ToNot(ContainSubstrings(
   143  						[]string{"my-quota", "does not exist"},
   144  					))
   145  
   146  					Expect(ui.Outputs()).To(ContainSubstrings(
   147  						[]string{"FAILED"},
   148  					))
   149  
   150  				})
   151  			})
   152  		})
   153  	})
   154  })