github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/servicekey/service_keys_test.go (about) 1 package servicekey_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 12 "code.cloudfoundry.org/cli/cf/api/apifakes" 13 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 14 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 15 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 16 17 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 18 19 . "github.com/onsi/ginkgo" 20 . "github.com/onsi/gomega" 21 ) 22 23 var _ = Describe("service-keys command", func() { 24 var ( 25 ui *testterm.FakeUI 26 config coreconfig.Repository 27 requirementsFactory *requirementsfakes.FakeFactory 28 serviceRepo *apifakes.FakeServiceRepository 29 serviceKeyRepo *apifakes.OldFakeServiceKeyRepo 30 deps commandregistry.Dependency 31 ) 32 33 updateCommandDependency := func(pluginCall bool) { 34 deps.UI = ui 35 deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo) 36 deps.RepoLocator = deps.RepoLocator.SetServiceKeyRepository(serviceKeyRepo) 37 deps.Config = config 38 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("service-keys").SetDependency(deps, pluginCall)) 39 } 40 41 BeforeEach(func() { 42 ui = &testterm.FakeUI{} 43 config = testconfig.NewRepositoryWithDefaults() 44 serviceRepo = new(apifakes.FakeServiceRepository) 45 serviceInstance := models.ServiceInstance{} 46 serviceInstance.GUID = "fake-instance-guid" 47 serviceInstance.Name = "fake-service-instance" 48 serviceRepo.FindInstanceByNameReturns(serviceInstance, nil) 49 serviceKeyRepo = apifakes.NewFakeServiceKeyRepo() 50 requirementsFactory = new(requirementsfakes.FakeFactory) 51 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 52 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 53 serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement) 54 requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq) 55 serviceInstanceReq.GetServiceInstanceReturns(serviceInstance) 56 }) 57 58 var callListServiceKeys = func(args []string) bool { 59 return testcmd.RunCLICommand("service-keys", args, requirementsFactory, updateCommandDependency, false, ui) 60 } 61 62 Describe("requirements", func() { 63 It("fails when not logged in", func() { 64 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 65 Expect(callListServiceKeys([]string{"fake-service-instance", "fake-service-key"})).To(BeFalse()) 66 }) 67 68 It("requires one argument to run", func() { 69 Expect(callListServiceKeys([]string{})).To(BeFalse()) 70 Expect(callListServiceKeys([]string{"fake-arg-one"})).To(BeTrue()) 71 Expect(callListServiceKeys([]string{"fake-arg-one", "fake-arg-two"})).To(BeFalse()) 72 }) 73 74 It("fails when service instance is not found", func() { 75 serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement) 76 serviceInstanceReq.ExecuteReturns(errors.New("no service instance")) 77 requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq) 78 Expect(callListServiceKeys([]string{"non-exist-service-instance"})).To(BeFalse()) 79 }) 80 81 It("fails when space is not targetted", func() { 82 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "no targeted space"}) 83 Expect(callListServiceKeys([]string{"non-exist-service-instance"})).To(BeFalse()) 84 }) 85 }) 86 87 Describe("requirements are satisfied", func() { 88 It("list service keys successfully", func() { 89 serviceKeyRepo.ListServiceKeysMethod.ServiceKeys = []models.ServiceKey{ 90 { 91 Fields: models.ServiceKeyFields{ 92 Name: "fake-service-key-1", 93 }, 94 }, 95 { 96 Fields: models.ServiceKeyFields{ 97 Name: "fake-service-key-2", 98 }, 99 }, 100 } 101 callListServiceKeys([]string{"fake-service-instance"}) 102 Expect(ui.Outputs()).To(ContainSubstrings( 103 []string{"Getting keys for service instance", "fake-service-instance", "as", "my-user"}, 104 []string{"name"}, 105 []string{"fake-service-key-1"}, 106 []string{"fake-service-key-2"}, 107 )) 108 Expect(ui.Outputs()[1]).To(BeEmpty()) 109 Expect(serviceKeyRepo.ListServiceKeysMethod.InstanceGUID).To(Equal("fake-instance-guid")) 110 }) 111 112 It("does not list service keys when none are returned", func() { 113 callListServiceKeys([]string{"fake-service-instance"}) 114 Expect(ui.Outputs()).To(ContainSubstrings( 115 []string{"Getting keys for service instance", "fake-service-instance", "as", "my-user"}, 116 []string{"No service key for service instance", "fake-service-instance"}, 117 )) 118 }) 119 }) 120 })