github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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 privateServicePlanName string 34 orgName string 35 ) 36 37 updateCommandDependency := func(pluginCall bool) { 38 deps.UI = ui 39 deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(tokenRefresher) 40 deps.ServicePlanHandler = actor 41 deps.Config = configRepo 42 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("enable-service-access").SetDependency(deps, pluginCall)) 43 } 44 45 BeforeEach(func() { 46 ui = &testterm.FakeUI{} 47 actor = new(actorsfakes.FakeServicePlanActor) 48 configRepo = configuration.NewRepositoryWithDefaults() 49 requirementsFactory = new(requirementsfakes.FakeFactory) 50 tokenRefresher = new(authenticationfakes.FakeRepository) 51 }) 52 53 runCommand := func(args []string) bool { 54 return testcmd.RunCLICommand("enable-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 privateServicePlanName = "private-service-plan" 80 orgName = "my-org" 81 }) 82 83 It("Refreshes the auth token", func() { 84 runCommand([]string{serviceName}) 85 Expect(tokenRefresher.RefreshAuthTokenCallCount()).To(Equal(1)) 86 }) 87 88 Context("when refreshing the auth token fails", func() { 89 It("fails and returns the error", func() { 90 tokenRefresher.RefreshAuthTokenReturns("", errors.New("Refreshing went wrong")) 91 runCommand([]string{serviceName}) 92 93 Expect(ui.Outputs()).To(ContainSubstrings( 94 []string{"Refreshing went wrong"}, 95 []string{"FAILED"}, 96 )) 97 }) 98 }) 99 100 Context("when the named service exists", func() { 101 It("returns OK when ran successfully", func() { 102 Expect(runCommand([]string{serviceName})).To(BeTrue()) 103 Expect(ui.Outputs()).To(ContainSubstrings( 104 []string{"OK"}, 105 )) 106 107 Expect(actor.UpdateAllPlansForServiceCallCount()).To(Equal(1)) 108 service, enable := actor.UpdateAllPlansForServiceArgsForCall(0) 109 Expect(service).To(Equal(serviceName)) 110 Expect(enable).To(BeTrue()) 111 }) 112 113 It("prints an error if updating the plans fails", func() { 114 actor.UpdateAllPlansForServiceReturns(errors.New("Kaboom!")) 115 116 Expect(runCommand([]string{serviceName})).To(BeFalse()) 117 Expect(ui.Outputs()).To(ContainSubstrings( 118 []string{"Kaboom!"}, 119 )) 120 }) 121 122 Context("The user provides a plan", func() { 123 It("prints an error if updating the plan fails", func() { 124 actor.UpdateSinglePlanForServiceReturns(errors.New("could not find service")) 125 126 Expect(runCommand([]string{"-p", servicePlanName, serviceName})).To(BeFalse()) 127 Expect(ui.Outputs()).To(ContainSubstrings( 128 []string{"could not find service"}, 129 )) 130 }) 131 132 It("enables the plan", func() { 133 Expect(runCommand([]string{"-p", publicServicePlanName, serviceName})).To(BeTrue()) 134 Expect(ui.Outputs()).To(ContainSubstrings( 135 []string{"OK"}, 136 )) 137 138 Expect(actor.UpdateSinglePlanForServiceCallCount()).To(Equal(1)) 139 service, plan, enable := actor.UpdateSinglePlanForServiceArgsForCall(0) 140 Expect(service).To(Equal(serviceName)) 141 Expect(plan).To(Equal(publicServicePlanName)) 142 Expect(enable).To(BeTrue()) 143 }) 144 }) 145 146 Context("the user provides a plan and org", func() { 147 It("prints an error if updating the plan fails", func() { 148 actor.UpdatePlanAndOrgForServiceReturns(errors.New("could not find org")) 149 150 Expect(runCommand([]string{"-p", servicePlanName, "-o", "not-findable-org", serviceName})).To(BeFalse()) 151 Expect(ui.Outputs()).To(ContainSubstrings( 152 []string{"could not find org"}, 153 )) 154 }) 155 156 It("enables the plan for the org", func() { 157 Expect(runCommand([]string{"-p", publicServicePlanName, "-o", orgName, serviceName})).To(BeTrue()) 158 Expect(ui.Outputs()).To(ContainSubstrings( 159 []string{"OK"}, 160 )) 161 162 Expect(actor.UpdatePlanAndOrgForServiceCallCount()).To(Equal(1)) 163 service, plan, org, enable := actor.UpdatePlanAndOrgForServiceArgsForCall(0) 164 Expect(service).To(Equal(serviceName)) 165 Expect(plan).To(Equal(publicServicePlanName)) 166 Expect(org).To(Equal(orgName)) 167 Expect(enable).To(BeTrue()) 168 }) 169 }) 170 171 Context("the user provides an org", func() { 172 It("prints an error if updating the plan fails", func() { 173 actor.UpdateOrgForServiceReturns(errors.New("could not find org")) 174 175 Expect(runCommand([]string{"-o", "not-findable-org", serviceName})).To(BeFalse()) 176 Expect(ui.Outputs()).To(ContainSubstrings( 177 []string{"could not find org"}, 178 )) 179 }) 180 181 It("tells the user if the service's plans are already accessible", func() { 182 Expect(runCommand([]string{"-o", orgName, serviceName})).To(BeTrue()) 183 Expect(ui.Outputs()).To(ContainSubstrings( 184 []string{"OK"}, 185 )) 186 187 Expect(actor.UpdateOrgForServiceCallCount()).To(Equal(1)) 188 service, org, enable := actor.UpdateOrgForServiceArgsForCall(0) 189 Expect(service).To(Equal(serviceName)) 190 Expect(org).To(Equal(orgName)) 191 Expect(enable).To(BeTrue()) 192 }) 193 }) 194 }) 195 }) 196 })