github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/marketplace_test.go (about) 1 package service_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/actors/service_builder/fakes" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/models" 8 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 9 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 10 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 11 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 . "github.com/cloudfoundry/cli/testhelpers/matchers" 16 ) 17 18 var _ = Describe("marketplace command", func() { 19 var ui *testterm.FakeUI 20 var requirementsFactory *testreq.FakeReqFactory 21 var config core_config.Repository 22 var serviceBuilder *testapi.FakeServiceBuilder 23 var fakeServiceOfferings []models.ServiceOffering 24 var serviceWithAPaidPlan models.ServiceOffering 25 var service2 models.ServiceOffering 26 var deps command_registry.Dependency 27 28 updateCommandDependency := func(pluginCall bool) { 29 deps.Ui = ui 30 deps.Config = config 31 deps.ServiceBuilder = serviceBuilder 32 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("marketplace").SetDependency(deps, pluginCall)) 33 } 34 35 BeforeEach(func() { 36 serviceBuilder = &testapi.FakeServiceBuilder{} 37 ui = &testterm.FakeUI{} 38 requirementsFactory = &testreq.FakeReqFactory{ApiEndpointSuccess: true} 39 40 serviceWithAPaidPlan = models.ServiceOffering{ 41 Plans: []models.ServicePlanFields{ 42 models.ServicePlanFields{Name: "service-plan-a", Description: "service-plan-a description", Free: true}, 43 models.ServicePlanFields{Name: "service-plan-b", Description: "service-plan-b description", Free: false}, 44 }, 45 ServiceOfferingFields: models.ServiceOfferingFields{ 46 Label: "zzz-my-service-offering", 47 Guid: "service-1-guid", 48 Description: "service offering 1 description", 49 }} 50 service2 = models.ServiceOffering{ 51 Plans: []models.ServicePlanFields{ 52 models.ServicePlanFields{Name: "service-plan-c", Free: true}, 53 models.ServicePlanFields{Name: "service-plan-d", Free: true}}, 54 ServiceOfferingFields: models.ServiceOfferingFields{ 55 Label: "aaa-my-service-offering", 56 Description: "service offering 2 description", 57 }, 58 } 59 fakeServiceOfferings = []models.ServiceOffering{serviceWithAPaidPlan, service2} 60 }) 61 62 Describe("Requirements", func() { 63 Context("when the an API endpoint is not targeted", func() { 64 It("does not meet its requirements", func() { 65 config = testconfig.NewRepository() 66 requirementsFactory.ApiEndpointSuccess = false 67 68 Expect(testcmd.RunCliCommand("marketplace", []string{}, requirementsFactory, updateCommandDependency, false)).To(BeFalse()) 69 }) 70 It("should fail with usage when provided any arguments", func() { 71 config = testconfig.NewRepository() 72 requirementsFactory.ApiEndpointSuccess = true 73 Expect(testcmd.RunCliCommand("marketplace", []string{"blahblah"}, requirementsFactory, updateCommandDependency, false)).To(BeFalse()) 74 Expect(ui.Outputs).To(ContainSubstrings( 75 []string{"Incorrect Usage", "No argument"}, 76 )) 77 }) 78 }) 79 }) 80 81 Context("when the user is logged in", func() { 82 BeforeEach(func() { 83 config = testconfig.NewRepositoryWithDefaults() 84 }) 85 86 Context("when the user has a space targeted", func() { 87 BeforeEach(func() { 88 config.SetSpaceFields(models.SpaceFields{ 89 Guid: "the-space-guid", 90 Name: "the-space-name", 91 }) 92 serviceBuilder.GetServicesForSpaceWithPlansReturns(fakeServiceOfferings, nil) 93 }) 94 95 It("lists all of the service offerings for the space", func() { 96 testcmd.RunCliCommand("marketplace", []string{}, requirementsFactory, updateCommandDependency, false) 97 98 args := serviceBuilder.GetServicesForSpaceWithPlansArgsForCall(0) 99 Expect(args).To(Equal("the-space-guid")) 100 101 Expect(ui.Outputs).To(ContainSubstrings( 102 []string{"Getting services from marketplace in org", "my-org", "the-space-name", "my-user"}, 103 []string{"OK"}, 104 []string{"service", "plans", "description"}, 105 []string{"aaa-my-service-offering", "service offering 2 description", "service-plan-c,", "service-plan-d"}, 106 []string{"zzz-my-service-offering", "service offering 1 description", "service-plan-a,", "service-plan-b*"}, 107 []string{"* These service plans have an associated cost. Creating a service instance will incur this cost."}, 108 []string{"TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service."}, 109 )) 110 }) 111 112 Context("when there are no paid plans", func() { 113 BeforeEach(func() { 114 serviceBuilder.GetServicesForSpaceWithPlansReturns([]models.ServiceOffering{service2}, nil) 115 }) 116 117 It("lists the service offerings without displaying the paid message", func() { 118 testcmd.RunCliCommand("marketplace", []string{}, requirementsFactory, updateCommandDependency, false) 119 120 Expect(ui.Outputs).To(ContainSubstrings( 121 []string{"Getting services from marketplace in org", "my-org", "the-space-name", "my-user"}, 122 []string{"OK"}, 123 []string{"service", "plans", "description"}, 124 []string{"aaa-my-service-offering", "service offering 2 description", "service-plan-c", "service-plan-d"}, 125 )) 126 Expect(ui.Outputs).NotTo(ContainSubstrings( 127 []string{"* The denoted service plans have specific costs associated with them. If a service instance of this type is created, a cost will be incurred."}, 128 )) 129 }) 130 131 }) 132 133 Context("when the user passes the -s flag", func() { 134 It("Displays the list of plans for each service with info", func() { 135 serviceBuilder.GetServiceByNameForSpaceWithPlansReturns(serviceWithAPaidPlan, nil) 136 137 testcmd.RunCliCommand("marketplace", []string{"-s", "aaa-my-service-offering"}, requirementsFactory, updateCommandDependency, false) 138 139 Expect(ui.Outputs).To(ContainSubstrings( 140 []string{"Getting service plan information for service aaa-my-service-offering as my-user..."}, 141 []string{"OK"}, 142 []string{"service plan", "description", "free or paid"}, 143 []string{"service-plan-a", "service-plan-a description", "free"}, 144 []string{"service-plan-b", "service-plan-b description", "paid"}, 145 )) 146 }) 147 148 It("informs the user if the service cannot be found", func() { 149 testcmd.RunCliCommand("marketplace", []string{"-s", "aaa-my-service-offering"}, requirementsFactory, updateCommandDependency, false) 150 151 Expect(ui.Outputs).To(ContainSubstrings( 152 []string{"Service offering not found"}, 153 )) 154 Expect(ui.Outputs).ToNot(ContainSubstrings( 155 []string{"service plan", "description", "free or paid"}, 156 )) 157 }) 158 }) 159 }) 160 161 Context("when the user doesn't have a space targeted", func() { 162 BeforeEach(func() { 163 config.SetSpaceFields(models.SpaceFields{}) 164 }) 165 166 It("tells the user to target a space", func() { 167 testcmd.RunCliCommand("marketplace", []string{}, requirementsFactory, updateCommandDependency, false) 168 Expect(ui.Outputs).To(ContainSubstrings( 169 []string{"without", "space"}, 170 )) 171 }) 172 }) 173 }) 174 175 Context("when user is not logged in", func() { 176 BeforeEach(func() { 177 config = testconfig.NewRepository() 178 }) 179 180 It("lists all public service offerings if any are available", func() { 181 serviceBuilder = &testapi.FakeServiceBuilder{} 182 serviceBuilder.GetAllServicesWithPlansReturns(fakeServiceOfferings, nil) 183 184 testcmd.RunCliCommand("marketplace", []string{}, requirementsFactory, updateCommandDependency, false) 185 186 Expect(ui.Outputs).To(ContainSubstrings( 187 []string{"Getting all services from marketplace"}, 188 []string{"OK"}, 189 []string{"service", "plans", "description"}, 190 []string{"aaa-my-service-offering", "service offering 2 description", "service-plan-c", "service-plan-d"}, 191 []string{"zzz-my-service-offering", "service offering 1 description", "service-plan-a", "service-plan-b"}, 192 )) 193 }) 194 195 It("does not display a table if no service offerings exist", func() { 196 serviceBuilder := &testapi.FakeServiceBuilder{} 197 serviceBuilder.GetAllServicesWithPlansReturns([]models.ServiceOffering{}, nil) 198 199 testcmd.RunCliCommand("marketplace", []string{}, requirementsFactory, updateCommandDependency, false) 200 201 Expect(ui.Outputs).To(ContainSubstrings( 202 []string{"No service offerings found"}, 203 )) 204 Expect(ui.Outputs).ToNot(ContainSubstrings( 205 []string{"service", "plans", "description"}, 206 )) 207 }) 208 209 Context("when the user passes the -s flag", func() { 210 It("Displays the list of plans for each service with info", func() { 211 serviceBuilder.GetServiceByNameWithPlansReturns(serviceWithAPaidPlan, nil) 212 testcmd.RunCliCommand("marketplace", []string{"-s", "aaa-my-service-offering"}, requirementsFactory, updateCommandDependency, false) 213 214 Expect(ui.Outputs).To(ContainSubstrings( 215 []string{"Getting service plan information for service aaa-my-service-offering"}, 216 []string{"OK"}, 217 []string{"service plan", "description", "free or paid"}, 218 []string{"service-plan-a", "service-plan-a description", "free"}, 219 []string{"service-plan-b", "service-plan-b description", "paid"}, 220 )) 221 }) 222 223 It("informs the user if the service cannot be found", func() { 224 testcmd.RunCliCommand("marketplace", []string{"-s", "aaa-my-service-offering"}, requirementsFactory, updateCommandDependency, false) 225 226 Expect(ui.Outputs).To(ContainSubstrings( 227 []string{"Service offering not found"}, 228 )) 229 Expect(ui.Outputs).ToNot(ContainSubstrings( 230 []string{"service plan", "description", "free or paid"}, 231 )) 232 }) 233 }) 234 }) 235 })