github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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/cf/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/util/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  			))
    97  
    98  		})
    99  	})
   100  	Context("when the domain exists", func() {
   101  		BeforeEach(func() {
   102  			domainRepo.FindByNameInOrgReturns(
   103  				models.DomainFields{
   104  					Name: "foo.com",
   105  					GUID: "foo-guid",
   106  				}, nil)
   107  		})
   108  
   109  		It("deletes domains", func() {
   110  			runCommand("foo.com")
   111  
   112  			Expect(domainRepo.DeleteArgsForCall(0)).To(Equal("foo-guid"))
   113  
   114  			Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the domain foo.com"}))
   115  			Expect(ui.Outputs()).To(ContainSubstrings(
   116  				[]string{"Deleting domain", "foo.com", "my-user"},
   117  				[]string{"OK"},
   118  			))
   119  		})
   120  
   121  		Context("when there is an error deleting the domain", func() {
   122  			BeforeEach(func() {
   123  				domainRepo.DeleteReturns(errors.New("failed badly"))
   124  			})
   125  
   126  			It("show the error the user", func() {
   127  				runCommand("foo.com")
   128  
   129  				Expect(domainRepo.DeleteArgsForCall(0)).To(Equal("foo-guid"))
   130  
   131  				Expect(ui.Outputs()).To(ContainSubstrings(
   132  					[]string{"Deleting domain", "foo.com"},
   133  					[]string{"FAILED"},
   134  					[]string{"foo.com"},
   135  					[]string{"failed badly"},
   136  				))
   137  			})
   138  		})
   139  
   140  		Context("when the user does not confirm", func() {
   141  			BeforeEach(func() {
   142  				ui.Inputs = []string{"no"}
   143  			})
   144  
   145  			It("does nothing", func() {
   146  				runCommand("foo.com")
   147  
   148  				Expect(domainRepo.DeleteCallCount()).To(BeZero())
   149  
   150  				Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "foo.com"}))
   151  
   152  				Expect(ui.Outputs()).To(BeEmpty())
   153  			})
   154  		})
   155  
   156  		Context("when the user provides the -f flag", func() {
   157  			BeforeEach(func() {
   158  				ui.Inputs = []string{}
   159  			})
   160  
   161  			It("skips confirmation", func() {
   162  				runCommand("-f", "foo.com")
   163  
   164  				Expect(domainRepo.DeleteArgsForCall(0)).To(Equal("foo-guid"))
   165  				Expect(ui.Prompts).To(BeEmpty())
   166  				Expect(ui.Outputs()).To(ContainSubstrings(
   167  					[]string{"Deleting domain", "foo.com"},
   168  					[]string{"OK"},
   169  				))
   170  			})
   171  		})
   172  	})
   173  
   174  	Context("when a domain with the given name doesn't exist", func() {
   175  		BeforeEach(func() {
   176  			domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.NewModelNotFoundError("Domain", "foo.com"))
   177  		})
   178  
   179  		It("fails", func() {
   180  			runCommand("foo.com")
   181  
   182  			Expect(domainRepo.DeleteCallCount()).To(BeZero())
   183  
   184  			Expect(ui.Outputs()).To(ContainSubstrings(
   185  				[]string{"OK"},
   186  				[]string{"foo.com", "not found"},
   187  			))
   188  		})
   189  	})
   190  
   191  	Context("when there is an error finding the domain", func() {
   192  		BeforeEach(func() {
   193  			domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.New("failed badly"))
   194  		})
   195  
   196  		It("shows the error to the user", func() {
   197  			runCommand("foo.com")
   198  
   199  			Expect(domainRepo.DeleteCallCount()).To(BeZero())
   200  
   201  			Expect(ui.Outputs()).To(ContainSubstrings(
   202  				[]string{"FAILED"},
   203  				[]string{"foo.com"},
   204  				[]string{"failed badly"},
   205  			))
   206  		})
   207  	})
   208  })