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