github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/service/unbind_service_test.go (about)

     1  package service_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/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    10  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    11  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    12  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  
    16  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    17  )
    18  
    19  var _ = Describe("unbind-service command", func() {
    20  	var (
    21  		app                 models.Application
    22  		ui                  *testterm.FakeUI
    23  		config              coreconfig.Repository
    24  		serviceInstance     models.ServiceInstance
    25  		requirementsFactory *requirementsfakes.FakeFactory
    26  		serviceBindingRepo  *apifakes.FakeServiceBindingRepository
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetServiceBindingRepository(serviceBindingRepo)
    33  		deps.Config = config
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unbind-service").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = new(testterm.FakeUI)
    39  		serviceBindingRepo = new(apifakes.FakeServiceBindingRepository)
    40  
    41  		app = models.Application{
    42  			ApplicationFields: models.ApplicationFields{
    43  				Name: "my-app",
    44  				GUID: "my-app-guid",
    45  			},
    46  		}
    47  
    48  		serviceInstance = models.ServiceInstance{
    49  			ServiceInstanceFields: models.ServiceInstanceFields{
    50  				Name: "my-service",
    51  				GUID: "my-service-guid",
    52  			},
    53  		}
    54  
    55  		config = testconfig.NewRepositoryWithDefaults()
    56  		requirementsFactory = new(requirementsfakes.FakeFactory)
    57  		applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    58  		applicationReq.GetApplicationReturns(app)
    59  		requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    60  		serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement)
    61  		serviceInstanceReq.GetServiceInstanceReturns(serviceInstance)
    62  		requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq)
    63  	})
    64  
    65  	callUnbindService := func(args []string) bool {
    66  		return testcmd.RunCLICommand("unbind-service", args, requirementsFactory, updateCommandDependency, false, ui)
    67  	}
    68  
    69  	Context("when not logged in", func() {
    70  		It("fails requirements when not logged in", func() {
    71  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    72  			Expect(testcmd.RunCLICommand("unbind-service", []string{"my-service", "my-app"}, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    73  		})
    74  	})
    75  
    76  	Context("when logged in", func() {
    77  		BeforeEach(func() {
    78  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    79  		})
    80  
    81  		Context("when the service instance exists", func() {
    82  			It("unbinds a service from an app", func() {
    83  				callUnbindService([]string{"my-app", "my-service"})
    84  
    85  				Expect(ui.Outputs()).To(ContainSubstrings(
    86  					[]string{"Unbinding app", "my-service", "my-app", "my-org", "my-space", "my-user"},
    87  					[]string{"OK"},
    88  				))
    89  
    90  				Expect(serviceBindingRepo.DeleteCallCount()).To(Equal(1))
    91  				serviceInstance, applicationGUID := serviceBindingRepo.DeleteArgsForCall(0)
    92  				Expect(serviceInstance).To(Equal(serviceInstance))
    93  				Expect(applicationGUID).To(Equal("my-app-guid"))
    94  			})
    95  		})
    96  
    97  		Context("when the service instance does not exist", func() {
    98  			BeforeEach(func() {
    99  				serviceBindingRepo.DeleteReturns(false, nil)
   100  			})
   101  
   102  			It("warns the user the the service instance does not exist", func() {
   103  				callUnbindService([]string{"my-app", "my-service"})
   104  
   105  				Expect(ui.Outputs()).To(ContainSubstrings(
   106  					[]string{"Unbinding app", "my-service", "my-app"},
   107  					[]string{"OK"},
   108  					[]string{"my-service", "my-app", "did not exist"},
   109  				))
   110  
   111  				Expect(serviceBindingRepo.DeleteCallCount()).To(Equal(1))
   112  				serviceInstance, applicationGUID := serviceBindingRepo.DeleteArgsForCall(0)
   113  				Expect(serviceInstance).To(Equal(serviceInstance))
   114  				Expect(applicationGUID).To(Equal("my-app-guid"))
   115  			})
   116  		})
   117  
   118  		It("when no parameters are given the command fails with usage", func() {
   119  			callUnbindService([]string{"my-service"})
   120  			Expect(ui.Outputs()).To(ContainSubstrings(
   121  				[]string{"Incorrect Usage", "Requires", "argument"},
   122  			))
   123  
   124  			ui = &testterm.FakeUI{}
   125  			callUnbindService([]string{"my-app"})
   126  			Expect(ui.Outputs()).To(ContainSubstrings(
   127  				[]string{"Incorrect Usage", "Requires", "argument"},
   128  			))
   129  
   130  			ui = &testterm.FakeUI{}
   131  			callUnbindService([]string{"my-app", "my-service"})
   132  			Expect(ui.Outputs()).ToNot(ContainSubstrings(
   133  				[]string{"Incorrect Usage", "Requires", "argument"},
   134  			))
   135  		})
   136  	})
   137  })