github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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/command_registry"
     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  		ui                  *testterm.FakeUI
    22  		configRepo          core_config.Repository
    23  		domainRepo          *testapi.FakeDomainRepository
    24  		requirementsFactory *testreq.FakeReqFactory
    25  		deps                command_registry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.Ui = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetDomainRepository(domainRepo)
    31  		deps.Config = configRepo
    32  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-domain").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{
    37  			Inputs: []string{"yes"},
    38  		}
    39  
    40  		domainRepo = &testapi.FakeDomainRepository{}
    41  		requirementsFactory = &testreq.FakeReqFactory{
    42  			LoginSuccess:       true,
    43  			TargetedOrgSuccess: true,
    44  		}
    45  		configRepo = testconfig.NewRepositoryWithDefaults()
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCliCommand("delete-domain", args, requirementsFactory, updateCommandDependency, false)
    50  	}
    51  
    52  	Describe("requirements", func() {
    53  		It("fails when the user is not logged in", func() {
    54  			requirementsFactory.LoginSuccess = false
    55  
    56  			Expect(runCommand("foo.com")).To(BeFalse())
    57  		})
    58  
    59  		It("fails when the an org is not targetted", func() {
    60  			requirementsFactory.TargetedOrgSuccess = false
    61  
    62  			Expect(runCommand("foo.com")).To(BeFalse())
    63  		})
    64  	})
    65  
    66  	Context("when the domain is shared", func() {
    67  		BeforeEach(func() {
    68  			domainRepo.FindByNameInOrgDomain = []models.DomainFields{
    69  				models.DomainFields{
    70  					Name:   "foo1.com",
    71  					Guid:   "foo1-guid",
    72  					Shared: true,
    73  				},
    74  			}
    75  		})
    76  		It("informs the user that the domain is shared", func() {
    77  			runCommand("foo1.com")
    78  
    79  			Expect(domainRepo.DeleteDomainGuid).To(Equal(""))
    80  			Expect(ui.Outputs).To(ContainSubstrings(
    81  				[]string{"FAILED"},
    82  				[]string{"domain"},
    83  				[]string{"foo1.com"},
    84  				[]string{"is a shared domain, not an owned domain."},
    85  				[]string{"TIP"},
    86  				[]string{"Use `cf delete-shared-domain` to delete shared domains."},
    87  			))
    88  
    89  		})
    90  	})
    91  	Context("when the domain exists", func() {
    92  		BeforeEach(func() {
    93  			domainRepo.FindByNameInOrgDomain = []models.DomainFields{
    94  				models.DomainFields{
    95  					Name: "foo.com",
    96  					Guid: "foo-guid",
    97  				},
    98  			}
    99  		})
   100  
   101  		It("deletes domains", func() {
   102  			runCommand("foo.com")
   103  
   104  			Expect(domainRepo.DeleteDomainGuid).To(Equal("foo-guid"))
   105  
   106  			Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the domain foo.com"}))
   107  			Expect(ui.Outputs).To(ContainSubstrings(
   108  				[]string{"Deleting domain", "foo.com", "my-user"},
   109  				[]string{"OK"},
   110  			))
   111  		})
   112  
   113  		Context("when there is an error deleting the domain", func() {
   114  			BeforeEach(func() {
   115  				domainRepo.DeleteApiResponse = errors.New("failed badly")
   116  			})
   117  
   118  			It("show the error the user", func() {
   119  				runCommand("foo.com")
   120  
   121  				Expect(domainRepo.DeleteDomainGuid).To(Equal("foo-guid"))
   122  
   123  				Expect(ui.Outputs).To(ContainSubstrings(
   124  					[]string{"Deleting domain", "foo.com"},
   125  					[]string{"FAILED"},
   126  					[]string{"foo.com"},
   127  					[]string{"failed badly"},
   128  				))
   129  			})
   130  		})
   131  
   132  		Context("when the user does not confirm", func() {
   133  			BeforeEach(func() {
   134  				ui.Inputs = []string{"no"}
   135  			})
   136  
   137  			It("does nothing", func() {
   138  				runCommand("foo.com")
   139  
   140  				Expect(domainRepo.DeleteDomainGuid).To(Equal(""))
   141  
   142  				Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "foo.com"}))
   143  
   144  				Expect(ui.Outputs).To(BeEmpty())
   145  			})
   146  		})
   147  
   148  		Context("when the user provides the -f flag", func() {
   149  			BeforeEach(func() {
   150  				ui.Inputs = []string{}
   151  			})
   152  
   153  			It("skips confirmation", func() {
   154  				runCommand("-f", "foo.com")
   155  
   156  				Expect(domainRepo.DeleteDomainGuid).To(Equal("foo-guid"))
   157  				Expect(ui.Prompts).To(BeEmpty())
   158  				Expect(ui.Outputs).To(ContainSubstrings(
   159  					[]string{"Deleting domain", "foo.com"},
   160  					[]string{"OK"},
   161  				))
   162  			})
   163  		})
   164  	})
   165  
   166  	Context("when a domain with the given name doesn't exist", func() {
   167  		BeforeEach(func() {
   168  			domainRepo.FindByNameInOrgApiResponse = errors.NewModelNotFoundError("Domain", "foo.com")
   169  		})
   170  
   171  		It("fails", func() {
   172  			runCommand("foo.com")
   173  
   174  			Expect(domainRepo.DeleteDomainGuid).To(Equal(""))
   175  
   176  			Expect(ui.Outputs).To(ContainSubstrings(
   177  				[]string{"OK"},
   178  				[]string{"foo.com", "not found"},
   179  			))
   180  		})
   181  	})
   182  
   183  	Context("when there is an error finding the domain", func() {
   184  		BeforeEach(func() {
   185  			domainRepo.FindByNameInOrgApiResponse = errors.New("failed badly")
   186  		})
   187  
   188  		It("shows the error to the user", func() {
   189  			runCommand("foo.com")
   190  
   191  			Expect(domainRepo.DeleteDomainGuid).To(Equal(""))
   192  
   193  			Expect(ui.Outputs).To(ContainSubstrings(
   194  				[]string{"FAILED"},
   195  				[]string{"foo.com"},
   196  				[]string{"failed badly"},
   197  			))
   198  		})
   199  	})
   200  })