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

     1  package servicekey_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     5  	"github.com/cloudfoundry/cli/cf/errors"
     6  	"github.com/cloudfoundry/cli/cf/models"
     7  	"github.com/cloudfoundry/cli/generic"
     8  
     9  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  
    15  	"github.com/cloudfoundry/cli/cf/command_registry"
    16  	. "github.com/cloudfoundry/cli/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              core_config.Repository
    26  		requirementsFactory *testreq.FakeReqFactory
    27  		serviceRepo         *testapi.FakeServiceRepo
    28  		serviceKeyRepo      *testapi.FakeServiceKeyRepo
    29  		deps                command_registry.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  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-service-key").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		config = testconfig.NewRepositoryWithDefaults()
    43  		serviceRepo = &testapi.FakeServiceRepo{}
    44  		serviceInstance := models.ServiceInstance{}
    45  		serviceInstance.Guid = "fake-service-instance-guid"
    46  		serviceRepo.FindInstanceByNameMap = generic.NewMap()
    47  		serviceRepo.FindInstanceByNameMap.Set("fake-service-instance", serviceInstance)
    48  		serviceKeyRepo = testapi.NewFakeServiceKeyRepo()
    49  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    50  	})
    51  
    52  	var callDeleteServiceKey = func(args []string) bool {
    53  		return testcmd.RunCliCommand("delete-service-key", args, requirementsFactory, updateCommandDependency, false)
    54  	}
    55  
    56  	Describe("requirements are not satisfied", func() {
    57  		It("fails when not logged in", func() {
    58  			requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: false}
    59  			Expect(callDeleteServiceKey([]string{"fake-service-key-name"})).To(BeFalse())
    60  		})
    61  
    62  		It("requires two arguments and one option to run", func() {
    63  			Expect(callDeleteServiceKey([]string{})).To(BeFalse())
    64  			Expect(callDeleteServiceKey([]string{"fake-arg-one"})).To(BeFalse())
    65  			Expect(callDeleteServiceKey([]string{"fake-arg-one", "fake-arg-two", "fake-arg-three"})).To(BeFalse())
    66  		})
    67  
    68  		It("fails when space is not targetted", func() {
    69  			requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: false}
    70  			Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeFalse())
    71  		})
    72  	})
    73  
    74  	Describe("requirements are satisfied", func() {
    75  		Context("deletes service key successfully", func() {
    76  			BeforeEach(func() {
    77  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{
    78  					Fields: models.ServiceKeyFields{
    79  						Name:                "fake-service-key",
    80  						Guid:                "fake-service-key-guid",
    81  						Url:                 "fake-service-key-url",
    82  						ServiceInstanceGuid: "fake-service-instance-guid",
    83  						ServiceInstanceUrl:  "fake-service-instance-url",
    84  					},
    85  					Credentials: map[string]interface{}{
    86  						"username": "fake-username",
    87  						"password": "fake-password",
    88  						"host":     "fake-host",
    89  						"port":     "3306",
    90  						"database": "fake-db-name",
    91  						"uri":      "mysql://fake-user:fake-password@fake-host:3306/fake-db-name",
    92  					},
    93  				}
    94  			})
    95  
    96  			It("deletes service key successfully when '-f' option is provided", func() {
    97  				requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    98  
    99  				Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key", "-f"})).To(BeTrue())
   100  				Expect(ui.Outputs).To(ContainSubstrings(
   101  					[]string{"Deleting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   102  					[]string{"OK"}))
   103  			})
   104  
   105  			It("deletes service key successfully when '-f' option is not provided and confirmed 'yes'", func() {
   106  				requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
   107  				ui.Inputs = append(ui.Inputs, "yes")
   108  
   109  				Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeTrue())
   110  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service key", "fake-service-key"}))
   111  				Expect(ui.Outputs).To(ContainSubstrings(
   112  					[]string{"Deleting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   113  					[]string{"OK"}))
   114  			})
   115  
   116  			It("skips to delete service key when '-f' option is not provided and confirmed 'no'", func() {
   117  				requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
   118  				ui.Inputs = append(ui.Inputs, "no")
   119  
   120  				Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeTrue())
   121  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service key", "fake-service-key"}))
   122  				Expect(ui.Outputs).To(BeEmpty())
   123  			})
   124  
   125  		})
   126  
   127  		Context("deletes service key unsuccessful", func() {
   128  			It("fails to delete service key when service instance does not exist", func() {
   129  				serviceRepo.FindInstanceByNameNotFound = true
   130  				callDeleteServiceKey([]string{"non-exist-service-instance", "fake-service-key", "-f"})
   131  
   132  				Expect(ui.Outputs).To(ContainSubstrings(
   133  					[]string{"Deleting key", "fake-service-key", "for service instance", "non-exist-service-instance", "as", "my-user..."},
   134  					[]string{"OK"},
   135  					[]string{"Service instance", "non-exist-service-instance", "does not exist."},
   136  				))
   137  			})
   138  
   139  			It("fails to delete service key when the service key repository returns an error", func() {
   140  				serviceKeyRepo.GetServiceKeyMethod.Error = errors.New("")
   141  				callDeleteServiceKey([]string{"fake-service-instance", "non-exist-service-key", "-f"})
   142  
   143  				Expect(ui.Outputs).To(ContainSubstrings(
   144  					[]string{"Deleting key", "non-exist-service-key", "for service instance", "fake-service-instance", "as", "my-user..."},
   145  					[]string{"OK"},
   146  					[]string{"Service key", "non-exist-service-key", "does not exist for service instance", "fake-service-instance"},
   147  				))
   148  			})
   149  
   150  			It("fails to delete service key when service key does not exist", func() {
   151  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{}
   152  				callDeleteServiceKey([]string{"fake-service-instance", "non-exist-service-key", "-f"})
   153  
   154  				Expect(ui.Outputs).To(ContainSubstrings(
   155  					[]string{"Deleting key", "non-exist-service-key", "for service instance", "fake-service-instance", "as", "my-user..."},
   156  					[]string{"OK"},
   157  					[]string{"Service key", "non-exist-service-key", "does not exist for service instance", "fake-service-instance"},
   158  				))
   159  			})
   160  
   161  			It("shows no service key is found", func() {
   162  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{}
   163  				serviceKeyRepo.GetServiceKeyMethod.Error = &errors.NotAuthorizedError{}
   164  				callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key", "-f"})
   165  
   166  				Expect(ui.Outputs).To(ContainSubstrings(
   167  					[]string{"Deleting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   168  					[]string{"No service key", "fake-service-key", "found for service instance", "fake-service-instance"},
   169  				))
   170  			})
   171  		})
   172  	})
   173  })