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