func testScore(t *testing.T, scoreBody string, expectedScore string) { passwordScoreResponse := testhelpers.TestResponse{Status: http.StatusOK, Body: scoreBody} passwordScoreEndpoint := testhelpers.CreateEndpoint( "POST", "/password/score", func(req *http.Request) bool { bodyMatcher := testhelpers.RequestBodyMatcher("password=new-password") contentTypeMatches := req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" return contentTypeMatches && bodyMatcher(req) }, passwordScoreResponse, ) scoreServer := httptest.NewTLSServer(http.HandlerFunc(passwordScoreEndpoint)) defer scoreServer.Close() targetServer := createInfoServer(scoreServer.URL) defer targetServer.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: targetServer.URL, } gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerPasswordRepository(config, gateway) score, err := repo.GetScore("new-password") assert.NoError(t, err) assert.Equal(t, score, expectedScore) }
func createInfoServer(tokenEndpoint string) *httptest.Server { targetInfoResponse := testhelpers.TestResponse{ Status: http.StatusOK, Body: fmt.Sprintf(`{"token_endpoint": "%s"}`, tokenEndpoint), } targetInfoEndpoint := testhelpers.CreateEndpoint("GET", "/info", nil, targetInfoResponse) return httptest.NewTLSServer(http.HandlerFunc(targetInfoEndpoint)) }
func TestListFiles(t *testing.T) { expectedResponse := "file 1\n file 2\n file 3" listFilesEndpoint := func(writer http.ResponseWriter, request *http.Request) { methodMatches := request.Method == "GET" pathMatches := request.URL.Path == "/some/path" if !methodMatches || !pathMatches { fmt.Printf("One of the matchers did not match. Method [%t] Path [%t]", methodMatches, pathMatches) writer.WriteHeader(http.StatusInternalServerError) return } writer.WriteHeader(http.StatusOK) fmt.Fprint(writer, expectedResponse) } listFilesServer := httptest.NewTLSServer(http.HandlerFunc(listFilesEndpoint)) defer listFilesServer.Close() listFilesRedirectEndpoint := func(writer http.ResponseWriter, req *http.Request) { baseEndpoint := testhelpers.CreateEndpoint( "GET", "/v2/apps/my-app-guid/instances/0/files/some/path", nil, testhelpers.TestResponse{Status: http.StatusTemporaryRedirect}, ) writer.Header().Add("Location", fmt.Sprintf("%s/some/path", listFilesServer.URL)) baseEndpoint(writer, req) } listFilesRedirectServer := httptest.NewTLSServer(http.HandlerFunc(listFilesRedirectEndpoint)) defer listFilesRedirectServer.Close() config := &configuration.Configuration{ Target: listFilesRedirectServer.URL, AccessToken: "BEARER my_access_token", } gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerAppFilesRepository(config, gateway) list, err := repo.ListFiles(cf.Application{Guid: "my-app-guid"}, "some/path") assert.NoError(t, err) assert.Equal(t, list, expectedResponse) }
func TestUpdatePassword(t *testing.T) { var passwordWasUpdated bool passwordUpdateResponse := testhelpers.TestResponse{Status: http.StatusOK} passwordUpdateEndpoint := testhelpers.CreateEndpoint( "PUT", "/Users/my-user-guid/password", func(req *http.Request) bool { passwordWasUpdated = true bodyMatcher := testhelpers.RequestBodyMatcher(`{"password":"******","oldPassword":"******"}`) contentTypeMatches := req.Header.Get("Content-Type") == "application/json" return contentTypeMatches && bodyMatcher(req) }, passwordUpdateResponse, ) passwordUpdateServer := httptest.NewTLSServer(http.HandlerFunc(passwordUpdateEndpoint)) defer passwordUpdateServer.Close() targetServer := createInfoServer(passwordUpdateServer.URL) defer targetServer.Close() tokenInfo := `{"user_id":"my-user-guid"}` encodedTokenInfo := base64.StdEncoding.EncodeToString([]byte(tokenInfo)) config := &configuration.Configuration{ AccessToken: fmt.Sprintf("BEARER my_access_token.%s.baz", encodedTokenInfo), Target: targetServer.URL, } gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerPasswordRepository(config, gateway) err := repo.UpdatePassword("old-password", "new-password") assert.NoError(t, err) assert.True(t, passwordWasUpdated) }
{ "resources": [ { "metadata": { "guid": "custom-linux-guid" }, "entity": { "name": "custom-linux" } } ] }`} var singleStackEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/stacks?q=name%3Alinux", nil, singleStackResponse, ) func TestStacksFindByName(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(singleStackEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerStackRepository(config, client) stack, err := repo.FindByName("linux")
} }, { "metadata": { "guid": "domain2-guid" }, "entity": { "name": "domain2.cf-app.com" } } ] }`} var multipleDomainsEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/spaces/my-space-guid/domains", nil, multipleDomainsResponse, ) func TestFindAll(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(multipleDomainsEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Space: cf.Space{Guid: "my-space-guid"}, } gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerDomainRepository(config, gateway)
} }, { "metadata": { "guid": "staging-space-guid" }, "entity": { "name": "staging" } } ] }`} var multipleSpacesEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/organizations/some-org-guid/spaces", nil, multipleSpacesResponse, ) func TestSpacesFindAll(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(multipleSpacesEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Organization: cf.Organization{Guid: "some-org-guid"}, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerSpaceRepository(config, client) spaces, err := repo.FindAll()
"domain": { "metadata": { "guid": "domain-2-guid" }, "entity": { "name": "example.com" } } } } ] }`} var findAllEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/routes?inline-relations-depth=1", nil, findAllRoutesResponse, ) func TestRoutesFindAll(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(findAllEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerRouteRepository(config, client) routes, err := repo.FindAll()
"description": "Offering 2 description", "version" : "1.5", "service_plans": [ { "metadata": {"guid": "offering-2-plan-1-guid"}, "entity": {"name": "Offering 2 Plan 1"} } ] } } ] }`} var multipleOfferingsEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/services?inline-relations-depth=1", nil, multipleOfferingsResponse, ) func TestGetServiceOfferings(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(multipleOfferingsEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerServiceRepository(config, client) offerings, err := repo.GetServiceOfferings()
} }, { "metadata": { "guid": "staging-space-guid" }, "entity": { "name": "staging" } } ] }`} var multipleSpacesEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/organizations/some-org-guid/spaces", nil, multipleSpacesResponse, ) func TestSpacesFindAll(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(multipleSpacesEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Organization: cf.Organization{Guid: "some-org-guid"}, } gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerSpaceRepository(config, gateway) spaces, err := repo.FindAll()
} }, { "metadata": { "guid": "org2-guid" }, "entity": { "name": "Org2" } } ] }`} var multipleOrgEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/organizations", nil, multipleOrgResponse, ) func TestOrganizationsFindAll(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(multipleOrgEndpoint)) defer ts.Close() config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: ts.URL} gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerOrganizationRepository(config, gateway) organizations, err := repo.FindAll() assert.NoError(t, err) assert.Equal(t, 2, len(organizations))
}, "entity": { "name": "cfapps.io" } } } } ] } } ] }`} var findAppEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/spaces/my-space-guid/apps?q=name%3AApp1&inline-relations-depth=1", nil, singleAppResponse, ) var appSummaryResponse = testhelpers.TestResponse{Status: http.StatusOK, Body: ` { "guid": "app1-guid", "name": "App1", "routes": [ { "guid": "route-1-guid", "host": "app1", "domain": { "guid": "domain-1-guid", "name": "cfapps.io" }
"net/http" "net/http/httptest" "strings" "testhelpers" "testing" "time" ) var instancesEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/apps/my-cool-app-guid/instances", nil, testhelpers.TestResponse{Status: http.StatusOK, Body: ` { "1": { "state": "STARTING", "since": 1379522342.6783738 }, "0": { "state": "RUNNING", "since": 1379522342.6783738 } }`}, ) var statsEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/apps/my-cool-app-guid/stats", nil, testhelpers.TestResponse{Status: http.StatusOK, Body: ` { "1":{
} }, { "metadata": { "guid": "org2-guid" }, "entity": { "name": "Org2" } } ] }`} var multipleOrgEndpoint = testhelpers.CreateEndpoint( "GET", "/v2/organizations", nil, multipleOrgResponse, ) func TestOrganizationsFindAll(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(multipleOrgEndpoint)) defer ts.Close() config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: ts.URL} client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerOrganizationRepository(config, client) organizations, err := repo.FindAll() assert.NoError(t, err) assert.Equal(t, 2, len(organizations))