github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/organizations/organizations_test.go (about) 1 package organizations_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "time" 7 8 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 9 "github.com/cloudfoundry/cli/cf/errors" 10 "github.com/cloudfoundry/cli/cf/models" 11 "github.com/cloudfoundry/cli/cf/net" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testnet "github.com/cloudfoundry/cli/testhelpers/net" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 16 . "github.com/cloudfoundry/cli/cf/api/organizations" 17 . "github.com/cloudfoundry/cli/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("Organization Repository", func() { 23 Describe("listing organizations", func() { 24 It("lists the orgs from the the /v2/orgs endpoint", func() { 25 firstPageOrgsRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 26 Method: "GET", 27 Path: "/v2/organizations", 28 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{ 29 "next_url": "/v2/organizations?page=2", 30 "resources": [ 31 { 32 "metadata": { "guid": "org1-guid" }, 33 "entity": { "name": "Org1" } 34 }, 35 { 36 "metadata": { "guid": "org2-guid" }, 37 "entity": { "name": "Org2" } 38 } 39 ]}`}, 40 }) 41 42 secondPageOrgsRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 43 Method: "GET", 44 Path: "/v2/organizations?page=2", 45 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{"resources": [ 46 { 47 "metadata": { "guid": "org3-guid" }, 48 "entity": { "name": "Org3" } 49 } 50 ]}`}, 51 }) 52 53 testserver, handler, repo := createOrganizationRepo(firstPageOrgsRequest, secondPageOrgsRequest) 54 defer testserver.Close() 55 56 orgs := []models.Organization{} 57 orgs, apiErr := repo.ListOrgs() 58 59 Expect(len(orgs)).To(Equal(3)) 60 Expect(orgs[0].Guid).To(Equal("org1-guid")) 61 Expect(orgs[1].Guid).To(Equal("org2-guid")) 62 Expect(orgs[2].Guid).To(Equal("org3-guid")) 63 Expect(apiErr).NotTo(HaveOccurred()) 64 Expect(handler).To(HaveAllRequestsCalled()) 65 }) 66 67 It("does not call the provided function when there are no orgs found", func() { 68 emptyOrgsRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 69 Method: "GET", 70 Path: "/v2/organizations", 71 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{"resources": []}`}, 72 }) 73 74 testserver, handler, repo := createOrganizationRepo(emptyOrgsRequest) 75 defer testserver.Close() 76 77 _, apiErr := repo.ListOrgs() 78 79 Expect(apiErr).NotTo(HaveOccurred()) 80 Expect(handler).To(HaveAllRequestsCalled()) 81 }) 82 }) 83 84 Describe("finding organizations by name", func() { 85 It("returns the org with that name", func() { 86 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 87 Method: "GET", 88 Path: "/v2/organizations?q=name%3Aorg1&inline-relations-depth=1", 89 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{"resources": [{ 90 "metadata": { "guid": "org1-guid" }, 91 "entity": { 92 "name": "Org1", 93 "quota_definition": { 94 "entity": { 95 "name": "not-your-average-quota", 96 "memory_limit": 128 97 } 98 }, 99 "spaces": [{ 100 "metadata": { "guid": "space1-guid" }, 101 "entity": { "name": "Space1" } 102 }], 103 "domains": [{ 104 "metadata": { "guid": "domain1-guid" }, 105 "entity": { "name": "cfapps.io" } 106 }], 107 "space_quota_definitions":[{ 108 "metadata": {"guid": "space-quota1-guid"}, 109 "entity": {"name": "space-quota1"} 110 }] 111 } 112 }]}`}, 113 }) 114 115 testserver, handler, repo := createOrganizationRepo(req) 116 defer testserver.Close() 117 existingOrg := models.Organization{} 118 existingOrg.Guid = "org1-guid" 119 existingOrg.Name = "Org1" 120 121 org, apiErr := repo.FindByName("Org1") 122 Expect(handler).To(HaveAllRequestsCalled()) 123 Expect(apiErr).NotTo(HaveOccurred()) 124 125 Expect(org.Name).To(Equal(existingOrg.Name)) 126 Expect(org.Guid).To(Equal(existingOrg.Guid)) 127 Expect(org.QuotaDefinition.Name).To(Equal("not-your-average-quota")) 128 Expect(org.QuotaDefinition.MemoryLimit).To(Equal(int64(128))) 129 Expect(len(org.Spaces)).To(Equal(1)) 130 Expect(org.Spaces[0].Name).To(Equal("Space1")) 131 Expect(org.Spaces[0].Guid).To(Equal("space1-guid")) 132 Expect(len(org.Domains)).To(Equal(1)) 133 Expect(org.Domains[0].Name).To(Equal("cfapps.io")) 134 Expect(org.Domains[0].Guid).To(Equal("domain1-guid")) 135 Expect(len(org.SpaceQuotas)).To(Equal(1)) 136 Expect(org.SpaceQuotas[0].Name).To(Equal("space-quota1")) 137 Expect(org.SpaceQuotas[0].Guid).To(Equal("space-quota1-guid")) 138 }) 139 140 It("returns a ModelNotFoundError when the org cannot be found", func() { 141 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 142 Method: "GET", 143 Path: "/v2/organizations?q=name%3Aorg1&inline-relations-depth=1", 144 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{"resources": []}`}, 145 }) 146 147 testserver, handler, repo := createOrganizationRepo(req) 148 defer testserver.Close() 149 150 _, apiErr := repo.FindByName("org1") 151 Expect(handler).To(HaveAllRequestsCalled()) 152 Expect(apiErr.(*errors.ModelNotFoundError)).NotTo(BeNil()) 153 }) 154 155 It("returns an api error when the response is not successful", func() { 156 requestHandler := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 157 Method: "GET", 158 Path: "/v2/organizations?q=name%3Aorg1&inline-relations-depth=1", 159 Response: testnet.TestResponse{Status: http.StatusBadGateway, Body: `{"resources": []}`}, 160 }) 161 162 testserver, handler, repo := createOrganizationRepo(requestHandler) 163 defer testserver.Close() 164 165 _, apiErr := repo.FindByName("org1") 166 _, ok := apiErr.(*errors.ModelNotFoundError) 167 Expect(ok).To(BeFalse()) 168 Expect(handler).To(HaveAllRequestsCalled()) 169 }) 170 }) 171 172 Describe(".Create", func() { 173 It("creates the org and sends only the org name if the quota flag is not provided", func() { 174 org := models.Organization{ 175 OrganizationFields: models.OrganizationFields{ 176 Name: "my-org", 177 }} 178 179 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 180 Method: "POST", 181 Path: "/v2/organizations", 182 Matcher: testnet.RequestBodyMatcher(`{"name":"my-org"}`), 183 Response: testnet.TestResponse{Status: http.StatusCreated}, 184 }) 185 186 testserver, handler, repo := createOrganizationRepo(req) 187 defer testserver.Close() 188 189 apiErr := repo.Create(org) 190 Expect(handler).To(HaveAllRequestsCalled()) 191 Expect(apiErr).NotTo(HaveOccurred()) 192 }) 193 194 It("creates the org with the provided quota", func() { 195 org := models.Organization{ 196 OrganizationFields: models.OrganizationFields{ 197 Name: "my-org", 198 QuotaDefinition: models.QuotaFields{ 199 Guid: "my-quota-guid", 200 }, 201 }} 202 203 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 204 Method: "POST", 205 Path: "/v2/organizations", 206 Matcher: testnet.RequestBodyMatcher(`{"name":"my-org", "quota_definition_guid":"my-quota-guid"}`), 207 Response: testnet.TestResponse{Status: http.StatusCreated}, 208 }) 209 210 testserver, handler, repo := createOrganizationRepo(req) 211 defer testserver.Close() 212 213 apiErr := repo.Create(org) 214 Expect(handler).To(HaveAllRequestsCalled()) 215 Expect(apiErr).NotTo(HaveOccurred()) 216 }) 217 }) 218 219 Describe("renaming orgs", func() { 220 It("renames the org with the given guid", func() { 221 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 222 Method: "PUT", 223 Path: "/v2/organizations/my-org-guid", 224 Matcher: testnet.RequestBodyMatcher(`{"name":"my-new-org"}`), 225 Response: testnet.TestResponse{Status: http.StatusCreated}, 226 }) 227 228 testserver, handler, repo := createOrganizationRepo(req) 229 defer testserver.Close() 230 231 apiErr := repo.Rename("my-org-guid", "my-new-org") 232 Expect(handler).To(HaveAllRequestsCalled()) 233 Expect(apiErr).NotTo(HaveOccurred()) 234 }) 235 }) 236 237 Describe("deleting orgs", func() { 238 It("deletes the org with the given guid", func() { 239 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 240 Method: "DELETE", 241 Path: "/v2/organizations/my-org-guid?recursive=true", 242 Response: testnet.TestResponse{Status: http.StatusOK}, 243 }) 244 245 testserver, handler, repo := createOrganizationRepo(req) 246 defer testserver.Close() 247 248 apiErr := repo.Delete("my-org-guid") 249 Expect(handler).To(HaveAllRequestsCalled()) 250 Expect(apiErr).NotTo(HaveOccurred()) 251 }) 252 }) 253 }) 254 255 func createOrganizationRepo(reqs ...testnet.TestRequest) (testserver *httptest.Server, handler *testnet.TestHandler, repo OrganizationRepository) { 256 testserver, handler = testnet.NewServer(reqs) 257 258 configRepo := testconfig.NewRepositoryWithDefaults() 259 configRepo.SetApiEndpoint(testserver.URL) 260 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 261 repo = NewCloudControllerOrganizationRepository(configRepo, gateway) 262 return 263 }