github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudlets/policy_test.go (about) 1 package cloudlets 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/tools" 11 12 "github.com/stretchr/testify/require" 13 "github.com/tj/assert" 14 ) 15 16 func TestListPolicies(t *testing.T) { 17 tests := map[string]struct { 18 params ListPoliciesRequest 19 responseStatus int 20 responseBody string 21 expectedPath string 22 expectedResponse []Policy 23 withError func(*testing.T, error) 24 }{ 25 "200 OK": { 26 params: ListPoliciesRequest{}, 27 responseStatus: http.StatusOK, 28 responseBody: ` 29 [ 30 { 31 "location": "/cloudlets/api/v2/policies/1001", 32 "serviceVersion": null, 33 "apiVersion": "2.0", 34 "policyId": 1001, 35 "cloudletId": 99, 36 "cloudletCode": "CC", 37 "groupId": 1234, 38 "name": "CookieCutter", 39 "description": "Custom cookie cutter", 40 "propertyName": "www.example.org", 41 "createdBy": "sjones", 42 "createDate": 1400535431324, 43 "lastModifiedBy": "sjones", 44 "lastModifiedDate": 1441829042000, 45 "activations": [ 46 { 47 "serviceVersion": null, 48 "apiVersion": "2.0", 49 "network": "prod", 50 "policyInfo": { 51 "policyId": 1001, 52 "name": "CookieCutter", 53 "version": 2, 54 "status": "INACTIVE", 55 "statusDetail": "waiting to complete tests in test environment", 56 "activatedBy": "jsmith", 57 "activationDate": 1441829042000 58 }, 59 "propertyInfo": { 60 "name": "www.example.org", 61 "version": 2, 62 "groupId": 1234, 63 "status": "INACTIVE", 64 "activatedBy": "sjones", 65 "activationDate": 1441137842000 66 } 67 }, 68 { 69 "serviceVersion": null, 70 "apiVersion": "2.0", 71 "network": "STAGING", 72 "policyInfo": { 73 "policyId": 1001, 74 "name": "CookieCutter", 75 "version": 22, 76 "status": "ACTIVE", 77 "statusDetail": "testing", 78 "activatedBy": "jsmith", 79 "activationDate": 1400535431000 80 }, 81 "propertyInfo": { 82 "name": "www.example.org", 83 "version": 22, 84 "groupId": 1234, 85 "status": "ACTIVE", 86 "activatedBy": "jsmith", 87 "activationDate": 1441137842000 88 } 89 } 90 ] 91 } 92 ]`, 93 expectedPath: "/cloudlets/api/v2/policies?includeDeleted=false&offset=0", 94 expectedResponse: []Policy{ 95 { 96 Location: "/cloudlets/api/v2/policies/1001", 97 APIVersion: "2.0", 98 PolicyID: 1001, 99 CloudletID: 99, 100 CloudletCode: "CC", 101 GroupID: 1234, 102 Name: "CookieCutter", 103 Description: "Custom cookie cutter", 104 CreatedBy: "sjones", 105 CreateDate: 1400535431324, 106 LastModifiedBy: "sjones", 107 LastModifiedDate: 1441829042000, 108 Activations: []PolicyActivation{ 109 { 110 APIVersion: "2.0", 111 Network: "prod", 112 PolicyInfo: PolicyInfo{ 113 PolicyID: 1001, 114 Name: "CookieCutter", 115 Version: 2, 116 Status: "INACTIVE", 117 StatusDetail: "waiting to complete tests in test environment", 118 ActivatedBy: "jsmith", 119 ActivationDate: 1441829042000, 120 }, 121 PropertyInfo: PropertyInfo{ 122 Name: "www.example.org", 123 Version: 2, 124 GroupID: 1234, 125 Status: "INACTIVE", 126 ActivatedBy: "sjones", 127 ActivationDate: 1441137842000, 128 }, 129 }, 130 { 131 APIVersion: "2.0", 132 Network: "staging", 133 PolicyInfo: PolicyInfo{ 134 PolicyID: 1001, 135 Name: "CookieCutter", 136 Version: 22, 137 Status: "ACTIVE", 138 StatusDetail: "testing", 139 ActivatedBy: "jsmith", 140 ActivationDate: 1400535431000, 141 }, 142 PropertyInfo: PropertyInfo{ 143 Name: "www.example.org", 144 Version: 22, 145 GroupID: 1234, 146 Status: "ACTIVE", 147 ActivatedBy: "jsmith", 148 ActivationDate: 1441137842000, 149 }, 150 }, 151 }, 152 }, 153 }, 154 }, 155 "200 OK with params": { 156 params: ListPoliciesRequest{ 157 CloudletID: tools.Int64Ptr(2), 158 IncludeDeleted: true, 159 Offset: 4, 160 PageSize: tools.IntPtr(5), 161 }, 162 responseStatus: http.StatusOK, 163 responseBody: `[]`, 164 expectedPath: "/cloudlets/api/v2/policies?cloudletId=2&includeDeleted=true&offset=4&pageSize=5", 165 expectedResponse: []Policy{}, 166 }, 167 "500 internal server error": { 168 params: ListPoliciesRequest{}, 169 responseStatus: http.StatusInternalServerError, 170 responseBody: ` 171 { 172 "type": "internal_error", 173 "title": "Internal Server Error", 174 "detail": "Error making request", 175 "status": 500 176 }`, 177 expectedPath: "/cloudlets/api/v2/policies?includeDeleted=false&offset=0", 178 withError: func(t *testing.T, err error) { 179 want := &Error{ 180 Type: "internal_error", 181 Title: "Internal Server Error", 182 Detail: "Error making request", 183 StatusCode: http.StatusInternalServerError, 184 } 185 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 186 }, 187 }, 188 } 189 190 for name, test := range tests { 191 t.Run(name, func(t *testing.T) { 192 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 193 assert.Equal(t, test.expectedPath, r.URL.String()) 194 assert.Equal(t, http.MethodGet, r.Method) 195 w.WriteHeader(test.responseStatus) 196 _, err := w.Write([]byte(test.responseBody)) 197 assert.NoError(t, err) 198 })) 199 client := mockAPIClient(t, mockServer) 200 result, err := client.ListPolicies(context.Background(), test.params) 201 if test.withError != nil { 202 test.withError(t, err) 203 return 204 } 205 require.NoError(t, err) 206 assert.Equal(t, test.expectedResponse, result) 207 }) 208 } 209 } 210 211 func TestGetPolicy(t *testing.T) { 212 tests := map[string]struct { 213 policyID int64 214 responseStatus int 215 responseBody string 216 expectedPath string 217 expectedResponse *Policy 218 withError func(*testing.T, error) 219 }{ 220 "200 OK": { 221 policyID: 1001, 222 responseStatus: http.StatusOK, 223 responseBody: ` 224 { 225 "location": "/cloudlets/api/v2/policies/1001", 226 "serviceVersion": null, 227 "apiVersion": "2.0", 228 "policyId": 1001, 229 "cloudletId": 99, 230 "cloudletCode": "CC", 231 "groupId": 1234, 232 "name": "CookieCutter", 233 "description": "Custom cookie cutter", 234 "propertyName": "www.example.org", 235 "createdBy": "sjones", 236 "createDate": 1400535431324, 237 "lastModifiedBy": "sjones", 238 "lastModifiedDate": 1441829042000, 239 "activations": [ 240 { 241 "serviceVersion": null, 242 "apiVersion": "2.0", 243 "network": "prod", 244 "policyInfo": { 245 "policyId": 1001, 246 "name": "CookieCutter", 247 "version": 2, 248 "status": "INACTIVE", 249 "statusDetail": "waiting to complete tests in test environment", 250 "activatedBy": "jsmith", 251 "activationDate": 1441829042000 252 }, 253 "propertyInfo": { 254 "name": "www.example.org", 255 "version": 2, 256 "groupId": 1234, 257 "status": "INACTIVE", 258 "activatedBy": "sjones", 259 "activationDate": 1441137842000 260 } 261 }, 262 { 263 "serviceVersion": null, 264 "apiVersion": "2.0", 265 "network": "staging", 266 "policyInfo": { 267 "policyId": 1001, 268 "name": "CookieCutter", 269 "version": 22, 270 "status": "ACTIVE", 271 "statusDetail": "testing", 272 "activatedBy": "jsmith", 273 "activationDate": 1400535431000 274 }, 275 "propertyInfo": { 276 "name": "www.example.org", 277 "version": 22, 278 "groupId": 1234, 279 "status": "ACTIVE", 280 "activatedBy": "jsmith", 281 "activationDate": 1441137842000 282 } 283 } 284 ] 285 }`, 286 expectedPath: "/cloudlets/api/v2/policies/1001", 287 expectedResponse: &Policy{ 288 Location: "/cloudlets/api/v2/policies/1001", 289 APIVersion: "2.0", 290 PolicyID: 1001, 291 CloudletID: 99, 292 CloudletCode: "CC", 293 GroupID: 1234, 294 Name: "CookieCutter", 295 Description: "Custom cookie cutter", 296 CreatedBy: "sjones", 297 CreateDate: 1400535431324, 298 LastModifiedBy: "sjones", 299 LastModifiedDate: 1441829042000, 300 Activations: []PolicyActivation{ 301 { 302 APIVersion: "2.0", 303 Network: "prod", 304 PolicyInfo: PolicyInfo{ 305 PolicyID: 1001, 306 Name: "CookieCutter", 307 Version: 2, 308 Status: "INACTIVE", 309 StatusDetail: "waiting to complete tests in test environment", 310 ActivatedBy: "jsmith", 311 ActivationDate: 1441829042000, 312 }, 313 PropertyInfo: PropertyInfo{ 314 Name: "www.example.org", 315 Version: 2, 316 GroupID: 1234, 317 Status: "INACTIVE", 318 ActivatedBy: "sjones", 319 ActivationDate: 1441137842000, 320 }, 321 }, 322 { 323 APIVersion: "2.0", 324 Network: "staging", 325 PolicyInfo: PolicyInfo{ 326 PolicyID: 1001, 327 Name: "CookieCutter", 328 Version: 22, 329 Status: "ACTIVE", 330 StatusDetail: "testing", 331 ActivatedBy: "jsmith", 332 ActivationDate: 1400535431000, 333 }, 334 PropertyInfo: PropertyInfo{ 335 Name: "www.example.org", 336 Version: 22, 337 GroupID: 1234, 338 Status: "ACTIVE", 339 ActivatedBy: "jsmith", 340 ActivationDate: 1441137842000, 341 }, 342 }, 343 }, 344 }, 345 }, 346 "500 internal server error": { 347 policyID: 1, 348 responseStatus: http.StatusInternalServerError, 349 responseBody: ` 350 { 351 "type": "internal_error", 352 "title": "Internal Server Error", 353 "detail": "Error making request", 354 "status": 500 355 }`, 356 expectedPath: "/cloudlets/api/v2/policies/1", 357 withError: func(t *testing.T, err error) { 358 want := &Error{ 359 Type: "internal_error", 360 Title: "Internal Server Error", 361 Detail: "Error making request", 362 StatusCode: http.StatusInternalServerError, 363 } 364 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 365 }, 366 }, 367 } 368 369 for name, test := range tests { 370 t.Run(name, func(t *testing.T) { 371 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 372 assert.Equal(t, test.expectedPath, r.URL.String()) 373 assert.Equal(t, http.MethodGet, r.Method) 374 w.WriteHeader(test.responseStatus) 375 _, err := w.Write([]byte(test.responseBody)) 376 assert.NoError(t, err) 377 })) 378 client := mockAPIClient(t, mockServer) 379 result, err := client.GetPolicy(context.Background(), GetPolicyRequest{PolicyID: test.policyID}) 380 if test.withError != nil { 381 test.withError(t, err) 382 return 383 } 384 require.NoError(t, err) 385 assert.Equal(t, test.expectedResponse, result) 386 }) 387 } 388 } 389 390 func TestCreatePolicy(t *testing.T) { 391 tests := map[string]struct { 392 request CreatePolicyRequest 393 responseStatus int 394 responseBody string 395 expectedPath string 396 expectedResponse *Policy 397 withError error 398 }{ 399 "201 created": { 400 request: CreatePolicyRequest{ 401 CloudletID: 0, 402 GroupID: 35730, 403 Name: "TestName1", 404 }, 405 responseStatus: http.StatusCreated, 406 responseBody: `{ 407 "activations": [], 408 "apiVersion": "2.0", 409 "cloudletCode": "ER", 410 "cloudletId": 0, 411 "createDate": 1629299944251, 412 "createdBy": "jsmith", 413 "deleted": false, 414 "description": null, 415 "groupId": 35730, 416 "lastModifiedBy": "jsmith", 417 "lastModifiedDate": 1629299944251, 418 "location": "/cloudlets/api/v2/policies/276858", 419 "name": "TestName1", 420 "policyId": 276858, 421 "propertyName": null, 422 "serviceVersion": null 423 }`, 424 expectedPath: "/cloudlets/api/v2/policies", 425 expectedResponse: &Policy{ 426 APIVersion: "2.0", 427 CloudletCode: "ER", 428 CloudletID: 0, 429 CreateDate: 1629299944251, 430 CreatedBy: "jsmith", 431 Deleted: false, 432 Description: "", 433 GroupID: 35730, 434 LastModifiedBy: "jsmith", 435 LastModifiedDate: 1629299944251, 436 Location: "/cloudlets/api/v2/policies/276858", 437 Name: "TestName1", 438 PolicyID: 276858, 439 Activations: []PolicyActivation{}, 440 }, 441 }, 442 "500 internal server error": { 443 request: CreatePolicyRequest{ 444 CloudletID: 0, 445 GroupID: 35730, 446 Name: "TestName1", 447 }, 448 responseStatus: http.StatusInternalServerError, 449 responseBody: ` 450 { 451 "type": "internal_error", 452 "title": "Internal Server Error", 453 "detail": "Error creating enrollment", 454 "status": 500 455 }`, 456 expectedPath: "/cloudlets/api/v2/policies", 457 withError: &Error{ 458 Type: "internal_error", 459 Title: "Internal Server Error", 460 Detail: "Error creating enrollment", 461 StatusCode: http.StatusInternalServerError, 462 }, 463 }, 464 "validation error": { 465 request: CreatePolicyRequest{}, 466 withError: ErrStructValidation, 467 }, 468 } 469 470 for name, test := range tests { 471 t.Run(name, func(t *testing.T) { 472 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 473 assert.Equal(t, test.expectedPath, r.URL.String()) 474 assert.Equal(t, http.MethodPost, r.Method) 475 w.WriteHeader(test.responseStatus) 476 _, err := w.Write([]byte(test.responseBody)) 477 assert.NoError(t, err) 478 })) 479 client := mockAPIClient(t, mockServer) 480 result, err := client.CreatePolicy(context.Background(), test.request) 481 if test.withError != nil { 482 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 483 return 484 } 485 require.NoError(t, err) 486 assert.Equal(t, test.expectedResponse, result) 487 }) 488 } 489 } 490 491 func TestDeletePolicy(t *testing.T) { 492 tests := map[string]struct { 493 policyID int64 494 responseStatus int 495 responseBody string 496 expectedPath string 497 withError error 498 }{ 499 "204 no content": { 500 policyID: 276858, 501 responseStatus: http.StatusNoContent, 502 responseBody: "", 503 expectedPath: "/cloudlets/api/v2/policies/276858", 504 }, 505 "500 internal server error": { 506 policyID: 0, 507 responseStatus: http.StatusInternalServerError, 508 responseBody: ` 509 { 510 "type": "internal_error", 511 "title": "Internal Server Error", 512 "detail": "Error creating enrollment", 513 "status": 500 514 }`, 515 expectedPath: "/cloudlets/api/v2/policies/0", 516 withError: &Error{ 517 Type: "internal_error", 518 Title: "Internal Server Error", 519 Detail: "Error creating enrollment", 520 StatusCode: http.StatusInternalServerError, 521 }, 522 }, 523 } 524 525 for name, test := range tests { 526 t.Run(name, func(t *testing.T) { 527 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 528 assert.Equal(t, test.expectedPath, r.URL.String()) 529 assert.Equal(t, http.MethodDelete, r.Method) 530 w.WriteHeader(test.responseStatus) 531 _, err := w.Write([]byte(test.responseBody)) 532 assert.NoError(t, err) 533 })) 534 client := mockAPIClient(t, mockServer) 535 err := client.RemovePolicy(context.Background(), RemovePolicyRequest{PolicyID: test.policyID}) 536 if test.withError != nil { 537 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 538 return 539 } 540 require.NoError(t, err) 541 }) 542 } 543 } 544 545 func TestUpdatePolicy(t *testing.T) { 546 tests := map[string]struct { 547 request UpdatePolicyRequest 548 responseStatus int 549 responseBody string 550 expectedPath string 551 expectedResponse *Policy 552 withError error 553 }{ 554 "200 updated": { 555 request: UpdatePolicyRequest{ 556 UpdatePolicy: UpdatePolicy{ 557 GroupID: 35730, 558 Name: "TestName1Updated", 559 Description: "Description", 560 }, 561 PolicyID: 276858, 562 }, 563 responseStatus: http.StatusOK, 564 responseBody: `{ 565 "activations": [], 566 "apiVersion": "2.0", 567 "cloudletCode": "ER", 568 "cloudletId": 0, 569 "createDate": 1629299944251, 570 "createdBy": "jsmith", 571 "deleted": false, 572 "description": "Description", 573 "groupId": 35730, 574 "lastModifiedBy": "jsmith", 575 "lastModifiedDate": 1629370566748, 576 "location": "/cloudlets/api/v2/policies/276858", 577 "name": "TestName1Updated", 578 "policyId": 276858, 579 "propertyName": null, 580 "serviceVersion": null 581 }`, 582 expectedPath: "/cloudlets/api/v2/policies/276858", 583 expectedResponse: &Policy{ 584 APIVersion: "2.0", 585 CloudletCode: "ER", 586 CloudletID: 0, 587 CreateDate: 1629299944251, 588 CreatedBy: "jsmith", 589 Deleted: false, 590 Description: "Description", 591 GroupID: 35730, 592 LastModifiedBy: "jsmith", 593 LastModifiedDate: 1629370566748, 594 Location: "/cloudlets/api/v2/policies/276858", 595 Name: "TestName1Updated", 596 PolicyID: 276858, 597 Activations: []PolicyActivation{}, 598 }, 599 }, 600 "500 internal server error": { 601 request: UpdatePolicyRequest{ 602 UpdatePolicy: UpdatePolicy{ 603 GroupID: 35730, 604 Name: "TestName1Updated", 605 }, 606 PolicyID: 276858, 607 }, 608 responseStatus: http.StatusInternalServerError, 609 responseBody: ` 610 { 611 "type": "internal_error", 612 "title": "Internal Server Error", 613 "detail": "Error creating enrollment", 614 "status": 500 615 }`, 616 expectedPath: "/cloudlets/api/v2/policies/276858", 617 withError: &Error{ 618 Type: "internal_error", 619 Title: "Internal Server Error", 620 Detail: "Error creating enrollment", 621 StatusCode: http.StatusInternalServerError, 622 }, 623 }, 624 "validation error": { 625 expectedPath: "/cloudlets/api/v2/policies/0", 626 request: UpdatePolicyRequest{ 627 UpdatePolicy: UpdatePolicy{ 628 Name: "A B", 629 }, 630 }, 631 withError: ErrStructValidation, 632 }, 633 } 634 635 for name, test := range tests { 636 t.Run(name, func(t *testing.T) { 637 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 638 assert.Equal(t, test.expectedPath, r.URL.String()) 639 assert.Equal(t, http.MethodPut, r.Method) 640 w.WriteHeader(test.responseStatus) 641 _, err := w.Write([]byte(test.responseBody)) 642 assert.NoError(t, err) 643 })) 644 client := mockAPIClient(t, mockServer) 645 result, err := client.UpdatePolicy(context.Background(), test.request) 646 if test.withError != nil { 647 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 648 return 649 } 650 require.NoError(t, err) 651 assert.Equal(t, test.expectedResponse, result) 652 }) 653 } 654 }