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

     1  package serviceaccess_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors/actorsfakes"
     7  	"code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    13  	"code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  
    16  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("enable-service-access command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		actor               *actorsfakes.FakeServicePlanActor
    25  		requirementsFactory *requirementsfakes.FakeFactory
    26  		configRepo          coreconfig.Repository
    27  		tokenRefresher      *authenticationfakes.FakeRepository
    28  		deps                commandregistry.Dependency
    29  
    30  		serviceName           string
    31  		servicePlanName       string
    32  		publicServicePlanName string
    33  		orgName               string
    34  	)
    35  
    36  	updateCommandDependency := func(pluginCall bool) {
    37  		deps.UI = ui
    38  		deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(tokenRefresher)
    39  		deps.ServicePlanHandler = actor
    40  		deps.Config = configRepo
    41  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("enable-service-access").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	BeforeEach(func() {
    45  		ui = &testterm.FakeUI{}
    46  		actor = new(actorsfakes.FakeServicePlanActor)
    47  		configRepo = configuration.NewRepositoryWithDefaults()
    48  		requirementsFactory = new(requirementsfakes.FakeFactory)
    49  		tokenRefresher = new(authenticationfakes.FakeRepository)
    50  	})
    51  
    52  	runCommand := func(args []string) bool {
    53  		return testcmd.RunCLICommand("enable-service-access", args, requirementsFactory, updateCommandDependency, false, ui)
    54  	}
    55  
    56  	Describe("requirements", func() {
    57  		It("requires the user to be logged in", func() {
    58  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    59  			Expect(runCommand([]string{"foo"})).To(BeFalse())
    60  		})
    61  
    62  		It("fails with usage when it does not recieve any arguments", func() {
    63  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    64  			runCommand(nil)
    65  			Expect(ui.Outputs()).To(ContainSubstrings(
    66  				[]string{"Incorrect Usage", "Requires", "argument"},
    67  			))
    68  		})
    69  	})
    70  
    71  	Describe("when logged in", func() {
    72  		BeforeEach(func() {
    73  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    74  
    75  			serviceName = "service"
    76  			servicePlanName = "service-plan"
    77  			publicServicePlanName = "public-service-plan"
    78  			orgName = "my-org"
    79  		})
    80  
    81  		It("Refreshes the auth token", func() {
    82  			runCommand([]string{serviceName})
    83  			Expect(tokenRefresher.RefreshAuthTokenCallCount()).To(Equal(1))
    84  		})
    85  
    86  		Context("when refreshing the auth token fails", func() {
    87  			It("fails and returns the error", func() {
    88  				tokenRefresher.RefreshAuthTokenReturns("", errors.New("Refreshing went wrong"))
    89  				runCommand([]string{serviceName})
    90  
    91  				Expect(ui.Outputs()).To(ContainSubstrings(
    92  					[]string{"Refreshing went wrong"},
    93  					[]string{"FAILED"},
    94  				))
    95  			})
    96  		})
    97  
    98  		Context("when the named service exists", func() {
    99  			It("returns OK when ran successfully", func() {
   100  				Expect(runCommand([]string{serviceName})).To(BeTrue())
   101  				Expect(ui.Outputs()).To(ContainSubstrings(
   102  					[]string{"OK"},
   103  				))
   104  
   105  				Expect(actor.UpdateAllPlansForServiceCallCount()).To(Equal(1))
   106  				service, enable := actor.UpdateAllPlansForServiceArgsForCall(0)
   107  				Expect(service).To(Equal(serviceName))
   108  				Expect(enable).To(BeTrue())
   109  			})
   110  
   111  			It("prints an error if updating the plans fails", func() {
   112  				actor.UpdateAllPlansForServiceReturns(errors.New("Kaboom!"))
   113  
   114  				Expect(runCommand([]string{serviceName})).To(BeFalse())
   115  				Expect(ui.Outputs()).To(ContainSubstrings(
   116  					[]string{"Kaboom!"},
   117  				))
   118  			})
   119  
   120  			Context("The user provides a plan", func() {
   121  				It("prints an error if updating the plan fails", func() {
   122  					actor.UpdateSinglePlanForServiceReturns(errors.New("could not find service"))
   123  
   124  					Expect(runCommand([]string{"-p", servicePlanName, serviceName})).To(BeFalse())
   125  					Expect(ui.Outputs()).To(ContainSubstrings(
   126  						[]string{"could not find service"},
   127  					))
   128  				})
   129  
   130  				It("enables the plan", func() {
   131  					Expect(runCommand([]string{"-p", publicServicePlanName, serviceName})).To(BeTrue())
   132  					Expect(ui.Outputs()).To(ContainSubstrings(
   133  						[]string{"OK"},
   134  					))
   135  
   136  					Expect(actor.UpdateSinglePlanForServiceCallCount()).To(Equal(1))
   137  					service, plan, enable := actor.UpdateSinglePlanForServiceArgsForCall(0)
   138  					Expect(service).To(Equal(serviceName))
   139  					Expect(plan).To(Equal(publicServicePlanName))
   140  					Expect(enable).To(BeTrue())
   141  				})
   142  			})
   143  
   144  			Context("the user provides a plan and org", func() {
   145  				It("prints an error if updating the plan fails", func() {
   146  					actor.UpdatePlanAndOrgForServiceReturns(errors.New("could not find org"))
   147  
   148  					Expect(runCommand([]string{"-p", servicePlanName, "-o", "not-findable-org", serviceName})).To(BeFalse())
   149  					Expect(ui.Outputs()).To(ContainSubstrings(
   150  						[]string{"could not find org"},
   151  					))
   152  				})
   153  
   154  				It("enables the plan for the org", func() {
   155  					Expect(runCommand([]string{"-p", publicServicePlanName, "-o", orgName, serviceName})).To(BeTrue())
   156  					Expect(ui.Outputs()).To(ContainSubstrings(
   157  						[]string{"OK"},
   158  					))
   159  
   160  					Expect(actor.UpdatePlanAndOrgForServiceCallCount()).To(Equal(1))
   161  					service, plan, org, enable := actor.UpdatePlanAndOrgForServiceArgsForCall(0)
   162  					Expect(service).To(Equal(serviceName))
   163  					Expect(plan).To(Equal(publicServicePlanName))
   164  					Expect(org).To(Equal(orgName))
   165  					Expect(enable).To(BeTrue())
   166  				})
   167  			})
   168  
   169  			Context("the user provides an org", func() {
   170  				It("prints an error if updating the plan fails", func() {
   171  					actor.UpdateOrgForServiceReturns(errors.New("could not find org"))
   172  
   173  					Expect(runCommand([]string{"-o", "not-findable-org", serviceName})).To(BeFalse())
   174  					Expect(ui.Outputs()).To(ContainSubstrings(
   175  						[]string{"could not find org"},
   176  					))
   177  				})
   178  
   179  				It("tells the user if the service's plans are already accessible", func() {
   180  					Expect(runCommand([]string{"-o", orgName, serviceName})).To(BeTrue())
   181  					Expect(ui.Outputs()).To(ContainSubstrings(
   182  						[]string{"OK"},
   183  					))
   184  
   185  					Expect(actor.UpdateOrgForServiceCallCount()).To(Equal(1))
   186  					service, org, enable := actor.UpdateOrgForServiceArgsForCall(0)
   187  					Expect(service).To(Equal(serviceName))
   188  					Expect(org).To(Equal(orgName))
   189  					Expect(enable).To(BeTrue())
   190  				})
   191  			})
   192  		})
   193  	})
   194  })