github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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(".GetManyOrgsByGuid", func() { 85 It("requests each org", func() { 86 firstOrgRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 87 Method: "GET", 88 Path: "/v2/organizations/org1-guid", 89 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{ 90 "metadata": { "guid": "org1-guid" }, 91 "entity": { "name": "Org1" } 92 }`}, 93 }) 94 secondOrgRequest := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 95 Method: "GET", 96 Path: "/v2/organizations/org2-guid", 97 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{ 98 "metadata": { "guid": "org2-guid" }, 99 "entity": { "name": "Org2" } 100 }`}, 101 }) 102 testserver, handler, repo := createOrganizationRepo(firstOrgRequest, secondOrgRequest) 103 defer testserver.Close() 104 105 orgGuids := []string{"org1-guid", "org2-guid"} 106 orgs, err := repo.GetManyOrgsByGuid(orgGuids) 107 Expect(err).NotTo(HaveOccurred()) 108 109 Expect(handler).To(HaveAllRequestsCalled()) 110 Expect(len(orgs)).To(Equal(2)) 111 Expect(orgs[0].Guid).To(Equal("org1-guid")) 112 Expect(orgs[1].Guid).To(Equal("org2-guid")) 113 }) 114 }) 115 116 Describe("finding organizations by name", func() { 117 It("returns the org with that name", func() { 118 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 119 Method: "GET", 120 Path: "/v2/organizations?q=name%3Aorg1&inline-relations-depth=1", 121 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{"resources": [{ 122 "metadata": { "guid": "org1-guid" }, 123 "entity": { 124 "name": "Org1", 125 "quota_definition": { 126 "entity": { 127 "name": "not-your-average-quota", 128 "memory_limit": 128 129 } 130 }, 131 "spaces": [{ 132 "metadata": { "guid": "space1-guid" }, 133 "entity": { "name": "Space1" } 134 }], 135 "domains": [{ 136 "metadata": { "guid": "domain1-guid" }, 137 "entity": { "name": "cfapps.io" } 138 }], 139 "space_quota_definitions":[{ 140 "metadata": {"guid": "space-quota1-guid"}, 141 "entity": {"name": "space-quota1"} 142 }] 143 } 144 }]}`}, 145 }) 146 147 testserver, handler, repo := createOrganizationRepo(req) 148 defer testserver.Close() 149 existingOrg := models.Organization{} 150 existingOrg.Guid = "org1-guid" 151 existingOrg.Name = "Org1" 152 153 org, apiErr := repo.FindByName("Org1") 154 Expect(handler).To(HaveAllRequestsCalled()) 155 Expect(apiErr).NotTo(HaveOccurred()) 156 157 Expect(org.Name).To(Equal(existingOrg.Name)) 158 Expect(org.Guid).To(Equal(existingOrg.Guid)) 159 Expect(org.QuotaDefinition.Name).To(Equal("not-your-average-quota")) 160 Expect(org.QuotaDefinition.MemoryLimit).To(Equal(int64(128))) 161 Expect(len(org.Spaces)).To(Equal(1)) 162 Expect(org.Spaces[0].Name).To(Equal("Space1")) 163 Expect(org.Spaces[0].Guid).To(Equal("space1-guid")) 164 Expect(len(org.Domains)).To(Equal(1)) 165 Expect(org.Domains[0].Name).To(Equal("cfapps.io")) 166 Expect(org.Domains[0].Guid).To(Equal("domain1-guid")) 167 Expect(len(org.SpaceQuotas)).To(Equal(1)) 168 Expect(org.SpaceQuotas[0].Name).To(Equal("space-quota1")) 169 Expect(org.SpaceQuotas[0].Guid).To(Equal("space-quota1-guid")) 170 }) 171 172 It("returns a ModelNotFoundError when the org cannot be found", func() { 173 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 174 Method: "GET", 175 Path: "/v2/organizations?q=name%3Aorg1&inline-relations-depth=1", 176 Response: testnet.TestResponse{Status: http.StatusOK, Body: `{"resources": []}`}, 177 }) 178 179 testserver, handler, repo := createOrganizationRepo(req) 180 defer testserver.Close() 181 182 _, apiErr := repo.FindByName("org1") 183 Expect(handler).To(HaveAllRequestsCalled()) 184 Expect(apiErr.(*errors.ModelNotFoundError)).NotTo(BeNil()) 185 }) 186 187 It("returns an api error when the response is not successful", func() { 188 requestHandler := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 189 Method: "GET", 190 Path: "/v2/organizations?q=name%3Aorg1&inline-relations-depth=1", 191 Response: testnet.TestResponse{Status: http.StatusBadGateway, Body: `{"resources": []}`}, 192 }) 193 194 testserver, handler, repo := createOrganizationRepo(requestHandler) 195 defer testserver.Close() 196 197 _, apiErr := repo.FindByName("org1") 198 _, ok := apiErr.(*errors.ModelNotFoundError) 199 Expect(ok).To(BeFalse()) 200 Expect(handler).To(HaveAllRequestsCalled()) 201 }) 202 }) 203 204 Describe(".Create", func() { 205 It("creates the org and sends only the org name if the quota flag is not provided", func() { 206 org := models.Organization{ 207 OrganizationFields: models.OrganizationFields{ 208 Name: "my-org", 209 }} 210 211 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 212 Method: "POST", 213 Path: "/v2/organizations", 214 Matcher: testnet.RequestBodyMatcher(`{"name":"my-org"}`), 215 Response: testnet.TestResponse{Status: http.StatusCreated}, 216 }) 217 218 testserver, handler, repo := createOrganizationRepo(req) 219 defer testserver.Close() 220 221 apiErr := repo.Create(org) 222 Expect(handler).To(HaveAllRequestsCalled()) 223 Expect(apiErr).NotTo(HaveOccurred()) 224 }) 225 226 It("creates the org with the provided quota", func() { 227 org := models.Organization{ 228 OrganizationFields: models.OrganizationFields{ 229 Name: "my-org", 230 QuotaDefinition: models.QuotaFields{ 231 Guid: "my-quota-guid", 232 }, 233 }} 234 235 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 236 Method: "POST", 237 Path: "/v2/organizations", 238 Matcher: testnet.RequestBodyMatcher(`{"name":"my-org", "quota_definition_guid":"my-quota-guid"}`), 239 Response: testnet.TestResponse{Status: http.StatusCreated}, 240 }) 241 242 testserver, handler, repo := createOrganizationRepo(req) 243 defer testserver.Close() 244 245 apiErr := repo.Create(org) 246 Expect(handler).To(HaveAllRequestsCalled()) 247 Expect(apiErr).NotTo(HaveOccurred()) 248 }) 249 }) 250 251 Describe("renaming orgs", func() { 252 It("renames the org with the given guid", func() { 253 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 254 Method: "PUT", 255 Path: "/v2/organizations/my-org-guid", 256 Matcher: testnet.RequestBodyMatcher(`{"name":"my-new-org"}`), 257 Response: testnet.TestResponse{Status: http.StatusCreated}, 258 }) 259 260 testserver, handler, repo := createOrganizationRepo(req) 261 defer testserver.Close() 262 263 apiErr := repo.Rename("my-org-guid", "my-new-org") 264 Expect(handler).To(HaveAllRequestsCalled()) 265 Expect(apiErr).NotTo(HaveOccurred()) 266 }) 267 }) 268 269 Describe("deleting orgs", func() { 270 It("deletes the org with the given guid", func() { 271 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 272 Method: "DELETE", 273 Path: "/v2/organizations/my-org-guid?recursive=true", 274 Response: testnet.TestResponse{Status: http.StatusOK}, 275 }) 276 277 testserver, handler, repo := createOrganizationRepo(req) 278 defer testserver.Close() 279 280 apiErr := repo.Delete("my-org-guid") 281 Expect(handler).To(HaveAllRequestsCalled()) 282 Expect(apiErr).NotTo(HaveOccurred()) 283 }) 284 }) 285 286 Describe("SharePrivateDomain", func() { 287 It("shares the private domain with the given org", func() { 288 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 289 Method: "PUT", 290 Path: "/v2/organizations/my-org-guid/private_domains/domain-guid", 291 Response: testnet.TestResponse{Status: http.StatusOK}, 292 }) 293 294 testserver, handler, repo := createOrganizationRepo(req) 295 defer testserver.Close() 296 297 apiErr := repo.SharePrivateDomain("my-org-guid", "domain-guid") 298 Expect(handler).To(HaveAllRequestsCalled()) 299 Expect(apiErr).NotTo(HaveOccurred()) 300 }) 301 }) 302 303 Describe("UnsharePrivateDomain", func() { 304 It("unshares the private domain with the given org", func() { 305 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 306 Method: "DELETE", 307 Path: "/v2/organizations/my-org-guid/private_domains/domain-guid", 308 Response: testnet.TestResponse{Status: http.StatusOK}, 309 }) 310 311 testserver, handler, repo := createOrganizationRepo(req) 312 defer testserver.Close() 313 314 apiErr := repo.UnsharePrivateDomain("my-org-guid", "domain-guid") 315 Expect(handler).To(HaveAllRequestsCalled()) 316 Expect(apiErr).NotTo(HaveOccurred()) 317 }) 318 }) 319 }) 320 321 func createOrganizationRepo(reqs ...testnet.TestRequest) (testserver *httptest.Server, handler *testnet.TestHandler, repo OrganizationRepository) { 322 testserver, handler = testnet.NewServer(reqs) 323 324 configRepo := testconfig.NewRepositoryWithDefaults() 325 configRepo.SetApiEndpoint(testserver.URL) 326 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 327 repo = NewCloudControllerOrganizationRepository(configRepo, gateway) 328 return 329 }