github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/servicekey/service_keys_test.go (about) 1 package servicekey_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/command_registry" 5 "github.com/cloudfoundry/cli/cf/configuration/core_config" 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/testhelpers/matchers" 16 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("service-keys command", func() { 22 var ( 23 ui *testterm.FakeUI 24 config core_config.Repository 25 requirementsFactory *testreq.FakeReqFactory 26 serviceRepo *testapi.FakeServiceRepo 27 serviceKeyRepo *testapi.FakeServiceKeyRepo 28 deps command_registry.Dependency 29 ) 30 31 updateCommandDependency := func(pluginCall bool) { 32 deps.Ui = ui 33 deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo) 34 deps.RepoLocator = deps.RepoLocator.SetServiceKeyRepository(serviceKeyRepo) 35 deps.Config = config 36 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("service-keys").SetDependency(deps, pluginCall)) 37 } 38 39 BeforeEach(func() { 40 ui = &testterm.FakeUI{} 41 config = testconfig.NewRepositoryWithDefaults() 42 serviceRepo = &testapi.FakeServiceRepo{} 43 serviceInstance := models.ServiceInstance{} 44 serviceInstance.Guid = "fake-instance-guid" 45 serviceInstance.Name = "fake-service-instance" 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, ServiceInstanceNotFound: false} 50 requirementsFactory.ServiceInstance = serviceInstance 51 }) 52 53 var callListServiceKeys = func(args []string) bool { 54 return testcmd.RunCliCommand("service-keys", args, requirementsFactory, updateCommandDependency, false) 55 } 56 57 Describe("requirements", func() { 58 It("fails when not logged in", func() { 59 requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: false} 60 Expect(callListServiceKeys([]string{"fake-service-instance", "fake-service-key"})).To(BeFalse()) 61 }) 62 63 It("requires one argument to run", func() { 64 Expect(callListServiceKeys([]string{})).To(BeFalse()) 65 Expect(callListServiceKeys([]string{"fake-arg-one"})).To(BeTrue()) 66 Expect(callListServiceKeys([]string{"fake-arg-one", "fake-arg-two"})).To(BeFalse()) 67 }) 68 69 It("fails when service instance is not found", func() { 70 requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, ServiceInstanceNotFound: true} 71 Expect(callListServiceKeys([]string{"non-exist-service-instance"})).To(BeFalse()) 72 }) 73 74 It("fails when space is not targetted", func() { 75 requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: false} 76 Expect(callListServiceKeys([]string{"non-exist-service-instance"})).To(BeFalse()) 77 }) 78 }) 79 80 Describe("requirements are satisfied", func() { 81 It("list service keys successfully", func() { 82 serviceKeyRepo.ListServiceKeysMethod.ServiceKeys = []models.ServiceKey{ 83 models.ServiceKey{ 84 Fields: models.ServiceKeyFields{ 85 Name: "fake-service-key-1", 86 }, 87 }, 88 models.ServiceKey{ 89 Fields: models.ServiceKeyFields{ 90 Name: "fake-service-key-2", 91 }, 92 }, 93 } 94 callListServiceKeys([]string{"fake-service-instance"}) 95 Expect(ui.Outputs).To(ContainSubstrings( 96 []string{"Getting keys for service instance", "fake-service-instance", "as", "my-user"}, 97 []string{"name"}, 98 []string{"fake-service-key-1"}, 99 []string{"fake-service-key-2"}, 100 )) 101 Expect(ui.Outputs[1]).To(BeEmpty()) 102 Expect(serviceKeyRepo.ListServiceKeysMethod.InstanceGuid).To(Equal("fake-instance-guid")) 103 }) 104 105 It("does not list service keys when none are returned", func() { 106 callListServiceKeys([]string{"fake-service-instance"}) 107 Expect(ui.Outputs).To(ContainSubstrings( 108 []string{"Getting keys for service instance", "fake-service-instance", "as", "my-user"}, 109 []string{"No service key for service instance", "fake-service-instance"}, 110 )) 111 }) 112 }) 113 })