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 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) }
} var createRouteResponse = testhelpers.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "my-route-guid" }, "entity": { "host": "my-cool-app" } }`} var createRouteEndpoint = testhelpers.CreateEndpoint( "POST", "/v2/routes", testhelpers.RequestBodyMatcher(`{"host":"my-cool-app","domain_guid":"my-domain-guid","space_guid":"my-space-guid"}`), createRouteResponse, ) func TestCreate(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(createRouteEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Space: cf.Space{Guid: "my-space-guid"}, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerRouteRepository(config, client)
assert.Equal(t, len(firstOffering.Plans), 2) plan := firstOffering.Plans[0] assert.Equal(t, plan.Name, "Offering 1 Plan 1") assert.Equal(t, plan.Guid, "offering-1-plan-1-guid") secondOffering := offerings[1] assert.Equal(t, secondOffering.Label, "Offering 2") assert.Equal(t, secondOffering.Guid, "offering-2-guid") assert.Equal(t, len(secondOffering.Plans), 1) } var createServiceInstanceEndpoint = testhelpers.CreateEndpoint( "POST", "/v2/service_instances", testhelpers.RequestBodyMatcher(`{"name":"instance-name","service_plan_guid":"plan-guid","space_guid":"space-guid"}`), testhelpers.TestResponse{Status: http.StatusCreated}, ) func TestCreateServiceInstance(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(createServiceInstanceEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Space: cf.Space{Guid: "space-guid"}, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerServiceRepository(config, client)
instance1 := space.ServiceInstances[0] assert.Equal(t, instance1.Name, "my-service-instance") assert.Equal(t, instance1.ServicePlan.Name, "spark") assert.Equal(t, instance1.ServicePlan.ServiceOffering.Label, "cleardb") assert.Equal(t, instance1.ServicePlan.ServiceOffering.Provider, "cleardb-provider") assert.Equal(t, instance1.ServicePlan.ServiceOffering.Version, "n/a") assert.Equal(t, len(instance1.ApplicationNames), 2) assert.Equal(t, instance1.ApplicationNames[0], "app1") assert.Equal(t, instance1.ApplicationNames[1], "app2") } var createSpaceEndpoint = testhelpers.CreateEndpoint( "POST", "/v2/spaces", testhelpers.RequestBodyMatcher(`{"name":"space-name","organization_guid":"org-guid"}`), testhelpers.TestResponse{Status: http.StatusCreated}, ) func TestCreateSpace(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(createSpaceEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Organization: cf.Organization{Guid: "org-guid"}, } gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerSpaceRepository(config, gateway)
org, err := repo.FindByName("Org1") assert.NoError(t, err) assert.Equal(t, org, existingOrg) org, err = repo.FindByName("org1") assert.NoError(t, err) assert.Equal(t, org, existingOrg) org, err = repo.FindByName("org that does not exist") assert.Error(t, err) } var createOrgEndpoint = testhelpers.CreateEndpoint( "POST", "/v2/organizations", testhelpers.RequestBodyMatcher(`{"name":"my-org"}`), testhelpers.TestResponse{Status: http.StatusCreated}, ) func TestCreateOrganization(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(createOrgEndpoint)) defer ts.Close() config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: ts.URL} gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerOrganizationRepository(config, gateway) err := repo.Create("my-org") assert.NoError(t, err) }
config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, Space: cf.Space{Name: "my-space", Guid: "my-space-guid"}, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerApplicationRepository(config, client) _, err := repo.FindByName("App1") assert.Error(t, err) } var setEnvEndpoint = testhelpers.CreateEndpoint( "PUT", "/v2/apps/app1-guid", testhelpers.RequestBodyMatcher(`{"environment_json":{"DATABASE_URL":"mysql://example.com/my-db"}}`), testhelpers.TestResponse{Status: http.StatusCreated}, ) func TestSetEnv(t *testing.T) { ts := httptest.NewTLSServer(http.HandlerFunc(setEnvEndpoint)) defer ts.Close() config := &configuration.Configuration{ AccessToken: "BEARER my_access_token", Target: ts.URL, } client := NewApiClient(&testhelpers.FakeAuthenticator{}) repo := NewCloudControllerApplicationRepository(config, client) app := cf.Application{Guid: "app1-guid", Name: "App1"}