github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/cf/commands/domain/delete_domain_test.go (about) 1 package domain_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/testhelpers/commands" 12 testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" 13 testterm "code.cloudfoundry.org/cli/testhelpers/terminal" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 17 . "code.cloudfoundry.org/cli/testhelpers/matchers" 18 ) 19 20 var _ = Describe("delete-domain command", func() { 21 var ( 22 ui *testterm.FakeUI 23 configRepo coreconfig.Repository 24 domainRepo *apifakes.FakeDomainRepository 25 requirementsFactory *requirementsfakes.FakeFactory 26 deps commandregistry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.UI = ui 31 deps.RepoLocator = deps.RepoLocator.SetDomainRepository(domainRepo) 32 deps.Config = configRepo 33 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-domain").SetDependency(deps, pluginCall)) 34 } 35 36 BeforeEach(func() { 37 ui = &testterm.FakeUI{ 38 Inputs: []string{"yes"}, 39 } 40 41 domainRepo = new(apifakes.FakeDomainRepository) 42 43 requirementsFactory = new(requirementsfakes.FakeFactory) 44 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 45 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 46 47 fakeOrgRequirement := new(requirementsfakes.FakeOrganizationRequirement) 48 fakeOrgRequirement.GetOrganizationReturns(models.Organization{ 49 OrganizationFields: models.OrganizationFields{ 50 Name: "my-org", 51 }, 52 }) 53 requirementsFactory.NewOrganizationRequirementReturns(fakeOrgRequirement) 54 55 configRepo = testconfig.NewRepositoryWithDefaults() 56 }) 57 58 runCommand := func(args ...string) bool { 59 return testcmd.RunCLICommand("delete-domain", args, requirementsFactory, updateCommandDependency, false, ui) 60 } 61 62 Describe("requirements", func() { 63 It("fails when the user is not logged in", func() { 64 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 65 66 Expect(runCommand("foo.com")).To(BeFalse()) 67 }) 68 69 It("fails when the an org is not targetted", func() { 70 targetedOrganizationReq := new(requirementsfakes.FakeTargetedOrgRequirement) 71 targetedOrganizationReq.ExecuteReturns(errors.New("not targeted")) 72 requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrganizationReq) 73 74 Expect(runCommand("foo.com")).To(BeFalse()) 75 }) 76 }) 77 78 Context("when the domain is shared", func() { 79 BeforeEach(func() { 80 domainRepo.FindByNameInOrgReturns( 81 models.DomainFields{ 82 Name: "foo1.com", 83 GUID: "foo1-guid", 84 Shared: true, 85 }, nil) 86 }) 87 It("informs the user that the domain is shared", func() { 88 runCommand("foo1.com") 89 90 Expect(domainRepo.DeleteCallCount()).To(BeZero()) 91 Expect(ui.Outputs()).To(ContainSubstrings( 92 []string{"FAILED"}, 93 []string{"domain"}, 94 []string{"foo1.com"}, 95 []string{"is a shared domain, not an owned domain."}, 96 []string{"TIP"}, 97 []string{"Use `cf delete-shared-domain` to delete shared domains."}, 98 )) 99 100 }) 101 }) 102 Context("when the domain exists", func() { 103 BeforeEach(func() { 104 domainRepo.FindByNameInOrgReturns( 105 models.DomainFields{ 106 Name: "foo.com", 107 GUID: "foo-guid", 108 }, nil) 109 }) 110 111 It("deletes domains", func() { 112 runCommand("foo.com") 113 114 Expect(domainRepo.DeleteArgsForCall(0)).To(Equal("foo-guid")) 115 116 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the domain foo.com"})) 117 Expect(ui.Outputs()).To(ContainSubstrings( 118 []string{"Deleting domain", "foo.com", "my-user"}, 119 []string{"OK"}, 120 )) 121 }) 122 123 Context("when there is an error deleting the domain", func() { 124 BeforeEach(func() { 125 domainRepo.DeleteReturns(errors.New("failed badly")) 126 }) 127 128 It("show the error the user", func() { 129 runCommand("foo.com") 130 131 Expect(domainRepo.DeleteArgsForCall(0)).To(Equal("foo-guid")) 132 133 Expect(ui.Outputs()).To(ContainSubstrings( 134 []string{"Deleting domain", "foo.com"}, 135 []string{"FAILED"}, 136 []string{"foo.com"}, 137 []string{"failed badly"}, 138 )) 139 }) 140 }) 141 142 Context("when the user does not confirm", func() { 143 BeforeEach(func() { 144 ui.Inputs = []string{"no"} 145 }) 146 147 It("does nothing", func() { 148 runCommand("foo.com") 149 150 Expect(domainRepo.DeleteCallCount()).To(BeZero()) 151 152 Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "foo.com"})) 153 154 Expect(ui.Outputs()).To(BeEmpty()) 155 }) 156 }) 157 158 Context("when the user provides the -f flag", func() { 159 BeforeEach(func() { 160 ui.Inputs = []string{} 161 }) 162 163 It("skips confirmation", func() { 164 runCommand("-f", "foo.com") 165 166 Expect(domainRepo.DeleteArgsForCall(0)).To(Equal("foo-guid")) 167 Expect(ui.Prompts).To(BeEmpty()) 168 Expect(ui.Outputs()).To(ContainSubstrings( 169 []string{"Deleting domain", "foo.com"}, 170 []string{"OK"}, 171 )) 172 }) 173 }) 174 }) 175 176 Context("when a domain with the given name doesn't exist", func() { 177 BeforeEach(func() { 178 domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.NewModelNotFoundError("Domain", "foo.com")) 179 }) 180 181 It("fails", func() { 182 runCommand("foo.com") 183 184 Expect(domainRepo.DeleteCallCount()).To(BeZero()) 185 186 Expect(ui.Outputs()).To(ContainSubstrings( 187 []string{"OK"}, 188 []string{"foo.com", "not found"}, 189 )) 190 }) 191 }) 192 193 Context("when there is an error finding the domain", func() { 194 BeforeEach(func() { 195 domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.New("failed badly")) 196 }) 197 198 It("shows the error to the user", func() { 199 runCommand("foo.com") 200 201 Expect(domainRepo.DeleteCallCount()).To(BeZero()) 202 203 Expect(ui.Outputs()).To(ContainSubstrings( 204 []string{"FAILED"}, 205 []string{"foo.com"}, 206 []string{"failed badly"}, 207 )) 208 }) 209 }) 210 })