testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" testnet "code.cloudfoundry.org/cli/testhelpers/net" . "code.cloudfoundry.org/cli/cf/api/password" "code.cloudfoundry.org/cli/cf/trace/tracefakes" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("CloudControllerPasswordRepository", func() { It("updates your password", func() { req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/Users/my-user-guid/password", Matcher: testnet.RequestBodyMatcher(`{"password":"******","oldPassword":"******"}`), Response: testnet.TestResponse{Status: http.StatusOK}, }) passwordUpdateServer, handler, repo := createPasswordRepo(req) defer passwordUpdateServer.Close() apiErr := repo.UpdatePassword("old-password", "new-password") Expect(handler).To(HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred()) }) }) func createPasswordRepo(req testnet.TestRequest) (passwordServer *httptest.Server, handler *testnet.TestHandler, repo Repository) { passwordServer, handler = testnet.NewServer([]testnet.TestRequest{req})
BeforeEach(func() { configRepo = testconfig.NewRepositoryWithDefaults() gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") repo = NewCloudControllerCopyApplicationSourceRepository(configRepo, gateway) }) AfterEach(func() { testServer.Close() }) Describe(".CopyApplication", func() { BeforeEach(func() { setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/apps/target-app-guid/copy_bits", Matcher: testnet.RequestBodyMatcher(`{ "source_app_guid": "source-app-guid" }`), Response: testnet.TestResponse{ Status: http.StatusCreated, }, })) }) It("should return a CopyApplicationModel", func() { err := repo.CopyApplication("source-app-guid", "target-app-guid") Expect(err).ToNot(HaveOccurred()) }) }) })
configRepo = testconfig.NewRepositoryWithDefaults() gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") repo = NewCloudControllerServiceAuthTokenRepository(configRepo, gateway) }) AfterEach(func() { testServer.Close() }) Describe("Create", func() { It("creates a service auth token", func() { setupTestServer(apifakes.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}, })) err := repo.Create(models.ServiceAuthTokenFields{ Label: "a label", Provider: "a provider", Token: "a token", }) Expect(testHandler).To(HaveAllRequestsCalled()) Expect(err).NotTo(HaveOccurred()) }) }) Describe("FindAll", func() {
Expect(err).NotTo(HaveOccurred()) Expect(testHandler).To(HaveAllRequestsCalled()) Expect(len(services)).To(Equal(2)) Expect(services[0].GUID).To(Equal("my-service-guid")) Expect(services[1].GUID).To(Equal("my-service-guid2")) }) }) Describe("creating a service instance", func() { It("makes the right request", func() { setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_instances?accepts_incomplete=true", Matcher: testnet.RequestBodyMatcher(`{"name":"instance-name","service_plan_guid":"plan-guid","space_guid":"my-space-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, })) err := repo.CreateServiceInstance("instance-name", "plan-guid", nil, nil) Expect(testHandler).To(HaveAllRequestsCalled()) Expect(err).NotTo(HaveOccurred()) }) Context("when there are parameters", func() { It("sends the parameters as part of the request body", func() { setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_instances?accepts_incomplete=true", Matcher: testnet.RequestBodyMatcher(`{"name":"instance-name","service_plan_guid":"plan-guid","space_guid":"my-space-guid","parameters": {"data": "hello"}}`), Response: testnet.TestResponse{Status: http.StatusCreated},
}) It("returns a 'not found' response when the space doesn't exist in the given org", func() { testSpacesDidNotFindByNameWithOrg("another-org-guid", func(repo SpaceRepository, spaceName string) (models.Space, error) { return repo.FindByNameInOrg(spaceName, "another-org-guid") }, ) }) }) It("creates spaces without a space-quota", func() { request := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/spaces", Matcher: testnet.RequestBodyMatcher(`{"name":"space-name","organization_guid":"my-org-guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "space-guid" }, "entity": { "name": "space-name" } }`}, }) ts, handler, repo := createSpacesRepo(request) defer ts.Close() space, apiErr := repo.Create("space-name", "my-org-guid", "")
) err := repo.Create("foobroker", "http://example.com", "foouser", "password", "space-guid") Expect(err).NotTo(HaveOccurred()) Expect(ccServer.ReceivedRequests()).To(HaveLen(1)) }) }) Describe("Update", func() { It("updates the service broker with the given guid", func() { expectedReqBody := `{"broker_url":"http://update.example.com","auth_username":"******","auth_password":"******"}` req := apifakes.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(req) defer ts.Close() serviceBroker := models.ServiceBroker{} serviceBroker.GUID = "my-guid" serviceBroker.Name = "foobroker" serviceBroker.URL = "http://update.example.com" serviceBroker.Username = "******" serviceBroker.Password = "******" apiErr := repo.Update(serviceBroker) Expect(handler).To(HaveAllRequestsCalled())
AfterEach(func() { testServer.Close() }) setupTestServer := func(reqs ...testnet.TestRequest) { testServer, testHandler = testnet.NewServer(reqs) configRepo.SetAPIEndpoint(testServer.URL) } Describe(".Create", func() { BeforeEach(func() { setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/service_plan_visibilities", Matcher: testnet.RequestBodyMatcher(`{"service_plan_guid":"service_plan_guid", "organization_guid":"org_guid"}`), Response: testnet.TestResponse{Status: http.StatusCreated}, })) }) It("creates a service plan visibility", func() { err := repo.Create("service_plan_guid", "org_guid") Expect(testHandler).To(HaveAllRequestsCalled()) Expect(err).NotTo(HaveOccurred()) }) }) Describe(".List", func() { BeforeEach(func() { setupTestServer(firstPlanVisibilityRequest, secondPlanVisibilityRequest)
. "code.cloudfoundry.org/cli/cf/api" "code.cloudfoundry.org/cli/cf/trace/tracefakes" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("UserProvidedServiceRepository", func() { Context("Create()", func() { It("creates a user provided service with a name and credentials", func() { req := apifakes.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":"","route_service_url":""}`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req}) defer ts.Close() apiErr := repo.Create("my-custom-service", "", "", map[string]interface{}{ "host": "example.com", "user": "******", "password": "******", }) Expect(handler).To(HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred()) })
`}, }) } var matchResourceRequest = testnet.TestRequest{ Method: "PUT", Path: "/v2/resource_match", Matcher: testnet.RequestBodyMatcher(testnet.RemoveWhiteSpaceFromBody(`[ { "sha1": "2474735f5163ba7612ef641f438f4b5bee00127b", "size": 51 }, { "sha1": "f097424ce1fa66c6cb9f5e8a18c317376ec12e05", "size": 70 }, { "sha1": "d9c3a51de5c89c11331d3b90b972789f1a14699a", "size": 59 }, { "sha1": "345f999aef9070fb9a608e65cf221b7038156b6d", "size": 229 } ]`)), Response: testnet.TestResponse{ Status: http.StatusOK, Body: matchedResources, }, } var matchResourceRequestImbalanced = testnet.TestRequest{
testServer.Close() }) setupTestServer := func(reqs ...testnet.TestRequest) { testServer, testHandler = testnet.NewServer(reqs) configRepo.SetAPIEndpoint(testServer.URL) } Describe(".Create", func() { It("can create an app security group, given some attributes", func() { req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/security_groups", // FIXME: this matcher depend on the order of the key/value pairs in the map Matcher: testnet.RequestBodyMatcher(`{ "name": "mygroup", "rules": [{"my-house": "my-rules"}] }`), Response: testnet.TestResponse{Status: http.StatusCreated}, }) setupTestServer(req) err := repo.Create( "mygroup", []map[string]interface{}{{"my-house": "my-rules"}}, ) Expect(err).NotTo(HaveOccurred()) Expect(testHandler).To(HaveAllRequestsCalled()) }) })
Expect(servicePlansFields[0].Active).To(BeTrue()) Expect(servicePlansFields[1].Name).To(Equal("The small second")) Expect(servicePlansFields[1].GUID).To(Equal("the-small-second")) Expect(servicePlansFields[1].Free).To(BeTrue()) Expect(servicePlansFields[1].Public).To(BeFalse()) Expect(servicePlansFields[1].Active).To(BeFalse()) }) }) }) Describe(".Update", func() { BeforeEach(func() { setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "PUT", Path: "/v2/service_plans/my-service-plan-guid", Matcher: testnet.RequestBodyMatcher(`{"public":true}`), Response: testnet.TestResponse{Status: http.StatusCreated}, })) }) It("updates public on the service to whatever is passed", func() { servicePlan := models.ServicePlanFields{ Name: "my-service-plan", GUID: "my-service-plan-guid", Description: "descriptive text", Free: true, Public: false, } err := repo.Update(servicePlan, "service-guid", true) Expect(testHandler).To(HaveAllRequestsCalled())
}`}})) _, apiErr := repo.FindByNameInOrg("domain2.cf-app.com", "my-org-guid") Expect(handler).To(HaveAllRequestsCalled()) Expect(apiErr.(*errors.ModelNotFoundError)).NotTo(BeNil()) }) }) Describe("creating domains", func() { Context("when the private domains endpoint is not available", func() { BeforeEach(func() { setupTestServer( apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/domains", Matcher: testnet.RequestBodyMatcher(`{"name":"example.com","owning_organization_guid":"org-guid", "wildcard": true}`), Response: testnet.TestResponse{Status: http.StatusCreated, Body: ` { "metadata": { "guid": "abc-123" }, "entity": { "name": "example.com" } }`}, }), ) }) It("uses the general domains endpoint", func() { createdDomain, apiErr := repo.Create("example.com", "org-guid") Expect(handler).To(HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred()) Expect(createdDomain.GUID).To(Equal("abc-123"))
updatedApp, apiErr := repo.Update(app.GUID, app.ToParams()) Expect(handler).To(HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred()) Expect(updatedApp.Command).To(Equal("some-command")) Expect(updatedApp.DetectedStartCommand).To(Equal("detected command")) Expect(updatedApp.Name).To(Equal("my-cool-app")) Expect(updatedApp.GUID).To(Equal("my-cool-app-guid")) }) It("sets environment variables", func() { request := apifakes.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([]testnet.TestRequest{request}) defer ts.Close() envParams := map[string]interface{}{"DATABASE_URL": "mysql://example.com/my-db"} params := models.AppParams{EnvironmentVars: &envParams} _, apiErr := repo.Update("app1-guid", params) Expect(handler).To(HaveAllRequestsCalled()) Expect(apiErr).NotTo(HaveOccurred()) })
}`, }}) one := 1 createdBuildpack, apiErr := repo.Create("name with space", &one, nil, nil) Expect(apiErr).To(HaveOccurred()) Expect(createdBuildpack).To(Equal(models.Buildpack{})) Expect(apiErr.(errors.HTTPError).ErrorCode()).To(Equal("290003")) Expect(apiErr.Error()).To(ContainSubstring("Buildpack is invalid")) }) It("sets the position flag when creating a buildpack", func() { setupTestServer(apifakes.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 } }`}, })) position := 999 created, apiErr := repo.Create("my-cool-buildpack", &position, nil, nil)
It("returns the header content type", func() { Expect(headers).To(ContainSubstring("Content-Type")) Expect(headers).To(ContainSubstring("text/plain")) }) It("returns the body as a JSON-encoded string", func() { Expect(removeWhitespace(body)).To(Equal(removeWhitespace(expectedJSONResponse))) }) }) Describe("POST requests", func() { BeforeEach(func() { req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ Method: "POST", Path: "/v2/endpoint", Matcher: testnet.RequestBodyMatcher(`{"key":"val"}`), 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("POST", "/v2/endpoint", "", `{"key":"val"}`) Expect(handler).To(HaveAllRequestsCalled()) })