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