github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/domain/delete_shared_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/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/errors"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  
    13  	. "github.com/cloudfoundry/cli/cf/commands/domain"
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("delete-shared-domain command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		domainRepo          *testapi.FakeDomainRepository
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		configRepo          core_config.ReadWriter
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		ui = &testterm.FakeUI{}
    29  		domainRepo = &testapi.FakeDomainRepository{}
    30  		requirementsFactory = &testreq.FakeReqFactory{}
    31  		configRepo = testconfig.NewRepositoryWithDefaults()
    32  	})
    33  
    34  	runCommand := func(args ...string) bool {
    35  		return testcmd.RunCommand(NewDeleteSharedDomain(ui, configRepo, domainRepo), args, requirementsFactory)
    36  	}
    37  
    38  	Describe("requirements", func() {
    39  		It("fails if you are not logged in", func() {
    40  			requirementsFactory.TargetedOrgSuccess = true
    41  
    42  			Expect(runCommand("foo.com")).To(BeFalse())
    43  		})
    44  
    45  		It("fails if an organiztion is not targeted", func() {
    46  			requirementsFactory.LoginSuccess = true
    47  
    48  			Expect(runCommand("foo.com")).To(BeFalse())
    49  		})
    50  	})
    51  
    52  	Context("when logged in and targeted an organiztion", func() {
    53  		BeforeEach(func() {
    54  			requirementsFactory.LoginSuccess = true
    55  			requirementsFactory.TargetedOrgSuccess = true
    56  			domainRepo.FindByNameInOrgDomain = models.DomainFields{
    57  				Name:   "foo.com",
    58  				Guid:   "foo-guid",
    59  				Shared: true,
    60  			}
    61  		})
    62  
    63  		Describe("and the command is invoked interactively", func() {
    64  			BeforeEach(func() {
    65  				ui.Inputs = []string{"y"}
    66  			})
    67  
    68  			It("when the domain is not found it tells the user", func() {
    69  				domainRepo.FindByNameInOrgApiResponse = errors.NewModelNotFoundError("Domain", "foo.com")
    70  				runCommand("foo.com")
    71  
    72  				Expect(ui.Outputs).To(ContainSubstrings(
    73  					[]string{"Deleting domain", "foo.com"},
    74  					[]string{"OK"},
    75  					[]string{"foo.com", "not found"},
    76  				))
    77  			})
    78  
    79  			It("fails when the api returns an error", func() {
    80  				domainRepo.FindByNameInOrgApiResponse = errors.New("couldn't find the droids you're lookin for")
    81  				runCommand("foo.com")
    82  
    83  				Expect(ui.Outputs).To(ContainSubstrings(
    84  					[]string{"Deleting domain", "foo.com"},
    85  					[]string{"FAILED"},
    86  					[]string{"foo.com"},
    87  					[]string{"couldn't find the droids you're lookin for"},
    88  				))
    89  			})
    90  
    91  			It("fails when deleting the domain encounters an error", func() {
    92  				domainRepo.DeleteSharedApiResponse = errors.New("failed badly")
    93  				runCommand("foo.com")
    94  
    95  				Expect(domainRepo.DeleteSharedDomainGuid).To(Equal("foo-guid"))
    96  				Expect(ui.Outputs).To(ContainSubstrings(
    97  					[]string{"Deleting domain", "foo.com"},
    98  					[]string{"FAILED"},
    99  					[]string{"foo.com"},
   100  					[]string{"failed badly"},
   101  				))
   102  			})
   103  
   104  			It("Prompts a user to delete the shared domain", func() {
   105  				runCommand("foo.com")
   106  
   107  				Expect(domainRepo.DeleteSharedDomainGuid).To(Equal("foo-guid"))
   108  				Expect(ui.Prompts).To(ContainSubstrings([]string{"delete", "domain", "foo.com"}))
   109  				Expect(ui.Outputs).To(ContainSubstrings(
   110  					[]string{"Deleting domain", "foo.com"},
   111  					[]string{"OK"},
   112  				))
   113  			})
   114  		})
   115  
   116  		It("skips confirmation if the force flag is passed", func() {
   117  			runCommand("-f", "foo.com")
   118  
   119  			Expect(domainRepo.DeleteSharedDomainGuid).To(Equal("foo-guid"))
   120  			Expect(ui.Prompts).To(BeEmpty())
   121  			Expect(ui.Outputs).To(ContainSubstrings(
   122  				[]string{"Deleting domain", "foo.com"},
   123  				[]string{"OK"},
   124  			))
   125  		})
   126  	})
   127  })