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

     1  package serviceaccess_test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/actors/actorsfakes"
     8  	"code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes"
     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  
    17  	"code.cloudfoundry.org/cli/cf/commandregistry"
    18  	"code.cloudfoundry.org/cli/cf/commands/serviceaccess"
    19  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    20  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("service-access command", func() {
    26  	var (
    27  		ui                  *testterm.FakeUI
    28  		actor               *actorsfakes.FakeServiceActor
    29  		requirementsFactory *requirementsfakes.FakeFactory
    30  		serviceBroker1      models.ServiceBroker
    31  		serviceBroker2      models.ServiceBroker
    32  		authRepo            *authenticationfakes.FakeRepository
    33  		configRepo          coreconfig.Repository
    34  		deps                commandregistry.Dependency
    35  	)
    36  
    37  	updateCommandDependency := func(pluginCall bool) {
    38  		deps.UI = ui
    39  		deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(authRepo)
    40  		deps.ServiceHandler = actor
    41  		deps.Config = configRepo
    42  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("service-access").SetDependency(deps, pluginCall))
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		ui = &testterm.FakeUI{}
    47  		actor = new(actorsfakes.FakeServiceActor)
    48  		requirementsFactory = new(requirementsfakes.FakeFactory)
    49  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    50  		authRepo = new(authenticationfakes.FakeRepository)
    51  		configRepo = testconfig.NewRepositoryWithDefaults()
    52  	})
    53  
    54  	runCommand := func(args ...string) bool {
    55  		return testcmd.RunCLICommand("service-access", args, requirementsFactory, updateCommandDependency, false, ui)
    56  	}
    57  
    58  	Describe("requirements", func() {
    59  		It("requires the user to be logged in", func() {
    60  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    61  			Expect(runCommand()).ToNot(HavePassedRequirements())
    62  		})
    63  
    64  		Context("when arguments are provided", func() {
    65  			var cmd commandregistry.Command
    66  			var flagContext flags.FlagContext
    67  
    68  			BeforeEach(func() {
    69  				cmd = &serviceaccess.ServiceAccess{}
    70  				cmd.SetDependency(deps, false)
    71  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    72  			})
    73  
    74  			It("should fail with usage", func() {
    75  				flagContext.Parse("blahblah")
    76  
    77  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    78  				Expect(err).NotTo(HaveOccurred())
    79  
    80  				err = testcmd.RunRequirements(reqs)
    81  				Expect(err).To(HaveOccurred())
    82  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    83  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    84  			})
    85  		})
    86  	})
    87  
    88  	Describe("when logged in", func() {
    89  		BeforeEach(func() {
    90  			serviceBroker1 = models.ServiceBroker{
    91  				GUID: "broker1",
    92  				Name: "brokername1",
    93  				Services: []models.ServiceOffering{
    94  					{
    95  						ServiceOfferingFields: models.ServiceOfferingFields{Label: "my-service-1"},
    96  						Plans: []models.ServicePlanFields{
    97  							{Name: "beep", Public: true},
    98  							{Name: "burp", Public: false},
    99  							{Name: "boop", Public: false, OrgNames: []string{"fwip", "brzzt"}},
   100  						},
   101  					},
   102  					{
   103  						ServiceOfferingFields: models.ServiceOfferingFields{Label: "my-service-2"},
   104  						Plans: []models.ServicePlanFields{
   105  							{Name: "petaloideous-noncelebration", Public: false},
   106  						},
   107  					},
   108  				},
   109  			}
   110  			serviceBroker2 = models.ServiceBroker{
   111  				GUID: "broker2",
   112  				Name: "brokername2",
   113  				Services: []models.ServiceOffering{
   114  					{ServiceOfferingFields: models.ServiceOfferingFields{Label: "my-service-3"}},
   115  				},
   116  			}
   117  
   118  			actor.FilterBrokersReturns([]models.ServiceBroker{
   119  				serviceBroker1,
   120  				serviceBroker2,
   121  			},
   122  				nil,
   123  			)
   124  		})
   125  
   126  		It("refreshes the auth token", func() {
   127  			runCommand()
   128  			Expect(authRepo.RefreshAuthTokenCallCount()).To(Equal(1))
   129  		})
   130  
   131  		Context("when refreshing the auth token fails", func() {
   132  			It("fails and returns the error", func() {
   133  				authRepo.RefreshAuthTokenReturns("", errors.New("Refreshing went wrong"))
   134  				runCommand()
   135  
   136  				Expect(ui.Outputs()).To(ContainSubstrings(
   137  					[]string{"Refreshing went wrong"},
   138  					[]string{"FAILED"},
   139  				))
   140  			})
   141  		})
   142  
   143  		Context("When no flags are provided", func() {
   144  			It("tells the user it is obtaining the service access", func() {
   145  				runCommand()
   146  				Expect(ui.Outputs()).To(ContainSubstrings(
   147  					[]string{"Getting service access as", "my-user"},
   148  				))
   149  			})
   150  
   151  			It("prints all of the brokers", func() {
   152  				runCommand()
   153  				Expect(ui.Outputs()).To(ContainSubstrings(
   154  					[]string{"broker: brokername1"},
   155  					[]string{"service", "plan", "access", "orgs"},
   156  					[]string{"my-service-1", "beep", "all"},
   157  					[]string{"my-service-1", "burp", "none"},
   158  					[]string{"my-service-1", "boop", "limited", "fwip", "brzzt"},
   159  					[]string{"my-service-2", "petaloideous-noncelebration"},
   160  					[]string{"broker: brokername2"},
   161  					[]string{"service", "plan", "access", "orgs"},
   162  					[]string{"my-service-3"},
   163  				))
   164  			})
   165  		})
   166  
   167  		Context("When the broker flag is provided", func() {
   168  			It("tells the user it is obtaining the services access for a particular broker", func() {
   169  				runCommand("-b", "brokername1")
   170  				Expect(ui.Outputs()).To(ContainSubstrings(
   171  					[]string{"Getting service access", "for broker brokername1 as", "my-user"},
   172  				))
   173  			})
   174  		})
   175  
   176  		Context("when the service flag is provided", func() {
   177  			It("tells the user it is obtaining the service access for a particular service", func() {
   178  				runCommand("-e", "my-service-1")
   179  				Expect(ui.Outputs()).To(ContainSubstrings(
   180  					[]string{"Getting service access", "for service my-service-1 as", "my-user"},
   181  				))
   182  			})
   183  		})
   184  
   185  		Context("when the org flag is provided", func() {
   186  			It("tells the user it is obtaining the service access for a particular org", func() {
   187  				runCommand("-o", "fwip")
   188  				Expect(ui.Outputs()).To(ContainSubstrings(
   189  					[]string{"Getting service access", "for organization fwip as", "my-user"},
   190  				))
   191  			})
   192  		})
   193  
   194  		Context("when the broker and service flag are both provided", func() {
   195  			It("tells the user it is obtaining the service access for a particular broker and service", func() {
   196  				runCommand("-b", "brokername1", "-e", "my-service-1")
   197  				Expect(ui.Outputs()).To(ContainSubstrings(
   198  					[]string{"Getting service access", "for broker brokername1", "and service my-service-1", "as", "my-user"},
   199  				))
   200  			})
   201  		})
   202  
   203  		Context("when the broker and org name are both provided", func() {
   204  			It("tells the user it is obtaining the service access for a particular broker and org", func() {
   205  				runCommand("-b", "brokername1", "-o", "fwip")
   206  				Expect(ui.Outputs()).To(ContainSubstrings(
   207  					[]string{"Getting service access", "for broker brokername1", "and organization fwip", "as", "my-user"},
   208  				))
   209  			})
   210  		})
   211  
   212  		Context("when the service and org name are both provided", func() {
   213  			It("tells the user it is obtaining the service access for a particular service and org", func() {
   214  				runCommand("-e", "my-service-1", "-o", "fwip")
   215  				Expect(ui.Outputs()).To(ContainSubstrings(
   216  					[]string{"Getting service access", "for service my-service-1", "and organization fwip", "as", "my-user"},
   217  				))
   218  			})
   219  		})
   220  
   221  		Context("when all flags are provided", func() {
   222  			It("tells the user it is filtering on all options", func() {
   223  				runCommand("-b", "brokername1", "-e", "my-service-1", "-o", "fwip")
   224  				Expect(ui.Outputs()).To(ContainSubstrings(
   225  					[]string{"Getting service access", "for broker brokername1", "and service my-service-1", "and organization fwip", "as", "my-user"},
   226  				))
   227  			})
   228  		})
   229  		Context("when filter brokers returns an error", func() {
   230  			It("gives only the access error", func() {
   231  				err := errors.New("Error finding service brokers")
   232  				actor.FilterBrokersReturns([]models.ServiceBroker{}, err)
   233  				runCommand()
   234  
   235  				Expect(strings.Join(ui.Outputs(), "\n")).To(MatchRegexp(`FAILED\nError finding service brokers`))
   236  			})
   237  		})
   238  	})
   239  })