func createServiceRepoWithConfig(reqs []testnet.TestRequest, config configuration.ReadWriter) (testServer *httptest.Server, handler *testnet.TestHandler, repo ServiceRepository) { testServer, handler = testnet.NewServer(reqs) config.SetApiEndpoint(testServer.URL) gateway := net.NewCloudControllerGateway(config) repo = NewCloudControllerServiceRepository(config, gateway) return }
Expect(ui.FailedWithUsage).To(BeTrue()) }) It("fails if the user has not set an api endpoint", func() { context := testcmd.NewContext("auth", []string{"username", "password"}) testcmd.RunCommand(cmd, context, requirementsFactory) Expect(testcmd.CommandDidPassRequirements).To(BeFalse()) }) }) Context("when an api endpoint is targeted", func() { BeforeEach(func() { requirementsFactory.ApiEndpointSuccess = true config.SetApiEndpoint("foo.example.org/authenticate") }) It("authenticates successfully", func() { requirementsFactory.ApiEndpointSuccess = true context := testcmd.NewContext("auth", []string{"*****@*****.**", "password"}) testcmd.RunCommand(cmd, context, requirementsFactory) Expect(ui.FailedWithUsage).To(BeFalse()) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"foo.example.org/authenticate"}, {"OK"}, }) Expect(repo.AuthenticateArgs.Credentials).To(Equal([]map[string]string{ {
{"SSL cert", "https://buttontomatoes.org"}, {"TIP", "--skip-ssl-validation"}, }) }) }) Context("when the user does not provide an endpoint", func() { Context("when the endpoint is set in the config", func() { var ( ui *testterm.FakeUI ctx *cli.Context requirementsFactory *testreq.FakeReqFactory ) BeforeEach(func() { config.SetApiEndpoint("https://api.run.pivotal.io") config.SetApiVersion("2.0") config.SetSSLDisabled(true) ui = new(testterm.FakeUI) ctx = testcmd.NewContext("api", []string{}) requirementsFactory = &testreq.FakeReqFactory{} }) JustBeforeEach(func() { testcmd.RunCommand(NewApi(ui, config, endpointRepo), ctx, requirementsFactory) }) It("prints out the api endpoint", func() { testassert.SliceContains(ui.Outputs, testassert.Lines{ {"https://api.run.pivotal.io", "2.0"},
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(authRepo.AuthenticateArgs.Credentials).To(Equal([]map[string]string{ { "username": "******", "password": "******", }, })) Expect(ui.ShowConfigurationCalled).To(BeTrue()) }) It("doesn't ask the user for the API url if they have it in their config", func() { Config.SetApiEndpoint("http://api.example.com") Flags = []string{"-o", "my-new-org", "-s", "my-space"} ui.Inputs = []string{"*****@*****.**", "password"} l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, testcmd.NewContext("login", Flags), nil) Expect(Config.ApiEndpoint()).To(Equal("http://api.example.com")) 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("http://api.example.com")) Expect(ui.ShowConfigurationCalled).To(BeTrue())
}) }) }) Context("when an api endpoint is set and the user logged in", func() { var config configuration.ReadWriter BeforeEach(func() { accessToken := configuration.TokenInfo{ UserGuid: "my-user-guid", Username: "******", Email: "my-user-email", } config = testconfig.NewRepositoryWithAccessToken(accessToken) config.SetApiEndpoint("https://test.example.org") config.SetApiVersion("☃☃☃") }) Describe("tells the user what is set in the config", func() { var output []string JustBeforeEach(func() { output = captureOutput(func() { ui := NewUI(os.Stdin) ui.ShowConfiguration(config) }) }) It("tells the user which api endpoint is set", func() { testassert.SliceContains(output, testassert.Lines{
testconfig "testhelpers/configuration" "testhelpers/maker" testnet "testhelpers/net" ) var _ = Describe("Services Repo", func() { var ( testServer *httptest.Server testHandler *testnet.TestHandler configRepo configuration.ReadWriter repo ServiceRepository ) setupTestServer := func(reqs ...testnet.TestRequest) { testServer, testHandler = testnet.NewServer(reqs) configRepo.SetApiEndpoint(testServer.URL) } BeforeEach(func() { configRepo = testconfig.NewRepositoryWithDefaults() configRepo.SetAccessToken("BEARER my_access_token") gateway := net.NewCloudControllerGateway(configRepo) repo = NewCloudControllerServiceRepository(configRepo, gateway) }) AfterEach(func() { testServer.Close() }) Describe("GetAllServiceOfferings", func() {
Expect(config.HasOrganization()).To(BeFalse()) Expect(config.HasSpace()).To(BeFalse()) }) It("does not clear the session if the api endpoint does not change", func() { testServerFn = validApiInfoEndpoint org := models.OrganizationFields{} org.Name = "my-org" org.Guid = "my-org-guid" space := models.SpaceFields{} space.Name = "my-space" space.Guid = "my-space-guid" config.SetApiEndpoint(testServer.URL) config.SetAccessToken("some access token") config.SetRefreshToken("some refresh token") config.SetOrganizationFields(org) config.SetSpaceFields(space) repo.UpdateEndpoint(testServer.URL) Expect(config.OrganizationFields()).To(Equal(org)) Expect(config.SpaceFields()).To(Equal(space)) Expect(config.AccessToken()).To(Equal("some access token")) Expect(config.RefreshToken()).To(Equal("some refresh token")) }) }) Context("when the API request fails", func() {
Expect(config.HasOrganization()).To(BeFalse()) Expect(config.HasSpace()).To(BeFalse()) }) It("TestUpdateEndpointWhenUrlIsAlreadyTargeted", func() { testServerFn = validApiInfoEndpoint org := models.OrganizationFields{} org.Name = "my-org" org.Guid = "my-org-guid" space := models.SpaceFields{} space.Name = "my-space" space.Guid = "my-space-guid" config.SetApiEndpoint(testServer.URL) config.SetAccessToken("some access token") config.SetRefreshToken("some refresh token") config.SetOrganizationFields(org) config.SetSpaceFields(space) repo.UpdateEndpoint(testServer.URL) Expect(config.OrganizationFields()).To(Equal(org)) Expect(config.SpaceFields()).To(Equal(space)) Expect(config.AccessToken()).To(Equal("some access token")) Expect(config.RefreshToken()).To(Equal("some refresh token")) }) It("returns a failure response when the API request fails", func() { testServerFn = func(w http.ResponseWriter, r *http.Request) {
requestHandler.lastPath = request.URL.Path Expect(request.URL.RawQuery).To(Equal("app=my-app-guid")) Expect(request.Method).To(Equal("GET")) Expect(request.Header.Get("Authorization")).To(ContainSubstring("BEARER my_access_token")) for _, msg := range messagesToSend { conn.Write(msg) } time.Sleep(time.Duration(50) * time.Millisecond) conn.Close() } testServer = httptest.NewTLSServer(websocket.Handler(requestHandler.handlerFunc)) configRepo = testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint("https://localhost") configRepo.SetLoggregatorEndpoint(strings.Replace(testServer.URL, "https", "wss", 1)) repo := NewLoggregatorLogsRepository(configRepo) logsRepo = &repo logsRepo.AddTrustedCerts(testServer.TLS.Certificates) }) AfterEach(func() { testServer.Close() }) Describe("RecentLogsFor", func() { BeforeEach(func() { err := logsRepo.RecentLogsFor("my-app-guid", func() {}, logChan) Expect(err).NotTo(HaveOccurred())
config.SetApiVersion("2.2.0") }) JustBeforeEach(func() { strategy := strategy.NewEndpointStrategy(config.ApiVersion()) gateway := net.NewCloudControllerGateway(config) repo = NewCloudControllerAppEventsRepository(config, gateway, strategy) }) AfterEach(func() { server.Close() }) setupTestServer := func(requests ...testnet.TestRequest) { server, handler = testnet.NewServer(requests) config.SetApiEndpoint(server.URL) } Describe("list recent events", func() { It("returns the most recent events", func() { setupTestServer(eventsRequest) list, err := repo.RecentEvents("my-app-guid", 2) Expect(err).ToNot(HaveOccurred()) Expect(list).To(Equal([]models.EventFields{ models.EventFields{ Guid: "event-1-guid", Name: "audit.app.update", Timestamp: testtime.MustParse(eventTimestampFormat, "2014-01-21T00:20:11+00:00"), Description: "instances: 1, memory: 256, command: PRIVATE DATA HIDDEN, environment_json: PRIVATE DATA HIDDEN",
uaaGateway := net.NewUAAGateway(config) repo = NewCloudControllerUserRepository(config, uaaGateway, ccGateway) }) AfterEach(func() { if uaaServer != nil { uaaServer.Close() } if ccServer != nil { ccServer.Close() } }) setupCCServer := func(requests ...testnet.TestRequest) { ccServer, ccHandler = testnet.NewServer(requests) config.SetApiEndpoint(ccServer.URL) } setupUAAServer := func(requests ...testnet.TestRequest) { uaaServer, uaaHandler = testnet.NewServer(requests) config.SetUaaEndpoint(uaaServer.URL) } Describe("listing the users with a given role", func() { It("lists the users in an organization with a given role", func() { ccReqs, uaaReqs := createUsersByRoleEndpoints("/v2/organizations/my-org-guid/managers") setupCCServer(ccReqs...) setupUAAServer(uaaReqs...) users, apiErr := repo.ListUsersInOrgForRole("my-org-guid", models.ORG_MANAGER)
Expect(Config.OrganizationFields().Guid).To(Equal("my-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(authRepo.AuthenticateArgs.Credentials).To(Equal(map[string]string{ "username": "******", "password": "******", })) Expect(ui.ShowConfigurationCalled).To(BeTrue()) }) It("doesn't ask the user for the API url if they have it in their config", func() { Config.SetApiEndpoint("http://api.example.com") Flags = []string{"-o", "my-org", "-s", "my-space"} ui.Inputs = []string{"*****@*****.**", "password"} l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, testcmd.NewContext("login", Flags), nil) Expect(Config.ApiEndpoint()).To(Equal("http://api.example.com")) Expect(Config.OrganizationFields().Guid).To(Equal("my-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("http://api.example.com")) Expect(authRepo.AuthenticateArgs.Credentials).To(Equal(map[string]string{
repo BuildpackRepository ) BeforeEach(func() { config = testconfig.NewRepositoryWithDefaults() gateway := net.NewCloudControllerGateway(config) repo = NewCloudControllerBuildpackRepository(config, gateway) }) AfterEach(func() { ts.Close() }) var setupTestServer = func(requests ...testnet.TestRequest) { ts, handler = testnet.NewServer(requests) config.SetApiEndpoint(ts.URL) } It("lists buildpacks", func() { setupTestServer( testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "GET", Path: "/v2/buildpacks", Response: testnet.TestResponse{ Status: http.StatusOK, Body: `{ "next_url": "/v2/buildpacks?page=2", "resources": [ { "metadata": { "guid": "buildpack1-guid"