github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/purge_service_offering_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     7  	"github.com/cloudfoundry/cli/cf/command_registry"
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	cferrors "github.com/cloudfoundry/cli/cf/errors"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	"github.com/cloudfoundry/cli/testhelpers/maker"
    13  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("purge-service command", func() {
    22  	var (
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		config              core_config.Repository
    25  		ui                  *testterm.FakeUI
    26  		serviceRepo         *testapi.FakeServiceRepo
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    33  		deps.Config = config
    34  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("purge-service-offering").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	runCommand := func(args []string) bool {
    38  		return testcmd.RunCliCommand("purge-service-offering", args, requirementsFactory, updateCommandDependency, false)
    39  	}
    40  
    41  	BeforeEach(func() {
    42  		ui = &testterm.FakeUI{}
    43  		config = testconfig.NewRepositoryWithDefaults()
    44  		serviceRepo = &testapi.FakeServiceRepo{}
    45  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    46  	})
    47  
    48  	Describe("requirements", func() {
    49  		It("fails when not logged in", func() {
    50  			requirementsFactory.LoginSuccess = false
    51  
    52  			passed := runCommand([]string{"-f", "whatever"})
    53  
    54  			Expect(passed).To(BeFalse())
    55  		})
    56  
    57  		It("fails when called without exactly one arg", func() {
    58  			requirementsFactory.LoginSuccess = true
    59  
    60  			passed := runCommand([]string{})
    61  
    62  			Expect(passed).To(BeFalse())
    63  			Expect(ui.Outputs).To(ContainSubstrings(
    64  				[]string{"Incorrect Usage", "Requires", "argument"},
    65  			))
    66  		})
    67  	})
    68  
    69  	It("works when given -p and a provider name", func() {
    70  		offering := maker.NewServiceOffering("the-service-name")
    71  		serviceRepo.FindServiceOfferingByLabelAndProviderServiceOffering = offering
    72  
    73  		ui.Inputs = []string{"yes"}
    74  
    75  		runCommand([]string{"-p", "the-provider", "the-service-name"})
    76  
    77  		Expect(serviceRepo.FindServiceOfferingByLabelAndProviderName).To(Equal("the-service-name"))
    78  		Expect(serviceRepo.FindServiceOfferingByLabelAndProviderProvider).To(Equal("the-provider"))
    79  		Expect(serviceRepo.PurgedServiceOffering).To(Equal(offering))
    80  	})
    81  
    82  	It("works when not given a provider", func() {
    83  		offering := maker.NewServiceOffering("the-service-name")
    84  		serviceRepo.FindServiceOfferingByLabelAndProviderServiceOffering = offering
    85  
    86  		ui.Inputs = []string{"yes"}
    87  
    88  		runCommand([]string{"the-service-name"})
    89  
    90  		Expect(ui.Outputs).To(ContainSubstrings([]string{"WARNING"}))
    91  		Expect(ui.Prompts).To(ContainSubstrings([]string{"Really purge service", "the-service-name"}))
    92  		Expect(ui.Outputs).To(ContainSubstrings([]string{"Purging service the-service-name..."}))
    93  
    94  		Expect(serviceRepo.FindServiceOfferingByLabelAndProviderName).To(Equal("the-service-name"))
    95  		Expect(serviceRepo.FindServiceOfferingByLabelAndProviderProvider).To(Equal(""))
    96  		Expect(serviceRepo.PurgedServiceOffering).To(Equal(offering))
    97  
    98  		Expect(ui.Outputs).To(ContainSubstrings([]string{"OK"}))
    99  	})
   100  
   101  	It("exits when the user does not acknowledge the confirmation", func() {
   102  		ui.Inputs = []string{"no"}
   103  
   104  		runCommand([]string{"the-service-name"})
   105  
   106  		Expect(serviceRepo.FindServiceOfferingByLabelAndProviderCalled).To(Equal(true))
   107  		Expect(serviceRepo.PurgeServiceOfferingCalled).To(Equal(false))
   108  	})
   109  
   110  	It("does not prompt with confirmation when -f is passed", func() {
   111  		offering := maker.NewServiceOffering("the-service-name")
   112  		serviceRepo.FindServiceOfferingByLabelAndProviderServiceOffering = offering
   113  
   114  		runCommand(
   115  			[]string{"-f", "the-service-name"},
   116  		)
   117  
   118  		Expect(len(ui.Prompts)).To(Equal(0))
   119  		Expect(serviceRepo.PurgeServiceOfferingCalled).To(Equal(true))
   120  	})
   121  
   122  	It("fails with an error message when the request fails", func() {
   123  		serviceRepo.FindServiceOfferingByLabelAndProviderApiResponse = cferrors.NewWithError("oh no!", errors.New("!"))
   124  
   125  		runCommand(
   126  			[]string{"-f", "-p", "the-provider", "the-service-name"},
   127  		)
   128  
   129  		Expect(ui.Outputs).To(ContainSubstrings(
   130  			[]string{"FAILED"},
   131  			[]string{"oh no!"},
   132  		))
   133  
   134  		Expect(serviceRepo.PurgeServiceOfferingCalled).To(Equal(false))
   135  	})
   136  
   137  	It("fails with an error message when the purging request fails", func() {
   138  		serviceRepo.PurgeServiceOfferingApiResponse = cferrors.New("crumpets insufficiently buttered")
   139  
   140  		runCommand(
   141  			[]string{"-f", "-p", "the-provider", "the-service-name"},
   142  		)
   143  
   144  		Expect(ui.Outputs).To(ContainSubstrings(
   145  			[]string{"FAILED"},
   146  			[]string{"crumpets insufficiently buttered"},
   147  		))
   148  	})
   149  
   150  	It("indicates when a service doesn't exist", func() {
   151  		serviceRepo.FindServiceOfferingByLabelAndProviderApiResponse = cferrors.NewModelNotFoundError("Service Offering", "")
   152  
   153  		ui.Inputs = []string{"yes"}
   154  
   155  		runCommand(
   156  			[]string{"-p", "the-provider", "the-service-name"},
   157  		)
   158  
   159  		Expect(ui.Outputs).To(ContainSubstrings([]string{"Service offering", "does not exist"}))
   160  		Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"WARNING"}))
   161  		Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"Ok"}))
   162  
   163  		Expect(serviceRepo.PurgeServiceOfferingCalled).To(Equal(false))
   164  	})
   165  })