github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/cf/commands/domain/delete_shared_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  
    15  	. "code.cloudfoundry.org/cli/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("delete-shared-domain command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		domainRepo          *apifakes.FakeDomainRepository
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		configRepo          coreconfig.Repository
    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-shared-domain").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		ui = &testterm.FakeUI{}
    38  		domainRepo = new(apifakes.FakeDomainRepository)
    39  		requirementsFactory = new(requirementsfakes.FakeFactory)
    40  		configRepo = testconfig.NewRepositoryWithDefaults()
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		return testcmd.RunCLICommand("delete-shared-domain", args, requirementsFactory, updateCommandDependency, false, ui)
    45  	}
    46  
    47  	Describe("requirements", func() {
    48  		It("fails if you are not logged in", func() {
    49  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    50  			Expect(runCommand("foo.com")).To(BeFalse())
    51  		})
    52  
    53  		It("fails if an organiztion is not targeted", func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    55  
    56  			targetedOrganizationReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    57  			targetedOrganizationReq.ExecuteReturns(errors.New("not targeted"))
    58  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrganizationReq)
    59  
    60  			Expect(runCommand("foo.com")).To(BeFalse())
    61  		})
    62  	})
    63  
    64  	Context("when the domain is owned", func() {
    65  		BeforeEach(func() {
    66  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    67  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    68  			domainRepo.FindByNameInOrgReturns(
    69  				models.DomainFields{
    70  					Name:   "foo1.com",
    71  					GUID:   "foo1-guid",
    72  					Shared: false,
    73  				}, nil)
    74  		})
    75  
    76  		It("informs the user that the domain is not shared", func() {
    77  			runCommand("foo1.com")
    78  
    79  			Expect(domainRepo.DeleteSharedDomainCallCount()).To(BeZero())
    80  			Expect(ui.Outputs()).To(ContainSubstrings(
    81  				[]string{"FAILED"},
    82  				[]string{"domain"},
    83  				[]string{"foo1.com"},
    84  				[]string{"is an owned domain, not a shared domain."},
    85  				[]string{"TIP"},
    86  				[]string{"Use `cf delete-domain` to delete owned domains."},
    87  			))
    88  		})
    89  	})
    90  
    91  	Context("when logged in and targeted an organiztion", func() {
    92  		BeforeEach(func() {
    93  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    94  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    95  			domainRepo.FindByNameInOrgReturns(
    96  				models.DomainFields{
    97  					Name:   "foo.com",
    98  					GUID:   "foo-guid",
    99  					Shared: true,
   100  				}, nil)
   101  		})
   102  
   103  		Describe("and the command is invoked interactively", func() {
   104  			BeforeEach(func() {
   105  				ui.Inputs = []string{"y"}
   106  			})
   107  
   108  			It("when the domain is not found it tells the user", func() {
   109  				domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.NewModelNotFoundError("Domain", "foo.com"))
   110  				runCommand("foo.com")
   111  
   112  				Expect(ui.Outputs()).To(ContainSubstrings(
   113  					[]string{"Deleting domain", "foo.com"},
   114  					[]string{"OK"},
   115  					[]string{"foo.com", "not found"},
   116  				))
   117  			})
   118  
   119  			It("fails when the api returns an error", func() {
   120  				domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.New("couldn't find the droids you're lookin for"))
   121  				runCommand("foo.com")
   122  
   123  				Expect(ui.Outputs()).To(ContainSubstrings(
   124  					[]string{"Deleting domain", "foo.com"},
   125  					[]string{"FAILED"},
   126  					[]string{"foo.com"},
   127  					[]string{"couldn't find the droids you're lookin for"},
   128  				))
   129  			})
   130  
   131  			It("fails when deleting the domain encounters an error", func() {
   132  				domainRepo.DeleteSharedDomainReturns(errors.New("failed badly"))
   133  				runCommand("foo.com")
   134  
   135  				Expect(domainRepo.DeleteSharedDomainArgsForCall(0)).To(Equal("foo-guid"))
   136  				Expect(ui.Outputs()).To(ContainSubstrings(
   137  					[]string{"Deleting domain", "foo.com"},
   138  					[]string{"FAILED"},
   139  					[]string{"foo.com"},
   140  					[]string{"failed badly"},
   141  				))
   142  			})
   143  
   144  			It("Prompts a user to delete the shared domain", func() {
   145  				runCommand("foo.com")
   146  
   147  				Expect(domainRepo.DeleteSharedDomainArgsForCall(0)).To(Equal("foo-guid"))
   148  				Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "domain", "foo.com"}))
   149  				Expect(ui.Outputs()).To(ContainSubstrings(
   150  					[]string{"Deleting domain", "foo.com"},
   151  					[]string{"OK"},
   152  				))
   153  			})
   154  		})
   155  
   156  		It("skips confirmation if the force flag is passed", func() {
   157  			runCommand("-f", "foo.com")
   158  
   159  			Expect(domainRepo.DeleteSharedDomainArgsForCall(0)).To(Equal("foo-guid"))
   160  			Expect(ui.Prompts).To(BeEmpty())
   161  			Expect(ui.Outputs()).To(ContainSubstrings(
   162  				[]string{"Deleting domain", "foo.com"},
   163  				[]string{"OK"},
   164  			))
   165  		})
   166  	})
   167  })