func TestCreateUser(t *testing.T) { ccReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/users", Matcher: testnet.RequestBodyMatcher(`{"guid":"my-user-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) uaaReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/Users", Matcher: testnet.RequestBodyMatcher(`{ "userName":"******", "emails":[{"value":"my-user"}], "password":"******", "name":{ "givenName":"my-user", "familyName":"my-user"} }`), Response: testnet.TestResponse{ Status: http.StatusCreated, Body: `{"id":"my-user-guid"}`, }, }) cc, ccHandler, uaa, uaaHandler, repo := createUsersRepo(t, []testnet.TestRequest{ccReq}, []testnet.TestRequest{uaaReq}) defer cc.Close() defer uaa.Close() apiResponse := repo.Create("my-user", "my-password") assert.True(t, ccHandler.AllRequestsCalled()) assert.True(t, uaaHandler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestCreateInSpace(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/routes", Matcher: testnet.RequestBodyMatcher(`{"host":"my-cool-app","domain_guid":"my-domain-guid","space_guid":"my-space-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "my-route-guid" }, "entity": { "host": "my-cool-app" } }`}, }) ts, handler, repo, _ := createRoutesRepo(t, request) defer ts.Close() domain := cf.Domain{Guid: "my-domain-guid"} newRoute := cf.Route{Host: "my-cool-app"} space := cf.Space{Guid: "my-space-guid"} createdRoute, apiResponse := repo.CreateInSpace(newRoute, domain, space) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) assert.Equal(t, createdRoute, cf.Route{Host: "my-cool-app", Guid: "my-route-guid", Domain: domain}) }
func TestUpdateServiceBroker(t *testing.T) { expectedReqBody := `{"broker_url":"http://update.example.com","auth_username":"******","auth_password":"******"}` req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/service_brokers/my-guid", Matcher: testnet.RequestBodyMatcher(expectedReqBody), Response: testnet.TestResponse{Status: http.StatusOK}, }) ts, handler, repo := createServiceBrokerRepo(t, req) defer ts.Close() serviceBroker := cf.ServiceBroker{ Guid: "my-guid", Name: "foobroker", Url: "http://update.example.com", Username: "******", Password: "******", } apiResponse := repo.Update(serviceBroker) assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func TestUpdateBuildpack(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/buildpacks/my-cool-buildpack-guid", Matcher: testnet.RequestBodyMatcher(`{"name":"my-cool-buildpack","position":555}`), Response: testnet.TestResponse{ Status: http.StatusCreated, Body: `{ "metadata": { "guid": "my-cool-buildpack-guid" }, "entity": { "name": "my-cool-buildpack", "position":555 } }`}, }) ts, handler, repo := createBuildpackRepo(t, []testnet.TestRequest{req}) defer ts.Close() position := 555 buildpack := cf.Buildpack{Name: "my-cool-buildpack", Guid: "my-cool-buildpack-guid", Position: &position} updated, apiResponse := repo.Update(buildpack) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) assert.Equal(t, buildpack, updated) }
func TestCreateBuildpackEnabled(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/buildpacks", Matcher: testnet.RequestBodyMatcher(`{"name":"my-cool-buildpack","position":999, "enabled":true}`), Response: testnet.TestResponse{ Status: http.StatusCreated, Body: `{ "metadata": { "guid": "my-cool-buildpack-guid" }, "entity": { "name": "my-cool-buildpack", "position":999, "enabled":true } }`}, }) ts, handler, repo := createBuildpackRepo(t, req) defer ts.Close() position := 999 enabled := true created, apiResponse := repo.Create("my-cool-buildpack", &position, &enabled) assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) assert.NotNil(t, created.Guid) assert.Equal(t, "my-cool-buildpack", created.Name) assert.Equal(t, 999, *created.Position) }
func TestCreateBuildpackWithPosition(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/buildpacks", Matcher: testnet.RequestBodyMatcher(`{"name":"my-cool-buildpack","position":999}`), Response: testnet.TestResponse{ Status: http.StatusCreated, Body: `{ "metadata": { "guid": "my-cool-buildpack-guid" }, "entity": { "name": "my-cool-buildpack", "position":999 } }`}, }) ts, handler, repo := createBuildpackRepo(t, []testnet.TestRequest{req}) defer ts.Close() position := 999 buildpack := cf.Buildpack{Name: "my-cool-buildpack", Position: &position} created, apiResponse := repo.Create(buildpack) assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) assert.NotNil(t, created.Guid) assert.Equal(t, buildpack.Name, created.Name) assert.Equal(t, *buildpack.Position, 999) }
func TestUpdateUserProvidedServiceInstance(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/user_provided_service_instances/my-instance-guid", Matcher: testnet.RequestBodyMatcher(`{"credentials":{"host":"example.com","password":"******","user":"******"},"syslog_drain_url":"syslog://example.com"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createUserProvidedServiceInstanceRepo(t, req) defer ts.Close() params := map[string]string{ "host": "example.com", "user": "******", "password": "******", } serviceInstance := cf.ServiceInstanceFields{} serviceInstance.Guid = "my-instance-guid" serviceInstance.Params = params serviceInstance.SysLogDrainUrl = "syslog://example.com" apiResponse := repo.Update(serviceInstance) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestStopApplication(t *testing.T) { stopApplicationRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/apps/my-cool-app-guid?inline-relations-depth=2", Matcher: testnet.RequestBodyMatcher(`{"console":true,"state":"STOPPED"}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "my-updated-app-guid" }, "entity": { "name": "cli1", "state": "STOPPED" } }`}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{stopApplicationRequest}) defer ts.Close() app := cf.Application{Name: "my-cool-app", Guid: "my-cool-app-guid"} updatedApp, apiResponse := repo.Stop(app) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) assert.Equal(t, "cli1", updatedApp.Name) assert.Equal(t, "stopped", updatedApp.State) assert.Equal(t, "my-updated-app-guid", updatedApp.Guid) }
func testRenameService(endpointPath string, serviceInstance models.ServiceInstance) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: endpointPath, Matcher: testnet.RequestBodyMatcher(`{"name":"new-name"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) testServer, handler, repo := createServiceRepo([]testnet.TestRequest{req}) defer testServer.Close() apiErr := repo.RenameService(serviceInstance, "new-name") Expect(handler).To(testnet.HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred()) }
func TestCreateServiceBinding(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_bindings", Matcher: testnet.RequestBodyMatcher(`{"app_guid":"my-app-guid","service_instance_guid":"my-service-instance-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createServiceBindingRepo(t, []testnet.TestRequest{req}) defer ts.Close() apiResponse := repo.Create("my-service-instance-guid", "my-app-guid") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestRenameSpace(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/spaces/my-space-guid", Matcher: testnet.RequestBodyMatcher(`{"name":"new-space-name"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createSpacesRepo(t, request) defer ts.Close() apiResponse := repo.Rename("my-space-guid", "new-space-name") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func testRenameService(t *testing.T, endpointPath string, serviceInstance cf.ServiceInstance) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: endpointPath, Matcher: testnet.RequestBodyMatcher(`{"name":"new-name"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createServiceRepo(t, []testnet.TestRequest{req}) defer ts.Close() apiResponse := repo.RenameService(serviceInstance, "new-name") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestCreateSpace(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/spaces", Matcher: testnet.RequestBodyMatcher(`{"name":"space-name","organization_guid":"some-org-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createSpacesRepo(t, request) defer ts.Close() apiResponse := repo.Create("space-name") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestUpdateQuota(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/organizations/my-org-guid", Matcher: testnet.RequestBodyMatcher(`{"quota_definition_guid":"my-quota-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createQuotaRepo(t, req) defer ts.Close() apiResponse := repo.Update("my-org-guid", "my-quota-guid") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestRenameOrganization(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/organizations/my-org-guid", Matcher: testnet.RequestBodyMatcher(`{"name":"my-new-org"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createOrganizationRepo(t, req) defer ts.Close() org := cf.Organization{Guid: "my-org-guid"} apiResponse := repo.Rename(org, "my-new-org") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestServiceAuthUpdate(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/service_auth_tokens/mysql-core-guid", Matcher: testnet.RequestBodyMatcher(`{"token":"a value"}`), Response: testnet.TestResponse{Status: http.StatusOK}, }) ts, handler, repo := createServiceAuthTokenRepo(t, req) defer ts.Close() apiResponse := repo.Update(cf.ServiceAuthToken{Guid: "mysql-core-guid", Token: "a value"}) assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func TestRenameServiceBroker(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/service_brokers/my-guid", Matcher: testnet.RequestBodyMatcher(`{"name":"update-foobroker"}`), Response: testnet.TestResponse{Status: http.StatusOK}, }) ts, handler, repo := createServiceBrokerRepo(t, req) defer ts.Close() apiResponse := repo.Rename("my-guid", "update-foobroker") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func TestServiceAuthCreate(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_auth_tokens", Matcher: testnet.RequestBodyMatcher(`{"label":"a label","provider":"a provider","token":"a token"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createServiceAuthTokenRepo(t, req) defer ts.Close() apiResponse := repo.Create(cf.ServiceAuthToken{Label: "a label", Provider: "a provider", Token: "a token"}) assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func testScale(t *testing.T, app cf.Application, expectedBody string) { scaleApplicationRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/apps/my-app-guid", Matcher: testnet.RequestBodyMatcher(expectedBody), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{scaleApplicationRequest}) defer ts.Close() apiResponse := repo.Scale(app) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestCreateServiceInstance(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_instances", Matcher: testnet.RequestBodyMatcher(`{"name":"instance-name","service_plan_guid":"plan-guid","space_guid":"my-space-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createServiceRepo(t, []testnet.TestRequest{req}) defer ts.Close() identicalAlreadyExists, apiResponse := repo.CreateServiceInstance("instance-name", "plan-guid") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) assert.Equal(t, identicalAlreadyExists, false) }
func TestSetEnv(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/apps/app1-guid", Matcher: testnet.RequestBodyMatcher(`{"environment_json":{"DATABASE_URL":"mysql://example.com/my-db"}}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{request}) defer ts.Close() app := cf.Application{Guid: "app1-guid", Name: "App1"} apiResponse := repo.SetEnv(app, map[string]string{"DATABASE_URL": "mysql://example.com/my-db"}) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestCreateServiceBroker(t *testing.T) { expectedReqBody := `{"name":"foobroker","broker_url":"http://example.com","auth_username":"******","auth_password":"******"}` req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_brokers", Matcher: testnet.RequestBodyMatcher(expectedReqBody), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createServiceBrokerRepo(t, req) defer ts.Close() apiResponse := repo.Create("foobroker", "http://example.com", "foouser", "password") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func TestUpdatePassword(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/Users/my-user-guid/password", Matcher: testnet.RequestBodyMatcher(`{"password":"******","oldPassword":"******"}`), Response: testnet.TestResponse{Status: http.StatusOK}, }) accessToken, err := testconfig.CreateAccessTokenWithTokenInfo(configuration.TokenInfo{UserGuid: "my-user-guid"}) assert.NoError(t, err) passwordUpdateServer, handler, repo := createPasswordRepo(t, req, accessToken) defer passwordUpdateServer.Close() apiResponse := repo.UpdatePassword("old-password", "new-password") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestUpdateApplicationSetCommandToNull(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/apps/my-app-guid", Matcher: testnet.RequestBodyMatcher(`{"command":""}`), Response: testnet.TestResponse{Status: http.StatusOK, Body: updateApplicationResponse}, }) ts, handler, repo := createAppRepo(t, []testnet.TestRequest{request}) defer ts.Close() app := cf.NewEmptyAppParams() app.Set("command", "") _, apiResponse := repo.Update("my-app-guid", app) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestShareDomain(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/domains", Matcher: testnet.RequestBodyMatcher(`{"name":"example.com","wildcard":true}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "abc-123" }, "entity": { "name": "example.com" } }`}, }) ts, handler, repo := createDomainRepo(t, []testnet.TestRequest{req}) defer ts.Close() apiResponse := repo.CreateSharedDomain("example.com") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsSuccessful()) }
func TestCreateUserProvidedServiceInstanceWithSyslogDrain(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/user_provided_service_instances", Matcher: testnet.RequestBodyMatcher(`{"name":"my-custom-service","credentials":{"host":"example.com","password":"******","user":"******"},"space_guid":"my-space-guid","syslog_drain_url":"syslog://example.com"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createUserProvidedServiceInstanceRepo(t, req) defer ts.Close() apiResponse := repo.Create("my-custom-service", "syslog://example.com", map[string]string{ "host": "example.com", "user": "******", "password": "******", }) assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) }
func TestCreateServiceInstanceWhenDifferentServiceAlreadyExists(t *testing.T) { errorReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_instances", Matcher: testnet.RequestBodyMatcher(`{"name":"my-service","service_plan_guid":"different-plan-guid","space_guid":"my-space-guid"}`), Response: testnet.TestResponse{ Status: http.StatusBadRequest, Body: `{"code":60002,"description":"The service instance name is taken: my-service"}`, }, }) ts, handler, repo := createServiceRepo(t, []testnet.TestRequest{errorReq, findServiceInstanceReq}) defer ts.Close() identicalAlreadyExists, apiResponse := repo.CreateServiceInstance("my-service", "different-plan-guid") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsNotSuccessful()) assert.Equal(t, identicalAlreadyExists, false) }
func TestCreateServiceBindingIfError(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_bindings", Matcher: testnet.RequestBodyMatcher(`{"app_guid":"my-app-guid","service_instance_guid":"my-service-instance-guid"}`), Response: testnet.TestResponse{ Status: http.StatusBadRequest, Body: `{"code":90003,"description":"The app space binding to service is taken: 7b959018-110a-4913-ac0a-d663e613cdea 346bf237-7eef-41a7-b892-68fb08068f09"}`, }, }) ts, handler, repo := createServiceBindingRepo(t, []testnet.TestRequest{req}) defer ts.Close() apiResponse := repo.Create("my-service-instance-guid", "my-app-guid") assert.True(t, handler.AllRequestsCalled()) assert.True(t, apiResponse.IsNotSuccessful()) assert.Equal(t, apiResponse.ErrorCode, "90003") }
func TestCreateDomain(t *testing.T) { req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/domains", Matcher: testnet.RequestBodyMatcher(`{"name":"example.com","owning_organization_guid":"org-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: `{ "metadata": { "guid": "abc-123" }, "entity": { "name": "example.com" } }`}, }) ts, handler, repo := createDomainRepo(t, []testnet.TestRequest{req}) defer ts.Close() createdDomain, apiResponse := repo.Create("example.com", "org-guid") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) assert.Equal(t, createdDomain.Guid, "abc-123") }
func TestCreateRoute(t *testing.T) { request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/routes?inline-relations-depth=1", Matcher: testnet.RequestBodyMatcher(`{"host":"my-cool-app","domain_guid":"my-domain-guid","space_guid":"my-space-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "my-route-guid" }, "entity": { "host": "my-cool-app" } }`}, }) ts, handler, repo, _ := createRoutesRepo(t, request) defer ts.Close() createdRoute, apiResponse := repo.Create("my-cool-app", "my-domain-guid") assert.True(t, handler.AllRequestsCalled()) assert.False(t, apiResponse.IsNotSuccessful()) assert.Equal(t, createdRoute.Guid, "my-route-guid") }