github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/marketplace_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  	. "code.cloudfoundry.org/cli/command/v6"
    10  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("marketplace Command", func() {
    19  	var (
    20  		cmd             MarketplaceCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v6fakes.FakeServicesSummariesActor
    25  		binaryName      string
    26  		executeErr      error
    27  		extraArgs       []string
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    34  		fakeActor = new(v6fakes.FakeServicesSummariesActor)
    35  
    36  		cmd = MarketplaceCommand{
    37  			UI:          testUI,
    38  			Config:      fakeConfig,
    39  			SharedActor: fakeSharedActor,
    40  			Actor:       fakeActor,
    41  		}
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  		extraArgs = nil
    46  	})
    47  
    48  	JustBeforeEach(func() {
    49  		executeErr = cmd.Execute(extraArgs)
    50  	})
    51  
    52  	When("too many arguments are provided", func() {
    53  		BeforeEach(func() {
    54  			extraArgs = []string{"extra"}
    55  		})
    56  
    57  		It("returns a TooManyArgumentsError", func() {
    58  			Expect(executeErr).To(MatchError(translatableerror.TooManyArgumentsError{
    59  				ExtraArgument: "extra",
    60  			}))
    61  		})
    62  	})
    63  
    64  	When("the user is not logged in", func() {
    65  		BeforeEach(func() {
    66  			fakeSharedActor.IsLoggedInReturns(false)
    67  		})
    68  
    69  		Context("and the -s flag is passed", func() {
    70  			BeforeEach(func() {
    71  				cmd.ServiceName = "service-a"
    72  			})
    73  
    74  			When("a service exists that has has multiple plans", func() {
    75  				BeforeEach(func() {
    76  					serviceSummary := v2action.ServiceSummary{
    77  						Service: v2action.Service{
    78  							Label:       "service-a",
    79  							Description: "fake service",
    80  						},
    81  						Plans: []v2action.ServicePlanSummary{
    82  							{
    83  								ServicePlan: v2action.ServicePlan{
    84  									Name:        "plan-a",
    85  									Description: "plan-a-description",
    86  									Free:        false,
    87  								},
    88  							},
    89  							{
    90  								ServicePlan: v2action.ServicePlan{
    91  									Name:        "plan-b",
    92  									Description: "plan-b-description",
    93  									Free:        true,
    94  								},
    95  							},
    96  						},
    97  					}
    98  
    99  					fakeActor.GetServiceSummaryByNameReturns(serviceSummary, v2action.Warnings{"warning"}, nil)
   100  				})
   101  
   102  				It("outputs a header", func() {
   103  					Expect(testUI.Out).To(Say("Getting service plan information for service service-a\\.\\.\\."))
   104  				})
   105  
   106  				It("outputs OK", func() {
   107  					Expect(testUI.Out).To(Say("OK"))
   108  				})
   109  
   110  				It("outputs details about the specific service", func() {
   111  					Expect(testUI.Out).Should(Say(
   112  						"service plan\\s+description\\s+free or paid\n" +
   113  							"plan-a\\s+plan-a-description\\s+paid" +
   114  							"\nplan-b\\s+plan-b-description\\s+free"))
   115  				})
   116  
   117  				It("outputs any warnings", func() {
   118  					Expect(testUI.Err).To(Say("warning"))
   119  				})
   120  			})
   121  
   122  			When("there is an error getting the service", func() {
   123  				BeforeEach(func() {
   124  					fakeActor.GetServiceSummaryByNameReturns(v2action.ServiceSummary{}, v2action.Warnings{"warning"}, errors.New("oops"))
   125  				})
   126  
   127  				It("returns the error", func() {
   128  					Expect(executeErr).To(MatchError("oops"))
   129  				})
   130  
   131  				It("outputs any warnings", func() {
   132  					Expect(testUI.Err).To(Say("warning"))
   133  				})
   134  			})
   135  		})
   136  
   137  		When("there are no flags passed", func() {
   138  			When("there are no services available", func() {
   139  				BeforeEach(func() {
   140  					fakeActor.GetServicesSummariesReturns([]v2action.ServiceSummary{}, v2action.Warnings{}, nil)
   141  				})
   142  
   143  				It("outputs a header", func() {
   144  					Expect(testUI.Out).To(Say("Getting all services from marketplace\\.\\.\\."))
   145  				})
   146  
   147  				It("outputs OK", func() {
   148  					Expect(testUI.Out).To(Say("OK"))
   149  				})
   150  
   151  				It("outputs that none are available", func() {
   152  					Expect(testUI.Out).To(Say("No service offerings found"))
   153  				})
   154  			})
   155  
   156  			When("there are multiple services available", func() {
   157  				BeforeEach(func() {
   158  					servicesSummaries := []v2action.ServiceSummary{
   159  						{
   160  							Service: v2action.Service{
   161  								Label:       "service-a",
   162  								Description: "fake service-a",
   163  							},
   164  							Plans: []v2action.ServicePlanSummary{
   165  								{
   166  									ServicePlan: v2action.ServicePlan{Name: "plan-a"},
   167  								},
   168  								{
   169  									ServicePlan: v2action.ServicePlan{Name: "plan-b"},
   170  								},
   171  							},
   172  						},
   173  						{
   174  							Service: v2action.Service{
   175  								Label:       "service-b",
   176  								Description: "fake service-b",
   177  							},
   178  							Plans: []v2action.ServicePlanSummary{
   179  								{
   180  									ServicePlan: v2action.ServicePlan{Name: "plan-c"},
   181  								},
   182  							},
   183  						},
   184  					}
   185  
   186  					fakeActor.GetServicesSummariesReturns(servicesSummaries, v2action.Warnings{"warning"}, nil)
   187  				})
   188  
   189  				It("outputs a header", func() {
   190  					Expect(testUI.Out).To(Say("Getting all services from marketplace\\.\\.\\."))
   191  				})
   192  
   193  				It("outputs OK", func() {
   194  					Expect(testUI.Out).To(Say("OK"))
   195  				})
   196  
   197  				It("outputs available services and plans", func() {
   198  					Expect(testUI.Out).Should(Say("service\\s+plans\\s+description\n" +
   199  						"service-a\\s+plan-a, plan-b\\s+fake service-a\n" +
   200  						"service-b\\s+plan-c\\s+fake service-b"))
   201  				})
   202  
   203  				It("outputs a tip to use the -s flag", func() {
   204  					Expect(testUI.Out).To(Say("TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service."))
   205  				})
   206  
   207  				It("outputs any warnings", func() {
   208  					Expect(testUI.Err).To(Say("warning"))
   209  				})
   210  			})
   211  
   212  			When("there is an error getting the available services", func() {
   213  				BeforeEach(func() {
   214  					fakeActor.GetServicesSummariesReturns([]v2action.ServiceSummary{}, v2action.Warnings{"warning"}, errors.New("oops"))
   215  				})
   216  
   217  				It("returns the error", func() {
   218  					Expect(executeErr).To(MatchError("oops"))
   219  				})
   220  
   221  				It("outputs any warnings", func() {
   222  					Expect(testUI.Err).To(Say("warning"))
   223  				})
   224  			})
   225  		})
   226  	})
   227  
   228  	When("the user is logged in but not targeting an org", func() {
   229  		BeforeEach(func() {
   230  			fakeSharedActor.IsLoggedInReturns(true)
   231  			fakeSharedActor.IsOrgTargetedReturns(false)
   232  		})
   233  
   234  		When("no flags are passed", func() {
   235  			It("returns an error saying the user must have a space targeted", func() {
   236  				Expect(executeErr).To(MatchError("Cannot list marketplace services without a targeted space"))
   237  			})
   238  		})
   239  
   240  		When("the -s flag is passed", func() {
   241  			BeforeEach(func() {
   242  				cmd.ServiceName = "service-a"
   243  			})
   244  
   245  			It("returns an error saying the user must have a space targeted", func() {
   246  				Expect(executeErr).To(MatchError("Cannot list plan information for service-a without a targeted space"))
   247  			})
   248  		})
   249  	})
   250  
   251  	When("the user is logged in and targeting and org but not a space", func() {
   252  		BeforeEach(func() {
   253  			fakeSharedActor.IsLoggedInReturns(true)
   254  			fakeSharedActor.IsOrgTargetedReturns(true)
   255  			fakeSharedActor.IsSpaceTargetedReturns(false)
   256  		})
   257  
   258  		When("no flags are passed", func() {
   259  			It("returns an error saying the user must have a space targeted", func() {
   260  				Expect(executeErr).To(MatchError("Cannot list marketplace services without a targeted space"))
   261  			})
   262  		})
   263  
   264  		When("the -s flag is passed", func() {
   265  			BeforeEach(func() {
   266  				cmd.ServiceName = "service-a"
   267  			})
   268  
   269  			It("returns an error saying the user must have a space targeted", func() {
   270  				Expect(executeErr).To(MatchError("Cannot list plan information for service-a without a targeted space"))
   271  			})
   272  		})
   273  	})
   274  
   275  	When("the user is logged in and targeting an org and space", func() {
   276  		BeforeEach(func() {
   277  			fakeSharedActor.IsLoggedInReturns(true)
   278  			fakeSharedActor.IsOrgTargetedReturns(true)
   279  			fakeSharedActor.IsSpaceTargetedReturns(true)
   280  
   281  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "org-a"})
   282  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "space-a", GUID: "space-guid"})
   283  		})
   284  
   285  		When("fetching the current user fails", func() {
   286  			BeforeEach(func() {
   287  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("kaboom"))
   288  			})
   289  
   290  			It("returns the error", func() {
   291  				Expect(executeErr).To(MatchError("kaboom"))
   292  			})
   293  		})
   294  
   295  		When("fetching the user succeeds", func() {
   296  			BeforeEach(func() {
   297  				fakeConfig.CurrentUserReturns(configv3.User{Name: "user-a"}, nil)
   298  			})
   299  
   300  			When("the -s flag is passed", func() {
   301  				BeforeEach(func() {
   302  					cmd.ServiceName = "service-a"
   303  				})
   304  
   305  				When("a service exists that has has multiple plans", func() {
   306  					BeforeEach(func() {
   307  						serviceSummary := v2action.ServiceSummary{
   308  							Service: v2action.Service{
   309  								Label:       "service-a",
   310  								Description: "fake service",
   311  							},
   312  							Plans: []v2action.ServicePlanSummary{
   313  								{
   314  									ServicePlan: v2action.ServicePlan{
   315  										Name:        "plan-a",
   316  										Description: "plan-a-description",
   317  										Free:        false,
   318  									},
   319  								},
   320  								{
   321  									ServicePlan: v2action.ServicePlan{
   322  										Name:        "plan-b",
   323  										Description: "plan-b-description",
   324  										Free:        true,
   325  									},
   326  								},
   327  							},
   328  						}
   329  
   330  						fakeActor.GetServiceSummaryForSpaceByNameReturns(serviceSummary, v2action.Warnings{"warning"}, nil)
   331  					})
   332  
   333  					It("outputs a header", func() {
   334  						Expect(testUI.Out).To(Say("Getting service plan information for service service-a as user-a\\.\\.\\."))
   335  					})
   336  
   337  					It("outputs OK", func() {
   338  						Expect(testUI.Out).To(Say("OK"))
   339  					})
   340  
   341  					It("outputs details about the specific service", func() {
   342  						Expect(testUI.Out).Should(Say(
   343  							"service plan\\s+description\\s+free or paid\n" +
   344  								"plan-a\\s+plan-a-description\\s+paid" +
   345  								"\nplan-b\\s+plan-b-description\\s+free"))
   346  					})
   347  
   348  					It("outputs any warnings", func() {
   349  						Expect(testUI.Err).To(Say("warning"))
   350  					})
   351  				})
   352  
   353  				When("there is an error getting the service", func() {
   354  					BeforeEach(func() {
   355  						fakeActor.GetServiceSummaryForSpaceByNameReturns(v2action.ServiceSummary{}, v2action.Warnings{"warning"}, errors.New("oops"))
   356  					})
   357  
   358  					It("returns the error", func() {
   359  						Expect(executeErr).To(MatchError("oops"))
   360  					})
   361  
   362  					It("outputs any warnings", func() {
   363  						Expect(testUI.Err).To(Say("warning"))
   364  					})
   365  				})
   366  			})
   367  
   368  			Context("and there are no flags passed", func() {
   369  				When("there are no services available", func() {
   370  					BeforeEach(func() {
   371  						fakeActor.GetServicesSummariesForSpaceReturns([]v2action.ServiceSummary{}, v2action.Warnings{}, nil)
   372  					})
   373  
   374  					It("gets services for the correct space", func() {
   375  						Expect(fakeActor.GetServicesSummariesForSpaceArgsForCall(0)).To(Equal("space-guid"))
   376  					})
   377  
   378  					It("outputs a header", func() {
   379  						Expect(testUI.Out).To(Say("Getting services from marketplace in org org-a / space space-a as user-a\\.\\.\\."))
   380  					})
   381  
   382  					It("outputs OK", func() {
   383  						Expect(testUI.Out).To(Say("OK"))
   384  					})
   385  
   386  					It("outputs that no services are available", func() {
   387  						Expect(testUI.Out).To(Say("No service offerings found"))
   388  					})
   389  				})
   390  
   391  				When("there are multiple services available", func() {
   392  					BeforeEach(func() {
   393  						servicesSummaries := []v2action.ServiceSummary{
   394  							{
   395  								Service: v2action.Service{
   396  									Label:       "service-a",
   397  									Description: "fake service-a",
   398  								},
   399  								Plans: []v2action.ServicePlanSummary{
   400  									{
   401  										ServicePlan: v2action.ServicePlan{Name: "plan-a"},
   402  									},
   403  									{
   404  										ServicePlan: v2action.ServicePlan{Name: "plan-b"},
   405  									},
   406  								},
   407  							},
   408  							{
   409  								Service: v2action.Service{
   410  									Label:       "service-b",
   411  									Description: "fake service-b",
   412  								},
   413  								Plans: []v2action.ServicePlanSummary{
   414  									{
   415  										ServicePlan: v2action.ServicePlan{Name: "plan-c"},
   416  									},
   417  								},
   418  							},
   419  						}
   420  
   421  						fakeActor.GetServicesSummariesForSpaceReturns(servicesSummaries, v2action.Warnings{"warning"}, nil)
   422  					})
   423  
   424  					It("gets services for the correct space", func() {
   425  						Expect(fakeActor.GetServicesSummariesForSpaceArgsForCall(0)).To(Equal("space-guid"))
   426  					})
   427  
   428  					It("outputs a header", func() {
   429  						Expect(testUI.Out).To(Say("Getting services from marketplace in org org-a / space space-a as user-a\\.\\.\\."))
   430  					})
   431  
   432  					It("outputs OK", func() {
   433  						Expect(testUI.Out).To(Say("OK"))
   434  					})
   435  
   436  					It("outputs available services and plans", func() {
   437  						Expect(testUI.Out).Should(Say("service\\s+plans\\s+description\n" +
   438  							"service-a\\s+plan-a, plan-b\\s+fake service-a\n" +
   439  							"service-b\\s+plan-c\\s+fake service-b"))
   440  					})
   441  
   442  					It("outputs a tip to use the -s flag", func() {
   443  						Expect(testUI.Out).To(Say("TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service."))
   444  					})
   445  
   446  					It("outputs any warnings", func() {
   447  						Expect(testUI.Err).To(Say("warning"))
   448  					})
   449  				})
   450  
   451  				When("there is an error getting the available services", func() {
   452  					BeforeEach(func() {
   453  						fakeActor.GetServicesSummariesForSpaceReturns([]v2action.ServiceSummary{}, v2action.Warnings{"warning"}, errors.New("oops"))
   454  					})
   455  
   456  					It("returns the error", func() {
   457  						Expect(executeErr).To(MatchError("oops"))
   458  					})
   459  
   460  					It("outputs any warnings", func() {
   461  						Expect(testUI.Err).To(Say("warning"))
   462  					})
   463  				})
   464  			})
   465  		})
   466  	})
   467  
   468  })