func NewRepositoryLocator(config core_config.ReadWriter, gatewaysByName map[string]net.Gateway) (loc RepositoryLocator) { strategy := strategy.NewEndpointStrategy(config.ApiVersion()) cloudControllerGateway := gatewaysByName["cloud-controller"] routingApiGateway := gatewaysByName["routing-api"] uaaGateway := gatewaysByName["uaa"] loc.authRepo = authentication.NewUAAAuthenticationRepository(uaaGateway, config) // ensure gateway refreshers are set before passing them by value to repositories cloudControllerGateway.SetTokenRefresher(loc.authRepo) uaaGateway.SetTokenRefresher(loc.authRepo) tlsConfig := net.NewTLSConfig([]tls.Certificate{}, config.IsSSLDisabled()) loggregatorConsumer := consumer.New(config.LoggregatorEndpoint(), tlsConfig, http.ProxyFromEnvironment) loggregatorConsumer.SetDebugPrinter(terminal.DebugPrinter{}) loc.appBitsRepo = application_bits.NewCloudControllerApplicationBitsRepository(config, cloudControllerGateway) loc.appEventsRepo = app_events.NewCloudControllerAppEventsRepository(config, cloudControllerGateway, strategy) loc.appFilesRepo = api_app_files.NewCloudControllerAppFilesRepository(config, cloudControllerGateway) loc.appRepo = applications.NewCloudControllerApplicationRepository(config, cloudControllerGateway) loc.appSummaryRepo = NewCloudControllerAppSummaryRepository(config, cloudControllerGateway) loc.appInstancesRepo = app_instances.NewCloudControllerAppInstancesRepository(config, cloudControllerGateway) loc.authTokenRepo = NewCloudControllerServiceAuthTokenRepository(config, cloudControllerGateway) loc.curlRepo = NewCloudControllerCurlRepository(config, cloudControllerGateway) loc.domainRepo = NewCloudControllerDomainRepository(config, cloudControllerGateway, strategy) loc.endpointRepo = NewEndpointRepository(config, cloudControllerGateway) loc.logsRepo = NewLoggregatorLogsRepository(config, loggregatorConsumer, loc.authRepo) loc.organizationRepo = organizations.NewCloudControllerOrganizationRepository(config, cloudControllerGateway) loc.passwordRepo = password.NewCloudControllerPasswordRepository(config, uaaGateway) loc.quotaRepo = quotas.NewCloudControllerQuotaRepository(config, cloudControllerGateway) loc.routeRepo = NewCloudControllerRouteRepository(config, cloudControllerGateway) loc.routeServiceBindingRepo = NewCloudControllerRouteServiceBindingRepository(config, cloudControllerGateway) loc.routingApiRepo = NewRoutingApiRepository(config, routingApiGateway) loc.stackRepo = stacks.NewCloudControllerStackRepository(config, cloudControllerGateway) loc.serviceRepo = NewCloudControllerServiceRepository(config, cloudControllerGateway) loc.serviceKeyRepo = NewCloudControllerServiceKeyRepository(config, cloudControllerGateway) loc.serviceBindingRepo = NewCloudControllerServiceBindingRepository(config, cloudControllerGateway) loc.serviceBrokerRepo = NewCloudControllerServiceBrokerRepository(config, cloudControllerGateway) loc.servicePlanRepo = NewCloudControllerServicePlanRepository(config, cloudControllerGateway) loc.servicePlanVisibilityRepo = NewCloudControllerServicePlanVisibilityRepository(config, cloudControllerGateway) loc.serviceSummaryRepo = NewCloudControllerServiceSummaryRepository(config, cloudControllerGateway) loc.spaceRepo = spaces.NewCloudControllerSpaceRepository(config, cloudControllerGateway) loc.userProvidedServiceInstanceRepo = NewCCUserProvidedServiceInstanceRepository(config, cloudControllerGateway) loc.userRepo = NewCloudControllerUserRepository(config, uaaGateway, cloudControllerGateway) loc.buildpackRepo = NewCloudControllerBuildpackRepository(config, cloudControllerGateway) loc.buildpackBitsRepo = NewCloudControllerBuildpackBitsRepository(config, cloudControllerGateway, app_files.ApplicationZipper{}) loc.securityGroupRepo = security_groups.NewSecurityGroupRepo(config, cloudControllerGateway) loc.stagingSecurityGroupRepo = staging.NewStagingSecurityGroupsRepo(config, cloudControllerGateway) loc.runningSecurityGroupRepo = running.NewRunningSecurityGroupsRepo(config, cloudControllerGateway) loc.securityGroupSpaceBinder = securitygroupspaces.NewSecurityGroupSpaceBinder(config, cloudControllerGateway) loc.spaceQuotaRepo = space_quotas.NewCloudControllerSpaceQuotaRepository(config, cloudControllerGateway) loc.featureFlagRepo = feature_flags.NewCloudControllerFeatureFlagRepository(config, cloudControllerGateway) loc.environmentVariableGroupRepo = environment_variable_groups.NewCloudControllerEnvironmentVariableGroupsRepository(config, cloudControllerGateway) loc.copyAppSourceRepo = copy_application_source.NewCloudControllerCopyApplicationSourceRepository(config, cloudControllerGateway) client := v3client.NewClient(config.ApiEndpoint(), config.AuthenticationEndpoint(), config.AccessToken(), config.RefreshToken()) loc.v3Repository = repository.NewRepository(config, client) return }
err = auth.Authenticate(map[string]string{ "username": "******", "password": "******", }) }) Describe("when login succeeds", func() { BeforeEach(func() { setupTestServer(successfulLoginRequest) }) It("stores the access and refresh tokens in the config", func() { Expect(handler).To(HaveAllRequestsCalled()) Expect(err).NotTo(HaveOccurred()) Expect(config.AuthenticationEndpoint()).To(Equal(testServer.URL)) Expect(config.AccessToken()).To(Equal("BEARER my_access_token")) Expect(config.RefreshToken()).To(Equal("my_refresh_token")) }) }) Describe("when login fails", func() { BeforeEach(func() { setupTestServer(unsuccessfulLoginRequest) }) It("returns an error", func() { Expect(handler).To(HaveAllRequestsCalled()) Expect(err).NotTo(BeNil()) Expect(err.Error()).To(Equal("Credentials were rejected, please try again.")) Expect(config.AccessToken()).To(BeEmpty()) Expect(config.RefreshToken()).To(BeEmpty())
"code": 10003, "description": "You are not authorized to perform the requested action", "error_code": "CF-NotAuthorized" }`), ), ) }) It("tries to unmarshal error response into provided resource", func() { type apiErrResponse struct { Code int `json:"code,omitempty"` Description string `json:"description,omitempty"` } errResponse := new(apiErrResponse) request, _ := ccGateway.NewRequest("GET", config.ApiEndpoint()+"/v2/some-endpoint", config.AccessToken(), nil) _, apiErr := ccGateway.PerformRequestForJSONResponse(request, errResponse) Ω(apiErr).To(HaveOccurred()) Ω(errResponse.Code).To(Equal(10003)) }) It("ignores any unmarshal error and does not alter the api err response", func() { request, _ := ccGateway.NewRequest("GET", config.ApiEndpoint()+"/v2/some-endpoint", config.AccessToken(), nil) _, apiErr := ccGateway.PerformRequestForJSONResponse(request, nil) Ω(apiErr.Error()).To(Equal("Server error, status code: 401, error code: 10003, message: You are not authorized to perform the requested action")) }) })
BeforeEach(func() { org.Name = "my-org" org.Guid = "my-org-guid" space.Name = "my-space" space.Guid = "my-space-guid" config.SetOrganizationFields(org) config.SetSpaceFields(space) testServerFn = validApiInfoEndpoint }) It("stores the data from the /info api in the config", func() { repo.UpdateEndpoint(testServer.URL) Expect(config.AccessToken()).To(Equal("")) Expect(config.AuthenticationEndpoint()).To(Equal("https://login.example.com")) Expect(config.LoggregatorEndpoint()).To(Equal("wss://loggregator.foo.example.org:4443")) Expect(config.DopplerEndpoint()).To(Equal("wss://doppler.foo.example.org:4443")) Expect(config.ApiEndpoint()).To(Equal(testServer.URL)) Expect(config.ApiVersion()).To(Equal("42.0.0")) Expect(config.HasOrganization()).To(BeFalse()) Expect(config.HasSpace()).To(BeFalse()) Expect(config.MinCliVersion()).To(Equal("6.5.0")) Expect(config.MinRecommendedCliVersion()).To(Equal("6.7.0")) }) Context("when the api endpoint does not change", func() { BeforeEach(func() { config.SetApiEndpoint(testServer.URL) config.SetAccessToken("some access token")
Describe("GetApplications", func() { It("tries to get applications from CC with a token handler", func() { r.GetApplications() Expect(ccClient.GetApplicationsCallCount()).To(Equal(1)) }) Context("when the client has updated tokens", func() { BeforeEach(func() { ccClient.TokensUpdatedReturns(true) ccClient.GetUpdatedTokensReturns("updated-access-token", "updated-refresh-token") }) It("stores the new tokens in the config", func() { r.GetApplications() Expect(config.AccessToken()).To(Equal("updated-access-token")) Expect(config.RefreshToken()).To(Equal("updated-refresh-token")) }) }) Context("when getting the applications succeeds", func() { BeforeEach(func() { ccClient.GetApplicationsReturns(getApplicationsJSON, nil) }) It("returns a slice of application model objects", func() { applications, err := r.GetApplications() Expect(err).NotTo(HaveOccurred()) Expect(applications).To(Equal([]models.V3Application{ { Name: "app-1-name",
l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, Flags, nil) Expect(ui.Outputs).To(ContainSubstrings( []string{"Select an org"}, []string{"1. some-org"}, []string{"2. my-new-org"}, []string{"Select a space"}, []string{"1. my-space"}, []string{"2. some-space"}, )) Expect(Config.OrganizationFields().Guid).To(Equal("my-new-org-guid")) Expect(Config.SpaceFields().Guid).To(Equal("my-space-guid")) Expect(Config.AccessToken()).To(Equal("my_access_token")) Expect(Config.RefreshToken()).To(Equal("my_refresh_token")) Expect(endpointRepo.UpdateEndpointReceived).To(Equal("api.example.com")) Expect(orgRepo.FindByNameArgsForCall(0)).To(Equal("my-new-org")) Expect(spaceRepo.FindByNameName).To(Equal("my-space")) Expect(ui.ShowConfigurationCalled).To(BeTrue()) }) It("lets the user select an org and space by name", func() { ui.Inputs = []string{"api.example.com", "*****@*****.**", "password", "my-new-org", "my-space"} orgRepo.FindByNameReturns(org2, nil) l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo)