github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/cf/commands/ssh_code_test.go (about) 1 package commands_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/commands" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes" 10 "code.cloudfoundry.org/cli/cf/flags" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 13 14 "code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes" 15 testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" 16 testterm "code.cloudfoundry.org/cli/testhelpers/terminal" 17 18 . "code.cloudfoundry.org/cli/testhelpers/matchers" 19 . "github.com/onsi/ginkgo" 20 . "github.com/onsi/gomega" 21 ) 22 23 var _ = Describe("OneTimeSSHCode", func() { 24 var ( 25 ui *testterm.FakeUI 26 configRepo coreconfig.Repository 27 authRepo *authenticationfakes.FakeRepository 28 endpointRepo *coreconfigfakes.FakeEndpointRepository 29 30 cmd commandregistry.Command 31 deps commandregistry.Dependency 32 factory *requirementsfakes.FakeFactory 33 flagContext flags.FlagContext 34 35 endpointRequirement requirements.Requirement 36 ) 37 38 BeforeEach(func() { 39 ui = &testterm.FakeUI{} 40 41 configRepo = testconfig.NewRepositoryWithDefaults() 42 configRepo.SetAPIEndpoint("fake-api-endpoint") 43 endpointRepo = new(coreconfigfakes.FakeEndpointRepository) 44 repoLocator := deps.RepoLocator.SetEndpointRepository(endpointRepo) 45 authRepo = new(authenticationfakes.FakeRepository) 46 repoLocator = repoLocator.SetAuthenticationRepository(authRepo) 47 48 deps = commandregistry.Dependency{ 49 UI: ui, 50 Config: configRepo, 51 RepoLocator: repoLocator, 52 } 53 54 cmd = &commands.OneTimeSSHCode{} 55 cmd.SetDependency(deps, false) 56 57 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 58 59 factory = new(requirementsfakes.FakeFactory) 60 61 endpointRequirement = &passingRequirement{Name: "endpoint-requirement"} 62 factory.NewAPIEndpointRequirementReturns(endpointRequirement) 63 }) 64 65 Describe("Requirements", func() { 66 It("returns an EndpointRequirement", func() { 67 actualRequirements, err := cmd.Requirements(factory, flagContext) 68 Expect(err).NotTo(HaveOccurred()) 69 Expect(factory.NewAPIEndpointRequirementCallCount()).To(Equal(1)) 70 Expect(actualRequirements).To(ContainElement(endpointRequirement)) 71 }) 72 73 Context("when not provided exactly zero args", func() { 74 BeforeEach(func() { 75 flagContext.Parse("domain-name") 76 }) 77 78 It("fails with usage", func() { 79 var firstErr error 80 81 reqs, err := cmd.Requirements(factory, flagContext) 82 Expect(err).NotTo(HaveOccurred()) 83 84 for _, req := range reqs { 85 err := req.Execute() 86 if err != nil { 87 firstErr = err 88 break 89 } 90 } 91 92 Expect(firstErr.Error()).To(ContainSubstring("Incorrect Usage. No argument required")) 93 }) 94 }) 95 }) 96 97 Describe("Execute", func() { 98 var runCLIerr error 99 100 BeforeEach(func() { 101 cmd.Requirements(factory, flagContext) 102 103 endpointRepo.GetCCInfoReturns( 104 &coreconfig.CCInfo{ 105 LoggregatorEndpoint: "loggregator/endpoint", 106 }, 107 "some-endpoint", 108 nil, 109 ) 110 }) 111 112 JustBeforeEach(func() { 113 runCLIerr = cmd.Execute(flagContext) 114 }) 115 116 It("tries to update the endpoint", func() { 117 Expect(runCLIerr).NotTo(HaveOccurred()) 118 Expect(endpointRepo.GetCCInfoCallCount()).To(Equal(1)) 119 Expect(endpointRepo.GetCCInfoArgsForCall(0)).To(Equal("fake-api-endpoint")) 120 }) 121 122 Context("when updating the endpoint succeeds", func() { 123 ccInfo := &coreconfig.CCInfo{ 124 APIVersion: "some-version", 125 AuthorizationEndpoint: "auth/endpoint", 126 LoggregatorEndpoint: "loggregator/endpoint", 127 MinCLIVersion: "min-cli-version", 128 MinRecommendedCLIVersion: "min-rec-cli-version", 129 SSHOAuthClient: "some-client", 130 RoutingAPIEndpoint: "routing/endpoint", 131 } 132 BeforeEach(func() { 133 endpointRepo.GetCCInfoReturns( 134 ccInfo, 135 "updated-endpoint", 136 nil, 137 ) 138 }) 139 140 It("tries to refresh the auth token", func() { 141 Expect(runCLIerr).NotTo(HaveOccurred()) 142 Expect(authRepo.RefreshAuthTokenCallCount()).To(Equal(1)) 143 }) 144 145 Context("when refreshing the token fails with an error", func() { 146 BeforeEach(func() { 147 authRepo.RefreshAuthTokenReturns("", errors.New("auth-error")) 148 }) 149 150 It("fails with error", func() { 151 Expect(runCLIerr).To(HaveOccurred()) 152 Expect(runCLIerr.Error()).To(Equal("Error refreshing oauth token: auth-error")) 153 }) 154 }) 155 156 Context("when refreshing the token succeeds", func() { 157 BeforeEach(func() { 158 authRepo.RefreshAuthTokenReturns("auth-token", nil) 159 }) 160 161 It("tries to get the ssh-code", func() { 162 Expect(runCLIerr).NotTo(HaveOccurred()) 163 Expect(authRepo.AuthorizeCallCount()).To(Equal(1)) 164 Expect(authRepo.AuthorizeArgsForCall(0)).To(Equal("auth-token")) 165 }) 166 167 Context("when getting the ssh-code succeeds", func() { 168 BeforeEach(func() { 169 authRepo.AuthorizeReturns("some-code", nil) 170 }) 171 172 It("displays the token", func() { 173 Expect(runCLIerr).NotTo(HaveOccurred()) 174 Expect(ui.Outputs()).To(ContainSubstrings( 175 []string{"some-code"}, 176 )) 177 }) 178 }) 179 180 Context("when getting the ssh-code fails", func() { 181 BeforeEach(func() { 182 authRepo.AuthorizeReturns("", errors.New("auth-err")) 183 }) 184 185 It("fails with error", func() { 186 Expect(runCLIerr).To(HaveOccurred()) 187 Expect(runCLIerr.Error()).To(Equal("Error getting SSH code: auth-err")) 188 }) 189 }) 190 }) 191 }) 192 }) 193 })