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

     1  package service_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/models"
     7  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     8  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     9  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    10  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    11  
    12  	"github.com/cloudfoundry/cli/cf/command_registry"
    13  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("update-user-provided-service test", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		configRepo          core_config.Repository
    22  		serviceRepo         *testapi.FakeUserProvidedServiceInstanceRepository
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		deps                command_registry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.Ui = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetUserProvidedServiceInstanceRepository(serviceRepo)
    30  		deps.Config = configRepo
    31  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("update-user-provided-service").SetDependency(deps, pluginCall))
    32  	}
    33  
    34  	BeforeEach(func() {
    35  		ui = &testterm.FakeUI{}
    36  		serviceRepo = &testapi.FakeUserProvidedServiceInstanceRepository{}
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		requirementsFactory = &testreq.FakeReqFactory{}
    39  	})
    40  
    41  	runCommand := func(args ...string) bool {
    42  		return testcmd.RunCliCommand("update-user-provided-service", args, requirementsFactory, updateCommandDependency, false)
    43  	}
    44  
    45  	Describe("requirements", func() {
    46  		It("fails with usage when not provided the name of the service to update", func() {
    47  			requirementsFactory.LoginSuccess = true
    48  			runCommand()
    49  			Expect(ui.Outputs).To(ContainSubstrings(
    50  				[]string{"Incorrect Usage", "Requires", "argument"},
    51  			))
    52  		})
    53  
    54  		It("fails when not logged in", func() {
    55  			Expect(runCommand("whoops")).To(BeFalse())
    56  		})
    57  	})
    58  
    59  	Context("when logged in", func() {
    60  		BeforeEach(func() {
    61  			requirementsFactory.LoginSuccess = true
    62  
    63  			serviceInstance := models.ServiceInstance{}
    64  			serviceInstance.Name = "service-name"
    65  			requirementsFactory.ServiceInstance = serviceInstance
    66  		})
    67  
    68  		Context("when no flags are provided", func() {
    69  			It("tells the user that no changes occurred", func() {
    70  				runCommand("service-name")
    71  
    72  				Expect(ui.Outputs).To(ContainSubstrings(
    73  					[]string{"Updating user provided service", "service-name", "my-org", "my-space", "my-user"},
    74  					[]string{"OK"},
    75  					[]string{"No changes"},
    76  				))
    77  			})
    78  		})
    79  
    80  		Context("when the user provides valid JSON with the -p flag", func() {
    81  			It("updates the user provided service specified", func() {
    82  				runCommand("-p", `{"foo":"bar"}`, "-l", "syslog://example.com", "service-name")
    83  
    84  				Expect(requirementsFactory.ServiceInstanceName).To(Equal("service-name"))
    85  				Expect(ui.Outputs).To(ContainSubstrings(
    86  					[]string{"Updating user provided service", "service-name", "my-org", "my-space", "my-user"},
    87  					[]string{"OK"},
    88  					[]string{"TIP"},
    89  				))
    90  
    91  				Expect(serviceRepo.UpdateArgsForCall(0).Name).To(Equal("service-name"))
    92  				Expect(serviceRepo.UpdateArgsForCall(0).Params).To(Equal(map[string]interface{}{"foo": "bar"}))
    93  				Expect(serviceRepo.UpdateArgsForCall(0).SysLogDrainUrl).To(Equal("syslog://example.com"))
    94  			})
    95  		})
    96  
    97  		Context("when the user provides invalid JSON with the -p flag", func() {
    98  			It("tells the user the JSON is invalid", func() {
    99  				runCommand("-p", `{"foo":"ba WHOOPS OH MY HOW DID THIS GET HERE???`, "service-name")
   100  
   101  				Expect(serviceRepo.UpdateCallCount()).To(Equal(0))
   102  
   103  				Expect(ui.Outputs).To(ContainSubstrings(
   104  					[]string{"FAILED"},
   105  					[]string{"JSON is invalid"},
   106  				))
   107  			})
   108  		})
   109  
   110  		Context("when the service with the given name is not user provided", func() {
   111  			BeforeEach(func() {
   112  				plan := models.ServicePlanFields{Guid: "my-plan-guid"}
   113  				serviceInstance := models.ServiceInstance{}
   114  				serviceInstance.Name = "found-service-name"
   115  				serviceInstance.ServicePlan = plan
   116  
   117  				requirementsFactory.ServiceInstance = serviceInstance
   118  			})
   119  
   120  			It("fails and tells the user what went wrong", func() {
   121  				runCommand("-p", `{"foo":"bar"}`, "service-name")
   122  
   123  				Expect(serviceRepo.UpdateCallCount()).To(Equal(0))
   124  
   125  				Expect(ui.Outputs).To(ContainSubstrings(
   126  					[]string{"FAILED"},
   127  					[]string{"Service Instance is not user provided"},
   128  				))
   129  			})
   130  		})
   131  	})
   132  })