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

     1  package servicekey_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     5  	"code.cloudfoundry.org/cli/cf/errors"
     6  	"code.cloudfoundry.org/cli/cf/models"
     7  	"code.cloudfoundry.org/cli/cf/requirements"
     8  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
     9  
    10  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    11  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  
    15  	"code.cloudfoundry.org/cli/cf/commandregistry"
    16  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    17  
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("delete-service-key command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		config              coreconfig.Repository
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  		serviceRepo         *apifakes.FakeServiceRepository
    28  		serviceKeyRepo      *apifakes.OldFakeServiceKeyRepo
    29  		deps                commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    35  		deps.RepoLocator = deps.RepoLocator.SetServiceKeyRepository(serviceKeyRepo)
    36  		deps.Config = config
    37  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-service-key").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		config = testconfig.NewRepositoryWithDefaults()
    43  		serviceRepo = &apifakes.FakeServiceRepository{}
    44  		serviceInstance := models.ServiceInstance{}
    45  		serviceInstance.GUID = "fake-service-instance-guid"
    46  		serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
    47  		serviceKeyRepo = apifakes.NewFakeServiceKeyRepo()
    48  		requirementsFactory = new(requirementsfakes.FakeFactory)
    49  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    50  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    51  	})
    52  
    53  	var callDeleteServiceKey = func(args []string) bool {
    54  		return testcmd.RunCLICommand("delete-service-key", args, requirementsFactory, updateCommandDependency, false, ui)
    55  	}
    56  
    57  	Describe("requirements are not satisfied", func() {
    58  		It("fails when not logged in", func() {
    59  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    60  			Expect(callDeleteServiceKey([]string{"fake-service-key-name"})).To(BeFalse())
    61  		})
    62  
    63  		It("requires two arguments and one option to run", func() {
    64  			Expect(callDeleteServiceKey([]string{})).To(BeFalse())
    65  			Expect(callDeleteServiceKey([]string{"fake-arg-one"})).To(BeFalse())
    66  			Expect(callDeleteServiceKey([]string{"fake-arg-one", "fake-arg-two", "fake-arg-three"})).To(BeFalse())
    67  		})
    68  
    69  		It("fails when space is not targeted", func() {
    70  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "no targeted space"})
    71  			Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeFalse())
    72  		})
    73  	})
    74  
    75  	Describe("requirements are satisfied", func() {
    76  		Context("deletes service key successfully", func() {
    77  			BeforeEach(func() {
    78  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{
    79  					Fields: models.ServiceKeyFields{
    80  						Name:                "fake-service-key",
    81  						GUID:                "fake-service-key-guid",
    82  						URL:                 "fake-service-key-url",
    83  						ServiceInstanceGUID: "fake-service-instance-guid",
    84  						ServiceInstanceURL:  "fake-service-instance-url",
    85  					},
    86  					Credentials: map[string]interface{}{
    87  						"username": "fake-username",
    88  						"password": "fake-password",
    89  						"host":     "fake-host",
    90  						"port":     "3306",
    91  						"database": "fake-db-name",
    92  						"uri":      "mysql://fake-user:fake-password@fake-host:3306/fake-db-name",
    93  					},
    94  				}
    95  			})
    96  
    97  			It("deletes service key successfully when '-f' option is provided", func() {
    98  				Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key", "-f"})).To(BeTrue())
    99  				Expect(ui.Outputs()).To(ContainSubstrings(
   100  					[]string{"Deleting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   101  					[]string{"OK"}))
   102  			})
   103  
   104  			It("deletes service key successfully when '-f' option is not provided and confirmed 'yes'", func() {
   105  				ui.Inputs = append(ui.Inputs, "yes")
   106  
   107  				Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeTrue())
   108  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service key", "fake-service-key"}))
   109  				Expect(ui.Outputs()).To(ContainSubstrings(
   110  					[]string{"Deleting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   111  					[]string{"OK"}))
   112  			})
   113  
   114  			It("skips to delete service key when '-f' option is not provided and confirmed 'no'", func() {
   115  				ui.Inputs = append(ui.Inputs, "no")
   116  
   117  				Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeTrue())
   118  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service key", "fake-service-key"}))
   119  				Expect(ui.Outputs()).To(BeEmpty())
   120  			})
   121  
   122  		})
   123  
   124  		Context("deletes service key unsuccessful", func() {
   125  			It("fails to delete service key when service instance does not exist", func() {
   126  				serviceRepo.FindInstanceByNameReturns(models.ServiceInstance{}, errors.NewModelNotFoundError("Service instance", "non-exist-service-instance"))
   127  
   128  				callDeleteServiceKey([]string{"non-exist-service-instance", "fake-service-key", "-f"})
   129  
   130  				Expect(ui.Outputs()).To(ContainSubstrings(
   131  					[]string{"Deleting key", "fake-service-key", "for service instance", "non-exist-service-instance", "as", "my-user..."},
   132  					[]string{"OK"},
   133  					[]string{"Service instance", "non-exist-service-instance", "does not exist."},
   134  				))
   135  			})
   136  
   137  			It("fails to delete service key when the service key repository returns an error", func() {
   138  				serviceKeyRepo.GetServiceKeyMethod.Error = errors.New("")
   139  				callDeleteServiceKey([]string{"fake-service-instance", "non-exist-service-key", "-f"})
   140  
   141  				Expect(ui.Outputs()).To(ContainSubstrings(
   142  					[]string{"Deleting key", "non-exist-service-key", "for service instance", "fake-service-instance", "as", "my-user..."},
   143  					[]string{"OK"},
   144  					[]string{"Service key", "non-exist-service-key", "does not exist for service instance", "fake-service-instance"},
   145  				))
   146  			})
   147  
   148  			It("fails to delete service key when service key does not exist", func() {
   149  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{}
   150  				callDeleteServiceKey([]string{"fake-service-instance", "non-exist-service-key", "-f"})
   151  
   152  				Expect(ui.Outputs()).To(ContainSubstrings(
   153  					[]string{"Deleting key", "non-exist-service-key", "for service instance", "fake-service-instance", "as", "my-user..."},
   154  					[]string{"OK"},
   155  					[]string{"Service key", "non-exist-service-key", "does not exist for service instance", "fake-service-instance"},
   156  				))
   157  			})
   158  
   159  			It("shows no service key is found", func() {
   160  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{}
   161  				serviceKeyRepo.GetServiceKeyMethod.Error = &errors.NotAuthorizedError{}
   162  				callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key", "-f"})
   163  
   164  				Expect(ui.Outputs()).To(ContainSubstrings(
   165  					[]string{"Deleting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   166  					[]string{"No service key", "fake-service-key", "found for service instance", "fake-service-instance"},
   167  				))
   168  			})
   169  		})
   170  	})
   171  })