github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/domain/delete_domain_test.go (about) 1 package domain_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 5 . "github.com/cloudfoundry/cli/cf/commands/domain" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/errors" 8 "github.com/cloudfoundry/cli/cf/models" 9 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 10 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 11 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 12 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 16 . "github.com/cloudfoundry/cli/testhelpers/matchers" 17 ) 18 19 var _ = Describe("delete-domain command", func() { 20 var ( 21 cmd *DeleteDomain 22 ui *testterm.FakeUI 23 configRepo core_config.ReadWriter 24 domainRepo *testapi.FakeDomainRepository 25 requirementsFactory *testreq.FakeReqFactory 26 ) 27 28 BeforeEach(func() { 29 ui = &testterm.FakeUI{ 30 Inputs: []string{"yes"}, 31 } 32 33 domainRepo = &testapi.FakeDomainRepository{} 34 requirementsFactory = &testreq.FakeReqFactory{ 35 LoginSuccess: true, 36 TargetedOrgSuccess: true, 37 } 38 configRepo = testconfig.NewRepositoryWithDefaults() 39 }) 40 41 runCommand := func(args ...string) bool { 42 cmd = NewDeleteDomain(ui, configRepo, domainRepo) 43 return testcmd.RunCommand(cmd, args, requirementsFactory) 44 } 45 46 Describe("requirements", func() { 47 It("fails when the user is not logged in", func() { 48 requirementsFactory.LoginSuccess = false 49 50 Expect(runCommand("foo.com")).To(BeFalse()) 51 }) 52 53 It("fails when the an org is not targetted", func() { 54 requirementsFactory.TargetedOrgSuccess = false 55 56 Expect(runCommand("foo.com")).To(BeFalse()) 57 }) 58 }) 59 60 Context("when the domain exists", func() { 61 BeforeEach(func() { 62 domainRepo.FindByNameInOrgDomain = models.DomainFields{ 63 Name: "foo.com", 64 Guid: "foo-guid", 65 } 66 }) 67 68 It("deletes domains", func() { 69 runCommand("foo.com") 70 71 Expect(domainRepo.DeleteDomainGuid).To(Equal("foo-guid")) 72 73 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the domain foo.com"})) 74 Expect(ui.Outputs).To(ContainSubstrings( 75 []string{"Deleting domain", "foo.com", "my-user"}, 76 []string{"OK"}, 77 )) 78 }) 79 80 Context("when there is an error deleting the domain", func() { 81 BeforeEach(func() { 82 domainRepo.DeleteApiResponse = errors.New("failed badly") 83 }) 84 85 It("show the error the user", func() { 86 runCommand("foo.com") 87 88 Expect(domainRepo.DeleteDomainGuid).To(Equal("foo-guid")) 89 90 Expect(ui.Outputs).To(ContainSubstrings( 91 []string{"Deleting domain", "foo.com"}, 92 []string{"FAILED"}, 93 []string{"foo.com"}, 94 []string{"failed badly"}, 95 )) 96 }) 97 }) 98 99 Context("when the user does not confirm", func() { 100 BeforeEach(func() { 101 ui.Inputs = []string{"no"} 102 }) 103 104 It("does nothing", func() { 105 runCommand("foo.com") 106 107 Expect(domainRepo.DeleteDomainGuid).To(Equal("")) 108 109 Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "foo.com"})) 110 111 Expect(ui.Outputs).To(BeEmpty()) 112 }) 113 }) 114 115 Context("when the user provides the -f flag", func() { 116 BeforeEach(func() { 117 ui.Inputs = []string{} 118 }) 119 120 It("skips confirmation", func() { 121 runCommand("-f", "foo.com") 122 123 Expect(domainRepo.DeleteDomainGuid).To(Equal("foo-guid")) 124 Expect(ui.Prompts).To(BeEmpty()) 125 Expect(ui.Outputs).To(ContainSubstrings( 126 []string{"Deleting domain", "foo.com"}, 127 []string{"OK"}, 128 )) 129 }) 130 }) 131 }) 132 133 Context("when a domain with the given name doesn't exist", func() { 134 BeforeEach(func() { 135 domainRepo.FindByNameInOrgApiResponse = errors.NewModelNotFoundError("Domain", "foo.com") 136 }) 137 138 It("fails", func() { 139 runCommand("foo.com") 140 141 Expect(domainRepo.DeleteDomainGuid).To(Equal("")) 142 143 Expect(ui.Outputs).To(ContainSubstrings( 144 []string{"OK"}, 145 []string{"foo.com", "not found"}, 146 )) 147 }) 148 }) 149 150 Context("when there is an error finding the domain", func() { 151 BeforeEach(func() { 152 domainRepo.FindByNameInOrgApiResponse = errors.New("failed badly") 153 }) 154 155 It("shows the error to the user", func() { 156 runCommand("foo.com") 157 158 Expect(domainRepo.DeleteDomainGuid).To(Equal("")) 159 160 Expect(ui.Outputs).To(ContainSubstrings( 161 []string{"FAILED"}, 162 []string{"foo.com"}, 163 []string{"failed badly"}, 164 )) 165 }) 166 }) 167 })