github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    14  
    15  	. "code.cloudfoundry.org/cli/util/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  			))
    86  		})
    87  	})
    88  
    89  	Context("when logged in and targeted an organiztion", func() {
    90  		BeforeEach(func() {
    91  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    92  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    93  			domainRepo.FindByNameInOrgReturns(
    94  				models.DomainFields{
    95  					Name:   "foo.com",
    96  					GUID:   "foo-guid",
    97  					Shared: true,
    98  				}, nil)
    99  		})
   100  
   101  		Describe("and the command is invoked interactively", func() {
   102  			BeforeEach(func() {
   103  				ui.Inputs = []string{"y"}
   104  			})
   105  
   106  			It("when the domain is not found it tells the user", func() {
   107  				domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.NewModelNotFoundError("Domain", "foo.com"))
   108  				runCommand("foo.com")
   109  
   110  				Expect(ui.Outputs()).To(ContainSubstrings(
   111  					[]string{"Deleting domain", "foo.com"},
   112  					[]string{"OK"},
   113  					[]string{"foo.com", "not found"},
   114  				))
   115  			})
   116  
   117  			It("fails when the api returns an error", func() {
   118  				domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.New("couldn't find the droids you're lookin for"))
   119  				runCommand("foo.com")
   120  
   121  				Expect(ui.Outputs()).To(ContainSubstrings(
   122  					[]string{"Deleting domain", "foo.com"},
   123  					[]string{"FAILED"},
   124  					[]string{"foo.com"},
   125  					[]string{"couldn't find the droids you're lookin for"},
   126  				))
   127  			})
   128  
   129  			It("fails when deleting the domain encounters an error", func() {
   130  				domainRepo.DeleteSharedDomainReturns(errors.New("failed badly"))
   131  				runCommand("foo.com")
   132  
   133  				Expect(domainRepo.DeleteSharedDomainArgsForCall(0)).To(Equal("foo-guid"))
   134  				Expect(ui.Outputs()).To(ContainSubstrings(
   135  					[]string{"Deleting domain", "foo.com"},
   136  					[]string{"FAILED"},
   137  					[]string{"foo.com"},
   138  					[]string{"failed badly"},
   139  				))
   140  			})
   141  
   142  			It("Prompts a user to delete the shared domain", func() {
   143  				runCommand("foo.com")
   144  
   145  				Expect(domainRepo.DeleteSharedDomainArgsForCall(0)).To(Equal("foo-guid"))
   146  				Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "domain", "foo.com"}))
   147  				Expect(ui.Outputs()).To(ContainSubstrings(
   148  					[]string{"Deleting domain", "foo.com"},
   149  					[]string{"OK"},
   150  				))
   151  			})
   152  		})
   153  
   154  		It("skips confirmation if the force flag is passed", func() {
   155  			runCommand("-f", "foo.com")
   156  
   157  			Expect(domainRepo.DeleteSharedDomainArgsForCall(0)).To(Equal("foo-guid"))
   158  			Expect(ui.Prompts).To(BeEmpty())
   159  			Expect(ui.Outputs()).To(ContainSubstrings(
   160  				[]string{"Deleting domain", "foo.com"},
   161  				[]string{"OK"},
   162  			))
   163  		})
   164  	})
   165  })