github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/servicekey/service_key_test.go (about) 1 package servicekey_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/commandregistry" 5 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 6 "code.cloudfoundry.org/cli/cf/errors" 7 "code.cloudfoundry.org/cli/cf/models" 8 "code.cloudfoundry.org/cli/cf/requirements" 9 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 10 11 "code.cloudfoundry.org/cli/cf/api/apifakes" 12 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 15 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("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("service-key").SetDependency(deps, pluginCall)) 38 } 39 40 BeforeEach(func() { 41 ui = &testterm.FakeUI{} 42 config = testconfig.NewRepositoryWithDefaults() 43 serviceRepo = new(apifakes.FakeServiceRepository) 44 serviceInstance := models.ServiceInstance{} 45 serviceInstance.GUID = "fake-service-instance-guid" 46 serviceInstance.Name = "fake-service-instance" 47 serviceRepo.FindInstanceByNameReturns(serviceInstance, nil) 48 serviceKeyRepo = apifakes.NewFakeServiceKeyRepo() 49 requirementsFactory = new(requirementsfakes.FakeFactory) 50 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 51 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 52 serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement) 53 requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq) 54 serviceInstanceReq.GetServiceInstanceReturns(serviceInstance) 55 }) 56 57 var callGetServiceKey = func(args []string) bool { 58 return testcmd.RunCLICommand("service-key", args, requirementsFactory, updateCommandDependency, false, ui) 59 } 60 61 Describe("requirements", func() { 62 It("fails when not logged in", func() { 63 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 64 Expect(callGetServiceKey([]string{"fake-service-key-name"})).To(BeFalse()) 65 }) 66 67 It("requires two arguments to run", func() { 68 Expect(callGetServiceKey([]string{})).To(BeFalse()) 69 Expect(callGetServiceKey([]string{"fake-arg-one"})).To(BeFalse()) 70 // This assertion is being skipped because the proper way to test this would be to refactor the existing integration style tests to real unit tests. We are going to rewrite the command and the tests in the refactor track anyways. 71 // Expect(callGetServiceKey([]string{"fake-arg-one", "fake-arg-two"})).To(BeTrue()) 72 Expect(callGetServiceKey([]string{"fake-arg-one", "fake-arg-two", "fake-arg-three"})).To(BeFalse()) 73 }) 74 75 It("fails when service instance is not found", func() { 76 serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement) 77 serviceInstanceReq.ExecuteReturns(errors.New("no service instance")) 78 requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq) 79 Expect(callGetServiceKey([]string{"non-exist-service-instance"})).To(BeFalse()) 80 }) 81 82 It("fails when space is not targeted", func() { 83 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "no targeted space"}) 84 Expect(callGetServiceKey([]string{"fake-service-instance", "fake-service-key-name"})).To(BeFalse()) 85 }) 86 }) 87 88 Describe("requirements are satisfied", func() { 89 Context("gets service key successfully", func() { 90 BeforeEach(func() { 91 serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{ 92 Fields: models.ServiceKeyFields{ 93 Name: "fake-service-key", 94 GUID: "fake-service-key-guid", 95 URL: "fake-service-key-url", 96 ServiceInstanceGUID: "fake-service-instance-guid", 97 ServiceInstanceURL: "fake-service-instance-url", 98 }, 99 Credentials: map[string]interface{}{ 100 "username": "fake-username", 101 "password": "fake-password", 102 "host": "fake-host", 103 "port": "3306", 104 "database": "fake-db-name", 105 "uri": "mysql://fake-user:fake-password@fake-host:3306/fake-db-name", 106 }, 107 } 108 }) 109 110 It("gets service credential", func() { 111 callGetServiceKey([]string{"fake-service-instance", "fake-service-key"}) 112 Expect(ui.Outputs()).To(ContainSubstrings( 113 []string{"Getting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"}, 114 []string{"username", "fake-username"}, 115 []string{"password", "fake-password"}, 116 []string{"host", "fake-host"}, 117 []string{"port", "3306"}, 118 []string{"database", "fake-db-name"}, 119 []string{"uri", "mysql://fake-user:fake-password@fake-host:3306/fake-db-name"}, 120 )) 121 Expect(ui.Outputs()[1]).To(BeEmpty()) 122 Expect(serviceKeyRepo.GetServiceKeyMethod.InstanceGUID).To(Equal("fake-service-instance-guid")) 123 }) 124 125 It("gets service guid when '--guid' flag is provided", func() { 126 callGetServiceKey([]string{"--guid", "fake-service-instance", "fake-service-key"}) 127 128 Expect(ui.Outputs()).To(ContainSubstrings([]string{"fake-service-key-guid"})) 129 Expect(ui.Outputs()).ToNot(ContainSubstrings( 130 []string{"Getting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"}, 131 )) 132 }) 133 }) 134 135 Context("when service key does not exist", func() { 136 It("shows no service key is found", func() { 137 callGetServiceKey([]string{"fake-service-instance", "non-exist-service-key"}) 138 Expect(ui.Outputs()).To(ContainSubstrings( 139 []string{"Getting key", "non-exist-service-key", "for service instance", "fake-service-instance", "as", "my-user"}, 140 []string{"No service key", "non-exist-service-key", "found for service instance", "fake-service-instance"}, 141 )) 142 }) 143 144 It("returns the empty string as guid when '--guid' flag is provided", func() { 145 callGetServiceKey([]string{"--guid", "fake-service-instance", "non-exist-service-key"}) 146 147 Expect(len(ui.Outputs())).To(Equal(1)) 148 Expect(ui.Outputs()[0]).To(BeEmpty()) 149 }) 150 }) 151 152 Context("when api returned NotAuthorizedError", func() { 153 It("shows no service key is found", func() { 154 serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{} 155 serviceKeyRepo.GetServiceKeyMethod.Error = &errors.NotAuthorizedError{} 156 157 callGetServiceKey([]string{"fake-service-instance", "fake-service-key"}) 158 Expect(ui.Outputs()).To(ContainSubstrings( 159 []string{"Getting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"}, 160 []string{"No service key", "fake-service-key", "found for service instance", "fake-service-instance"}, 161 )) 162 }) 163 }) 164 }) 165 })