github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/delete_quota_test.go (about)

     1  package spacequota_test
     2  
     3  import (
     4  	test_org "github.com/cloudfoundry/cli/cf/api/organizations/fakes"
     5  	"github.com/cloudfoundry/cli/cf/api/space_quotas/fakes"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/errors"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  
    15  	. "github.com/cloudfoundry/cli/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           *fakes.FakeSpaceQuotaRepository
    24  		orgRepo             *test_org.FakeOrganizationRepository
    25  		requirementsFactory *testreq.FakeReqFactory
    26  		configRepo          core_config.Repository
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.Config = configRepo
    33  		deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo)
    34  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    35  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-space-quota").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		quotaRepo = &fakes.FakeSpaceQuotaRepository{}
    41  		orgRepo = &test_org.FakeOrganizationRepository{}
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		requirementsFactory = &testreq.FakeReqFactory{}
    44  
    45  		org := models.Organization{}
    46  		org.Name = "my-org"
    47  		org.Guid = "my-org-guid"
    48  		orgRepo.ListOrgsReturns([]models.Organization{org}, nil)
    49  		orgRepo.FindByNameReturns(org, nil)
    50  	})
    51  
    52  	runCommand := func(args ...string) bool {
    53  		return testcmd.RunCliCommand("delete-space-quota", args, requirementsFactory, updateCommandDependency, false)
    54  	}
    55  
    56  	Context("when the user is not logged in", func() {
    57  		BeforeEach(func() {
    58  			requirementsFactory.LoginSuccess = false
    59  		})
    60  
    61  		It("fails requirements", func() {
    62  			Expect(runCommand("my-quota")).To(BeFalse())
    63  		})
    64  	})
    65  
    66  	Context("when the user is logged in", func() {
    67  		BeforeEach(func() {
    68  			requirementsFactory.LoginSuccess = true
    69  		})
    70  
    71  		It("fails requirements when called without a quota name", func() {
    72  			runCommand()
    73  			Expect(ui.Outputs).To(ContainSubstrings(
    74  				[]string{"Incorrect Usage", "Requires an argument"},
    75  			))
    76  		})
    77  
    78  		It("fails requirements when an org is not targeted", func() {
    79  			requirementsFactory.TargetedOrgSuccess = false
    80  			Expect(runCommand()).To(BeFalse())
    81  		})
    82  
    83  		Context("When the quota provided exists", func() {
    84  			BeforeEach(func() {
    85  				quota := models.SpaceQuota{}
    86  				quota.Name = "my-quota"
    87  				quota.Guid = "my-quota-guid"
    88  				quota.OrgGuid = "my-org-guid"
    89  				quotaRepo.FindByNameReturns(quota, nil)
    90  			})
    91  
    92  			It("deletes a quota with a given name when the user confirms", func() {
    93  				ui.Inputs = []string{"y"}
    94  
    95  				runCommand("my-quota")
    96  				Expect(quotaRepo.DeleteArgsForCall(0)).To(Equal("my-quota-guid"))
    97  
    98  				Expect(ui.Prompts).To(ContainSubstrings(
    99  					[]string{"Really delete the quota", "my-quota"},
   100  				))
   101  
   102  				Expect(ui.Outputs).To(ContainSubstrings(
   103  					[]string{"Deleting space quota", "my-quota", "as", "my-user"},
   104  					[]string{"OK"},
   105  				))
   106  			})
   107  
   108  			It("does not prompt when the -f flag is provided", func() {
   109  				runCommand("-f", "my-quota")
   110  
   111  				Expect(quotaRepo.DeleteArgsForCall(0)).To(Equal("my-quota-guid"))
   112  
   113  				Expect(ui.Prompts).To(BeEmpty())
   114  			})
   115  
   116  			It("shows an error when deletion fails", func() {
   117  				quotaRepo.DeleteReturns(errors.New("some error"))
   118  
   119  				runCommand("-f", "my-quota")
   120  
   121  				Expect(ui.Outputs).To(ContainSubstrings(
   122  					[]string{"Deleting", "my-quota"},
   123  					[]string{"FAILED"},
   124  				))
   125  			})
   126  		})
   127  
   128  		Context("when finding the quota fails", func() {
   129  			Context("when the quota provided does not exist", func() {
   130  				BeforeEach(func() {
   131  					quotaRepo.FindByNameReturns(models.SpaceQuota{}, errors.NewModelNotFoundError("Quota", "non-existent-quota"))
   132  				})
   133  
   134  				It("warns the user when that the quota does not exist", func() {
   135  					runCommand("-f", "non-existent-quota")
   136  
   137  					Expect(ui.Outputs).To(ContainSubstrings(
   138  						[]string{"Deleting", "non-existent-quota"},
   139  						[]string{"OK"},
   140  					))
   141  
   142  					Expect(ui.WarnOutputs).To(ContainSubstrings(
   143  						[]string{"non-existent-quota", "does not exist"},
   144  					))
   145  				})
   146  			})
   147  
   148  			Context("when other types of error occur", func() {
   149  				BeforeEach(func() {
   150  					quotaRepo.FindByNameReturns(models.SpaceQuota{}, errors.New("some error"))
   151  				})
   152  
   153  				It("shows an error", func() {
   154  					runCommand("-f", "my-quota")
   155  
   156  					Expect(ui.WarnOutputs).ToNot(ContainSubstrings(
   157  						[]string{"my-quota", "does not exist"},
   158  					))
   159  
   160  					Expect(ui.Outputs).To(ContainSubstrings(
   161  						[]string{"FAILED"},
   162  					))
   163  
   164  				})
   165  			})
   166  		})
   167  	})
   168  })