github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/service_brokers_test.go (about)

     1  package servicebroker_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  
    19  	"strings"
    20  
    21  	"code.cloudfoundry.org/cli/cf/commands/servicebroker"
    22  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    23  )
    24  
    25  var _ = Describe("service-brokers command", func() {
    26  	var (
    27  		ui                  *testterm.FakeUI
    28  		config              coreconfig.Repository
    29  		repo                *apifakes.FakeServiceBrokerRepository
    30  		requirementsFactory *requirementsfakes.FakeFactory
    31  		deps                commandregistry.Dependency
    32  	)
    33  
    34  	updateCommandDependency := func(pluginCall bool) {
    35  		deps.UI = ui
    36  		deps.RepoLocator = deps.RepoLocator.SetServiceBrokerRepository(repo)
    37  		deps.Config = config
    38  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("service-brokers").SetDependency(deps, pluginCall))
    39  	}
    40  
    41  	BeforeEach(func() {
    42  		ui = &testterm.FakeUI{}
    43  		config = testconfig.NewRepositoryWithDefaults()
    44  		repo = new(apifakes.FakeServiceBrokerRepository)
    45  		requirementsFactory = new(requirementsfakes.FakeFactory)
    46  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    47  	})
    48  
    49  	Describe("login requirements", func() {
    50  		It("fails if the user is not logged in", func() {
    51  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    52  			Expect(testcmd.RunCLICommand("service-brokers", []string{}, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    53  		})
    54  
    55  		Context("when arguments are provided", func() {
    56  			var cmd commandregistry.Command
    57  			var flagContext flags.FlagContext
    58  
    59  			BeforeEach(func() {
    60  				cmd = &servicebroker.ListServiceBrokers{}
    61  				cmd.SetDependency(deps, false)
    62  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    63  			})
    64  
    65  			It("should fail with usage", func() {
    66  				flagContext.Parse("blahblah")
    67  
    68  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    69  				Expect(err).NotTo(HaveOccurred())
    70  
    71  				err = testcmd.RunRequirements(reqs)
    72  				Expect(err).To(HaveOccurred())
    73  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    74  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    75  			})
    76  		})
    77  	})
    78  
    79  	It("lists service brokers", func() {
    80  		repo.ListServiceBrokersStub = func(callback func(models.ServiceBroker) bool) error {
    81  			sbs := []models.ServiceBroker{
    82  				{
    83  					Name: "service-broker-to-list-a",
    84  					GUID: "service-broker-to-list-guid-a",
    85  					URL:  "http://service-a-url.com",
    86  				},
    87  				{
    88  					Name: "service-broker-to-list-b",
    89  					GUID: "service-broker-to-list-guid-b",
    90  					URL:  "http://service-b-url.com",
    91  				},
    92  				{
    93  					Name: "service-broker-to-list-c",
    94  					GUID: "service-broker-to-list-guid-c",
    95  					URL:  "http://service-c-url.com",
    96  				},
    97  			}
    98  
    99  			for _, sb := range sbs {
   100  				callback(sb)
   101  			}
   102  
   103  			return nil
   104  		}
   105  
   106  		testcmd.RunCLICommand("service-brokers", []string{}, requirementsFactory, updateCommandDependency, false, ui)
   107  
   108  		Expect(ui.Outputs()).To(ContainSubstrings(
   109  			[]string{"Getting service brokers as", "my-user"},
   110  			[]string{"name", "url"},
   111  			[]string{"service-broker-to-list-a", "http://service-a-url.com"},
   112  			[]string{"service-broker-to-list-b", "http://service-b-url.com"},
   113  			[]string{"service-broker-to-list-c", "http://service-c-url.com"},
   114  		))
   115  	})
   116  
   117  	It("lists service brokers by alphabetical order", func() {
   118  		repo.ListServiceBrokersStub = func(callback func(models.ServiceBroker) bool) error {
   119  			sbs := []models.ServiceBroker{
   120  				{
   121  					Name: "z-service-broker-to-list",
   122  					GUID: "z-service-broker-to-list-guid-a",
   123  					URL:  "http://service-a-url.com",
   124  				},
   125  				{
   126  					Name: "a-service-broker-to-list",
   127  					GUID: "a-service-broker-to-list-guid-c",
   128  					URL:  "http://service-c-url.com",
   129  				},
   130  				{
   131  					Name: "fun-service-broker-to-list",
   132  					GUID: "fun-service-broker-to-list-guid-b",
   133  					URL:  "http://service-b-url.com",
   134  				},
   135  				{
   136  					Name: "123-service-broker-to-list",
   137  					GUID: "123-service-broker-to-list-guid-c",
   138  					URL:  "http://service-d-url.com",
   139  				},
   140  			}
   141  
   142  			for _, sb := range sbs {
   143  				callback(sb)
   144  			}
   145  
   146  			return nil
   147  		}
   148  
   149  		testcmd.RunCLICommand("service-brokers", []string{}, requirementsFactory, updateCommandDependency, false, ui)
   150  
   151  		Expect(ui.Outputs()).To(BeInDisplayOrder(
   152  			[]string{"Getting service brokers as", "my-user"},
   153  			[]string{"name", "url"},
   154  			[]string{"123-service-broker-to-list", "http://service-d-url.com"},
   155  			[]string{"a-service-broker-to-list", "http://service-c-url.com"},
   156  			[]string{"fun-service-broker-to-list", "http://service-b-url.com"},
   157  			[]string{"z-service-broker-to-list", "http://service-a-url.com"},
   158  		))
   159  	})
   160  
   161  	It("says when no service brokers were found", func() {
   162  		testcmd.RunCLICommand("service-brokers", []string{}, requirementsFactory, updateCommandDependency, false, ui)
   163  
   164  		Expect(ui.Outputs()).To(ContainSubstrings(
   165  			[]string{"Getting service brokers as", "my-user"},
   166  			[]string{"No service brokers found"},
   167  		))
   168  	})
   169  
   170  	It("reports errors when listing service brokers", func() {
   171  		repo.ListServiceBrokersReturns(errors.New("Error finding service brokers"))
   172  		testcmd.RunCLICommand("service-brokers", []string{}, requirementsFactory, updateCommandDependency, false, ui)
   173  
   174  		Expect(ui.Outputs()).To(ContainSubstrings(
   175  			[]string{"Getting service brokers as ", "my-user"},
   176  		))
   177  		Expect(strings.Join(ui.Outputs(), "\n")).To(MatchRegexp(`FAILED\nError finding service brokers`))
   178  	})
   179  })