github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/security_groups/security_groups_test.go (about) 1 package security_groups_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/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/errors" 11 "github.com/cloudfoundry/cli/cf/models" 12 "github.com/cloudfoundry/cli/cf/net" 13 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 14 testnet "github.com/cloudfoundry/cli/testhelpers/net" 15 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 16 17 . "github.com/cloudfoundry/cli/cf/api/security_groups" 18 . "github.com/cloudfoundry/cli/testhelpers/matchers" 19 . "github.com/onsi/ginkgo" 20 . "github.com/onsi/gomega" 21 ) 22 23 var _ = Describe("app security group api", func() { 24 var ( 25 testServer *httptest.Server 26 testHandler *testnet.TestHandler 27 configRepo core_config.ReadWriter 28 repo SecurityGroupRepo 29 ) 30 31 BeforeEach(func() { 32 configRepo = testconfig.NewRepositoryWithDefaults() 33 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 34 repo = NewSecurityGroupRepo(configRepo, gateway) 35 }) 36 37 AfterEach(func() { 38 testServer.Close() 39 }) 40 41 setupTestServer := func(reqs ...testnet.TestRequest) { 42 testServer, testHandler = testnet.NewServer(reqs) 43 configRepo.SetApiEndpoint(testServer.URL) 44 } 45 46 Describe(".Create", func() { 47 It("can create an app security group, given some attributes", func() { 48 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 49 Method: "POST", 50 Path: "/v2/security_groups", 51 // FIXME: this matcher depend on the order of the key/value pairs in the map 52 Matcher: testnet.RequestBodyMatcher(`{ 53 "name": "mygroup", 54 "rules": [{"my-house": "my-rules"}] 55 }`), 56 Response: testnet.TestResponse{Status: http.StatusCreated}, 57 }) 58 setupTestServer(req) 59 60 err := repo.Create( 61 "mygroup", 62 []map[string]interface{}{{"my-house": "my-rules"}}, 63 ) 64 65 Expect(err).NotTo(HaveOccurred()) 66 Expect(testHandler).To(HaveAllRequestsCalled()) 67 }) 68 }) 69 70 Describe(".Read", func() { 71 It("returns the app security group with the given name", func() { 72 73 req1 := testnet.TestRequest{ 74 Method: "GET", 75 Path: "/v2/security_groups?q=name:the-name", 76 Response: testnet.TestResponse{ 77 Status: http.StatusOK, 78 Body: ` 79 { 80 "resources": [ 81 { 82 "metadata": { 83 "guid": "the-group-guid" 84 }, 85 "entity": { 86 "name": "the-name", 87 "rules": [{"key": "value"}], 88 "spaces_url": "/v2/security_groups/guid-id/spaces" 89 } 90 } 91 ] 92 } 93 `, 94 }, 95 } 96 97 req2 := testnet.TestRequest{ 98 Method: "GET", 99 Path: "/v2/security_groups/guid-id/spaces?inline-relations-depth=1", 100 Response: testnet.TestResponse{ 101 Status: http.StatusOK, 102 Body: ` 103 { 104 "resources": [ 105 { 106 "metadata":{ 107 "guid": "my-space-guid" 108 }, 109 "entity": { 110 "name": "my-space", 111 "organization": { 112 "metadata": { 113 "guid": "my-org-guid" 114 }, 115 "entity": { 116 "name": "my-org" 117 } 118 } 119 } 120 }, 121 { 122 "metadata":{ 123 "guid": "my-space-guid2" 124 }, 125 "entity": { 126 "name": "my-space2", 127 "organization": { 128 "metadata": { 129 "guid": "my-org-guid2" 130 }, 131 "entity": { 132 "name": "my-org2" 133 } 134 } 135 } 136 } 137 ] 138 } 139 `, 140 }, 141 } 142 143 setupTestServer(testapi.NewCloudControllerTestRequest(req1), testapi.NewCloudControllerTestRequest(req2)) 144 145 group, err := repo.Read("the-name") 146 147 Expect(err).ToNot(HaveOccurred()) 148 Expect(testHandler).To(HaveAllRequestsCalled()) 149 Expect(group).To(Equal(models.SecurityGroup{ 150 SecurityGroupFields: models.SecurityGroupFields{ 151 Name: "the-name", 152 Guid: "the-group-guid", 153 SpaceUrl: "/v2/security_groups/guid-id/spaces", 154 Rules: []map[string]interface{}{{"key": "value"}}, 155 }, 156 Spaces: []models.Space{ 157 { 158 SpaceFields: models.SpaceFields{Guid: "my-space-guid", Name: "my-space"}, 159 Organization: models.OrganizationFields{Guid: "my-org-guid", Name: "my-org"}, 160 }, 161 { 162 SpaceFields: models.SpaceFields{Guid: "my-space-guid2", Name: "my-space2"}, 163 Organization: models.OrganizationFields{Guid: "my-org-guid2", Name: "my-org2"}, 164 }, 165 }, 166 })) 167 }) 168 169 It("returns a ModelNotFound error if the security group cannot be found", func() { 170 setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 171 Method: "GET", 172 Path: "/v2/security_groups?q=name:the-name", 173 Response: testnet.TestResponse{ 174 Status: http.StatusOK, 175 Body: `{"resources": []}`, 176 }, 177 })) 178 179 _, err := repo.Read("the-name") 180 181 Expect(err).To(HaveOccurred()) 182 Expect(err).To(BeAssignableToTypeOf(errors.NewModelNotFoundError("model-type", "description"))) 183 }) 184 }) 185 186 Describe(".Delete", func() { 187 It("deletes the security group", func() { 188 securityGroupGuid := "the-security-group-guid" 189 setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 190 Method: "DELETE", 191 Path: "/v2/security_groups/" + securityGroupGuid, 192 Response: testnet.TestResponse{ 193 Status: http.StatusNoContent, 194 }, 195 })) 196 197 err := repo.Delete(securityGroupGuid) 198 199 Expect(err).ToNot(HaveOccurred()) 200 }) 201 }) 202 203 Describe(".FindAll", func() { 204 It("returns all the security groups", func() { 205 setupTestServer( 206 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 207 Method: "GET", 208 Path: "/v2/security_groups", 209 Response: testnet.TestResponse{ 210 Status: http.StatusOK, 211 Body: firstListItem(), 212 }, 213 }), 214 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 215 Method: "GET", 216 Path: "/v2/security_groups?page=2", 217 Response: testnet.TestResponse{ 218 Status: http.StatusOK, 219 Body: secondListItem(), 220 }, 221 }), 222 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 223 Method: "GET", 224 Path: "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502/spaces?inline-relations-depth=1", 225 Response: testnet.TestResponse{ 226 Status: http.StatusOK, 227 Body: spacesItems(), 228 }, 229 }), 230 testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 231 Method: "GET", 232 Path: "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces?inline-relations-depth=1", 233 Response: testnet.TestResponse{ 234 Status: http.StatusOK, 235 Body: spacesItems(), 236 }, 237 }), 238 ) 239 240 groups, err := repo.FindAll() 241 242 Expect(err).ToNot(HaveOccurred()) 243 Expect(testHandler).To(HaveAllRequestsCalled()) 244 Expect(groups[0]).To(Equal(models.SecurityGroup{ 245 SecurityGroupFields: models.SecurityGroupFields{ 246 Name: "name-71", 247 Guid: "cd186158-b356-474d-9861-724f34f48502", 248 Rules: []map[string]interface{}{{"protocol": "udp"}}, 249 SpaceUrl: "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502/spaces", 250 }, 251 Spaces: []models.Space{ 252 { 253 SpaceFields: models.SpaceFields{Guid: "my-space-guid", Name: "my-space"}, 254 Organization: models.OrganizationFields{Guid: "my-org-guid", Name: "my-org"}, 255 }, 256 }, 257 })) 258 Expect(groups[1]).To(Equal(models.SecurityGroup{ 259 SecurityGroupFields: models.SecurityGroupFields{ 260 Name: "name-72", 261 Guid: "d3374b62-7eac-4823-afbd-460d2bf44c67", 262 Rules: []map[string]interface{}{{"destination": "198.41.191.47/1"}}, 263 SpaceUrl: "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces", 264 }, 265 Spaces: []models.Space{ 266 { 267 SpaceFields: models.SpaceFields{Guid: "my-space-guid", Name: "my-space"}, 268 Organization: models.OrganizationFields{Guid: "my-org-guid", Name: "my-org"}, 269 }, 270 }, 271 })) 272 }) 273 }) 274 }) 275 276 func firstListItem() string { 277 return `{ 278 "next_url": "/v2/security_groups?inline-relations-depth=2&page=2", 279 "resources": [ 280 { 281 "metadata": { 282 "guid": "cd186158-b356-474d-9861-724f34f48502", 283 "url": "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502", 284 "created_at": "2014-06-23T22:55:30+00:00", 285 "updated_at": null 286 }, 287 "entity": { 288 "name": "name-71", 289 "rules": [ 290 { 291 "protocol": "udp" 292 } 293 ], 294 "spaces_url": "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502/spaces" 295 } 296 } 297 ] 298 }` 299 } 300 301 func secondListItem() string { 302 return `{ 303 "next_url": null, 304 "resources": [ 305 { 306 "metadata": { 307 "guid": "d3374b62-7eac-4823-afbd-460d2bf44c67", 308 "url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67", 309 "created_at": "2014-06-23T22:55:30+00:00", 310 "updated_at": null 311 }, 312 "entity": { 313 "name": "name-72", 314 "rules": [ 315 { 316 "destination": "198.41.191.47/1" 317 } 318 ], 319 "spaces": [ 320 { 321 "metadata":{ 322 "guid": "my-space-guid" 323 }, 324 "entity": { 325 "name": "my-space", 326 "organization": { 327 "metadata": { 328 "guid": "my-org-guid" 329 }, 330 "entity": { 331 "name": "my-org" 332 } 333 } 334 } 335 } 336 ], 337 "spaces_url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces" 338 } 339 } 340 ] 341 }` 342 } 343 344 func spacesItems() string { 345 return `{ 346 "resources": [ 347 { 348 "metadata":{ 349 "guid": "my-space-guid" 350 }, 351 "entity": { 352 "name": "my-space", 353 "organization": { 354 "metadata": { 355 "guid": "my-org-guid" 356 }, 357 "entity": { 358 "name": "my-org" 359 } 360 } 361 } 362 } 363 ] 364 }` 365 }