) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo) deps.RepoLocator = deps.RepoLocator.SetServiceKeyRepository(serviceKeyRepo) deps.Config = config commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-service-key").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{} config = testconfig.NewRepositoryWithDefaults() serviceRepo = &apifakes.FakeServiceRepository{} serviceInstance := models.ServiceInstance{} serviceInstance.GUID = "fake-service-instance-guid" serviceRepo.FindInstanceByNameReturns(serviceInstance, nil) serviceKeyRepo = apifakes.NewFakeServiceKeyRepo() requirementsFactory = new(requirementsfakes.FakeFactory) requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) }) var callDeleteServiceKey = func(args []string) bool { return testcmd.RunCLICommand("delete-service-key", args, requirementsFactory, updateCommandDependency, false, ui) } Describe("requirements are not satisfied", func() { It("fails when not logged in", func() { requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) Expect(callDeleteServiceKey([]string{"fake-service-key-name"})).To(BeFalse())
}) }) }) Describe("Delete", func() { Context("when binding does exist", func() { var serviceInstance models.ServiceInstance BeforeEach(func() { setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "DELETE", Path: "/v2/service_bindings/service-binding-2-guid", Response: testnet.TestResponse{Status: http.StatusOK}, })) serviceInstance.GUID = "my-service-instance-guid" binding := models.ServiceBindingFields{} binding.URL = "/v2/service_bindings/service-binding-1-guid" binding.AppGUID = "app-1-guid" binding2 := models.ServiceBindingFields{} binding2.URL = "/v2/service_bindings/service-binding-2-guid" binding2.AppGUID = "app-2-guid" serviceInstance.ServiceBindings = []models.ServiceBindingFields{binding, binding2} }) It("deletes the service binding with the given guid", func() { found, apiErr := repo.Delete(serviceInstance, "app-2-guid") Expect(testHandler).To(HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred())
It("fails requirements when not logged in", func() { Expect(callBindService([]string{"service", "app"})).To(BeFalse()) }) Context("when logged in", func() { BeforeEach(func() { requirementsFactory.LoginSuccess = true }) It("binds a service instance to an app", func() { app := models.Application{} app.Name = "my-app" app.GUID = "my-app-guid" serviceInstance := models.ServiceInstance{} serviceInstance.Name = "my-service" serviceInstance.GUID = "my-service-guid" requirementsFactory.Application = app requirementsFactory.ServiceInstance = serviceInstance callBindService([]string{"my-app", "my-service"}) Expect(requirementsFactory.ApplicationName).To(Equal("my-app")) Expect(requirementsFactory.ServiceInstanceName).To(Equal("my-service")) Expect(ui.Outputs).To(ContainSubstrings( []string{"Binding service", "my-service", "my-app", "my-org", "my-space", "my-user"}, []string{"OK"}, []string{"TIP", "my-app"}, )) Expect(serviceBindingRepo.CreateServiceInstanceGUID).To(Equal("my-service-guid")) Expect(serviceBindingRepo.CreateApplicationGUID).To(Equal("my-app-guid")) })
It("fails when a space is not targeted", func() { requirementsFactory.LoginSuccess = true Expect(runCommand("okay-this-time-please??")).To(BeFalse()) }) }) Describe("After Requirement", func() { createServiceInstanceWithState := func(state string) { offering := models.ServiceOfferingFields{Label: "mysql", DocumentationURL: "http://documentation.url", Description: "the-description"} plan := models.ServicePlanFields{GUID: "plan-guid", Name: "plan-name"} serviceInstance := models.ServiceInstance{} serviceInstance.Name = "service1" serviceInstance.GUID = "service1-guid" serviceInstance.LastOperation.Type = "create" serviceInstance.LastOperation.State = "in progress" serviceInstance.LastOperation.Description = "creating resource - step 1" serviceInstance.ServicePlan = plan serviceInstance.ServiceOffering = offering serviceInstance.DashboardURL = "some-url" serviceInstance.LastOperation.State = state serviceInstance.LastOperation.CreatedAt = "created-date" serviceInstance.LastOperation.UpdatedAt = "updated-date" requirementsFactory.ServiceInstance = serviceInstance } createServiceInstance := func() { createServiceInstanceWithState("") }
}) It("fails when a space is not targeted", func() { requirementsFactory.LoginSuccess = true Expect(runCommand("banana", "faaaaasdf")).To(BeFalse()) }) }) Context("when logged in and a space is targeted", func() { var serviceInstance models.ServiceInstance BeforeEach(func() { serviceInstance = models.ServiceInstance{} serviceInstance.Name = "different-name" serviceInstance.GUID = "different-name-guid" requirementsFactory.LoginSuccess = true requirementsFactory.TargetedSpaceSuccess = true requirementsFactory.ServiceInstance = serviceInstance }) It("renames the service, obviously", func() { runCommand("my-service", "new-name") Expect(ui.Outputs).To(ContainSubstrings( []string{"Renaming service", "different-name", "new-name", "my-org", "my-space", "my-user"}, []string{"OK"}, )) actualServiceInstance, actualServiceName := serviceRepo.RenameServiceArgsForCall(0)