func testUploadApp(dir string, requests []testnet.TestRequest) (app models.Application, apiErr errors.Error) { ts, handler := testnet.NewServer(requests) defer ts.Close() configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway(configRepo) gateway.PollingThrottle = time.Duration(0) zipper := app_files.ApplicationZipper{} repo := NewCloudControllerApplicationBitsRepository(configRepo, gateway, zipper) var ( reportedPath string reportedFileCount, reportedUploadSize uint64 ) apiErr = repo.UploadApp("my-cool-app-guid", dir, func(path string, uploadSize, fileCount uint64) { reportedPath = path reportedUploadSize = uploadSize reportedFileCount = fileCount }) Expect(reportedPath).To(Equal(dir)) Expect(reportedFileCount).To(Equal(uint64(len(expectedApplicationContent)))) Expect(reportedUploadSize).To(Equal(uint64(759))) Expect(handler).To(testnet.HaveAllRequestsCalled()) return }
func setupAuthDependencies(request testnet.TestRequest) (*httptest.Server, *testnet.TestHandler, configuration.ReadWriter) { ts, handler := testnet.NewServer([]testnet.TestRequest{request}) config := testconfig.NewRepository() config.SetAuthenticationEndpoint(ts.URL) return ts, handler, config }
func createUserProvidedServiceInstanceRepo(req testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo UserProvidedServiceInstanceRepository) { ts, handler = testnet.NewServer([]testnet.TestRequest{req}) configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway(configRepo) repo = NewCCUserProvidedServiceInstanceRepository(configRepo, gateway) return }
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 }
func createDomainRepo(reqs []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo DomainRepository) { ts, handler = testnet.NewServer(reqs) config := testconfig.NewRepositoryWithDefaults() config.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway() repo = NewCloudControllerDomainRepository(config, gateway) return }
func createServiceBindingRepo(requests []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo ServiceBindingRepository) { ts, handler = testnet.NewServer(requests) configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway() repo = NewCloudControllerServiceBindingRepository(configRepo, gateway) return }
func createOrganizationRepo(reqs ...testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo OrganizationRepository) { ts, handler = testnet.NewServer(reqs) configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway(configRepo) repo = NewCloudControllerOrganizationRepository(configRepo, gateway) return }
func createPasswordRepo(req testnet.TestRequest) (passwordServer *httptest.Server, handler *testnet.TestHandler, repo PasswordRepository) { passwordServer, handler = testnet.NewServer([]testnet.TestRequest{req}) configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetUaaEndpoint(passwordServer.URL) gateway := net.NewCloudControllerGateway(configRepo) repo = NewCloudControllerPasswordRepository(configRepo, gateway) return }
func createAppInstancesRepo(requests []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo AppInstancesRepository) { ts, handler = testnet.NewServer(requests) space := models.SpaceFields{} space.Guid = "my-space-guid" configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway() repo = NewCloudControllerAppInstancesRepository(configRepo, gateway) return }
func createPasswordRepo(req testnet.TestRequest) (passwordServer *httptest.Server, handler *testnet.TestHandler, repo PasswordRepository) { passwordServer, handler = testnet.NewServer([]testnet.TestRequest{req}) endpointRepo := &testapi.FakeEndpointRepo{} endpointRepo.UAAEndpointReturns.Endpoint = passwordServer.URL configRepo := testconfig.NewRepositoryWithDefaults() gateway := net.NewCloudControllerGateway() repo = NewCloudControllerPasswordRepository(configRepo, gateway, endpointRepo) return }
func createRoutesRepo(requests ...testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo CloudControllerRouteRepository, domainRepo *testapi.FakeDomainRepository) { ts, handler = testnet.NewServer(requests) configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway(configRepo) domainRepo = &testapi.FakeDomainRepository{} repo = NewCloudControllerRouteRepository(configRepo, gateway, domainRepo) return }
func setupEventTest(requests []testnet.TestRequest) (deps eventTestDependencies) { deps.server, deps.handler = testnet.NewServer(requests) configRepo := testconfig.NewRepository() configRepo.SetApiEndpoint(deps.server.URL) configRepo.SetAccessToken("BEARER my_access_token") deps.config = configRepo deps.gateway = net.NewCloudControllerGateway() return }
func createUsersRepo(ccReqs []testnet.TestRequest, uaaReqs []testnet.TestRequest) (cc *httptest.Server, ccHandler *testnet.TestHandler, uaa *httptest.Server, uaaHandler *testnet.TestHandler, repo UserRepository) { ccTarget := "" uaaTarget := "" if len(ccReqs) > 0 { cc, ccHandler = testnet.NewServer(ccReqs) ccTarget = cc.URL } if len(uaaReqs) > 0 { uaa, uaaHandler = testnet.NewServer(uaaReqs) uaaTarget = uaa.URL } configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ccTarget) ccGateway := net.NewCloudControllerGateway(configRepo) uaaGateway := net.NewUAAGateway(configRepo) configRepo.SetUaaEndpoint(uaaTarget) repo = NewCloudControllerUserRepository(configRepo, uaaGateway, ccGateway) return }
repo CloudControllerBuildpackBitsRepository buildpack models.Buildpack testServer *httptest.Server testServerHandler *testnet.TestHandler ) BeforeEach(func() { gateway := net.NewCloudControllerGateway(configRepo) pwd, _ := os.Getwd() buildpacksDir = filepath.Join(pwd, "../../fixtures/buildpacks") configRepo = testconfig.NewRepositoryWithDefaults() repo = NewCloudControllerBuildpackBitsRepository(configRepo, gateway, app_files.ApplicationZipper{}) buildpack = models.Buildpack{Name: "my-cool-buildpack", Guid: "my-cool-buildpack-guid"} testServer, testServerHandler = testnet.NewServer([]testnet.TestRequest{uploadBuildpackRequest()}) configRepo.SetApiEndpoint(testServer.URL) }) AfterEach(func() { testServer.Close() }) Describe("#UploadBuildpack", func() { It("fails to upload a buildpack with an invalid directory", func() { apiErr := repo.UploadBuildpack(buildpack, "/foo/bar") Expect(apiErr).NotTo(BeNil()) Expect(apiErr.Error()).To(ContainSubstring("Error opening buildpack file")) }) It("uploads a valid buildpack directory", func() {
domainRepo = &testapi.FakeDomainRepository{} repo = NewCloudControllerRouteRepository(configRepo, gateway, domainRepo) }) AfterEach(func() { ts.Close() }) Describe("List routes", func() { It("lists routes in the current space", func() { ts, handler = testnet.NewServer([]testnet.TestRequest{ testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "GET", Path: "/v2/spaces/the-space-guid/routes?inline-relations-depth=1", Response: firstPageRoutesResponse, }), testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "GET", Path: "/v2/spaces/the-space-guid/routes?inline-relations-depth=1&page=2", Response: secondPageRoutesResponse, }), }) configRepo.SetApiEndpoint(ts.URL) routes := []models.Route{} apiErr := repo.ListRoutes(func(route models.Route) bool { routes = append(routes, route) return true }) Expect(len(routes)).To(Equal(2)) Expect(routes[0].Guid).To(Equal("route-1-guid"))
testapi "testhelpers/api" 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() })
config configuration.ReadWriter auth AuthenticationRepository ) BeforeEach(func() { config = testconfig.NewRepository() gateway = net.NewUAAGateway(config) auth = NewUAAAuthenticationRepository(gateway, config) }) AfterEach(func() { testServer.Close() }) var setupTestServer = func(request testnet.TestRequest) { testServer, handler = testnet.NewServer([]testnet.TestRequest{request}) config.SetAuthenticationEndpoint(testServer.URL) } Describe("authenticating", func() { var err error JustBeforeEach(func() { err = auth.Authenticate(map[string]string{ "username": "******", "password": "******", }) }) Describe("when login succeeds", func() { BeforeEach(func() {
cc, ccHandler, _, _, repo := createUsersRepo(ccReqs, []testnet.TestRequest{}) defer cc.Close() _, apiErr := repo.ListUsersInOrgForRole("my-org-guid", models.ORG_MANAGER) Expect(ccHandler).To(testnet.HaveAllRequestsCalled()) httpErr, ok := apiErr.(errors.HttpError) Expect(ok).To(BeTrue()) Expect(httpErr.StatusCode()).To(Equal(http.StatusGatewayTimeout)) }) It("returns an error when the UAA endpoint cannot be determined", func() { ccReqs, _ := createUsersByRoleEndpoints("/v2/organizations/my-org-guid/managers") ts, _ := testnet.NewServer(ccReqs) defer ts.Close() configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) ccGateway := net.NewCloudControllerGateway() uaaGateway := net.NewUAAGateway() endpointRepo := &testapi.FakeEndpointRepo{} endpointRepo.UAAEndpointReturns.Error = errors.NewErrorWithError("Failed to get endpoint!", errors.New("Failed!")) repo := NewCloudControllerUserRepository(configRepo, uaaGateway, ccGateway, endpointRepo) _, apiErr := repo.ListUsersInOrgForRole("my-org-guid", models.ORG_MANAGER) Expect(apiErr).To(Equal(endpointRepo.UAAEndpointReturns.Error)) })
BeforeEach(func() { config = testconfig.NewRepositoryWithDefaults() }) JustBeforeEach(func() { gateway := net.NewCloudControllerGateway(config) strategy := strategy.NewEndpointStrategy(config.ApiVersion()) repo = NewCloudControllerDomainRepository(config, gateway, strategy) }) AfterEach(func() { ts.Close() }) var setupTestServer = func(reqs ...testnet.TestRequest) { ts, handler = testnet.NewServer(reqs) config.SetApiEndpoint(ts.URL) } Describe("listing domains", func() { BeforeEach(func() { config.SetApiVersion("2.2.0") setupTestServer(firstPageDomainsRequest, secondPageDomainsRequest) }) It("uses the organization-scoped domains endpoint", func() { receivedDomains := []models.DomainFields{} apiErr := repo.ListDomainsForOrg("my-org-guid", func(d models.DomainFields) bool { receivedDomains = append(receivedDomains, d) return true })
ccGateway := net.NewCloudControllerGateway(config) 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...)
var _ = Describe("CloudControllerApplicationBitsRepository", func() { var ( fixturesDir string reportedFilePath string reportedUploadSize uint64 reportedFileCount uint64 ) BeforeEach(func() { cwd, err := os.Getwd() Expect(err).NotTo(HaveOccurred()) fixturesDir = filepath.Join(cwd, "../../fixtures/applications") }) var testUploadApp = func(dir string, requests ...testnet.TestRequest) (apiErr error) { ts, handler := testnet.NewServer(requests) defer ts.Close() configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(ts.URL) gateway := net.NewCloudControllerGateway(configRepo) gateway.PollingThrottle = time.Duration(0) zipper := app_files.ApplicationZipper{} repo := NewCloudControllerApplicationBitsRepository(configRepo, gateway, zipper) apiErr = repo.UploadApp("my-cool-app-guid", dir, func(path string, uploadSize, fileCount uint64) { reportedFilePath = path reportedUploadSize = uploadSize reportedFileCount = fileCount })
config.SetAccessToken("BEARER my_access_token") 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"),
deps.config = testconfig.NewRepository() deps.config.SetAccessToken("BEARER my_access_token") deps.gateway = net.NewCloudControllerGateway(deps.config) return } var _ = Describe("curl command", func() { It("TestCurlGetRequest", func() { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "GET", Path: "/v2/endpoint", Response: testnet.TestResponse{ Status: http.StatusOK, Body: expectedJSONResponse}, }) ts, handler := testnet.NewServer([]testnet.TestRequest{req}) defer ts.Close() deps := newCurlDependencies() deps.config.SetApiEndpoint(ts.URL) repo := NewCloudControllerCurlRepository(deps.config, deps.gateway) headers, body, apiErr := repo.Request("GET", "/v2/endpoint", "", "") Expect(handler).To(testnet.HaveAllRequestsCalled()) Expect(headers).To(ContainSubstring("200")) Expect(headers).To(ContainSubstring("Content-Type")) Expect(headers).To(ContainSubstring("text/plain")) testassert.JSONStringEquals(body, expectedJSONResponse) Expect(apiErr).NotTo(HaveOccurred()) })
listFilesServer := httptest.NewServer(http.HandlerFunc(listFilesEndpoint)) defer listFilesServer.Close() req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "GET", Path: "/v2/apps/my-app-guid/instances/0/files/some/path", Response: testnet.TestResponse{ Status: http.StatusTemporaryRedirect, Header: http.Header{ "Location": {fmt.Sprintf("%s/some/path", listFilesServer.URL)}, }, }, }) listFilesRedirectServer, handler := testnet.NewServer([]testnet.TestRequest{req}) defer listFilesRedirectServer.Close() configRepo := testconfig.NewRepositoryWithDefaults() configRepo.SetApiEndpoint(listFilesRedirectServer.URL) gateway := net.NewCloudControllerGateway(configRepo) repo := NewCloudControllerAppFilesRepository(configRepo, gateway) list, err := repo.ListFiles("my-app-guid", "some/path") Expect(handler).To(testnet.HaveAllRequestsCalled()) Expect(err).ToNot(HaveOccurred()) Expect(list).To(Equal(expectedResponse)) }) })